From 6b041d10921016d2713a9058f39dcdfbbae5dc38 Mon Sep 17 00:00:00 2001
From: Bernd Zobl <b.zobl@commend.com>
Date: Fri, 7 May 2021 08:18:40 +0200
Subject: [PATCH] res_pjsip_sdp_rtp: Evaluate remotely held for Session
 Progress

With the fix for ASTERISK_28754 channels are no longer put on hold if an
outbound INVITE is answered with a "Session Progress" containing
"inactive" audio.

The previous change moved the evaluation of the media attributes to
`negotiate_incoming_sdp_stream()` to have the `remotely_held` status
available when building the SDP in `create_outgoing_sdp_stream()`.
This however means that an answer to an outbound INVITE, which does not
traverse `negotiate_incoming_sdp_stream()`, cannot set the
`remotely_held` status anymore.

This change moves the check so that both, `negotiate_incoming_sdp_stream()` and
`apply_negotiated_sdp_stream()` can do the checks.

ASTERISK-29479

Change-Id: Icde805a819399d5123b688e1ed1d2bcd9d5b0f75
---
 res/res_pjsip_sdp_rtp.c | 42 ++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c
index a6f497568b..d422f01794 100644
--- a/res/res_pjsip_sdp_rtp.c
+++ b/res/res_pjsip_sdp_rtp.c
@@ -1458,6 +1458,29 @@ static void process_extmap_attributes(struct ast_sip_session *session, struct as
 	}
 }
 
+static void set_session_media_remotely_held(struct ast_sip_session_media *session_media,
+											const struct ast_sip_session *session,
+											const pjmedia_sdp_media *media,
+											const struct ast_stream *stream,
+											const struct ast_sockaddr *addrs)
+{
+	if (ast_sip_session_is_pending_stream_default(session, stream) &&
+		(session_media->type == AST_MEDIA_TYPE_AUDIO)) {
+		if (((addrs != NULL) && ast_sockaddr_isnull(addrs)) ||
+			((addrs != NULL) && ast_sockaddr_is_any(addrs)) ||
+			pjmedia_sdp_media_find_attr2(media, "sendonly", NULL) ||
+			pjmedia_sdp_media_find_attr2(media, "inactive", NULL)) {
+			if (!session_media->remotely_held) {
+				session_media->remotely_held = 1;
+				session_media->remotely_held_changed = 1;
+			}
+		} else if (session_media->remotely_held) {
+			session_media->remotely_held = 0;
+			session_media->remotely_held_changed = 1;
+		}
+	}
+}
+
 /*! \brief Function which negotiates an incoming media stream */
 static int negotiate_incoming_sdp_stream(struct ast_sip_session *session,
 	struct ast_sip_session_media *session_media, const pjmedia_sdp_session *sdp,
@@ -1551,21 +1574,8 @@ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session,
 		process_ice_auth_attrb(session, session_media, sdp, stream);
 	}
 
-	if (ast_sip_session_is_pending_stream_default(session, asterisk_stream) && media_type == AST_MEDIA_TYPE_AUDIO) {
-		/* Check if incomming SDP is changing the remotely held state */
-		if (ast_sockaddr_isnull(addrs) ||
-			ast_sockaddr_is_any(addrs) ||
-			pjmedia_sdp_media_find_attr2(stream, "sendonly", NULL) ||
-			pjmedia_sdp_media_find_attr2(stream, "inactive", NULL)) {
-			if (!session_media->remotely_held) {
-				session_media->remotely_held = 1;
-				session_media->remotely_held_changed = 1;
-			}
-		} else if (session_media->remotely_held) {
-			session_media->remotely_held = 0;
-			session_media->remotely_held_changed = 1;
-		}
-	}
+	/* Check if incoming SDP is changing the remotely held state */
+	set_session_media_remotely_held(session_media, session, stream, asterisk_stream, addrs);
 
 	joint = set_incoming_call_offer_cap(session, session_media, stream);
 	res = apply_cap_to_bundled(session_media, session_media_transport, asterisk_stream, joint);
@@ -2164,6 +2174,8 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session,
 		SCOPE_EXIT_RTN_VALUE(1, "moh\n");
 	}
 
+	set_session_media_remotely_held(session_media, session, remote_stream, asterisk_stream, addrs);
+
 	if (session_media->remotely_held_changed) {
 		if (session_media->remotely_held) {
 			/* The remote side has put us on hold */
-- 
GitLab