]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/udp.c
udp_socket_create returns socket descriptor
[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
39 typedef struct {
40     int udp_fd;
41     int ttl;
42     int buffer_size;
43     int is_multicast;
44     int local_port;
45     int reuse_socket;
46     struct sockaddr_in dest_addr;
47     int dest_addr_len;
48         
49         frsh_send_endpoint_t sepoint;
50         frsh_send_endpoint_t repoint;
51         frsh_vres_id_t vres;
52         frsh_send_endpoint_protocol_info_t send_pinfo;
53         frsh_contract_t contract;
54         frsh_rel_time_t budget, period;
55 } UDPContext;
56
57 #define UDP_TX_BUF_SIZE 32768
58 #define UDP_MAX_PKT_SIZE 65536
59
60 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) 
61 {
62     return 0;
63 }
64
65 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) 
66 {
67     return 0;
68 }
69
70 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) 
71 {
72     return 0;
73 }
74
75 static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
76 {
77     /* set the destination address */
78     if (resolve_host(&addr->sin_addr, hostname) < 0)
79         return AVERROR(EIO);
80     addr->sin_family = AF_INET;
81     addr->sin_port = htons(port);
82
83     return sizeof(struct sockaddr_in);
84 }
85
86 static int is_multicast_address(struct sockaddr_in *addr)
87 {
88     return 0;
89 }
90
91 static int 
92 udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
93 {
94         int ret,udp_fd;
95
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);
100
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;              
105         
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, 
109                                         &s->sepoint);
110         if (udp_fd < 0) {
111                         
112                         return -1;
113         }
114         
115         /* Contract negotiation */
116         ret = frsh_contract_init(&s->contract);
117         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
118         if (ret) return -1;
119                 
120         ret = frsh_contract_set_basic_params(&s->contract,
121                                              &s->budget,
122                                              &s->period,
123                                              FRSH_WT_BOUNDED,
124                                              FRSH_CT_REGULAR);
125         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
126         if (ret) return -1;
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");
130         if (ret) return -1;
131
132         ret = frsh_contract_negotiate(&s->contract, &s->vres);
133         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
134         if (ret) return -1;
135         
136         printf("Send endpoint created\n");
137         frsh_send_endpoint_bind(s->vres, s->sepoint);
138         printf("Send endpoint bounded\n");
139         
140         //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
141         //              &port, &attr, &fd);
142
143         return udp_fd;
144 }
145
146 static int udp_port(struct sockaddr_in *addr, int len)
147 {
148     return ntohs(addr->sin_port);
149 }
150
151 /**
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.
155  *
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
161  *
162  * @param s1 media file context
163  * @param uri of the remote server
164  * @return zero if no error.
165  */
166 int udp_set_remote_url(URLContext *h, const char *uri)
167 {
168     UDPContext *s = h->priv_data;
169     char hostname[256];
170     int port;
171
172     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
173
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) {
177         return AVERROR(EIO);
178     }
179     s->is_multicast = is_multicast_address(&s->dest_addr);
180
181     return 0;
182 }
183
184 /**
185  * Return the local port used by the UDP connexion
186  * @param s1 media file context
187  * @return the local port number
188  */
189 int udp_get_local_port(URLContext *h)
190 {
191     UDPContext *s = h->priv_data;
192     return s->local_port;
193 }
194
195 /**
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
199  */
200 #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
201 static
202 #endif
203 int udp_get_file_handle(URLContext *h)
204 {
205     UDPContext *s = h->priv_data;
206     return s->udp_fd;
207 }
208
209 /* put it in UDP context */
210 /* return non zero if error */
211 static int udp_open(URLContext *h, const char *uri, int flags)
212 {
213     char hostname[1024];
214     int port, udp_fd = -1, tmp;
215     UDPContext *s = NULL;
216     int is_output;
217     const char *p;
218     char buf[256];
219     struct sockaddr_in my_addr;
220     int len;
221
222     h->is_streamed = 1;
223     h->max_packet_size = 1472;
224
225     is_output = (flags & URL_WRONLY);
226
227     if(!ff_network_init())
228         return AVERROR(EIO);
229
230     s = av_mallocz(sizeof(UDPContext));
231     if (!s)
232         return AVERROR(ENOMEM);
233
234     h->priv_data = s;
235     s->ttl = 16;
236     s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
237
238     p = strchr(uri, '?');
239     if (p) {
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);
243         }
244         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
245             s->local_port = strtol(buf, NULL, 10);
246         }
247         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
248             h->max_packet_size = strtol(buf, NULL, 10);
249         }
250         if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
251             s->buffer_size = strtol(buf, NULL, 10);
252         }
253     }
254
255     /* fill the dest addr */
256     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
257
258     /* XXX: fix url_split */
259     if (hostname[0] == '\0' || hostname[0] == '?') {
260         /* only accepts null hostname if input */
261         if (flags & URL_WRONLY)
262             goto fail;
263     } else {
264         udp_set_remote_url(h, uri);
265     }
266
267     udp_fd = udp_socket_create(s, &my_addr, &len);
268     if (udp_fd < 0)
269         goto fail;
270 #if 0
271     if (s->reuse_socket)
272         if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
273             goto fail;
274
275     /* bind to the local address if not multicast or if the multicast
276      * bind failed */
277     /*if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
278         goto fail;*/
279 #endif
280
281     len = sizeof(my_addr);
282     getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
283     s->local_port = udp_port(&my_addr, len);
284
285     if (is_output) {
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));
290             goto fail;
291         }
292     } else {
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));
298         }
299         /* make the socket non-blocking */
300         ff_socket_nonblock(udp_fd, 1);
301     }
302     s->udp_fd = udp_fd;
303     return 0;
304  
305  fail:
306     if (udp_fd >= 0)
307         closesocket(udp_fd);
308     av_free(s);
309     return AVERROR(EIO);
310 }
311
312 static int udp_read(URLContext *h, uint8_t *buf, int size)
313 {
314     UDPContext *s = h->priv_data;
315     unsigned int from;
316         int len;
317
318     return frsh_receive_sync(s->repoint, buf, size, &len, &from);
319 }
320
321 static int udp_write(URLContext *h, uint8_t *buf, int size)
322 {
323     UDPContext *s = h->priv_data;
324     int ret;
325         
326         ret = frsh_send_async(s->sepoint, buf, size);
327         return ret;
328 }
329
330 static int udp_close(URLContext *h)
331 {
332     UDPContext *s = h->priv_data;
333     int ret1, ret2;
334         
335         ret1 = frsh_send_endpoint_destroy(s->sepoint);
336         ret2 = frsh_receive_endpoint_destroy(s->repoint);
337         return (ret1 || ret2);
338 }
339
340 URLProtocol udp_protocol = {
341     "udp",
342     udp_open,
343     udp_read,
344     udp_write,
345     NULL, /* seek */
346     udp_close,
347     .url_get_file_handle = udp_get_file_handle,
348 };