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
27 #include "libavutil/parseutils.h"
28 #include "libavutil/avstring.h"
30 #include "avio_internal.h"
37 #include "os_support.h"
43 typedef struct RTPContext {
44 URLContext *rtp_hd, *rtcp_hd;
49 * If no filename is given to av_open_input_file because you want to
50 * get the local port first, then you must call this function to set
51 * the remote server address.
53 * @param h media file context
54 * @param uri of the remote server
55 * @return zero if no error.
58 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
60 RTPContext *s = h->priv_data;
67 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
68 path, sizeof(path), uri);
70 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
71 ff_udp_set_remote_url(s->rtp_hd, buf);
73 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
74 ff_udp_set_remote_url(s->rtcp_hd, buf);
80 * add option to url of the form:
81 * "http://host:port/path?option1=val1&option2=val2...
84 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
91 av_strlcat(buf, "&", buf_size);
93 av_strlcat(buf, "?", buf_size);
94 vsnprintf(buf1, sizeof(buf1), fmt, ap);
95 av_strlcat(buf, buf1, buf_size);
99 static void build_udp_url(char *buf, int buf_size,
100 const char *hostname, int port,
101 int local_port, int ttl,
102 int max_packet_size, int connect)
104 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
106 url_add_option(buf, buf_size, "localport=%d", local_port);
108 url_add_option(buf, buf_size, "ttl=%d", ttl);
109 if (max_packet_size >=0)
110 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
112 url_add_option(buf, buf_size, "connect=1");
113 url_add_option(buf, buf_size, "fifo_size=0");
117 * url syntax: rtp://host:port[?option=val...]
118 * option: 'ttl=n' : set the ttl value (for multicast only)
119 * 'rtcpport=n' : set the remote rtcp port to n
120 * 'localrtpport=n' : set the local rtp port to n
121 * 'localrtcpport=n' : set the local rtcp port to n
122 * 'pkt_size=n' : set max packet size
123 * 'connect=0/1' : do a connect() on the UDP socket
125 * 'localport=n' : set the local port to n
127 * if rtcpport isn't set the rtcp port will be the rtp port + 1
128 * if local rtp port isn't set any available port will be used for the local
130 * if the local rtcp port is not set it will be the local rtp port + 1
133 static int rtp_open(URLContext *h, const char *uri, int flags)
135 RTPContext *s = h->priv_data;
136 int rtp_port, rtcp_port,
138 local_rtp_port, local_rtcp_port, max_packet_size;
144 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
145 path, sizeof(path), uri);
146 /* extract parameters */
148 rtcp_port = rtp_port+1;
150 local_rtcp_port = -1;
151 max_packet_size = -1;
154 p = strchr(uri, '?');
156 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
157 ttl = strtol(buf, NULL, 10);
159 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
160 rtcp_port = strtol(buf, NULL, 10);
162 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
163 local_rtp_port = strtol(buf, NULL, 10);
165 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
166 local_rtp_port = strtol(buf, NULL, 10);
168 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
169 local_rtcp_port = strtol(buf, NULL, 10);
171 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
172 max_packet_size = strtol(buf, NULL, 10);
174 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
175 connect = strtol(buf, NULL, 10);
179 build_udp_url(buf, sizeof(buf),
180 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
182 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
184 if (local_rtp_port>=0 && local_rtcp_port<0)
185 local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
187 build_udp_url(buf, sizeof(buf),
188 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
190 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
193 /* just to ease handle access. XXX: need to suppress direct handle
195 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
196 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
198 h->max_packet_size = s->rtp_hd->max_packet_size;
204 ffurl_close(s->rtp_hd);
206 ffurl_close(s->rtcp_hd);
210 static int rtp_read(URLContext *h, uint8_t *buf, int size)
212 RTPContext *s = h->priv_data;
213 struct sockaddr_storage from;
216 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
219 if (ff_check_interrupt(&h->interrupt_callback))
221 /* build fdset to listen to RTP and RTCP packets */
225 if (p[1].revents & POLLIN) {
226 from_len = sizeof(from);
227 len = recvfrom (s->rtcp_fd, buf, size, 0,
228 (struct sockaddr *)&from, &from_len);
230 if (ff_neterrno() == AVERROR(EAGAIN) ||
231 ff_neterrno() == AVERROR(EINTR))
238 if (p[0].revents & POLLIN) {
239 from_len = sizeof(from);
240 len = recvfrom (s->rtp_fd, buf, size, 0,
241 (struct sockaddr *)&from, &from_len);
243 if (ff_neterrno() == AVERROR(EAGAIN) ||
244 ff_neterrno() == AVERROR(EINTR))
251 if (ff_neterrno() == AVERROR(EINTR))
259 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
261 RTPContext *s = h->priv_data;
265 if (RTP_PT_IS_RTCP(buf[1])) {
266 /* RTCP payload type */
269 /* RTP payload type */
273 ret = ffurl_write(hd, buf, size);
277 static int rtp_close(URLContext *h)
279 RTPContext *s = h->priv_data;
281 ffurl_close(s->rtp_hd);
282 ffurl_close(s->rtcp_hd);
287 * Return the local rtp port used by the RTP connection
288 * @param h media file context
289 * @return the local port number
292 int ff_rtp_get_local_rtp_port(URLContext *h)
294 RTPContext *s = h->priv_data;
295 return ff_udp_get_local_port(s->rtp_hd);
299 * Return the local rtcp port used by the RTP connection
300 * @param h media file context
301 * @return the local port number
304 int ff_rtp_get_local_rtcp_port(URLContext *h)
306 RTPContext *s = h->priv_data;
307 return ff_udp_get_local_port(s->rtcp_hd);
310 static int rtp_get_file_handle(URLContext *h)
312 RTPContext *s = h->priv_data;
316 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
319 RTPContext *s = h->priv_data;
320 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
322 return AVERROR(ENOMEM);
329 URLProtocol ff_rtp_protocol = {
331 .url_open = rtp_open,
332 .url_read = rtp_read,
333 .url_write = rtp_write,
334 .url_close = rtp_close,
335 .url_get_file_handle = rtp_get_file_handle,
336 .url_get_multi_file_handle = rtp_get_multi_file_handle,
337 .priv_data_size = sizeof(RTPContext),
338 .flags = URL_PROTOCOL_FLAG_NETWORK,