Commit a3f8656840 for asterisk.org

commit a3f865684093b26c80a15f4c2ed0d2f626f81312
Author: Ben Ford <bford@sangoma.com>
Date:   Fri Sep 4 13:47:45 2026 -0500

    res_pjsip_sdp_rtp: Don't timeout if RTP has been received on any stream.

    When scheduling RTP timeout checks, it was done on a per stream basis,
    meaning that if RTP was not being received on one, the whole call would
    shut down. Now, RTP timeout checks are done at the session level, and
    each RTP instance on each session media is checked to see if any RTP has
    been received. If it has, the call remains up.

    Resolves: #1940

    UserNote: RTP timeout now checks to see if any RTP has been received
    across all streams on the session. If it has, the call remains up. The
    disconnect message no longer displays media type because of this. The
    initial timeout value used will be the lowest non-zero value between
    rtp_timeout and rtp_timeout_hold in pjsip.conf.

    DeveloperNote: timeout_sched_id on ast_sip_session_media is deprecated
    in favor of rtp_timeout_sched_task on ast_sip_session to keep track of
    the RTP timeout callback. It is no longer used or set, but has been kept
    on the struct for ABI.

diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h
index 3a2be8e895..fd9486d16d 100644
--- a/include/asterisk/res_pjsip_session.h
+++ b/include/asterisk/res_pjsip_session.h
@@ -91,7 +91,15 @@ struct ast_sip_session_media {
 	pj_str_t transport;
 	/*! \brief Scheduler ID for RTP keepalive */
 	int keepalive_sched_id;
-	/*! \brief Scheduler ID for RTP timeout */
+	/*!
+	 * \deprecated Moved to a task on ast_sip_session
+	 * \version 24.1.0 deprecated
+	 * \version 23.7.0 deprecated
+	 * \version 22.13.0 deprecated
+	 * \version 20.23.0 deprecated
+	 * Scheduler ID for RTP timeout (XXX no longer set or used, see
+	 * rtp_timeout_sched_task in ast_sip_session instead, kept for ABI)
+	 */
 	int timeout_sched_id;
 	/*! \brief Stream is on hold by remote side */
 	unsigned int remotely_held:1;
@@ -249,6 +257,8 @@ struct ast_sip_session {
 	enum ast_sip_session_call_direction call_direction;
 	/*! Originating Line Info (ANI II digits) */
 	int ani2;
+	/*! \brief The RTP timeout scheduler task for this session */
+	struct ast_sip_sched_task *rtp_timeout_sched_task;
 };

 typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c
index bb7bfbf978..c31b72f3b6 100644
--- a/res/res_pjsip_sdp_rtp.c
+++ b/res/res_pjsip_sdp_rtp.c
@@ -101,57 +101,163 @@ static int send_keepalive(const void *data)
 	return (keepalive - interval) * 1000;
 }

