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;
150 is_output = (flags & URL_WRONLY);
152 s = av_mallocz(sizeof(RTPContext));
154 return AVERROR(ENOMEM);
157 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
158 path, sizeof(path), uri);
159 /* extract parameters */
162 max_packet_size = -1;
164 p = strchr(uri, '?');
166 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
167 ttl = strtol(buf, NULL, 10);
169 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
170 local_port = strtol(buf, NULL, 10);
172 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
173 max_packet_size = strtol(buf, NULL, 10);
177 build_udp_url(buf, sizeof(buf),
178 hostname, port, local_port, ttl, max_packet_size, "RTP",
179 frsh_rtp_budget, frsh_rtp_period_ms, frsh_rtp_deadline_ms);
180 if (url_open(&s->rtp_hd, buf, flags) < 0)
182 local_port = udp_get_local_port(s->rtp_hd);
183 /* XXX: need to open another connection if the port is not even */
185 /* well, should suppress localport in path */
187 build_udp_url(buf, sizeof(buf),
188 hostname, port + 1, local_port + 1, ttl, max_packet_size, "RTCP",
190 if (url_open(&s->rtcp_hd, buf, flags) < 0)
193 /* just to ease handle access. XXX: need to suppress direct handle
195 s->rtp_fd = url_get_file_handle(s->rtp_hd);
196 s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
198 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
204 url_close(s->rtp_hd);
206 url_close(s->rtcp_hd);
211 static int rtp_read(URLContext *h, uint8_t *buf, int size)
213 RTPContext *s = h->priv_data;
214 struct sockaddr_in from;
220 from_len = sizeof(from);
221 len = recvfrom (s->rtp_fd, buf, size, 0,
222 (struct sockaddr *)&from, &from_len);
224 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
225 ff_neterrno() == FF_NETERROR(EINTR))
233 /* build fdset to listen to RTP and RTCP packets */
236 FD_SET(s->rtp_fd, &rfds);
237 if (s->rtcp_fd > fd_max)
239 FD_SET(s->rtcp_fd, &rfds);
240 n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
243 if (FD_ISSET(s->rtcp_fd, &rfds)) {
244 from_len = sizeof(from);
245 len = recvfrom (s->rtcp_fd, buf, size, 0,
246 (struct sockaddr *)&from, &from_len);
248 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
249 ff_neterrno() == FF_NETERROR(EINTR))
256 if (FD_ISSET(s->rtp_fd, &rfds)) {
257 from_len = sizeof(from);
258 len = recvfrom (s->rtp_fd, buf, size, 0,
259 (struct sockaddr *)&from, &from_len);
261 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
262 ff_neterrno() == FF_NETERROR(EINTR))
274 static int rtp_write(URLContext *h, uint8_t *buf, int size)
276 RTPContext *s = h->priv_data;
280 if (buf[1] >= 200 && buf[1] <= 204) {
281 /* RTCP payload type */
284 /* RTP payload type */
288 ret = url_write(hd, buf, size);
293 ts.tv_nsec = 10 * 1000000;
294 nanosleep(&ts, NULL);
300 static int rtp_close(URLContext *h)
302 RTPContext *s = h->priv_data;
304 url_close(s->rtp_hd);
305 url_close(s->rtcp_hd);
311 * Return the local port used by the RTP connection
312 * @param s1 media file context
313 * @return the local port number
316 int rtp_get_local_port(URLContext *h)
318 RTPContext *s = h->priv_data;
319 return udp_get_local_port(s->rtp_hd);
322 #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
324 * Return the rtp and rtcp file handles for select() usage to wait for
325 * several RTP streams at the same time.
326 * @param h media file context
329 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
331 RTPContext *s = h->priv_data;
333 *prtp_fd = s->rtp_fd;
334 *prtcp_fd = s->rtcp_fd;
338 static int rtp_get_file_handle(URLContext *h)
340 RTPContext *s = h->priv_data;
344 URLProtocol rtp_protocol = {
351 .url_get_file_handle = rtp_get_file_handle,