From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id BD7A949ED7 for ; Sat, 23 Aug 2025 01:20:23 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id C745F680132; Sat, 23 Aug 2025 04:20:18 +0300 (EEST) Received: from 0f4167fb2350 (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 317C8680132 for ; Sat, 23 Aug 2025 04:20:17 +0300 (EEST) MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] =?utf-8?q?=5BPATCH=5D_avformat/whip=3A_add_genera?= =?utf-8?q?te=5Funique=5Fssrc_to_avoid_ssrc_collision_=28PR_=2320318=29?= X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Jack Lau via ffmpeg-devel Reply-To: FFmpeg development discussions and patches Cc: Jack Lau Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Message-Id: <20250823012018.C745F680132@ffbox0-bg.ffmpeg.org> Date: Sat, 23 Aug 2025 04:20:18 +0300 (EEST) Archived-At: List-Archive: List-Post: PR #20318 opened by Jack Lau (JackLau) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20318 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20318.patch Signed-off-by: Jack Lau >From 8f27dbfcaac61282dbe30553dbe957566dbc336d Mon Sep 17 00:00:00 2001 From: Jack Lau Date: Sat, 23 Aug 2025 09:09:47 +0800 Subject: [PATCH] avformat/whip: add generate_unique_ssrc to avoid ssrc collision Signed-off-by: Jack Lau --- libavformat/whip.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/libavformat/whip.c b/libavformat/whip.c index e2fa774c6f..a754066aa2 100644 --- a/libavformat/whip.c +++ b/libavformat/whip.c @@ -150,6 +150,8 @@ #define WHIP_SDP_SESSION_ID "4489045141692799359" #define WHIP_SDP_CREATOR_IP "127.0.0.1" +#define SSRC_GENERATOR_MAX_TRY 100 + /* Calculate the elapsed time from starttime to endtime in milliseconds. */ #define ELAPSED(starttime, endtime) ((int)(endtime - starttime) / 1000) @@ -216,6 +218,9 @@ typedef struct WHIPContext { /* The ICE username and pwd fragment generated by the muxer. */ char ice_ufrag_local[9]; char ice_pwd_local[33]; + + uint32_t ssrc_buffer[3]; //video, audio, video rtx + int nb_ssrc; /* The SSRC of the audio and video stream, generated by the muxer. */ uint32_t audio_ssrc; uint32_t video_ssrc; @@ -546,6 +551,35 @@ static int parse_codec(AVFormatContext *s) return ret; } +static uint32_t generate_unique_ssrc(WHIPContext *whip) +{ + uint32_t candidate = 0; + int times, is_unique, i; + + for (times = 0; times < SSRC_GENERATOR_MAX_TRY; times++) { + candidate = av_lfg_get(&whip->rnd); + if (candidate == 0) + continue; + + is_unique = 1; + for (i = 0; i < whip->nb_ssrc; i++) { + if (whip->ssrc_buffer[i] == candidate) { + is_unique = 0; + break; + } + } + + if (is_unique) { + whip->ssrc_buffer[whip->nb_ssrc] = candidate; + whip->nb_ssrc++; + return candidate; + } + } + + av_log(whip, AV_LOG_ERROR, "Failed to generate unique SSRC after %d tries", times); + return candidate; +} + /** * Generate SDP offer according to the codec parameters, DTLS and ICE information. * @@ -576,8 +610,8 @@ static int generate_sdp_offer(AVFormatContext *s) av_lfg_get(&whip->rnd), av_lfg_get(&whip->rnd), av_lfg_get(&whip->rnd), av_lfg_get(&whip->rnd)); - whip->audio_ssrc = av_lfg_get(&whip->rnd); - whip->video_ssrc = av_lfg_get(&whip->rnd); + whip->audio_ssrc = generate_unique_ssrc(whip); + whip->video_ssrc = generate_unique_ssrc(whip); whip->audio_payload_type = WHIP_RTP_PAYLOAD_TYPE_OPUS; whip->video_payload_type = WHIP_RTP_PAYLOAD_TYPE_H264; -- 2.49.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".