3 * Copyright (c) 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
22 int frsh_rtp_budget=0, frsh_rtp_period_ms=0, frsh_rtp_deadline_ms=0;
25 * @file libavformat/rtpproto.c
29 #include "libavutil/avstring.h"
35 #include "os_support.h"
38 #include <sys/select.h>
41 #define RTP_TX_BUF_SIZE (64 * 1024)
42 #define RTP_RX_BUF_SIZE (128 * 1024)
45 #include "libavformat_config.h"
48 #ifdef CONFIG_FFMPEG_WITH_FRSH
49 #define URL_PROTO "frsh"
51 #define URL_PROTO "udp"
54 typedef struct RTPContext {
55 URLContext *rtp_hd, *rtcp_hd;
60 * If no filename is given to av_open_input_file because you want to
61 * get the local port first, then you must call this function to set
62 * the remote server address.
64 * @param s1 media file context
65 * @param uri of the remote server
66 * @return zero if no error.
69 int rtp_set_remote_url(URLContext *h, const char *uri)
71 RTPContext *s = h->priv_data;
78 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
79 path, sizeof(path), uri);
81 snprintf(buf, sizeof(buf), URL_PROTO"://%s:%d%s", hostname, port, path);
82 udp_set_remote_url(s->rtp_hd, buf);
84 snprintf(buf, sizeof(buf), URL_PROTO"://%s:%d%s", hostname, port + 1, path);
85 udp_set_remote_url(s->rtcp_hd, buf);
91 * add option to url of the form:
92 * "http://host:port/path?option1=val1&option2=val2...
95 static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
101 if (strchr(buf, '?'))
102 av_strlcat(buf, "&", buf_size);
104 av_strlcat(buf, "?", buf_size);
105 vsnprintf(buf1, sizeof(buf1), fmt, ap);
106 av_strlcat(buf, buf1, buf_size);
110 static void build_udp_url(char *buf, int buf_size,
111 const char *hostname, int port,
112 int local_port, int ttl,
114 const char *label, int budget, int period_ms, int deadline_ms)
116 snprintf(buf, buf_size, URL_PROTO"://%s:%d", hostname, port);
118 url_add_option(buf, buf_size, "localport=%d", local_port);
120 url_add_option(buf, buf_size, "ttl=%d", ttl);
121 if (max_packet_size >=0)
122 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
124 url_add_option(buf, buf_size, "contract_label=%s", label);
126 url_add_option(buf, buf_size, "budget=%d", budget);
128 url_add_option(buf, buf_size, "period=%d", period_ms);
130 url_add_option(buf, buf_size, "deadline=%d", deadline_ms);
134 * url syntax: rtp://host:port[?option=val...]
135 * option: 'ttl=n' : set the ttl value (for multicast only)
136 * 'localport=n' : set the local port to n
137 * 'pkt_size=n' : set max packet size
141 static int rtp_open(URLContext *h, const char *uri, int flags)
144 int port, is_output, ttl, local_port, max_packet_size;
149 int budget=frsh_rtp_budget;
150 int period_ms=frsh_rtp_period_ms;
151 int deadline_ms=frsh_rtp_deadline_ms;
153 is_output = (flags & URL_WRONLY);
155 s = av_mallocz(sizeof(RTPContext));
157 return AVERROR(ENOMEM);
160 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
161 path, sizeof(path), uri);
162 /* extract parameters */
165 max_packet_size = -1;
167 p = strchr(uri, '?');
169 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
170 ttl = strtol(buf, NULL, 10);
172 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
173 local_port = strtol(buf, NULL, 10);
175 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
176 max_packet_size = strtol(buf, NULL, 10);
178 if (find_info_tag(buf, sizeof(buf), "budget", p)) {
179 budget = strtol(buf, NULL, 10);
181 if (find_info_tag(buf, sizeof(buf), "period", p)) {
182 period_ms = strtol(buf, NULL, 10);
184 if (find_info_tag(buf, sizeof(buf), "deadline", p)) {
185 deadline_ms = strtol(buf, NULL, 10);
189 build_udp_url(buf, sizeof(buf),
190 hostname, port, local_port, ttl, max_packet_size, "RTP",
191 budget, period_ms, deadline_ms);
192 if (url_open(&s->rtp_hd, buf, flags) < 0)
194 local_port = udp_get_local_port(s->rtp_hd);
195 /* XXX: need to open another connection if the port is not even */
197 /* well, should suppress localport in path */
199 build_udp_url(buf, sizeof(buf),
200 hostname, port + 1, local_port + 1, ttl, max_packet_size, "RTCP",
202 if (url_open(&s->rtcp_hd, buf, flags) < 0)
205 /* just to ease handle access. XXX: need to suppress direct handle
207 s->rtp_fd = url_get_file_handle(s->rtp_hd);
208 s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
210 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
216 url_close(s->rtp_hd);
218 url_close(s->rtcp_hd);
223 static int rtp_read(URLContext *h, uint8_t *buf, int size)
225 RTPContext *s = h->priv_data;
226 struct sockaddr_in from;
232 from_len = sizeof(from);
233 len = recvfrom (s->rtp_fd, buf, size, 0,
234 (struct sockaddr *)&from, &from_len);
236 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
237 ff_neterrno() == FF_NETERROR(EINTR))
245 /* build fdset to listen to RTP and RTCP packets */
248 FD_SET(s->rtp_fd, &rfds);
249 if (s->rtcp_fd > fd_max)
251 FD_SET(s->rtcp_fd, &rfds);
252 n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
255 if (FD_ISSET(s->rtcp_fd, &rfds)) {
256 from_len = sizeof(from);
257 len = recvfrom (s->rtcp_fd, buf, size, 0,
258 (struct sockaddr *)&from, &from_len);
260 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
261 ff_neterrno() == FF_NETERROR(EINTR))
268 if (FD_ISSET(s->rtp_fd, &rfds)) {
269 from_len = sizeof(from);
270 len = recvfrom (s->rtp_fd, buf, size, 0,
271 (struct sockaddr *)&from, &from_len);
273 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
274 ff_neterrno() == FF_NETERROR(EINTR))
286 static int rtp_write(URLContext *h, uint8_t *buf, int size)
288 RTPContext *s = h->priv_data;
292 if (buf[1] >= 200 && buf[1] <= 204) {
293 /* RTCP payload type */
296 /* RTP payload type */
300 ret = url_write(hd, buf, size);
305 ts.tv_nsec = 10 * 1000000;
306 nanosleep(&ts, NULL);
312 static int rtp_close(URLContext *h)
314 RTPContext *s = h->priv_data;
316 url_close(s->rtp_hd);
317 url_close(s->rtcp_hd);
323 * Return the local port used by the RTP connection
324 * @param s1 media file context
325 * @return the local port number
328 int rtp_get_local_port(URLContext *h)
330 RTPContext *s = h->priv_data;
331 return udp_get_local_port(s->rtp_hd);
334 #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
336 * Return the rtp and rtcp file handles for select() usage to wait for
337 * several RTP streams at the same time.
338 * @param h media file context
341 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
343 RTPContext *s = h->priv_data;
345 *prtp_fd = s->rtp_fd;
346 *prtcp_fd = s->rtcp_fd;
350 static int rtp_get_file_handle(URLContext *h)
352 RTPContext *s = h->priv_data;
356 URLProtocol rtp_protocol = {
363 .url_get_file_handle = rtp_get_file_handle,