-/*! \brief Check whether RTP is being received or not */
-static int rtp_check_timeout(const void *data)
+/*!
+ * \brief Get the lowest positive value between rtp.timeout and rtp.timeout_hold, zero otherwise
+ *
+ * \note Assumes session has been NULL-checked beforehand
+ */
+static int get_lowest_rtp_timeout(struct ast_sip_session *session)
 {
-	struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data;
-	struct ast_rtp_instance *rtp = session_media->rtp;
-	struct ast_channel *chan;
-	int elapsed;
+	if (session->endpoint->media.rtp.timeout > 0 && session->endpoint->media.rtp.timeout_hold > 0) {
+		return session->endpoint->media.rtp.timeout < session->endpoint->media.rtp.timeout_hold ?
+			session->endpoint->media.rtp.timeout : session->endpoint->media.rtp.timeout_hold;
+	} else if (session->endpoint->media.rtp.timeout > 0) {
+		return session->endpoint->media.rtp.timeout;
+	} else if (session->endpoint->media.rtp.timeout_hold > 0) {
+		return session->endpoint->media.rtp.timeout_hold;
+	} else {
+		return 0;
+	}
+}
+
+/*! \brief Check whether RTP is being received on any RTP instance in the session or not */
+static int rtp_check_timeout(void *data)
+{
+	struct ast_sip_session_media_state *media_state;
+	int i;
 	int now;
-	int timeout;
+	int lowest_timeout_ms = -1; /* The lowest non-zero timeout to reschedule for */
+	int lowest_elapsed = -1; /* The lowest elapsed time if we did not receive RTP on any instance */
+	int schedule_again = 0; /* Set if we should reschedule this task; canceled otherwise */
+	int rtp_count = 0; /* Tracks the number of RTP instances that are present */
+	struct ast_sip_session *session = data;

-	if (!rtp) {
+	/* The scheduled task will be cleaned up by session termination */
+
+	/* Check if the session is terminated or not. inv_session should never be NULL here
+	 * due to it only being assigned when a session is allocated, and cleaned up in the
+	 * session destructor.
+	 */
+	if (session->inv_session && session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
 		return 0;
 	}

-	chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp));
-	if (!chan) {
+	if (!session->channel) {
+		return 0;
+	}
+
+	media_state = session->active_media_state;
+	if (!media_state) {
 		return 0;
 	}

-	/* Store these values locally to avoid multiple function calls */
 	now = time(NULL);
-	timeout = ast_rtp_instance_get_timeout(rtp);

-	/* If the channel is not in UP state or call is redirected
-	 * outside Asterisk return for later check.
+	for (i = 0; i < AST_VECTOR_SIZE(&media_state->sessions); i++) {
+		struct ast_sip_session_media *session_media;
+		struct ast_rtp_instance *rtp;
+		int elapsed;
+		int timeout;
+
+		session_media = AST_VECTOR_GET(&media_state->sessions, i);
+		if (!session_media) {
+			continue;
+		}
+
+		rtp = session_media->rtp;
+		if (!rtp) {
+			continue;
+		}
+		rtp_count++;
+
+		timeout = ast_rtp_instance_get_timeout(rtp);
+		/* Keep checking the other RTP instances to see if there is a timeout value to use,
+		 * otherwise we will schedule again using a timeout value on the session itself.
+		 */
+		if (timeout <= 0) {
+			schedule_again = 1;
+			continue;
+		}
+
+		/* If the call is redirected outside Asterisk or the channel state
+		 * isn't UP, use half the timeout as the current timeout value to check
+		 * for smallest timeout to avoid possible races and faster reacting to
+		 * cases when there is no RTP at all.
+		 */
+		if (ast_channel_state(session->channel) != AST_STATE_UP || !ast_sockaddr_isnull(&session_media->direct_media_addr)) {
+			/* Avoiding immediate disconnect after channel up or direct media has been stopped */
+			ast_rtp_instance_set_last_rx(rtp, now);
+			schedule_again = 1;
+			/* Only use half the timeout value (timeout * 1000 / 2) */
+			timeout *= 500;
+
+			/* If no timeout or this timeout is less than the lowest timeout so far, use it */
+			if (lowest_timeout_ms == -1 || timeout < lowest_timeout_ms) {
+				lowest_timeout_ms = timeout;
+			}
+		} else {
+			elapsed = now - ast_rtp_instance_get_last_rx(rtp);
+			if (elapsed < timeout) {
+				/* Use the timeout minus whatever time has gone over the timeout
+				 * value as the reschedule timeout.
+				 */
+				timeout = (timeout - elapsed) * 1000;
+				schedule_again = 1;
+
+				/* If no lowest timeout or this timeout is less than the lowest timeout so
+				 * far, use it. We only do this when RTP has actually been received within
+				 * the timeout window, otherwise we would get some crazy numbers for the
+				 * timeout if, for instance, another stream was on hold and this one had
+				 * not received RTP for a while.
+				 */
+				if (lowest_timeout_ms == -1 || timeout < lowest_timeout_ms) {
+					lowest_timeout_ms = timeout;
+				}
+			} else {
+				/* Update lowest elapsed time for disconnect log message below */
+				if (lowest_elapsed == -1 || elapsed < lowest_elapsed) {
+					lowest_elapsed = elapsed;
+				}
+			}
+		}
+	}
+
+	/* If there were no RTP instances to check, go ahead and reschedule.
+	 * This prevents cases where RTP started and then stopped (such as
+	 * switching over to T.38) but we don't want to terminate the call.
+	 */
+	if (!rtp_count) {
+		schedule_again = 1;
+	}
+
+	/* If we didn't get a timeout value greater than 0, use the lowest
+	 * non-zero timeout value between rtp.timeout and rtp.timeout_hold.
 	 */
-	if (ast_channel_state(chan) != AST_STATE_UP || !ast_sockaddr_isnull(&session_media->direct_media_addr)) {
-		/* Avoiding immediately disconnect after channel up or direct media has been stopped */
-		ast_rtp_instance_set_last_rx(rtp, now);
-		ast_channel_unref(chan);
-		/* Recheck after half timeout for avoiding possible races
-		* and faster reacting to cases while there is no an RTP at all.
-		*/
-		return timeout * 500;
+	if (lowest_timeout_ms <= 0) {
+		lowest_timeout_ms = get_lowest_rtp_timeout(session);
+		lowest_timeout_ms *= 1000;
+
+		/* If, for some reason, neither RTP timeout value is greater than 0, we can't reschedule.
+		 * This should be impossible because the endpoint can't change during the session and if
+		 * we are at this point, a timeout must have been configured.
+		 */
+		if (lowest_timeout_ms <= 0) {
+			schedule_again = 0;
+		}
 	}

-	elapsed = now - ast_rtp_instance_get_last_rx(rtp);
-	if (elapsed < timeout) {
-		ast_channel_unref(chan);
-		return (timeout - elapsed) * 1000;
+	if (schedule_again) {
+		return lowest_timeout_ms;
 	}

-	ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of %s RTP activity in %d seconds\n",
-		ast_channel_name(chan), ast_codec_media_type2str(session_media->type), elapsed);
+	ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of RTP activity in %d seconds\n",
+		ast_channel_name(session->channel), lowest_elapsed);

