]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/frsh.c
Do not tell FFMPEG that the packet is bigger than current budget
[frescor/ffmpeg.git] / libavformat / frsh.c
1 /*
2  * FRSH 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/frsh.c
24  * FRSH 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 typedef struct {
40     int frsh_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_receive_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_contract_label_t label;
55         frsh_rel_time_t budget, period, deadline;
56 } FRSHContext;
57
58 #define FRSH_TX_BUF_SIZE 32768
59 #define FRSH_MAX_PKT_SIZE 65536
60
61 static int frsh_set_url(struct sockaddr_in *addr, const char *hostname, int port)
62 {
63     /* set the destination address */
64     if (resolve_host(&addr->sin_addr, hostname) < 0)
65         return AVERROR(EIO);
66     addr->sin_family = AF_INET;
67     addr->sin_port = htons(port);
68
69     return sizeof(struct sockaddr_in);
70 }
71
72 static int is_multicast_address(struct sockaddr_in *addr)
73 {
74     return 0;
75 }
76
77 static int 
78 frsh_output_socket_create(FRSHContext *s)
79 {
80         static long int netcont_num = 0;
81         int ret,frsh_fd;
82         char netcont_name[20];
83         struct sockaddr_in addr;
84         int addr_len;
85
86     addr.sin_family = AF_INET;
87     addr.sin_addr.s_addr = htonl (INADDR_ANY);
88     addr.sin_port = htons(s->local_port);
89     addr_len = sizeof(addr);
90
91         /* set params for contract */
92         s->send_pinfo.body = NULL;              
93         
94         frsh_fd = frsh_send_endpoint_create(FRSH_NETPF_FWP, 
95                                         s->dest_addr.sin_addr.s_addr,
96                                         ntohs(s->dest_addr.sin_port), s->send_pinfo, 
97                                         &s->sepoint);
98         if (frsh_fd < 0) {
99                         
100                         return -1;
101         }
102         
103         /* Contract negotiation */
104         ret = frsh_contract_init(&s->contract);
105         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
106         if (ret) return -1;
107                 
108         ret = frsh_contract_set_basic_params(&s->contract,
109                                              &s->budget,
110                                              &s->period,
111                                              FRSH_WT_BOUNDED,
112                                              FRSH_CT_REGULAR);
113         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
114         if (ret) return -1;
115         snprintf(netcont_name, sizeof(netcont_name), "ffmpeg%02ld", ++netcont_num);
116         ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
117                                                    FRSH_NETPF_FWP,
118                                                    s->label[0] ? s->label : netcont_name);
119         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
120         if (ret) return -1;
121
122         if (frsh_rel_time_to_nsec(s->deadline) > 0) {
123                 ret = frsh_contract_set_timing_reqs(&s->contract, false, &s->deadline);
124                 if (ret) return -1;
125         }
126
127         ret = frsh_contract_negotiate(&s->contract, &s->vres);
128         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
129         if (ret) return -1;
130         
131         printf("Send endpoint created\n");
132         frsh_send_endpoint_bind(s->vres, s->sepoint);
133         printf("Send endpoint bounded\n");
134         
135         //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
136         //              &port, &attr, &fd);
137
138         return frsh_fd;
139 }
140
141 static int 
142 frsh_input_socket_create(FRSHContext *s)
143 {
144         frsh_receive_endpoint_protocol_info_t pi = { NULL, 0 };
145         frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
146         
147         return frsh_receive_endpoint_create(FRSH_NETPF_FWP, s->local_port, qi, pi,
148                                             &s->repoint);
149 }
150                 
151
152
153 static int frsh_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: frsh://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 frsh_set_remote_url(URLContext *h, const char *uri)
174 {
175     FRSHContext *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 = frsh_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 FRSH connexion
193  * @param s1 media file context
194  * @return the local port number
195  */
196 int frsh_get_local_port(URLContext *h)
197 {
198     FRSHContext *s = h->priv_data;
199     return s->local_port;
200 }
201
202 /**
203  * Return the frsh 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 frsh_get_file_handle(URLContext *h)
211 {
212     FRSHContext *s = h->priv_data;
213     return s->frsh_fd;
214 }
215
216 /* put it in FRSH context */
217 /* return non zero if error */
218 static int frsh_open(URLContext *h, const char *uri, int flags)
219 {
220     char hostname[1024];
221     int port, frsh_fd = -1, tmp;
222     FRSHContext *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(FRSHContext));
238     if (!s)
239         return AVERROR(ENOMEM);
240
241     h->priv_data = s;
242     s->ttl = 16;
243     s->buffer_size = is_output ? FRSH_TX_BUF_SIZE : FRSH_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         if (find_info_tag(buf, sizeof(buf), "budget", p)) {
265                 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, strtol(buf, NULL, 10), &s->budget);
266         }
267         if (find_info_tag(buf, sizeof(buf), "period", p)) {
268                 s->period=frsh_msec_to_rel_time(strtol(buf, NULL, 10));
269         }
270         if (find_info_tag(buf, sizeof(buf), "deadline", p)) {
271                 s->deadline=frsh_msec_to_rel_time(strtol(buf, NULL, 10));
272         }
273     }
274
275     /* fill the dest addr */
276     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
277
278     /* XXX: fix url_split */
279     if (hostname[0] == '\0' || hostname[0] == '?') {
280         /* only accepts null hostname if input */
281         if (flags & URL_WRONLY)
282             goto fail;
283     } else {
284         frsh_set_remote_url(h, uri);
285     }
286
287     if (is_output)
288             frsh_fd = frsh_output_socket_create(s);
289     else
290             frsh_fd = frsh_input_socket_create(s);
291     if (frsh_fd < 0)
292         goto fail;
293 #if 0
294     if (s->reuse_socket)
295         if (setsockopt (frsh_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
296             goto fail;
297
298     /* bind to the local address if not multicast or if the multicast
299      * bind failed */
300     /*if (bind_ret < 0 && bind(frsh_fd,(struct sockaddr *)&my_addr, len) < 0)
301         goto fail;*/
302 #endif
303
304     len = sizeof(my_addr);
305     getsockname(frsh_fd, (struct sockaddr *)&my_addr, &len);
306     s->local_port = frsh_port(&my_addr, len);
307
308     if (is_output) {
309         /* limit the tx buf size to limit latency */
310         tmp = s->buffer_size;
311         if (setsockopt(frsh_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
312             av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
313             goto fail;
314         }
315     } else {
316         /* set frsh recv buffer size to the largest possible frsh packet size to
317          * avoid losing data on OSes that set this too low by default. */
318         tmp = s->buffer_size;
319         if (setsockopt(frsh_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
320             av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
321         }
322         /* make the socket non-blocking */
323         ff_socket_nonblock(frsh_fd, 1);
324     }
325     s->frsh_fd = frsh_fd;
326     return 0;
327  
328  fail:
329     if (frsh_fd >= 0)
330         closesocket(frsh_fd);
331     av_free(s);
332     return AVERROR(EIO);
333 }
334
335 static int frsh_read(URLContext *h, uint8_t *buf, int size)
336 {
337     FRSHContext *s = h->priv_data;
338     unsigned int from;
339         int len;
340
341     return frsh_receive_sync(s->repoint, buf, size, &len, &from);
342 }
343
344 static int frsh_write(URLContext *h, uint8_t *buf, int size)
345 {
346     FRSHContext *s = h->priv_data;
347     int ret;
348         
349         ret = frsh_send_sync(s->sepoint, buf, size);
350         /* Do not tell FFMPEG that the packet is bigger than current
351          * budget. */
352         if (ret < 0 && errno == ENOSR)
353                 ret = 0;
354         return ret;
355 }
356
357 static int frsh_close(URLContext *h)
358 {
359     FRSHContext *s = h->priv_data;
360     int ret;
361         
362         frsh_send_endpoint_unbind(s->sepoint);
363         ret = frsh_contract_cancel(s->vres);
364         frsh_contract_destroy(s->contract);
365         return ret;
366 }
367
368 URLProtocol frsh_protocol = {
369     "frsh",
370     frsh_open,
371     frsh_read,
372     frsh_write,
373     NULL, /* seek */
374     frsh_close,
375     .url_get_file_handle = frsh_get_file_handle,
376 };