Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avformat/whip: add generate_unique_ssrc to avoid ssrc collision (PR #20318)
@ 2025-08-23  1:20 Jack Lau via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: Jack Lau via ffmpeg-devel @ 2025-08-23  1:20 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Jack Lau

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".

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-08-23  1:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-23  1:20 [FFmpeg-devel] [PATCH] avformat/whip: add generate_unique_ssrc to avoid ssrc collision (PR #20318) Jack Lau via ffmpeg-devel

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