2 * FRSH prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file libavformat/frsh.c
30 #include "os_support.h"
32 #include <sys/select.h>
37 #include <frsh_core_types.h>
46 struct sockaddr_in dest_addr;
49 frsh_send_endpoint_t sepoint;
50 frsh_receive_endpoint_t repoint;
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;
58 #define FRSH_TX_BUF_SIZE 32768
59 #define FRSH_MAX_PKT_SIZE 65536
61 static int frsh_set_url(struct sockaddr_in *addr, const char *hostname, int port)
63 /* set the destination address */
64 if (resolve_host(&addr->sin_addr, hostname) < 0)
66 addr->sin_family = AF_INET;
67 addr->sin_port = htons(port);
69 return sizeof(struct sockaddr_in);
72 static int is_multicast_address(struct sockaddr_in *addr)
78 frsh_output_socket_create(FRSHContext *s)
80 static long int netcont_num = 0;
82 char netcont_name[20];
83 struct sockaddr_in addr;
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);
91 /* set params for contract */
92 s->send_pinfo.body = NULL;
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,
103 /* Contract negotiation */
104 ret = frsh_contract_init(&s->contract);
105 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
108 ret = frsh_contract_set_basic_params(&s->contract,
113 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
115 snprintf(netcont_name, sizeof(netcont_name), "ffmpeg%02ld", ++netcont_num);
116 ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
118 s->label[0] ? s->label : netcont_name);
119 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
122 if (frsh_rel_time_to_nsec(s->deadline) > 0) {
123 ret = frsh_contract_set_timing_reqs(&s->contract, false, &s->deadline);
127 ret = frsh_contract_negotiate(&s->contract, &s->vres);
128 //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
131 printf("Send endpoint created\n");
132 frsh_send_endpoint_bind(s->vres, s->sepoint);
133 printf("Send endpoint bounded\n");
135 //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
136 // &port, &attr, &fd);
142 frsh_input_socket_create(FRSHContext *s)
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 };
147 return frsh_receive_endpoint_create(FRSH_NETPF_FWP, s->local_port, qi, pi,
153 static int frsh_port(struct sockaddr_in *addr, int len)
155 return ntohs(addr->sin_port);
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.
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
169 * @param s1 media file context
170 * @param uri of the remote server
171 * @return zero if no error.
173 int frsh_set_remote_url(URLContext *h, const char *uri)
175 FRSHContext *s = h->priv_data;
179 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
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) {
186 s->is_multicast = is_multicast_address(&s->dest_addr);
192 * Return the local port used by the FRSH connexion
193 * @param s1 media file context
194 * @return the local port number
196 int frsh_get_local_port(URLContext *h)
198 FRSHContext *s = h->priv_data;
199 return s->local_port;
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
207 #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
210 int frsh_get_file_handle(URLContext *h)
212 FRSHContext *s = h->priv_data;
216 /* put it in FRSH context */
217 /* return non zero if error */
218 static int frsh_open(URLContext *h, const char *uri, int flags)
221 int port, frsh_fd = -1, tmp;
222 FRSHContext *s = NULL;
226 struct sockaddr_in my_addr;
230 h->max_packet_size = 1472;
232 is_output = (flags & URL_WRONLY);
234 if(!ff_network_init())
237 s = av_mallocz(sizeof(FRSHContext));
239 return AVERROR(ENOMEM);
243 s->buffer_size = is_output ? FRSH_TX_BUF_SIZE : FRSH_MAX_PKT_SIZE;
245 p = strchr(uri, '?');
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);
251 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
252 s->local_port = strtol(buf, NULL, 10);
254 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
255 h->max_packet_size = strtol(buf, NULL, 10);
257 if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
258 s->buffer_size = strtol(buf, NULL, 10);
260 if (!find_info_tag(s->label, sizeof(s->label),
261 "contract_label", p)) {
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);
267 if (find_info_tag(buf, sizeof(buf), "period", p)) {
268 s->period=frsh_msec_to_rel_time(strtol(buf, NULL, 10));
270 if (find_info_tag(buf, sizeof(buf), "deadline", p)) {
271 s->deadline=frsh_msec_to_rel_time(strtol(buf, NULL, 10));
275 /* fill the dest addr */
276 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
278 /* XXX: fix url_split */
279 if (hostname[0] == '\0' || hostname[0] == '?') {
280 /* only accepts null hostname if input */
281 if (flags & URL_WRONLY)
284 frsh_set_remote_url(h, uri);
288 frsh_fd = frsh_output_socket_create(s);
290 frsh_fd = frsh_input_socket_create(s);
295 if (setsockopt (frsh_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
298 /* bind to the local address if not multicast or if the multicast
300 /*if (bind_ret < 0 && bind(frsh_fd,(struct sockaddr *)&my_addr, len) < 0)
304 len = sizeof(my_addr);
305 getsockname(frsh_fd, (struct sockaddr *)&my_addr, &len);
306 s->local_port = frsh_port(&my_addr, len);
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));
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));
322 /* make the socket non-blocking */
323 ff_socket_nonblock(frsh_fd, 1);
325 s->frsh_fd = frsh_fd;
330 closesocket(frsh_fd);
335 static int frsh_read(URLContext *h, uint8_t *buf, int size)
337 FRSHContext *s = h->priv_data;
341 return frsh_receive_sync(s->repoint, buf, size, &len, &from);
344 static int frsh_write(URLContext *h, uint8_t *buf, int size)
346 FRSHContext *s = h->priv_data;
349 ret = frsh_send_sync(s->sepoint, buf, size);
350 /* Do not tell FFMPEG that the packet is bigger than current
352 if (ret < 0 && errno == ENOSR)
357 static int frsh_close(URLContext *h)
359 FRSHContext *s = h->priv_data;
362 frsh_send_endpoint_unbind(s->sepoint);
363 ret = frsh_contract_cancel(s->vres);
364 frsh_contract_destroy(s->contract);
368 URLProtocol frsh_protocol = {
375 .url_get_file_handle = frsh_get_file_handle,