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