2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavformat/udp.c
27 #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
31 #include "os_support.h"
33 #include <sys/select.h>
46 struct sockaddr_in dest_addr;
49 frsh_send_endpoint_t sepoint;
50 frsh_send_endpoint_t repoint;
52 frsh_send_endpoint_protocol_info_t send_pinfo;
53 frsh_contract_t contract;
54 frsh_rel_time_t budget, period;
57 #define UDP_TX_BUF_SIZE 32768
58 #define UDP_MAX_PKT_SIZE 65536
60 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr)
65 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
70 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
75 static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
77 /* set the destination address */
78 if (resolve_host(&addr->sin_addr, hostname) < 0)
80 addr->sin_family = AF_INET;
81 addr->sin_port = htons(port);
83 return sizeof(struct sockaddr_in);
86 static int is_multicast_address(struct sockaddr_in *addr)
92 udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
96 addr->sin_family = AF_INET;
97 addr->sin_addr.s_addr = htonl (INADDR_ANY);
98 addr->sin_port = htons(s->local_port);
99 *addr_len = sizeof(struct sockaddr_in);
101 /* set params for contract */
102 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, 8600000, &s->budget);
103 s->period = fosa_msec_to_rel_time(100);
104 s->send_pinfo.body = NULL;
106 udp_fd = frsh_send_endpoint_create(FRSH_NETPF_FWP,
107 s->dest_addr.sin_addr.s_addr,
108 ntohs(s->dest_addr.sin_port), s->send_pinfo,
115 /* Contract negotiation */
116 ret = frsh_contract_init(&s->contract);
117 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
120 ret = frsh_contract_set_basic_params(&s->contract,
125 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
127 ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
128 FRSH_NETPF_FWP, "net_cont1");
129 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
132 ret = frsh_contract_negotiate(&s->contract, &s->vres);
133 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
136 printf("Send endpoint created\n");
137 frsh_send_endpoint_bind(s->vres, s->sepoint);
138 printf("Send endpoint bounded\n");
140 //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
141 // &port, &attr, &fd);
146 static int udp_port(struct sockaddr_in *addr, int len)
148 return ntohs(addr->sin_port);
152 * If no filename is given to av_open_input_file because you want to
153 * get the local port first, then you must call this function to set
154 * the remote server address.
156 * url syntax: udp://host:port[?option=val...]
157 * option: 'ttl=n' : set the ttl value (for multicast only)
158 * 'localport=n' : set the local port
159 * 'pkt_size=n' : set max packet size
160 * 'reuse=1' : enable reusing the socket
162 * @param s1 media file context
163 * @param uri of the remote server
164 * @return zero if no error.
166 int udp_set_remote_url(URLContext *h, const char *uri)
168 UDPContext *s = h->priv_data;
172 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
174 /* set the destination address */
175 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
176 if (s->dest_addr_len < 0) {
179 s->is_multicast = is_multicast_address(&s->dest_addr);
185 * Return the local port used by the UDP connexion
186 * @param s1 media file context
187 * @return the local port number
189 int udp_get_local_port(URLContext *h)
191 UDPContext *s = h->priv_data;
192 return s->local_port;
196 * Return the udp file handle for select() usage to wait for several RTP
197 * streams at the same time.
198 * @param h media file context
200 #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
203 int udp_get_file_handle(URLContext *h)
205 UDPContext *s = h->priv_data;
209 /* put it in UDP context */
210 /* return non zero if error */
211 static int udp_open(URLContext *h, const char *uri, int flags)
214 int port, udp_fd = -1, tmp;
215 UDPContext *s = NULL;
219 struct sockaddr_in my_addr;
223 h->max_packet_size = 1472;
225 is_output = (flags & URL_WRONLY);
227 if(!ff_network_init())
230 s = av_mallocz(sizeof(UDPContext));
232 return AVERROR(ENOMEM);
236 s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
238 p = strchr(uri, '?');
240 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
241 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
242 s->ttl = strtol(buf, NULL, 10);
244 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
245 s->local_port = strtol(buf, NULL, 10);
247 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
248 h->max_packet_size = strtol(buf, NULL, 10);
250 if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
251 s->buffer_size = strtol(buf, NULL, 10);
255 /* fill the dest addr */
256 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
258 /* XXX: fix url_split */
259 if (hostname[0] == '\0' || hostname[0] == '?') {
260 /* only accepts null hostname if input */
261 if (flags & URL_WRONLY)
264 udp_set_remote_url(h, uri);
267 udp_fd = udp_socket_create(s, &my_addr, &len);
272 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
275 /* bind to the local address if not multicast or if the multicast
277 /*if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
281 len = sizeof(my_addr);
282 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
283 s->local_port = udp_port(&my_addr, len);
286 /* limit the tx buf size to limit latency */
287 tmp = s->buffer_size;
288 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
289 av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
293 /* set udp recv buffer size to the largest possible udp packet size to
294 * avoid losing data on OSes that set this too low by default. */
295 tmp = s->buffer_size;
296 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
297 av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
299 /* make the socket non-blocking */
300 ff_socket_nonblock(udp_fd, 1);
312 static int udp_read(URLContext *h, uint8_t *buf, int size)
314 UDPContext *s = h->priv_data;
318 return frsh_receive_sync(s->repoint, buf, size, &len, &from);
321 static int udp_write(URLContext *h, uint8_t *buf, int size)
323 UDPContext *s = h->priv_data;
326 ret = frsh_send_async(s->sepoint, buf, size);
330 static int udp_close(URLContext *h)
332 UDPContext *s = h->priv_data;
335 ret1 = frsh_send_endpoint_destroy(s->sepoint);
336 ret2 = frsh_receive_endpoint_destroy(s->repoint);
337 return (ret1 || ret2);
340 URLProtocol udp_protocol = {
347 .url_get_file_handle = udp_get_file_handle,