-	ast_channel_lock(chan);
-	ast_channel_hangupcause_set(chan, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
-	ast_channel_unlock(chan);
+	ast_channel_lock(session->channel);
+	ast_channel_hangupcause_set(session->channel, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
+	ast_channel_unlock(session->channel);

-	ast_softhangup(chan, AST_SOFTHANGUP_DEV);
-	ast_channel_unref(chan);
+	ast_softhangup(session->channel, AST_SOFTHANGUP_DEV);

 	return 0;
 }
@@ -2408,7 +2514,7 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session,
 	enum ast_media_type media_type = session_media->type;
 	char host[NI_MAXHOST];
 	int res;
-	int rtp_timeout;
+	int rtp_timeout = 0;
 	struct ast_sip_session_media *session_media_transport;
 	SCOPE_ENTER(1, "%s Stream: %s\n", ast_sip_session_get_name(session),
 		ast_str_tmp(128, ast_stream_to_str(asterisk_stream, &STR_TMP)));
@@ -2542,14 +2648,10 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session,
 			session_media, 1);
 	}

-	/* As the channel lock is not held during this process the scheduled item won't block if
-	 * it is hanging up the channel at the same point we are applying this negotiated SDP.
-	 */
-	AST_SCHED_DEL(sched, session_media->timeout_sched_id);
-
 	/* Due to the fact that we only ever have one scheduled timeout item for when we are both
 	 * off hold and on hold we don't need to store the two timeouts differently on the RTP
-	 * instance itself.
+	 * instance itself. The callback will automatically pick the lowest value across all
+	 * RTP instances on the session and use that to reschedule.
 	 */
 	ast_rtp_instance_set_timeout(session_media->rtp, 0);
 	if (session->endpoint->media.rtp.timeout && !session_media->remotely_held && !session_media->locally_held) {
@@ -2558,11 +2660,22 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session,
 		ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout_hold);
 	}

-	rtp_timeout = ast_rtp_instance_get_timeout(session_media->rtp);
-
-	if (rtp_timeout) {
-		session_media->timeout_sched_id = ast_sched_add_variable(sched,	rtp_timeout*1000, rtp_check_timeout,
-			session_media, 1);
+	/* If we don't have a scheduled task for RTP timeout, go ahead and schedule one.
+	 * Use the smaller of the two timeout values between rtp.timeout and rtp.timeout_hold
+	 * for the initial scheduling, then let the callback handle reschedule timing.
+	 */
+	if (!session->rtp_timeout_sched_task) {
+		rtp_timeout = get_lowest_rtp_timeout(session);
+
+		if (rtp_timeout > 0) {
+			session->rtp_timeout_sched_task = ast_sip_schedule_task(session->serializer,
+				rtp_timeout * 1000, rtp_check_timeout, NULL,
+				session, AST_SIP_SCHED_TASK_VARIABLE | AST_SIP_SCHED_TASK_DATA_AO2);
+			if (!session->rtp_timeout_sched_task) {
+				ast_log(LOG_ERROR, "Failed to schedule RTP timeout for channel '%s'\n",
+					ast_channel_name(session->channel));
+			}
+		}
 	}

 	SCOPE_EXIT_RTN_VALUE(1, "Handled\n");
@@ -2602,7 +2715,6 @@ static void stream_stop(struct ast_sip_session_media *session_media)
 	}

 	AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
-	AST_SCHED_DEL(sched, session_media->timeout_sched_id);
 	ast_rtp_instance_stop(session_media->rtp);
 }

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index e571cd447d..5ceca4dcaf 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -553,7 +553,6 @@ struct ast_sip_session_media *ast_sip_session_media_state_add(struct ast_sip_ses
 		session_media->remote_ice = session->endpoint->media.rtp.ice_support;
 		session_media->remote_rtcp_mux = session->endpoint->media.rtcp_mux;
 		session_media->keepalive_sched_id = -1;
-		session_media->timeout_sched_id = -1;
 		session_media->type = type;
 		session_media->stream_num = position;

@@ -3484,6 +3483,17 @@ void ast_sip_session_terminate(struct ast_sip_session *session, int response)
 		response = 603;
 	}

+	/* We do this here instead of in hangup because everything should be
+	 * handled by the serializer. If, for some reason, something goes
+	 * wrong when hanging up and this never gets called, there are much
+	 * bigger problems going on
+	 */
+	if (session->rtp_timeout_sched_task) {
+		ast_sip_sched_task_cancel(session->rtp_timeout_sched_task);
+		ao2_ref(session->rtp_timeout_sched_task, -1);
+		session->rtp_timeout_sched_task = NULL;
+	}
+
 	/* The media sessions need to exist for the lifetime of the underlying channel
 	 * to ensure that anything (such as bridge_native_rtp) has access to them as
 	 * appropriate. Since ast_sip_session_terminate is called by chan_pjsip and other
@@ -5732,7 +5742,6 @@ static struct ast_sip_session_media *test_media_add(
 	}

 	session_media->keepalive_sched_id = -1;
-	session_media->timeout_sched_id = -1;
 	session_media->type = type;
 	session_media->stream_num = position;
 	session_media->bundle_group = -1;