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"
31 #include "avio_internal.h"
40 #include "os_support.h"
46 typedef struct RTPContext {
48 URLContext *rtp_hd, *rtcp_hd, *fec_hd;
50 IPSourceFilters filters;
52 struct sockaddr_storage last_rtp_source, last_rtcp_source;
53 socklen_t last_rtp_source_len, last_rtcp_source_len;
56 int rtcp_port, local_rtpport, local_rtcpport;
62 char *fec_options_str;
65 #define OFFSET(x) offsetof(RTPContext, x)
66 #define D AV_OPT_FLAG_DECODING_PARAM
67 #define E AV_OPT_FLAG_ENCODING_PARAM
68 static const AVOption options[] = {
69 { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
70 { "buffer_size", "Send/Receive buffer size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
71 { "rtcp_port", "Custom rtcp port", OFFSET(rtcp_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
72 { "local_rtpport", "Local rtp port", OFFSET(local_rtpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
73 { "local_rtcpport", "Local rtcp port", OFFSET(local_rtcpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
74 { "connect", "Connect socket", OFFSET(connect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
75 { "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 },
76 { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
77 { "dscp", "DSCP class", OFFSET(dscp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
78 { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
79 { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
80 { "fec", "FEC", OFFSET(fec_options_str), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = E },
84 static const AVClass rtp_class = {
86 .item_name = av_default_item_name,
88 .version = LIBAVUTIL_VERSION_INT,
92 * If no filename is given to av_open_input_file because you want to
93 * get the local port first, then you must call this function to set
94 * the remote server address.
96 * @param h media file context
97 * @param uri of the remote server
98 * @return zero if no error.
101 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
103 RTPContext *s = h->priv_data;
111 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
112 path, sizeof(path), uri);
113 rtcp_port = port + 1;
115 p = strchr(uri, '?');
117 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
118 rtcp_port = strtol(buf, NULL, 10);
122 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
123 ff_udp_set_remote_url(s->rtp_hd, buf);
125 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
126 ff_udp_set_remote_url(s->rtcp_hd, buf);
130 static int get_port(const struct sockaddr_storage *ss)
132 if (ss->ss_family == AF_INET)
133 return ntohs(((const struct sockaddr_in *)ss)->sin_port);
134 #if HAVE_STRUCT_SOCKADDR_IN6
135 if (ss->ss_family == AF_INET6)
136 return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
141 static void set_port(struct sockaddr_storage *ss, int port)
143 if (ss->ss_family == AF_INET)
144 ((struct sockaddr_in *)ss)->sin_port = htons(port);
145 #if HAVE_STRUCT_SOCKADDR_IN6
146 else if (ss->ss_family == AF_INET6)
147 ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
152 * add option to url of the form:
153 * "http://host:port/path?option1=val1&option2=val2...
156 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
162 if (strchr(buf, '?'))
163 av_strlcat(buf, "&", buf_size);
165 av_strlcat(buf, "?", buf_size);
166 vsnprintf(buf1, sizeof(buf1), fmt, ap);
167 av_strlcat(buf, buf1, buf_size);
171 static void build_udp_url(RTPContext *s,
172 char *buf, int buf_size,
173 const char *hostname,
174 int port, int local_port,
175 const char *include_sources,
176 const char *exclude_sources)
178 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
180 url_add_option(buf, buf_size, "localport=%d", local_port);
182 url_add_option(buf, buf_size, "ttl=%d", s->ttl);
183 if (s->buffer_size >= 0)
184 url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
185 if (s->pkt_size >= 0)
186 url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
188 url_add_option(buf, buf_size, "connect=1");
190 url_add_option(buf, buf_size, "dscp=%d", s->dscp);
191 url_add_option(buf, buf_size, "fifo_size=0");
192 if (include_sources && include_sources[0])
193 url_add_option(buf, buf_size, "sources=%s", include_sources);
194 if (exclude_sources && exclude_sources[0])
195 url_add_option(buf, buf_size, "block=%s", exclude_sources);
199 * url syntax: rtp://host:port[?option=val...]
200 * option: 'ttl=n' : set the ttl value (for multicast only)
201 * 'rtcpport=n' : set the remote rtcp port to n
202 * 'localrtpport=n' : set the local rtp port to n
203 * 'localrtcpport=n' : set the local rtcp port to n
204 * 'pkt_size=n' : set max packet size
205 * 'connect=0/1' : do a connect() on the UDP socket
206 * 'sources=ip[,ip]' : list allowed source IP addresses
207 * 'block=ip[,ip]' : list disallowed source IP addresses
208 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
209 * 'dscp=n' : set DSCP value to n (QoS)
211 * 'localport=n' : set the local port to n
213 * if rtcpport isn't set the rtcp port will be the rtp port + 1
214 * if local rtp port isn't set any available port will be used for the local
216 * if the local rtcp port is not set it will be the local rtp port + 1
219 static int rtp_open(URLContext *h, const char *uri, int flags)
221 RTPContext *s = h->priv_data;
222 AVDictionary *fec_opts = NULL;
224 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
225 char *sources = include_sources, *block = exclude_sources;
226 char *fec_protocol = NULL;
230 int i, max_retry_count = 3;
233 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
234 path, sizeof(path), uri);
235 /* extract parameters */
236 if (s->rtcp_port < 0)
237 s->rtcp_port = rtp_port + 1;
239 p = strchr(uri, '?');
241 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
242 s->ttl = strtol(buf, NULL, 10);
244 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
245 s->rtcp_port = strtol(buf, NULL, 10);
247 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
248 s->local_rtpport = strtol(buf, NULL, 10);
250 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
251 s->local_rtpport = strtol(buf, NULL, 10);
253 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
254 s->local_rtcpport = strtol(buf, NULL, 10);
256 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
257 s->pkt_size = strtol(buf, NULL, 10);
259 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
260 s->connect = strtol(buf, NULL, 10);
262 if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
263 s->write_to_source = strtol(buf, NULL, 10);
265 if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
266 s->dscp = strtol(buf, NULL, 10);
268 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
269 av_strlcpy(include_sources, buf, sizeof(include_sources));
270 ff_ip_parse_sources(h, buf, &s->filters);
272 ff_ip_parse_sources(h, s->sources, &s->filters);
273 sources = s->sources;
275 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
276 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
277 ff_ip_parse_blocks(h, buf, &s->filters);
279 ff_ip_parse_blocks(h, s->block, &s->filters);
284 if (s->fec_options_str) {
285 p = s->fec_options_str;
287 if (!(fec_protocol = av_get_token(&p, "="))) {
288 av_log(h, AV_LOG_ERROR, "Failed to parse the FEC protocol value\n");
291 if (strcmp(fec_protocol, "prompeg")) {
292 av_log(h, AV_LOG_ERROR, "Unsupported FEC protocol %s\n", fec_protocol);
296 p = s->fec_options_str + strlen(fec_protocol);
297 while (*p && *p == '=') p++;
299 if (av_dict_parse_string(&fec_opts, p, "=", ":", 0) < 0) {
300 av_log(h, AV_LOG_ERROR, "Failed to parse the FEC options\n");
304 snprintf(buf, sizeof (buf), "%d", s->ttl);
305 av_dict_set(&fec_opts, "ttl", buf, 0);
309 for (i = 0; i < max_retry_count; i++) {
310 build_udp_url(s, buf, sizeof(buf),
311 hostname, rtp_port, s->local_rtpport,
313 if (ffurl_open_whitelist(&s->rtp_hd, buf, flags, &h->interrupt_callback,
314 NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
316 s->local_rtpport = ff_udp_get_local_port(s->rtp_hd);
317 if(s->local_rtpport == 65535) {
318 s->local_rtpport = -1;
321 rtcpflags = flags | AVIO_FLAG_WRITE;
322 if (s->local_rtcpport < 0) {
323 s->local_rtcpport = s->local_rtpport + 1;
324 build_udp_url(s, buf, sizeof(buf),
325 hostname, s->rtcp_port, s->local_rtcpport,
327 if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags,
328 &h->interrupt_callback, NULL,
329 h->protocol_whitelist, h->protocol_blacklist, h) < 0) {
330 s->local_rtpport = s->local_rtcpport = -1;
335 build_udp_url(s, buf, sizeof(buf),
336 hostname, s->rtcp_port, s->local_rtcpport,
338 if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags, &h->interrupt_callback,
339 NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
346 ff_url_join(buf, sizeof(buf), fec_protocol, NULL, hostname, rtp_port, NULL);
347 if (ffurl_open_whitelist(&s->fec_hd, buf, flags, &h->interrupt_callback,
348 &fec_opts, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
352 /* just to ease handle access. XXX: need to suppress direct handle
354 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
355 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
357 h->max_packet_size = s->rtp_hd->max_packet_size;
360 av_free(fec_protocol);
361 av_dict_free(&fec_opts);
367 ffurl_close(s->rtp_hd);
369 ffurl_close(s->rtcp_hd);
370 ffurl_closep(&s->fec_hd);
371 av_free(fec_protocol);
372 av_dict_free(&fec_opts);
376 static int rtp_read(URLContext *h, uint8_t *buf, int size)
378 RTPContext *s = h->priv_data;
380 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
381 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
382 struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
383 socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
386 if (ff_check_interrupt(&h->interrupt_callback))
388 n = poll(p, 2, poll_delay);
390 /* first try RTCP, then RTP */
391 for (i = 1; i >= 0; i--) {
392 if (!(p[i].revents & POLLIN))
394 *addr_lens[i] = sizeof(*addrs[i]);
395 len = recvfrom(p[i].fd, buf, size, 0,
396 (struct sockaddr *)addrs[i], addr_lens[i]);
398 if (ff_neterrno() == AVERROR(EAGAIN) ||
399 ff_neterrno() == AVERROR(EINTR))
403 if (ff_ip_check_source_lists(addrs[i], &s->filters))
408 if (ff_neterrno() == AVERROR(EINTR))
412 if (h->flags & AVIO_FLAG_NONBLOCK)
413 return AVERROR(EAGAIN);
417 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
419 RTPContext *s = h->priv_data;
424 return AVERROR(EINVAL);
426 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
427 av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
428 "make sure the RTP muxer is used\n");
430 if (s->write_to_source) {
432 struct sockaddr_storage *source, temp_source;
433 socklen_t *source_len, temp_len;
434 if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
435 av_log(h, AV_LOG_ERROR,
436 "Unable to send packet to source, no packets received yet\n");
437 // Intentionally not returning an error here
441 if (RTP_PT_IS_RTCP(buf[1])) {
443 source = &s->last_rtcp_source;
444 source_len = &s->last_rtcp_source_len;
447 source = &s->last_rtp_source;
448 source_len = &s->last_rtp_source_len;
450 if (!source->ss_family) {
451 source = &temp_source;
452 source_len = &temp_len;
453 if (RTP_PT_IS_RTCP(buf[1])) {
454 temp_source = s->last_rtp_source;
455 temp_len = s->last_rtp_source_len;
456 set_port(source, get_port(source) + 1);
457 av_log(h, AV_LOG_INFO,
458 "Not received any RTCP packets yet, inferring peer port "
459 "from the RTP port\n");
461 temp_source = s->last_rtcp_source;
462 temp_len = s->last_rtcp_source_len;
463 set_port(source, get_port(source) - 1);
464 av_log(h, AV_LOG_INFO,
465 "Not received any RTP packets yet, inferring peer port "
466 "from the RTCP port\n");
470 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
471 ret = ff_network_wait_fd(fd, 1);
475 ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
478 return ret < 0 ? ff_neterrno() : ret;
481 if (RTP_PT_IS_RTCP(buf[1])) {
482 /* RTCP payload type */
485 /* RTP payload type */
489 if ((ret = ffurl_write(hd, buf, size)) < 0) {
493 if (s->fec_hd && !RTP_PT_IS_RTCP(buf[1])) {
494 if ((ret_fec = ffurl_write(s->fec_hd, buf, size)) < 0) {
495 av_log(h, AV_LOG_ERROR, "Failed to send FEC\n");
503 static int rtp_close(URLContext *h)
505 RTPContext *s = h->priv_data;
507 ff_ip_reset_filters(&s->filters);
509 ffurl_close(s->rtp_hd);
510 ffurl_close(s->rtcp_hd);
511 ffurl_closep(&s->fec_hd);
516 * Return the local rtp port used by the RTP connection
517 * @param h media file context
518 * @return the local port number
521 int ff_rtp_get_local_rtp_port(URLContext *h)
523 RTPContext *s = h->priv_data;
524 return ff_udp_get_local_port(s->rtp_hd);
528 * Return the local rtcp port used by the RTP connection
529 * @param h media file context
530 * @return the local port number
533 static int rtp_get_file_handle(URLContext *h)
535 RTPContext *s = h->priv_data;
539 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
542 RTPContext *s = h->priv_data;
543 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
545 return AVERROR(ENOMEM);
552 const URLProtocol ff_rtp_protocol = {
554 .url_open = rtp_open,
555 .url_read = rtp_read,
556 .url_write = rtp_write,
557 .url_close = rtp_close,
558 .url_get_file_handle = rtp_get_file_handle,
559 .url_get_multi_file_handle = rtp_get_multi_file_handle,
560 .priv_data_size = sizeof(RTPContext),
561 .flags = URL_PROTOCOL_FLAG_NETWORK,
562 .priv_data_class = &rtp_class,