]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/udp.c
f05d332b52c0aea7288f04c0a26f2f01967b1078
[frescor/ffmpeg.git] / libavformat / udp.c
1 /*
2  * UDP prototype streaming system
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file libavformat/udp.c
24  * UDP protocol
25  */
26
27 #define _BSD_SOURCE     /* Needed for using struct ip_mreq with recent glibc */
28 #include "avformat.h"
29 #include <unistd.h>
30 #include "network.h"
31 #include "os_support.h"
32 #if HAVE_SYS_SELECT_H
33 #include <sys/select.h>
34 #endif
35 #include <sys/time.h>
36
37 #include <frsh.h>
38 long int udp_budget, udp_period;
39
40 typedef struct {
41     int udp_fd;
42     int ttl;
43     int buffer_size;
44     int is_multicast;
45     int local_port;
46     int reuse_socket;
47     struct sockaddr_in dest_addr;
48     int dest_addr_len;
49         
50         frsh_send_endpoint_t sepoint;
51         frsh_send_endpoint_t repoint;
52         frsh_vres_id_t vres;
53         frsh_send_endpoint_protocol_info_t send_pinfo;
54         frsh_contract_t contract;
55         frsh_rel_time_t budget, period;
56 } UDPContext;
57
58 #define UDP_TX_BUF_SIZE 32768
59 #define UDP_MAX_PKT_SIZE 65536
60
61 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) 
62 {
63     return 0;
64 }
65
66 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) 
67 {
68     return 0;
69 }
70
71 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) 
72 {
73     return 0;
74 }
75
76 static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
77 {
78     /* set the destination address */
79     if (resolve_host(&addr->sin_addr, hostname) < 0)
80         return AVERROR(EIO);
81     addr->sin_family = AF_INET;
82     addr->sin_port = htons(port);
83
84     return sizeof(struct sockaddr_in);
85 }
86
87 static int is_multicast_address(struct sockaddr_in *addr)
88 {
89     return 0;
90 }
91
92 static int 
93 udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
94 {
95         static long int netcont_num = 0;
96         int ret,udp_fd;
97         char netcont_name[20];
98
99     addr->sin_family = AF_INET;
100     addr->sin_addr.s_addr = htonl (INADDR_ANY);
101     addr->sin_port = htons(s->local_port);
102     *addr_len = sizeof(struct sockaddr_in);
103
104         /* set params for contract */
105         frsh_network_bytes_to_budget(FRSH_NETPF_FWP, udp_budget, &s->budget);
106         s->period = fosa_msec_to_rel_time(udp_period);
107         s->send_pinfo.body = NULL;              
108         
109         udp_fd = frsh_send_endpoint_create(FRSH_NETPF_FWP, 
110                                         s->dest_addr.sin_addr.s_addr,
111                                         ntohs(s->dest_addr.sin_port), s->send_pinfo, 
112                                         &s->sepoint);
113         if (udp_fd < 0) {
114                         
115                         return -1;
116         }
117         
118         /* Contract negotiation */
119         ret = frsh_contract_init(&s->contract);
120         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
121         if (ret) return -1;
122                 
123         ret = frsh_contract_set_basic_params(&s->contract,
124                                              &s->budget,
125                                              &s->period,
126                                              FRSH_WT_BOUNDED,
127                                              FRSH_CT_REGULAR);
128         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
129         if (ret) return -1;
130         snprintf(netcont_name, sizeof(netcont_name), "net_cont%d", ++netcont_num);
131         ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
132                                                 FRSH_NETPF_FWP, netcont_name);
133         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
134         if (ret) return -1;
135
136         ret = frsh_contract_negotiate(&s->contract, &s->vres);
137         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
138         if (ret) return -1;
139         
140         printf("Send endpoint created\n");
141         frsh_send_endpoint_bind(s->vres, s->sepoint);
142         printf("Send endpoint bounded\n");
143         
144         //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
145         //              &port, &attr, &fd);
146
147         return udp_fd;
148 }
149
150 static int udp_port(struct sockaddr_in *addr, int len)
151 {
152     return ntohs(addr->sin_port);
153 }
154
155 /**
156  * If no filename is given to av_open_input_file because you want to
157  * get the local port first, then you must call this function to set
158  * the remote server address.
159  *
160  * url syntax: udp://host:port[?option=val...]
161  * option: 'ttl=n'       : set the ttl value (for multicast only)
162  *         'localport=n' : set the local port
163  *         'pkt_size=n'  : set max packet size
164  *         'reuse=1'     : enable reusing the socket
165  *
166  * @param s1 media file context
167  * @param uri of the remote server
168  * @return zero if no error.
169  */
170 int udp_set_remote_url(URLContext *h, const char *uri)
171 {
172     UDPContext *s = h->priv_data;
173     char hostname[256];
174     int port;
175
176     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
177
178     /* set the destination address */
179     s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
180     if (s->dest_addr_len < 0) {
181         return AVERROR(EIO);
182     }
183     s->is_multicast = is_multicast_address(&s->dest_addr);
184
185     return 0;
186 }
187
188 /**
189  * Return the local port used by the UDP connexion
190  * @param s1 media file context
191  * @return the local port number
192  */
193 int udp_get_local_port(URLContext *h)
194 {
195     UDPContext *s = h->priv_data;
196     return s->local_port;
197 }
198
199 /**
200  * Return the udp file handle for select() usage to wait for several RTP
201  * streams at the same time.
202  * @param h media file context
203  */
204 #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
205 static
206 #endif
207 int udp_get_file_handle(URLContext *h)
208 {
209     UDPContext *s = h->priv_data;
210     return s->udp_fd;
211 }
212
213 /* put it in UDP context */
214 /* return non zero if error */
215 static int udp_open(URLContext *h, const char *uri, int flags)
216 {
217     char hostname[1024];
218     int port, udp_fd = -1, tmp;
219     UDPContext *s = NULL;
220     int is_output;
221     const char *p;
222     char buf[256];
223     struct sockaddr_in my_addr;
224     int len;
225
226     h->is_streamed = 1;
227     h->max_packet_size = 1472;
228
229     is_output = (flags & URL_WRONLY);
230
231     if(!ff_network_init())
232         return AVERROR(EIO);
233
234     s = av_mallocz(sizeof(UDPContext));
235     if (!s)
236         return AVERROR(ENOMEM);
237
238     h->priv_data = s;
239     s->ttl = 16;
240     s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
241
242     p = strchr(uri, '?');
243     if (p) {
244         s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
245         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
246             s->ttl = strtol(buf, NULL, 10);
247         }
248         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
249             s->local_port = strtol(buf, NULL, 10);
250         }
251         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
252             h->max_packet_size = strtol(buf, NULL, 10);
253         }
254         if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
255             s->buffer_size = strtol(buf, NULL, 10);
256         }
257     }
258
259     /* fill the dest addr */
260     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
261
262     /* XXX: fix url_split */
263     if (hostname[0] == '\0' || hostname[0] == '?') {
264         /* only accepts null hostname if input */
265         if (flags & URL_WRONLY)
266             goto fail;
267     } else {
268         udp_set_remote_url(h, uri);
269     }
270
271     udp_fd = udp_socket_create(s, &my_addr, &len);
272     if (udp_fd < 0)
273         goto fail;
274 #if 0
275     if (s->reuse_socket)
276         if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
277             goto fail;
278
279     /* bind to the local address if not multicast or if the multicast
280      * bind failed */
281     /*if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
282         goto fail;*/
283 #endif
284
285     len = sizeof(my_addr);
286     getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
287     s->local_port = udp_port(&my_addr, len);
288
289     if (is_output) {
290         /* limit the tx buf size to limit latency */
291         tmp = s->buffer_size;
292         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
293             av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
294             goto fail;
295         }
296     } else {
297         /* set udp recv buffer size to the largest possible udp packet size to
298          * avoid losing data on OSes that set this too low by default. */
299         tmp = s->buffer_size;
300         if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
301             av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
302         }
303         /* make the socket non-blocking */
304         ff_socket_nonblock(udp_fd, 1);
305     }
306     s->udp_fd = udp_fd;
307     return 0;
308  
309  fail:
310     if (udp_fd >= 0)
311         closesocket(udp_fd);
312     av_free(s);
313     return AVERROR(EIO);
314 }
315
316 static int udp_read(URLContext *h, uint8_t *buf, int size)
317 {
318     UDPContext *s = h->priv_data;
319     unsigned int from;
320         int len;
321
322     return frsh_receive_sync(s->repoint, buf, size, &len, &from);
323 }
324
325 static int udp_write(URLContext *h, uint8_t *buf, int size)
326 {
327     UDPContext *s = h->priv_data;
328     int ret;
329         
330         ret = frsh_send_async(s->sepoint, buf, size);
331         return ret;
332 }
333
334 static int udp_close(URLContext *h)
335 {
336     UDPContext *s = h->priv_data;
337     int ret;
338         
339         frsh_send_endpoint_unbind(s->sepoint);
340         ret = frsh_contract_cancel(s->vres);
341         return ret;
342 }
343
344 URLProtocol udp_protocol = {
345     "udp",
346     udp_open,
347     udp_read,
348     udp_write,
349     NULL, /* seek */
350     udp_close,
351     .url_get_file_handle = udp_get_file_handle,
352 };