Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Jack Lau via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Jack Lau <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avformat/whip: add generate_unique_ssrc to avoid ssrc collision (PR #20318)
Date: Sat, 23 Aug 2025 04:20:18 +0300 (EEST)
Message-ID: <20250823012018.C745F680132@ffbox0-bg.ffmpeg.org> (raw)

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 <jacklau1222@qq.com>


From 8f27dbfcaac61282dbe30553dbe957566dbc336d Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau1222@qq.com>
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 <jacklau1222@qq.com>
---
 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".

                 reply	other threads:[~2025-08-23  1:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250823012018.C745F680132@ffbox0-bg.ffmpeg.org \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git