]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/frsh.c
frsh: Print error messages
[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 void print_error(int err, char *str)
78 {
79     char error_string[1024];
80     frsh_strerror(err , error_string, sizeof(error_string));
81     av_log(NULL, AV_LOG_ERROR, "%s: error %d: %s\n", str, err, error_string);
82 }
83
84 static int 
85 frsh_output_socket_create(FRSHContext *s)
86 {
87         static long int netcont_num = 0;
88         int ret,frsh_fd;
89         char netcont_name[20];
90         struct sockaddr_in addr;
91         int addr_len;
92
93     addr.sin_family = AF_INET;
94     addr.sin_addr.s_addr = htonl (INADDR_ANY);
95     addr.sin_port = htons(s->local_port);
96     addr_len = sizeof(addr);
97
98         /* set params for contract */
99         s->send_pinfo.body = NULL;              
100         
101         frsh_fd = frsh_send_endpoint_create(FRSH_NETPF_FWP, 
102                                         s->dest_addr.sin_addr.s_addr,
103                                         ntohs(s->dest_addr.sin_port), s->send_pinfo, 
104                                         &s->sepoint);
105         if (frsh_fd < 0) {
106                         
107                         return -1;
108         }
109         
110         /* Contract negotiation */
111         ret = frsh_contract_init(&s->contract);
112         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
113         if (ret) return -1;
114                 
115         ret = frsh_contract_set_basic_params(&s->contract,
116                                              &s->budget,
117                                              &s->period,
118                                              FRSH_WT_BOUNDED,
119                                              FRSH_CT_REGULAR);
120         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
121         if (ret) return -1;
122         snprintf(netcont_name, sizeof(netcont_name), "ffmpeg%02ld", ++netcont_num);
123         ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
124                                                    FRSH_NETPF_FWP,
125                                                    s->label[0] ? s->label : netcont_name);
126         //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
127         if (ret) return -1;
128
129         if (frsh_rel_time_to_nsec(s->deadline) > 0) {
130                 ret = frsh_contract_set_timing_reqs(&s->contract, false, &s->deadline);
131                 if (ret) return -1;
132         }
133
134         ret = frsh_contract_negotiate(&s->contract, &s->vres);
135         if (ret) print_error(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 frsh_fd;
146 }
147
148 static int 
149 frsh_input_socket_create(FRSHContext *s)
150 {
151         frsh_receive_endpoint_protocol_info_t pi = { NULL, 0 };
152         frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
153         
154         return frsh_receive_endpoint_create(FRSH_NETPF_FWP, s->local_port, qi, pi,
155                                             &s->repoint);
156 }
157                 
158
159
160 static int frsh_port(struct sockaddr_in *addr, int len)
161 {
162     return ntohs(addr->sin_port);
163 }
164
165 /**
166  * If no filename is given to av_open_input_file because you want to
167  * get the local port first, then you must call this function to set
168  * the remote server address.
169  *
170  * url syntax: frsh://host:port[?option=val...]
171  * option: 'ttl=n'       : set the ttl value (for multicast only)
172  *         'localport=n' : set the local port
173  *         'pkt_size=n'  : set max packet size
174  *         'reuse=1'     : enable reusing the socket
175  *
176  * @param s1 media file context
177  * @param uri of the remote server
178  * @return zero if no error.
179  */
180 int frsh_set_remote_url(URLContext *h, const char *uri)
181 {
182     FRSHContext *s = h->priv_data;
183     char hostname[256];
184     int port;
185
186     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
187
188     /* set the destination address */
189     s->dest_addr_len = frsh_set_url(&s->dest_addr, hostname, port);
190     if (s->dest_addr_len < 0) {
191         return AVERROR(EIO);
192     }
193     s->is_multicast = is_multicast_address(&s->dest_addr);
194
195     return 0;
196 }
197
198 /**
199  * Return the local port used by the FRSH connexion
200  * @param s1 media file context
201  * @return the local port number
202  */
203 int frsh_get_local_port(URLContext *h)
204 {
205     FRSHContext *s = h->priv_data;
206     return s->local_port;
207 }
208
209 /**
210  * Return the frsh file handle for select() usage to wait for several RTP
211  * streams at the same time.
212  * @param h media file context
213  */
214 #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
215 static
216 #endif
217 int frsh_get_file_handle(URLContext *h)
218 {
219     FRSHContext *s = h->priv_data;
220     return s->frsh_fd;
221 }
222
223 /* put it in FRSH context */
224 /* return non zero if error */
225 static int frsh_open(URLContext *h, const char *uri, int flags)
226 {
227     char hostname[1024];
228     int port, frsh_fd = -1, tmp;
229     FRSHContext *s = NULL;
230     int is_output;
231     const char *p;
232     char buf[256];
233     struct sockaddr_in my_addr;
234     int len;
235
236     h->is_streamed = 1;
237     h->max_packet_size = 1472;
238
239     is_output = (flags & URL_WRONLY);
240
241     if(!ff_network_init())
242         return AVERROR(EIO);
243
244     s = av_mallocz(sizeof(FRSHContext));
245     if (!s)
246         return AVERROR(ENOMEM);
247
248     h->priv_data = s;
249     s->ttl = 16;
250     s->buffer_size = is_output ? FRSH_TX_BUF_SIZE : FRSH_MAX_PKT_SIZE;
251
252     p = strchr(uri, '?');
253     if (p) {
254         s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
255         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
256             s->ttl = strtol(buf, NULL, 10);
257         }
258         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
259             s->local_port = strtol(buf, NULL, 10);
260         }
261         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
262             h->max_packet_size = strtol(buf, NULL, 10);
263         }
264         if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
265             s->buffer_size = strtol(buf, NULL, 10);
266         }
267         if (!find_info_tag(s->label, sizeof(s->label),
268                           "contract_label", p)) {
269                 s->label[0]=0;
270         }
271         if (find_info_tag(buf, sizeof(buf), "budget", p)) {
272                 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, strtol(buf, NULL, 10), &s->budget);
273         }
274         if (find_info_tag(buf, sizeof(buf), "period", p)) {
275                 s->period=frsh_msec_to_rel_time(strtol(buf, NULL, 10));
276         }
277         if (find_info_tag(buf, sizeof(buf), "deadline", p)) {
278                 s->deadline=frsh_msec_to_rel_time(strtol(buf, NULL, 10));
279         }
280     }
281
282     /* fill the dest addr */
283     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
284
285     /* XXX: fix url_split */
286     if (hostname[0] == '\0' || hostname[0] == '?') {
287         /* only accepts null hostname if input */
288         if (flags & URL_WRONLY)
289             goto fail;
290     } else {
291         frsh_set_remote_url(h, uri);
292     }
293
294     if (is_output)
295             frsh_fd = frsh_output_socket_create(s);
296     else
297             frsh_fd = frsh_input_socket_create(s);
298     if (frsh_fd < 0)
299         goto fail;
300 #if 0
301     if (s->reuse_socket)
302         if (setsockopt (frsh_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
303             goto fail;
304
305     /* bind to the local address if not multicast or if the multicast
306      * bind failed */
307     /*if (bind_ret < 0 && bind(frsh_fd,(struct sockaddr *)&my_addr, len) < 0)
308         goto fail;*/
309 #endif
310
311     len = sizeof(my_addr);
312     getsockname(frsh_fd, (struct sockaddr *)&my_addr, &len);
313     s->local_port = frsh_port(&my_addr, len);
314
315     if (is_output) {
316         /* limit the tx buf size to limit latency */
317         tmp = s->buffer_size;
318         if (setsockopt(frsh_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
319             av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
320             goto fail;
321         }
322     } else {
323         /* set frsh recv buffer size to the largest possible frsh packet size to
324          * avoid losing data on OSes that set this too low by default. */
325         tmp = s->buffer_size;
326         if (setsockopt(frsh_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
327             av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
328         }
329         /* make the socket non-blocking */
330         ff_socket_nonblock(frsh_fd, 1);
331     }
332     s->frsh_fd = frsh_fd;
333     return 0;
334  
335  fail:
336     if (frsh_fd >= 0)
337         closesocket(frsh_fd);
338     av_free(s);
339     return AVERROR(EIO);
340 }
341
342 static int frsh_read(URLContext *h, uint8_t *buf, int size)
343 {
344     FRSHContext *s = h->priv_data;
345     unsigned int from;
346         int len;
347
348     return frsh_receive_sync(s->repoint, buf, size, &len, &from);
349 }
350
351 static int frsh_write(URLContext *h, uint8_t *buf, int size)
352 {
353     FRSHContext *s = h->priv_data;
354     int ret;
355         
356         ret = frsh_send_sync(s->sepoint, buf, size);
357         /* Do not tell FFMPEG that the packet is bigger than current
358          * budget. */
359         if (ret < 0 && errno == ENOSR)
360                 ret = 0;
361         return ret;
362 }
363
364 static int frsh_close(URLContext *h)
365 {
366     FRSHContext *s = h->priv_data;
367     int ret;
368         
369         frsh_send_endpoint_unbind(s->sepoint);
370         ret = frsh_contract_cancel(s->vres);
371         frsh_contract_destroy(s->contract);
372         return ret;
373 }
374
375 URLProtocol frsh_protocol = {
376     "frsh",
377     frsh_open,
378     frsh_read,
379     frsh_write,
380     NULL, /* seek */
381     frsh_close,
382     .url_get_file_handle = frsh_get_file_handle,
383 };