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"
29 #include "libavutil/opt.h"
39 #include "os_support.h"
45 typedef struct RTPContext {
47 URLContext *rtp_hd, *rtcp_hd, *fec_hd;
49 IPSourceFilters filters;
51 struct sockaddr_storage last_rtp_source, last_rtcp_source;
52 socklen_t last_rtp_source_len, last_rtcp_source_len;
55 int rtcp_port, local_rtpport, local_rtcpport;
61 char *fec_options_str;
66 #define OFFSET(x) offsetof(RTPContext, x)
67 #define D AV_OPT_FLAG_DECODING_PARAM
68 #define E AV_OPT_FLAG_ENCODING_PARAM
69 static const AVOption options[] = {
70 { "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, .flags = D|E },
71 { "buffer_size", "Send/Receive buffer size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
72 { "rtcp_port", "Custom rtcp port", OFFSET(rtcp_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
73 { "local_rtpport", "Local rtp port", OFFSET(local_rtpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
74 { "local_rtcpport", "Local rtcp port", OFFSET(local_rtcpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
75 { "connect", "Connect socket", OFFSET(connect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
76 { "write_to_source", "Send packets to the source address of the latest received packet", OFFSET(write_to_source), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
77 { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
78 { "dscp", "DSCP class", OFFSET(dscp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
79 { "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
80 { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
81 { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
82 { "fec", "FEC", OFFSET(fec_options_str), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = E },
83 { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
87 static const AVClass rtp_class = {
89 .item_name = av_default_item_name,
91 .version = LIBAVUTIL_VERSION_INT,
95 * If no filename is given to av_open_input_file because you want to
96 * get the local port first, then you must call this function to set
97 * the remote server address.
99 * @param h media file context
100 * @param uri of the remote server
101 * @return zero if no error.
104 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
106 RTPContext *s = h->priv_data;
114 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
115 path, sizeof(path), uri);
116 rtcp_port = port + 1;
118 p = strchr(uri, '?');
120 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
121 rtcp_port = strtol(buf, NULL, 10);
125 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
126 ff_udp_set_remote_url(s->rtp_hd, buf);
128 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
129 ff_udp_set_remote_url(s->rtcp_hd, buf);
133 static int get_port(const struct sockaddr_storage *ss)
135 if (ss->ss_family == AF_INET)
136 return ntohs(((const struct sockaddr_in *)ss)->sin_port);
137 #if HAVE_STRUCT_SOCKADDR_IN6
138 if (ss->ss_family == AF_INET6)
139 return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
144 static void set_port(struct sockaddr_storage *ss, int port)
146 if (ss->ss_family == AF_INET)
147 ((struct sockaddr_in *)ss)->sin_port = htons(port);
148 #if HAVE_STRUCT_SOCKADDR_IN6
149 else if (ss->ss_family == AF_INET6)
150 ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
155 * add option to url of the form:
156 * "http://host:port/path?option1=val1&option2=val2...
159 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
165 if (strchr(buf, '?'))
166 av_strlcat(buf, "&", buf_size);
168 av_strlcat(buf, "?", buf_size);
169 vsnprintf(buf1, sizeof(buf1), fmt, ap);
170 av_strlcat(buf, buf1, buf_size);
174 static void build_udp_url(RTPContext *s,
175 char *buf, int buf_size,
176 const char *hostname,
177 const char *localaddr,
178 int port, int local_port,
179 const char *include_sources,
180 const char *exclude_sources)
182 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
184 url_add_option(buf, buf_size, "localport=%d", local_port);
186 url_add_option(buf, buf_size, "ttl=%d", s->ttl);
187 if (s->buffer_size >= 0)
188 url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
189 if (s->pkt_size >= 0)
190 url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
192 url_add_option(buf, buf_size, "connect=1");
194 url_add_option(buf, buf_size, "dscp=%d", s->dscp);
195 url_add_option(buf, buf_size, "fifo_size=0");
196 if (include_sources && include_sources[0])
197 url_add_option(buf, buf_size, "sources=%s", include_sources);
198 if (exclude_sources && exclude_sources[0])
199 url_add_option(buf, buf_size, "block=%s", exclude_sources);
200 if (localaddr && localaddr[0])
201 url_add_option(buf, buf_size, "localaddr=%s", localaddr);
205 * url syntax: rtp://host:port[?option=val...]
206 * option: 'ttl=n' : set the ttl value (for multicast only)
207 * 'rtcpport=n' : set the remote rtcp port to n
208 * 'localrtpport=n' : set the local rtp port to n
209 * 'localrtcpport=n' : set the local rtcp port to n
210 * 'pkt_size=n' : set max packet size
211 * 'connect=0/1' : do a connect() on the UDP socket
212 * 'sources=ip[,ip]' : list allowed source IP addresses
213 * 'block=ip[,ip]' : list disallowed source IP addresses
214 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
215 * 'dscp=n' : set DSCP value to n (QoS)
217 * 'localport=n' : set the local port to n
219 * if rtcpport isn't set the rtcp port will be the rtp port + 1
220 * if local rtp port isn't set any available port will be used for the local
222 * if the local rtcp port is not set it will be the local rtp port + 1
225 static int rtp_open(URLContext *h, const char *uri, int flags)
227 RTPContext *s = h->priv_data;
228 AVDictionary *fec_opts = NULL;
230 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
231 char *sources = include_sources, *block = exclude_sources;
232 char *fec_protocol = NULL;
236 int i, max_retry_count = 3;
239 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
240 path, sizeof(path), uri);
241 /* extract parameters */
242 if (s->rtcp_port < 0)
243 s->rtcp_port = rtp_port + 1;
245 p = strchr(uri, '?');
247 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
248 s->ttl = strtol(buf, NULL, 10);
250 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
251 s->rtcp_port = strtol(buf, NULL, 10);
253 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
254 s->local_rtpport = strtol(buf, NULL, 10);
256 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
257 s->local_rtpport = strtol(buf, NULL, 10);
259 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
260 s->local_rtcpport = strtol(buf, NULL, 10);
262 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
263 s->pkt_size = strtol(buf, NULL, 10);
265 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
266 s->connect = strtol(buf, NULL, 10);
268 if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
269 s->write_to_source = strtol(buf, NULL, 10);
271 if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
272 s->dscp = strtol(buf, NULL, 10);
274 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
275 s->rw_timeout = strtol(buf, NULL, 10);
277 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
278 av_strlcpy(include_sources, buf, sizeof(include_sources));
279 ff_ip_parse_sources(h, buf, &s->filters);
281 ff_ip_parse_sources(h, s->sources, &s->filters);
282 sources = s->sources;
284 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
285 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
286 ff_ip_parse_blocks(h, buf, &s->filters);
288 ff_ip_parse_blocks(h, s->block, &s->filters);
291 if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
292 av_freep(&s->localaddr);
293 s->localaddr = av_strdup(buf);
298 if (s->rw_timeout >= 0)
299 h->rw_timeout = s->rw_timeout;
301 if (s->fec_options_str) {
302 p = s->fec_options_str;
304 if (!(fec_protocol = av_get_token(&p, "="))) {
305 av_log(h, AV_LOG_ERROR, "Failed to parse the FEC protocol value\n");
308 if (strcmp(fec_protocol, "prompeg")) {
309 av_log(h, AV_LOG_ERROR, "Unsupported FEC protocol %s\n", fec_protocol);
313 p = s->fec_options_str + strlen(fec_protocol);
314 while (*p && *p == '=') p++;
316 if (av_dict_parse_string(&fec_opts, p, "=", ":", 0) < 0) {
317 av_log(h, AV_LOG_ERROR, "Failed to parse the FEC options\n");
321 av_dict_set_int(&fec_opts, "ttl", s->ttl, 0);
325 for (i = 0; i < max_retry_count; i++) {
326 build_udp_url(s, buf, sizeof(buf),
327 hostname, s->localaddr, rtp_port, s->local_rtpport,
329 if (ffurl_open_whitelist(&s->rtp_hd, buf, flags, &h->interrupt_callback,
330 NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
332 s->local_rtpport = ff_udp_get_local_port(s->rtp_hd);
333 if(s->local_rtpport == 65535) {
334 s->local_rtpport = -1;
337 rtcpflags = flags | AVIO_FLAG_WRITE;
338 if (s->local_rtcpport < 0) {
339 s->local_rtcpport = s->local_rtpport + 1;
340 build_udp_url(s, buf, sizeof(buf),
341 hostname, s->localaddr, s->rtcp_port, s->local_rtcpport,
343 if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags,
344 &h->interrupt_callback, NULL,
345 h->protocol_whitelist, h->protocol_blacklist, h) < 0) {
346 s->local_rtpport = s->local_rtcpport = -1;
351 build_udp_url(s, buf, sizeof(buf),
352 hostname, s->localaddr, s->rtcp_port, s->local_rtcpport,
354 if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags, &h->interrupt_callback,
355 NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
362 ff_url_join(buf, sizeof(buf), fec_protocol, NULL, hostname, rtp_port, NULL);
363 if (ffurl_open_whitelist(&s->fec_hd, buf, flags, &h->interrupt_callback,
364 &fec_opts, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
368 /* just to ease handle access. XXX: need to suppress direct handle
370 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
371 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
373 h->max_packet_size = s->rtp_hd->max_packet_size;
376 av_free(fec_protocol);
377 av_dict_free(&fec_opts);
382 ffurl_closep(&s->rtp_hd);
383 ffurl_closep(&s->rtcp_hd);
384 ffurl_closep(&s->fec_hd);
385 av_free(fec_protocol);
386 av_dict_free(&fec_opts);
390 static int rtp_read(URLContext *h, uint8_t *buf, int size)
392 RTPContext *s = h->priv_data;
394 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
395 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : POLLING_TIME;
396 struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
397 socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
398 int runs = h->rw_timeout / 1000 / POLLING_TIME;
401 if (ff_check_interrupt(&h->interrupt_callback))
403 n = poll(p, 2, poll_delay);
405 /* first try RTCP, then RTP */
406 for (i = 1; i >= 0; i--) {
407 if (!(p[i].revents & POLLIN))
409 *addr_lens[i] = sizeof(*addrs[i]);
410 len = recvfrom(p[i].fd, buf, size, 0,
411 (struct sockaddr *)addrs[i], addr_lens[i]);
413 if (ff_neterrno() == AVERROR(EAGAIN) ||
414 ff_neterrno() == AVERROR(EINTR))
418 if (ff_ip_check_source_lists(addrs[i], &s->filters))
422 } else if (n == 0 && h->rw_timeout > 0 && --runs <= 0) {
423 return AVERROR(ETIMEDOUT);
425 if (ff_neterrno() == AVERROR(EINTR))
429 if (h->flags & AVIO_FLAG_NONBLOCK)
430 return AVERROR(EAGAIN);
434 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
436 RTPContext *s = h->priv_data;
441 return AVERROR(EINVAL);
443 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
444 av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
445 "make sure the RTP muxer is used\n");
447 if (s->write_to_source) {
449 struct sockaddr_storage *source, temp_source;
450 socklen_t *source_len, temp_len;
451 if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
452 av_log(h, AV_LOG_ERROR,
453 "Unable to send packet to source, no packets received yet\n");
454 // Intentionally not returning an error here
458 if (RTP_PT_IS_RTCP(buf[1])) {
460 source = &s->last_rtcp_source;
461 source_len = &s->last_rtcp_source_len;
464 source = &s->last_rtp_source;
465 source_len = &s->last_rtp_source_len;
467 if (!source->ss_family) {
468 source = &temp_source;
469 source_len = &temp_len;
470 if (RTP_PT_IS_RTCP(buf[1])) {
471 temp_source = s->last_rtp_source;
472 temp_len = s->last_rtp_source_len;
473 set_port(source, get_port(source) + 1);
474 av_log(h, AV_LOG_INFO,
475 "Not received any RTCP packets yet, inferring peer port "
476 "from the RTP port\n");
478 temp_source = s->last_rtcp_source;
479 temp_len = s->last_rtcp_source_len;
480 set_port(source, get_port(source) - 1);
481 av_log(h, AV_LOG_INFO,
482 "Not received any RTP packets yet, inferring peer port "
483 "from the RTCP port\n");
487 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
488 ret = ff_network_wait_fd(fd, 1);
492 ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
495 return ret < 0 ? ff_neterrno() : ret;
498 if (RTP_PT_IS_RTCP(buf[1])) {
499 /* RTCP payload type */
502 /* RTP payload type */
506 if ((ret = ffurl_write(hd, buf, size)) < 0) {
510 if (s->fec_hd && !RTP_PT_IS_RTCP(buf[1])) {
511 if ((ret_fec = ffurl_write(s->fec_hd, buf, size)) < 0) {
512 av_log(h, AV_LOG_ERROR, "Failed to send FEC\n");
520 static int rtp_close(URLContext *h)
522 RTPContext *s = h->priv_data;
524 ff_ip_reset_filters(&s->filters);
526 ffurl_closep(&s->rtp_hd);
527 ffurl_closep(&s->rtcp_hd);
528 ffurl_closep(&s->fec_hd);
533 * Return the local rtp port used by the RTP connection
534 * @param h media file context
535 * @return the local port number
538 int ff_rtp_get_local_rtp_port(URLContext *h)
540 RTPContext *s = h->priv_data;
541 return ff_udp_get_local_port(s->rtp_hd);
545 * Return the local rtcp port used by the RTP connection
546 * @param h media file context
547 * @return the local port number
550 static int rtp_get_file_handle(URLContext *h)
552 RTPContext *s = h->priv_data;
556 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
559 RTPContext *s = h->priv_data;
560 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
562 return AVERROR(ENOMEM);
569 const URLProtocol ff_rtp_protocol = {
571 .url_open = rtp_open,
572 .url_read = rtp_read,
573 .url_write = rtp_write,
574 .url_close = rtp_close,
575 .url_get_file_handle = rtp_get_file_handle,
576 .url_get_multi_file_handle = rtp_get_multi_file_handle,
577 .priv_data_size = sizeof(RTPContext),
578 .flags = URL_PROTOCOL_FLAG_NETWORK,
579 .priv_data_class = &rtp_class,