PR #26321 opened by progmanos URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26321 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26321.patch
by first changing the RTSPSource to track the destination address obtained from the source filter. For each RTSPStream, only add the source filter from the sdp if sdp_ip string matches source-filter's destination address. Before issuing the setup request, change the lower_transport to multicast if the sdp_ip is a multicast address. Change the multicast case to append sources (from the source-filter) to the rtp url to make the source specific multicast join work. Support wildcard source-filter destinations. Change source-filter processing to only happen after the first media description was processed and either when the next one starts or when we have reached the end of the sdp. This handles a wider variety of cases and doesn't rely on any optional media descriptions. # Summary of changes Briefly describe what this PR does and why. <!-- If this PR requires new FATE test samples, attach them to the PR and list their target paths below (relative to the fate-suite root). Attached filenames must match the sample's filename: ```fate-samples # e.g. vorbis/new-sample.ogg ``` --> >From 9b5dacbf01ccddcaad24bbe8ab891b5941ff67a2 Mon Sep 17 00:00:00 2001 From: Rashad Tatum <[email protected]> Date: Thu, 13 Feb 2025 20:31:34 -0500 Subject: [PATCH] libavformat/rtsp: Make source specific multicast work for rtsp streams by first changing the RTSPSource to track the destination address obtained from the source filter. For each RTSPStream, only add the source filter from the sdp if sdp_ip string matches source-filter's destination address. Before issuing the setup request, change the lower_transport to multicast if the sdp_ip is a multicast address. Change the multicast case to append sources (from the source-filter) to the rtp url to make the source specific multicast join work. Support wildcard source-filter destinations. Change source-filter processing to only happen after the first media description was processed and either when the next one starts or when we have reached the end of the sdp. This handles a wider variety of cases and doesn't rely on any optional media descriptions. --- libavformat/rtsp.c | 108 +++++++++++++++++++++++++++++++++------------ libavformat/rtsp.h | 3 +- 2 files changed, 81 insertions(+), 30 deletions(-) diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c index d455438fa56..65bd28c9136 100644 --- a/libavformat/rtsp.c +++ b/libavformat/rtsp.c @@ -19,6 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include <stdbool.h> + #include "config_components.h" #include "libavutil/avassert.h" @@ -416,16 +418,19 @@ typedef struct SDPParseState { } SDPParseState; static void copy_default_source_addrs(struct RTSPSource **addrs, int count, - struct RTSPSource ***dest, int *dest_count) + struct RTSPSource ***dest, int *dest_count, const char* sdp_ip_str) { RTSPSource *rtsp_src, *rtsp_src2; int i; + const char* wildcard_dest = "*"; for (i = 0; i < count; i++) { rtsp_src = addrs[i]; - rtsp_src2 = av_memdup(rtsp_src, sizeof(*rtsp_src)); - if (!rtsp_src2) - continue; - dynarray_add(dest, dest_count, rtsp_src2); + if ((strcmp(rtsp_src->dest_addr, sdp_ip_str) == 0) || (strcmp(rtsp_src->dest_addr, wildcard_dest) == 0)) { + rtsp_src2 = av_memdup(rtsp_src, sizeof(*rtsp_src)); + if (!rtsp_src2) + continue; + dynarray_add(dest, dest_count, rtsp_src2); + } } } @@ -450,7 +455,7 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1, { RTSPState *rt = s->priv_data; char buf1[64], st_type[64]; - const char *p; + const char *p, *dest_addr; enum AVMediaType codec_type; int payload_type; AVStream *st; @@ -475,6 +480,7 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1, get_word_sep(buf1, sizeof(buf1), "/", &p); if (get_sockaddr(s, buf1, &sdp_ip)) return; + ttl = 16; if (*p == '/') { p++; @@ -488,6 +494,7 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1, rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1]; rtsp_st->sdp_ip = sdp_ip; rtsp_st->sdp_ttl = ttl; + } break; case 's': @@ -531,14 +538,7 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1, rtsp_st->sdp_ip = s1->default_ip; rtsp_st->sdp_ttl = s1->default_ttl; - copy_default_source_addrs(s1->default_include_source_addrs, - s1->nb_default_include_source_addrs, - &rtsp_st->include_source_addrs, - &rtsp_st->nb_include_source_addrs); - copy_default_source_addrs(s1->default_exclude_source_addrs, - s1->nb_default_exclude_source_addrs, - &rtsp_st->exclude_source_addrs, - &rtsp_st->nb_exclude_source_addrs); + get_word(buf1, sizeof(buf1), &p); /* port */ rtsp_st->sdp_port = atoi(buf1); @@ -716,11 +716,15 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1, // not checking that the destination address actually matches or is wildcard get_word(buf1, sizeof(buf1), &p); + dest_addr = av_strdup(buf1); while (*p != '\0') { rtsp_src = av_mallocz(sizeof(*rtsp_src)); - if (!rtsp_src) + if (!rtsp_src) { + av_freep(&dest_addr); return; - get_word(rtsp_src->addr, sizeof(rtsp_src->addr), &p); + } + av_strlcpy(rtsp_src->dest_addr, dest_addr, sizeof(rtsp_src->dest_addr)); + get_word(rtsp_src->src_addr, sizeof(rtsp_src->src_addr), &p); if (exclude) { if (s->nb_streams == 0) { dynarray_add(&s1->default_exclude_source_addrs, &s1->nb_default_exclude_source_addrs, rtsp_src); @@ -737,6 +741,7 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1, } } } + av_freep(&dest_addr); } else { if (rt->server_type == RTSP_SERVER_WMS) ff_wms_parse_sdp_a_line(s, p); @@ -761,7 +766,8 @@ int ff_sdp_parse(AVFormatContext *s, const char *content) { const char *p; int letter, i; - char buf[SDP_MAX_SIZE], *q; + char buf[SDP_MAX_SIZE], sdp_ip_str[50], *q; + bool parsed_first_media_desc = false; SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state; s->duration = AV_NOPTS_VALUE; @@ -770,6 +776,23 @@ int ff_sdp_parse(AVFormatContext *s, const char *content) for (;;) { p += strspn(p, SPACE_CHARS); letter = *p; + if(parsed_first_media_desc && (letter == 'm' || letter == '\0')) { + RTSPState *rt = s->priv_data; + RTSPStream *rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1]; + getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, + sizeof(rtsp_st->sdp_ip), + sdp_ip_str, sizeof(sdp_ip_str), NULL, 0, NI_NUMERICHOST); + copy_default_source_addrs(s1->default_include_source_addrs, + s1->nb_default_include_source_addrs, + &rtsp_st->include_source_addrs, + &rtsp_st->nb_include_source_addrs, + sdp_ip_str); + copy_default_source_addrs(s1->default_exclude_source_addrs, + s1->nb_default_exclude_source_addrs, + &rtsp_st->exclude_source_addrs, + &rtsp_st->nb_exclude_source_addrs, + sdp_ip_str); + } if (letter == '\0') break; p++; @@ -784,7 +807,11 @@ int ff_sdp_parse(AVFormatContext *s, const char *content) p++; } *q = '\0'; + sdp_parse_line(s, s1, letter, buf); + if(letter == 'm') + parsed_first_media_desc = true; + next_line: while (*p != '\n' && *p != '\0') p++; @@ -909,10 +936,11 @@ int ff_rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st) rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); - else if (CONFIG_RTPDEC) + else if (CONFIG_RTPDEC) { rtsp_st->transport_priv = ff_rtp_parse_open(s, st, rtsp_st->sdp_payload_type, reordering_queue_size); + } if (!rtsp_st->transport_priv) { return AVERROR(ENOMEM); @@ -1626,6 +1654,18 @@ int ff_rtsp_read_reply_async_stored(AVFormatContext *s, RTSPMessageHeader **repl return 0; } +static void append_source_addrs(char *buf, int size, const char *name, + int count, struct RTSPSource **addrs) +{ + int i; + if (!count) + return; + av_strlcatf(buf, size, "&%s=%s", name, addrs[0]->src_addr); + for (i = 1; i < count; i++) { + av_strlcatf(buf, size, ",%s", addrs[i]->src_addr); + } +} + int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port, int lower_transport, const char *real_challenge) { @@ -1684,6 +1724,11 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port, } else rtsp_st = rt->rtsp_streams[i]; + /* RTP/UDP Source Specific Multicast (SSM) */ + if (ff_is_multicast_address((struct sockaddr*) &rtsp_st->sdp_ip)) { + lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST; + } + /* RTP/UDP */ if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) { char buf[256]; @@ -1840,7 +1885,7 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port, break; } case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: { - char url[MAX_URL_SIZE], namebuf[50], optbuf[20] = ""; + char url[MAX_URL_SIZE], namebuf[50], optbuf[1024] = ""; struct sockaddr_storage addr; int port, ttl; AVDictionary *opts = map_to_opts(rt); @@ -1856,6 +1901,20 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port, } if (ttl > 0) snprintf(optbuf, sizeof(optbuf), "?ttl=%d", ttl); + + if (rtsp_st->nb_include_source_addrs > 0) { + if(strlen(optbuf) == 0 ) { + snprintf(optbuf, sizeof(optbuf), "?"); + } + + append_source_addrs(optbuf, sizeof(optbuf), "sources", + rtsp_st->nb_include_source_addrs, + rtsp_st->include_source_addrs); + append_source_addrs(optbuf, sizeof(optbuf), "block", + rtsp_st->nb_exclude_source_addrs, + rtsp_st->exclude_source_addrs); + } + getnameinfo((struct sockaddr*) &addr, sizeof(addr), namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST); ff_url_join(url, sizeof(url), "rtp", NULL, namebuf, @@ -2643,16 +2702,7 @@ static int sdp_probe(const AVProbeData *p1) return 0; } -static void append_source_addrs(char *buf, int size, const char *name, - int count, struct RTSPSource **addrs) -{ - int i; - if (!count) - return; - av_strlcatf(buf, size, "&%s=%s", name, addrs[0]->addr); - for (i = 1; i < count; i++) - av_strlcatf(buf, size, ",%s", addrs[i]->addr); -} + static int sdp_read_header(AVFormatContext *s) { diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h index a235fc81a7f..0fbcbd6e9c7 100644 --- a/libavformat/rtsp.h +++ b/libavformat/rtsp.h @@ -476,7 +476,8 @@ typedef struct RTSPState { #define RTSP_FLAG_SATIP_RAW 0x20 /**< Export SAT>IP stream as raw MPEG-TS */ typedef struct RTSPSource { - char addr[128]; /**< Source-specific multicast include source IP address (from SDP content) */ + char dest_addr[128]; /**< Source-specific multicast destination IP address (from SDP content) */ + char src_addr[128]; /**< Source-specific multicast include source IP address (from SDP content) */ } RTSPSource; /** -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
