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"
39 #include "os_support.h"
45 typedef struct RTPContext
{
47 URLContext
*rtp_hd
, *rtcp_hd
;
48 int rtp_fd
, rtcp_fd
, nb_ssm_include_addrs
, nb_ssm_exclude_addrs
;
49 struct sockaddr_storage
**ssm_include_addrs
, **ssm_exclude_addrs
;
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
;
63 #define OFFSET(x) offsetof(RTPContext, x)
64 #define D AV_OPT_FLAG_DECODING_PARAM
65 #define E AV_OPT_FLAG_ENCODING_PARAM
66 static const AVOption options
[] = {
67 { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
68 { "buffer_size", "Send/Receive buffer size (in bytes)", OFFSET(buffer_size
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
69 { "rtcp_port", "Custom rtcp port", OFFSET(rtcp_port
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
70 { "local_rtpport", "Local rtp port", OFFSET(local_rtpport
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
71 { "local_rtcpport", "Local rtcp port", OFFSET(local_rtcpport
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
72 { "connect", "Connect socket", OFFSET(connect
), AV_OPT_TYPE_BOOL
, { .i64
= 0 }, 0, 1, .flags
= D
|E
},
73 { "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
},
74 { "pkt_size", "Maximum packet size", OFFSET(pkt_size
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
75 { "dscp", "DSCP class", OFFSET(dscp
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
76 { "sources", "Source list", OFFSET(sources
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, .flags
= D
|E
},
77 { "block", "Block list", OFFSET(block
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, .flags
= D
|E
},
81 static const AVClass rtp_class
= {
83 .item_name
= av_default_item_name
,
85 .version
= LIBAVUTIL_VERSION_INT
,
89 * If no filename is given to av_open_input_file because you want to
90 * get the local port first, then you must call this function to set
91 * the remote server address.
93 * @param h media file context
94 * @param uri of the remote server
95 * @return zero if no error.
98 int ff_rtp_set_remote_url(URLContext
*h
, const char *uri
)
100 RTPContext
*s
= h
->priv_data
;
108 av_url_split(NULL
, 0, NULL
, 0, hostname
, sizeof(hostname
), &port
,
109 path
, sizeof(path
), uri
);
110 rtcp_port
= port
+ 1;
112 p
= strchr(uri
, '?');
114 if (av_find_info_tag(buf
, sizeof(buf
), "rtcpport", p
)) {
115 rtcp_port
= strtol(buf
, NULL
, 10);
119 ff_url_join(buf
, sizeof(buf
), "udp", NULL
, hostname
, port
, "%s", path
);
120 ff_udp_set_remote_url(s
->rtp_hd
, buf
);
122 ff_url_join(buf
, sizeof(buf
), "udp", NULL
, hostname
, rtcp_port
, "%s", path
);
123 ff_udp_set_remote_url(s
->rtcp_hd
, buf
);
127 static struct addrinfo
* rtp_resolve_host(const char *hostname
, int port
,
128 int type
, int family
, int flags
)
130 struct addrinfo hints
= { 0 }, *res
= 0;
134 snprintf(service
, sizeof(service
), "%d", port
);
135 hints
.ai_socktype
= type
;
136 hints
.ai_family
= family
;
137 hints
.ai_flags
= flags
;
138 if ((error
= getaddrinfo(hostname
, service
, &hints
, &res
))) {
140 av_log(NULL
, AV_LOG_ERROR
, "rtp_resolve_host: %s\n", gai_strerror(error
));
146 static int compare_addr(const struct sockaddr_storage
*a
,
147 const struct sockaddr_storage
*b
)
149 if (a
->ss_family
!= b
->ss_family
)
151 if (a
->ss_family
== AF_INET
) {
152 return (((const struct sockaddr_in
*)a
)->sin_addr
.s_addr
!=
153 ((const struct sockaddr_in
*)b
)->sin_addr
.s_addr
);
156 #if HAVE_STRUCT_SOCKADDR_IN6
157 if (a
->ss_family
== AF_INET6
) {
158 const uint8_t *s6_addr_a
= ((const struct sockaddr_in6
*)a
)->sin6_addr
.s6_addr
;
159 const uint8_t *s6_addr_b
= ((const struct sockaddr_in6
*)b
)->sin6_addr
.s6_addr
;
160 return memcmp(s6_addr_a
, s6_addr_b
, 16);
166 static int get_port(const struct sockaddr_storage
*ss
)
168 if (ss
->ss_family
== AF_INET
)
169 return ntohs(((const struct sockaddr_in
*)ss
)->sin_port
);
170 #if HAVE_STRUCT_SOCKADDR_IN6
171 if (ss
->ss_family
== AF_INET6
)
172 return ntohs(((const struct sockaddr_in6
*)ss
)->sin6_port
);
177 static void set_port(struct sockaddr_storage
*ss
, int port
)
179 if (ss
->ss_family
== AF_INET
)
180 ((struct sockaddr_in
*)ss
)->sin_port
= htons(port
);
181 #if HAVE_STRUCT_SOCKADDR_IN6
182 else if (ss
->ss_family
== AF_INET6
)
183 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(port
);
187 static int rtp_check_source_lists(RTPContext
*s
, struct sockaddr_storage
*source_addr_ptr
)
190 if (s
->nb_ssm_exclude_addrs
) {
191 for (i
= 0; i
< s
->nb_ssm_exclude_addrs
; i
++) {
192 if (!compare_addr(source_addr_ptr
, s
->ssm_exclude_addrs
[i
]))
196 if (s
->nb_ssm_include_addrs
) {
197 for (i
= 0; i
< s
->nb_ssm_include_addrs
; i
++) {
198 if (!compare_addr(source_addr_ptr
, s
->ssm_include_addrs
[i
]))
207 * add option to url of the form:
208 * "http://host:port/path?option1=val1&option2=val2...
211 static av_printf_format(3, 4) void url_add_option(char *buf
, int buf_size
, const char *fmt
, ...)
217 if (strchr(buf
, '?'))
218 av_strlcat(buf
, "&", buf_size
);
220 av_strlcat(buf
, "?", buf_size
);
221 vsnprintf(buf1
, sizeof(buf1
), fmt
, ap
);
222 av_strlcat(buf
, buf1
, buf_size
);
226 static void build_udp_url(RTPContext
*s
,
227 char *buf
, int buf_size
,
228 const char *hostname
,
229 int port
, int local_port
,
230 const char *include_sources
,
231 const char *exclude_sources
)
233 ff_url_join(buf
, buf_size
, "udp", NULL
, hostname
, port
, NULL
);
235 url_add_option(buf
, buf_size
, "localport=%d", local_port
);
237 url_add_option(buf
, buf_size
, "ttl=%d", s
->ttl
);
238 if (s
->buffer_size
>= 0)
239 url_add_option(buf
, buf_size
, "buffer_size=%d", s
->buffer_size
);
240 if (s
->pkt_size
>= 0)
241 url_add_option(buf
, buf_size
, "pkt_size=%d", s
->pkt_size
);
243 url_add_option(buf
, buf_size
, "connect=1");
245 url_add_option(buf
, buf_size
, "dscp=%d", s
->dscp
);
246 url_add_option(buf
, buf_size
, "fifo_size=0");
247 if (include_sources
&& include_sources
[0])
248 url_add_option(buf
, buf_size
, "sources=%s", include_sources
);
249 if (exclude_sources
&& exclude_sources
[0])
250 url_add_option(buf
, buf_size
, "block=%s", exclude_sources
);
253 static void rtp_parse_addr_list(URLContext
*h
, char *buf
,
254 struct sockaddr_storage
***address_list_ptr
,
255 int *address_list_size_ptr
)
257 struct addrinfo
*ai
= NULL
;
258 struct sockaddr_storage
*source_addr
;
259 char tmp
= '\0', *p
= buf
, *next
;
261 /* Resolve all of the IPs */
264 next
= strchr(p
, ',');
271 ai
= rtp_resolve_host(p
, 0, SOCK_DGRAM
, AF_UNSPEC
, 0);
273 source_addr
= av_mallocz(sizeof(struct sockaddr_storage
));
279 memcpy(source_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
281 dynarray_add(address_list_ptr
, address_list_size_ptr
, source_addr
);
283 av_log(h
, AV_LOG_WARNING
, "Unable to resolve %s\n", p
);
296 * url syntax: rtp://host:port[?option=val...]
297 * option: 'ttl=n' : set the ttl value (for multicast only)
298 * 'rtcpport=n' : set the remote rtcp port to n
299 * 'localrtpport=n' : set the local rtp port to n
300 * 'localrtcpport=n' : set the local rtcp port to n
301 * 'pkt_size=n' : set max packet size
302 * 'connect=0/1' : do a connect() on the UDP socket
303 * 'sources=ip[,ip]' : list allowed source IP addresses
304 * 'block=ip[,ip]' : list disallowed source IP addresses
305 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
306 * 'dscp=n' : set DSCP value to n (QoS)
308 * 'localport=n' : set the local port to n
310 * if rtcpport isn't set the rtcp port will be the rtp port + 1
311 * if local rtp port isn't set any available port will be used for the local
313 * if the local rtcp port is not set it will be the local rtp port + 1
316 static int rtp_open(URLContext
*h
, const char *uri
, int flags
)
318 RTPContext
*s
= h
->priv_data
;
320 char hostname
[256], include_sources
[1024] = "", exclude_sources
[1024] = "";
321 char *sources
= include_sources
, *block
= exclude_sources
;
325 int i
, max_retry_count
= 3;
328 av_url_split(NULL
, 0, NULL
, 0, hostname
, sizeof(hostname
), &rtp_port
,
329 path
, sizeof(path
), uri
);
330 /* extract parameters */
331 if (s
->rtcp_port
< 0)
332 s
->rtcp_port
= rtp_port
+ 1;
334 p
= strchr(uri
, '?');
336 if (av_find_info_tag(buf
, sizeof(buf
), "ttl", p
)) {
337 s
->ttl
= strtol(buf
, NULL
, 10);
339 if (av_find_info_tag(buf
, sizeof(buf
), "rtcpport", p
)) {
340 s
->rtcp_port
= strtol(buf
, NULL
, 10);
342 if (av_find_info_tag(buf
, sizeof(buf
), "localport", p
)) {
343 s
->local_rtpport
= strtol(buf
, NULL
, 10);
345 if (av_find_info_tag(buf
, sizeof(buf
), "localrtpport", p
)) {
346 s
->local_rtpport
= strtol(buf
, NULL
, 10);
348 if (av_find_info_tag(buf
, sizeof(buf
), "localrtcpport", p
)) {
349 s
->local_rtcpport
= strtol(buf
, NULL
, 10);
351 if (av_find_info_tag(buf
, sizeof(buf
), "pkt_size", p
)) {
352 s
->pkt_size
= strtol(buf
, NULL
, 10);
354 if (av_find_info_tag(buf
, sizeof(buf
), "connect", p
)) {
355 s
->connect
= strtol(buf
, NULL
, 10);
357 if (av_find_info_tag(buf
, sizeof(buf
), "write_to_source", p
)) {
358 s
->write_to_source
= strtol(buf
, NULL
, 10);
360 if (av_find_info_tag(buf
, sizeof(buf
), "dscp", p
)) {
361 s
->dscp
= strtol(buf
, NULL
, 10);
363 if (av_find_info_tag(buf
, sizeof(buf
), "sources", p
)) {
364 av_strlcpy(include_sources
, buf
, sizeof(include_sources
));
366 rtp_parse_addr_list(h
, buf
, &s
->ssm_include_addrs
, &s
->nb_ssm_include_addrs
);
368 rtp_parse_addr_list(h
, s
->sources
, &s
->ssm_include_addrs
, &s
->nb_ssm_include_addrs
);
369 sources
= s
->sources
;
371 if (av_find_info_tag(buf
, sizeof(buf
), "block", p
)) {
372 av_strlcpy(exclude_sources
, buf
, sizeof(exclude_sources
));
373 rtp_parse_addr_list(h
, buf
, &s
->ssm_exclude_addrs
, &s
->nb_ssm_exclude_addrs
);
375 rtp_parse_addr_list(h
, s
->block
, &s
->ssm_exclude_addrs
, &s
->nb_ssm_exclude_addrs
);
380 for (i
= 0; i
< max_retry_count
; i
++) {
381 build_udp_url(s
, buf
, sizeof(buf
),
382 hostname
, rtp_port
, s
->local_rtpport
,
384 if (ffurl_open_whitelist(&s
->rtp_hd
, buf
, flags
, &h
->interrupt_callback
,
385 NULL
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
) < 0)
387 s
->local_rtpport
= ff_udp_get_local_port(s
->rtp_hd
);
388 if(s
->local_rtpport
== 65535) {
389 s
->local_rtpport
= -1;
392 rtcpflags
= flags
| AVIO_FLAG_WRITE
;
393 if (s
->local_rtcpport
< 0) {
394 s
->local_rtcpport
= s
->local_rtpport
+ 1;
395 build_udp_url(s
, buf
, sizeof(buf
),
396 hostname
, s
->rtcp_port
, s
->local_rtcpport
,
398 if (ffurl_open_whitelist(&s
->rtcp_hd
, buf
, rtcpflags
,
399 &h
->interrupt_callback
, NULL
,
400 h
->protocol_whitelist
, h
->protocol_blacklist
, h
) < 0) {
401 s
->local_rtpport
= s
->local_rtcpport
= -1;
406 build_udp_url(s
, buf
, sizeof(buf
),
407 hostname
, s
->rtcp_port
, s
->local_rtcpport
,
409 if (ffurl_open_whitelist(&s
->rtcp_hd
, buf
, rtcpflags
, &h
->interrupt_callback
,
410 NULL
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
) < 0)
415 /* just to ease handle access. XXX: need to suppress direct handle
417 s
->rtp_fd
= ffurl_get_file_handle(s
->rtp_hd
);
418 s
->rtcp_fd
= ffurl_get_file_handle(s
->rtcp_hd
);
420 h
->max_packet_size
= s
->rtp_hd
->max_packet_size
;
426 ffurl_close(s
->rtp_hd
);
428 ffurl_close(s
->rtcp_hd
);
432 static int rtp_read(URLContext
*h
, uint8_t *buf
, int size
)
434 RTPContext
*s
= h
->priv_data
;
436 struct pollfd p
[2] = {{s
->rtp_fd
, POLLIN
, 0}, {s
->rtcp_fd
, POLLIN
, 0}};
437 int poll_delay
= h
->flags
& AVIO_FLAG_NONBLOCK
? 0 : 100;
438 struct sockaddr_storage
*addrs
[2] = { &s
->last_rtp_source
, &s
->last_rtcp_source
};
439 socklen_t
*addr_lens
[2] = { &s
->last_rtp_source_len
, &s
->last_rtcp_source_len
};
442 if (ff_check_interrupt(&h
->interrupt_callback
))
444 n
= poll(p
, 2, poll_delay
);
446 /* first try RTCP, then RTP */
447 for (i
= 1; i
>= 0; i
--) {
448 if (!(p
[i
].revents
& POLLIN
))
450 *addr_lens
[i
] = sizeof(*addrs
[i
]);
451 len
= recvfrom(p
[i
].fd
, buf
, size
, 0,
452 (struct sockaddr
*)addrs
[i
], addr_lens
[i
]);
454 if (ff_neterrno() == AVERROR(EAGAIN
) ||
455 ff_neterrno() == AVERROR(EINTR
))
459 if (rtp_check_source_lists(s
, addrs
[i
]))
464 if (ff_neterrno() == AVERROR(EINTR
))
468 if (h
->flags
& AVIO_FLAG_NONBLOCK
)
469 return AVERROR(EAGAIN
);
474 static int rtp_write(URLContext
*h
, const uint8_t *buf
, int size
)
476 RTPContext
*s
= h
->priv_data
;
481 return AVERROR(EINVAL
);
483 if ((buf
[0] & 0xc0) != (RTP_VERSION
<< 6))
484 av_log(h
, AV_LOG_WARNING
, "Data doesn't look like RTP packets, "
485 "make sure the RTP muxer is used\n");
487 if (s
->write_to_source
) {
489 struct sockaddr_storage
*source
, temp_source
;
490 socklen_t
*source_len
, temp_len
;
491 if (!s
->last_rtp_source
.ss_family
&& !s
->last_rtcp_source
.ss_family
) {
492 av_log(h
, AV_LOG_ERROR
,
493 "Unable to send packet to source, no packets received yet\n");
494 // Intentionally not returning an error here
498 if (RTP_PT_IS_RTCP(buf
[1])) {
500 source
= &s
->last_rtcp_source
;
501 source_len
= &s
->last_rtcp_source_len
;
504 source
= &s
->last_rtp_source
;
505 source_len
= &s
->last_rtp_source_len
;
507 if (!source
->ss_family
) {
508 source
= &temp_source
;
509 source_len
= &temp_len
;
510 if (RTP_PT_IS_RTCP(buf
[1])) {
511 temp_source
= s
->last_rtp_source
;
512 temp_len
= s
->last_rtp_source_len
;
513 set_port(source
, get_port(source
) + 1);
514 av_log(h
, AV_LOG_INFO
,
515 "Not received any RTCP packets yet, inferring peer port "
516 "from the RTP port\n");
518 temp_source
= s
->last_rtcp_source
;
519 temp_len
= s
->last_rtcp_source_len
;
520 set_port(source
, get_port(source
) - 1);
521 av_log(h
, AV_LOG_INFO
,
522 "Not received any RTP packets yet, inferring peer port "
523 "from the RTCP port\n");
527 if (!(h
->flags
& AVIO_FLAG_NONBLOCK
)) {
528 ret
= ff_network_wait_fd(fd
, 1);
532 ret
= sendto(fd
, buf
, size
, 0, (struct sockaddr
*) source
,
535 return ret
< 0 ? ff_neterrno() : ret
;
538 if (RTP_PT_IS_RTCP(buf
[1])) {
539 /* RTCP payload type */
542 /* RTP payload type */
546 ret
= ffurl_write(hd
, buf
, size
);
550 static int rtp_close(URLContext
*h
)
552 RTPContext
*s
= h
->priv_data
;
555 for (i
= 0; i
< s
->nb_ssm_include_addrs
; i
++)
556 av_freep(&s
->ssm_include_addrs
[i
]);
557 av_freep(&s
->ssm_include_addrs
);
558 for (i
= 0; i
< s
->nb_ssm_exclude_addrs
; i
++)
559 av_freep(&s
->ssm_exclude_addrs
[i
]);
560 av_freep(&s
->ssm_exclude_addrs
);
562 ffurl_close(s
->rtp_hd
);
563 ffurl_close(s
->rtcp_hd
);
568 * Return the local rtp port used by the RTP connection
569 * @param h media file context
570 * @return the local port number
573 int ff_rtp_get_local_rtp_port(URLContext
*h
)
575 RTPContext
*s
= h
->priv_data
;
576 return ff_udp_get_local_port(s
->rtp_hd
);
580 * Return the local rtcp port used by the RTP connection
581 * @param h media file context
582 * @return the local port number
585 int ff_rtp_get_local_rtcp_port(URLContext
*h
)
587 RTPContext
*s
= h
->priv_data
;
588 return ff_udp_get_local_port(s
->rtcp_hd
);
591 static int rtp_get_file_handle(URLContext
*h
)
593 RTPContext
*s
= h
->priv_data
;
597 static int rtp_get_multi_file_handle(URLContext
*h
, int **handles
,
600 RTPContext
*s
= h
->priv_data
;
601 int *hs
= *handles
= av_malloc(sizeof(**handles
) * 2);
603 return AVERROR(ENOMEM
);
610 const URLProtocol ff_rtp_protocol
= {
612 .url_open
= rtp_open
,
613 .url_read
= rtp_read
,
614 .url_write
= rtp_write
,
615 .url_close
= rtp_close
,
616 .url_get_file_handle
= rtp_get_file_handle
,
617 .url_get_multi_file_handle
= rtp_get_multi_file_handle
,
618 .priv_data_size
= sizeof(RTPContext
),
619 .flags
= URL_PROTOCOL_FLAG_NETWORK
,
620 .priv_data_class
= &rtp_class
,