]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/udp.c
91e994506af5c6af25687853640e147cadc193bc
[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 #include <fwp_endpoint.h>
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         int ret,fd;
96         unsigned int node, port;
97         fwp_endpoint_attr_t attr;
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, 30000, &s->budget);
106         s->period = fosa_msec_to_rel_time(1000);
107         s->send_pinfo.body = NULL;              
108         
109         if (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) < 0) {
113                         
114                         return -1;
115         }
116         
117         /* Contract negotiation */
118         ret = frsh_contract_init(&s->contract);
119         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
120         if (ret) return -1;
121                 
122         ret = frsh_contract_set_basic_params(&s->contract,
123                                              &s->budget,
124                                              &s->period,
125                                              FRSH_WT_BOUNDED,
126                                              FRSH_CT_REGULAR);
127         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
128         if (ret) return -1;
129         ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
130                                                 FRSH_NETPF_FWP, "net_cont1");
131         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
132         if (ret) return -1;
133
134         ret = frsh_contract_negotiate(&s->contract, &s->vres);
135         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
136         if (ret) return -1;
137         
138         printf("Send endpoint created\n");
139         frsh_send_endpoint_bind(s->vres, s->sepoint);
140         printf("Send endpoint bounded\n");
141         
142         //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
143         //              &port, &attr, &fd);
144
145         return 0;
146 }
147
148 static int udp_port(struct sockaddr_in *addr, int len)
149 {
150     return ntohs(addr->sin_port);
151 }
152
153 /**
154  * If no filename is given to av_open_input_file because you want to
155  * get the local port first, then you must call this function to set
156  * the remote server address.
157  *
158  * url syntax: udp://host:port[?option=val...]
159  * option: 'ttl=n'       : set the ttl value (for multicast only)
160  *         'localport=n' : set the local port
161  *         'pkt_size=n'  : set max packet size
162  *         'reuse=1'     : enable reusing the socket
163  *
164  * @param s1 media file context
165  * @param uri of the remote server
166  * @return zero if no error.
167  */
168 int udp_set_remote_url(URLContext *h, const char *uri)
169 {
170     UDPContext *s = h->priv_data;
171     char hostname[256];
172     int port;
173
174     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
175
176     /* set the destination address */
177     s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
178     if (s->dest_addr_len < 0) {
179         return AVERROR(EIO);
180     }
181     s->is_multicast = is_multicast_address(&s->dest_addr);
182
183     return 0;
184 }
185
186 /**
187  * Return the local port used by the UDP connexion
188  * @param s1 media file context
189  * @return the local port number
190  */
191 int udp_get_local_port(URLContext *h)
192 {
193     UDPContext *s = h->priv_data;
194     return s->local_port;
195 }
196
197 /**
198  * Return the udp file handle for select() usage to wait for several RTP
199  * streams at the same time.
200  * @param h media file context
201  */
202 #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
203 static
204 #endif
205 int udp_get_file_handle(URLContext *h)
206 {
207     UDPContext *s = h->priv_data;
208     return s->udp_fd;
209 }
210
211 /* put it in UDP context */
212 /* return non zero if error */
213 static int udp_open(URLContext *h, const char *uri, int flags)
214 {
215     char hostname[1024];
216     int port, udp_fd = -1, tmp;
217     UDPContext *s = NULL;
218     int is_output;
219     const char *p;
220     char buf[256];
221     struct sockaddr_in my_addr;
222     int len;
223
224     h->is_streamed = 1;
225     h->max_packet_size = 1472;
226
227     is_output = (flags & URL_WRONLY);
228
229     if(!ff_network_init())
230         return AVERROR(EIO);
231
232     s = av_mallocz(sizeof(UDPContext));
233     if (!s)
234         return AVERROR(ENOMEM);
235
236     h->priv_data = s;
237     s->ttl = 16;
238     s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
239
240     p = strchr(uri, '?');
241     if (p) {
242         s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
243         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
244             s->ttl = strtol(buf, NULL, 10);
245         }
246         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
247             s->local_port = strtol(buf, NULL, 10);
248         }
249         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
250             h->max_packet_size = strtol(buf, NULL, 10);
251         }
252         if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
253             s->buffer_size = strtol(buf, NULL, 10);
254         }
255     }
256
257     /* fill the dest addr */
258     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
259
260     /* XXX: fix url_split */
261     if (hostname[0] == '\0' || hostname[0] == '?') {
262         /* only accepts null hostname if input */
263         if (flags & URL_WRONLY)
264             goto fail;
265     } else {
266         udp_set_remote_url(h, uri);
267     }
268
269     udp_fd = udp_socket_create(s, &my_addr, &len);
270     if (udp_fd < 0)
271         goto fail;
272 #if 0
273     if (s->reuse_socket)
274         if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
275             goto fail;
276
277     /* bind to the local address if not multicast or if the multicast
278      * bind failed */
279     /*if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
280         goto fail;*/
281
282     len = sizeof(my_addr);
283     getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
284     s->local_port = udp_port(&my_addr, len);
285
286     if (is_output) {
287         /* limit the tx buf size to limit latency */
288         tmp = s->buffer_size;
289         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
290             av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
291             goto fail;
292         }
293     } else {
294         /* set udp recv buffer size to the largest possible udp packet size to
295          * avoid losing data on OSes that set this too low by default. */
296         tmp = s->buffer_size;
297         if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
298             av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
299         }
300         /* make the socket non-blocking */
301         ff_socket_nonblock(udp_fd, 1);
302     }
303 #endif
304     s->udp_fd = udp_fd;
305     return 0;
306  fail:
307     if (udp_fd >= 0)
308         closesocket(udp_fd);
309     av_free(s);
310     return AVERROR(EIO);
311 }
312
313 static int udp_read(URLContext *h, uint8_t *buf, int size)
314 {
315     UDPContext *s = h->priv_data;
316     unsigned int from;
317         int len;
318
319     return frsh_receive_sync(s->repoint, buf, size, &len, &from);
320 }
321
322 static int udp_write(URLContext *h, uint8_t *buf, int size)
323 {
324     UDPContext *s = h->priv_data;
325     int ret;
326         
327         ret = frsh_send_async(s->sepoint, buf, size);
328         return ret;
329 }
330
331 static int udp_close(URLContext *h)
332 {
333     UDPContext *s = h->priv_data;
334     int ret1, ret2;
335         
336         ret1 = frsh_send_endpoint_destroy(s->sepoint);
337         ret2 = frsh_receive_endpoint_destroy(s->repoint);
338         return (ret1 || ret2);
339 }
340
341 URLProtocol udp_protocol = {
342     "udp",
343     udp_open,
344     udp_read,
345     udp_write,
346     NULL, /* seek */
347     udp_close,
348     .url_get_file_handle = udp_get_file_handle,
349 };