From 0f92aaad76b43970287799f57e0c48e4a19c9462 Mon Sep 17 00:00:00 2001
From: Grzegorz Sluja <grzegorz.sluja@iopsys.eu>
Date: Mon, 23 Oct 2023 10:12:49 +0000
Subject: [PATCH] Set the SSRC correctly to avoid a collision error in libsrtp

---
 res/res_rtp_asterisk.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index d9c3319a82..c23f972b96 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -5066,9 +5066,17 @@ static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *fr
 		packet_len = frame->datalen + hdrlen;
 		rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
 
+		if (frame->ssrc && frame->ssrc != rtp->ssrc) {
+			struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance, 0);
+			if (srtp) {
+				res_srtp->change_source(srtp, rtp->ssrc, frame->ssrc);
+			}
+			rtp->ssrc = frame->ssrc;
+		}
+
 		put_unaligned_uint32(rtpheader, htonl((2 << 30) | (ext << 28) | (codec << 16) | (frame->seqno ? frame->seqno : seqno) | (mark << 23)));
 		put_unaligned_uint32(rtpheader + 4, htonl(frame->ts ? frame->ts : rtp->lastts));
-		put_unaligned_uint32(rtpheader + 8, htonl(frame->ssrc ? frame->ssrc : rtp->ssrc));
+		put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
 
 		/* We assume right now that we will only ever have the abs-send-time extension in the packet
 		 * which simplifies things a bit.
-- 
GitLab