From b903f1c019f01c1b3cf63702ca1c100bc0367dae Mon Sep 17 00:00:00 2001
From: Yalu Zhang <yalu.zhang@iopsys.eu>
Date: Mon, 20 Jan 2025 09:21:05 +0000
Subject: [PATCH] Add the rejected media stream and its attributes in the local
 SDP for answer, REF 14613

The attributes of the rejected media stream were missing in SDP answer before. With this
commit, all attributes from the SDP offer for the rejected media stream are kept in the
SDP answer.
---
 res/res_pjsip_session.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index a643ff6ba1..df42ae2577 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -5553,6 +5553,25 @@ static int add_sdp_streams(struct ast_sip_session_media *session_media,
 
 	handler_list = ao2_find(sdp_handlers, ast_codec_media_type2str(session_media->type), OBJ_KEY);
 	if (!handler_list) {
+		/* If there is no handler for this media, set port as 0 in m line and copy all media specific
+		 * attributes from the remote (offer) */
+		if ((ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED || !ast_stream_get_formats(stream) ||
+			!ast_format_cap_count(ast_stream_get_formats(stream))) && remote && remote->media[ast_stream_get_position(stream)]) {
+			pj_pool_t *pool = session->inv_session->pool_prov;
+			struct pjmedia_sdp_media *media = pjmedia_sdp_media_clone(pool, remote->media[ast_stream_get_position(stream)]);
+			if (media) {
+				/* Update port */
+				media->desc.port = 0;
+				media->desc.port_count = 1;
+
+				/* Add this media to the local SDP */
+				answer->media[answer->media_count++] = media;
+				ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
+
+				SCOPE_EXIT_RTN_VALUE(0, "Add a rejected media stream and its attributes\n");
+			}
+		}
+
 		SCOPE_EXIT_RTN_VALUE(0, "No handlers\n");
 	}
 
-- 
GitLab