[Pkg-voip-commits] [janus] 171/282: Improved RTP headers rewriting in case of context switches

Jonas Smedegaard dr at jones.dk
Wed Dec 20 21:53:38 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/0.2.6-1
in repository janus.

commit c408dd57ba874378e7fedb9a17b5e14f17fa6f8f
Author: Lorenzo Miniero <lminiero at gmail.com>
Date:   Fri Nov 3 13:23:30 2017 +0100

    Improved RTP headers rewriting in case of context switches
---
 rtp.c | 38 +++++++++++++++++++++++++++++++++++---
 rtp.h |  3 ++-
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/rtp.c b/rtp.c
index 4cc6b43..78113af 100644
--- a/rtp.c
+++ b/rtp.c
@@ -14,6 +14,7 @@
 #include "rtp.h"
 #include "rtpsrtp.h"
 #include "debug.h"
+#include "utils.h"
 
 char *janus_rtp_payload(char *buf, int len, int *plen) {
 	if(!buf || len < 12)
@@ -231,8 +232,12 @@ void janus_rtp_switching_context_reset(janus_rtp_switching_context *context) {
 }
 
 void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step) {
-	if(header == NULL || context == NULL || step < 0)
+	if(header == NULL || context == NULL)
 		return;
+	/* Note: while the step property is still there for compatibility reasons, to
+	 * keep the signature as it was before, it's ignored: whenever there's a switch
+	 * to take into account, we compute how much time passed between the last RTP
+	 * packet with the old SSRC and this new one, and prepare a timestamp accordingly */
 	uint32_t ssrc = ntohl(header->ssrc);
 	uint32_t timestamp = ntohl(header->timestamp);
 	uint16_t seq = ntohs(header->seq_number);
@@ -246,6 +251,16 @@ void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_conte
 			context->v_base_ts = timestamp;
 			context->v_base_seq_prev = context->v_last_seq;
 			context->v_base_seq = seq;
+			/* How much time since the last video RTP packet? We compute an offset accordingly */
+			if(context->v_last_time > 0) {
+				gint64 time_diff = janus_get_monotonic_time() - context->v_last_time;
+				time_diff = (time_diff/1000)*90;	/* We're assuming 90khz here */
+				if(time_diff == 0)
+					time_diff = 1;
+				context->v_base_ts_prev += (guint32)time_diff;
+				context->v_last_ts += (guint32)time_diff;
+				JANUS_LOG(LOG_VERB, "Computed offset for video RTP timestamp: %"SCNu32"\n", (guint32)time_diff);
+			}
 		}
 		if(context->v_seq_reset) {
 			/* Video sequence number was paused for a while: just update that */
@@ -254,11 +269,13 @@ void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_conte
 			context->v_base_seq = seq;
 		}
 		/* Compute a coherent timestamp and sequence number */
-		context->v_last_ts = (timestamp-context->v_base_ts) + context->v_base_ts_prev+step;
+		context->v_last_ts = (timestamp-context->v_base_ts) + context->v_base_ts_prev;
 		context->v_last_seq = (seq-context->v_base_seq)+context->v_base_seq_prev+1;
 		/* Update the timestamp and sequence number in the RTP packet */
 		header->timestamp = htonl(context->v_last_ts);
 		header->seq_number = htons(context->v_last_seq);
+		/* Take note of when we last handled this RTP packet */
+		context->v_last_time = janus_get_monotonic_time();
 	} else {
 		if(ssrc != context->a_last_ssrc) {
 			/* Audio SSRC changed: update both sequence number and timestamp */
@@ -269,6 +286,19 @@ void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_conte
 			context->a_base_ts = timestamp;
 			context->a_base_seq_prev = context->a_last_seq;
 			context->a_base_seq = seq;
+			/* How much time since the last audio RTP packet? We compute an offset accordingly */
+			if(context->a_last_time > 0) {
+				gint64 time_diff = janus_get_monotonic_time() - context->a_last_time;
+				int akhz = 48;
+				if(header->type == 0 || header->type == 8 || header->type == 9)
+					akhz = 8;	/* We're assuming 48khz here (Opus), unless it's G.711/G.722 (8khz) */
+				time_diff = (time_diff/1000)*(akhz);
+				if(time_diff == 0)
+					time_diff = 1;
+				context->a_base_ts_prev += (guint32)time_diff;
+				context->a_last_ts += (guint32)time_diff;
+				JANUS_LOG(LOG_VERB, "Computed offset for audio RTP timestamp: %"SCNu32"\n", (guint32)time_diff);
+			}
 		}
 		if(context->a_seq_reset) {
 			/* Audio sequence number was paused for a while: just update that */
@@ -277,11 +307,13 @@ void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_conte
 			context->a_base_seq = seq;
 		}
 		/* Compute a coherent timestamp and sequence number */
-		context->a_last_ts = (timestamp-context->a_base_ts) + context->a_base_ts_prev+step;
+		context->a_last_ts = (timestamp-context->a_base_ts) + context->a_base_ts_prev;
 		context->a_last_seq = (seq-context->a_base_seq)+context->a_base_seq_prev+1;
 		/* Update the timestamp and sequence number in the RTP packet */
 		header->timestamp = htonl(context->a_last_ts);
 		header->seq_number = htons(context->a_last_seq);
+		/* Take note of when we last handled this RTP packet */
+		context->a_last_time = janus_get_monotonic_time();
 	}
 }
 
diff --git a/rtp.h b/rtp.h
index b702331..b56e3fc 100644
--- a/rtp.h
+++ b/rtp.h
@@ -150,6 +150,7 @@ typedef struct janus_rtp_switching_context {
 	uint16_t a_last_seq, a_base_seq, a_base_seq_prev,
 			v_last_seq, v_base_seq, v_base_seq_prev;
 	gboolean a_seq_reset, v_seq_reset;
+	gint64 a_last_time, v_last_time;
 } janus_rtp_switching_context;
 
 /*! \brief Set (or reset) the context fields to their default values
@@ -160,7 +161,7 @@ void janus_rtp_switching_context_reset(janus_rtp_switching_context *context);
  * @param[in] header The RTP header to update
  * @param[in] context The context to use as a reference
  * @param[in] video Whether this is an audio or a video packet
- * @param[in] step The expected timestamp step */
+ * @param[in] step \b deprecated The expected timestamp step */
 void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step);
 
 #endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-voip/janus.git



More information about the Pkg-voip-commits mailing list