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