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