From 7e9c81c7aef58b2cb4687d089f4f96c4b0604906 Mon Sep 17 00:00:00 2001
From: Grzegorz Sluja <grzegorz.sluja@iopsys.eu>
Date: Wed, 14 Sep 2022 14:01:42 +0000
Subject: [PATCH] Use the original sequence numbers when generating RTP headers
 in chan_brcm

Sequence numbers received in RTP packet need to be forwarded to brcm endpoint since based on this parameter
some of RTP statistics are calculated. It was wrong to use the locally generated sequence numbers.
---
 channels/chan_brcm.c | 27 ++++++++++++---------------
 channels/chan_brcm.h |  1 -
 2 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/channels/chan_brcm.c b/channels/chan_brcm.c
index 7b2f7ae291..756a862107 100644
--- a/channels/chan_brcm.c
+++ b/channels/chan_brcm.c
@@ -77,7 +77,7 @@ static int brcm_in_conference(const struct brcm_pvt *p);
 static int cwtimeout_cb(const void *data);
 static int cwbeep_cb(const void *data);
 static int r4hanguptimeout_cb(const void *data);
-static void brcm_generate_rtp_packet(struct brcm_subchannel *p, uint8_t *packet_buf, int type, int marker, int dtmf_timestamp);
+static void brcm_generate_rtp_packet(struct brcm_subchannel *p, uint8_t *packet_buf, int type, int marker, int dtmf_timestamp, int seqno);
 static void brcm_interpret_rtcp_packet(struct brcm_subchannel *p, uint8_t *rtcp_frame, uint32_t rtcp_size);
 static int brcm_mute_connection(struct brcm_subchannel *p);
 static int brcm_unmute_connection(struct brcm_subchannel *p);
@@ -1367,7 +1367,7 @@ static int brcm_write(struct ast_channel *ast, struct ast_frame *frame)
 		pvt_lock(sub->parent, "TELCHAN write frame");
 
 		/* generate the rtp header */
-		brcm_generate_rtp_packet(sub, ap->rtp, CN, 0, 0);
+		brcm_generate_rtp_packet(sub, ap->rtp, CN, 0, 0, frame->seqno);
 
 		/* set rtp id sent to endpoint */
 		sub->codec = CN;
@@ -1397,7 +1397,7 @@ static int brcm_write(struct ast_channel *ast, struct ast_frame *frame)
 		pvt_lock(sub->parent, "TELCHAN write frame");
 
 		/* generate the rtp header */
-		brcm_generate_rtp_packet(sub, ap->rtp, map_ast_codec_id_to_rtp(frame->subclass.format), 0, 0);
+		brcm_generate_rtp_packet(sub, ap->rtp, map_ast_codec_id_to_rtp(frame->subclass.format), 0, 0, frame->seqno);
 
 		/* set rtp id sent to endpoint */
 		sub->codec = map_ast_codec_id_to_rtp(frame->subclass.format);
@@ -3069,7 +3069,6 @@ static struct brcm_pvt *brcm_allocate_pvt(void)
 				sub->connection_init = 0;
 				sub->channel_state = ONHOOK;
 				sub->time_stamp = 0;
-				sub->sequence_number = 0;
 				sub->ssrc = 0;
 				sub->codec = -1;
 				sub->parent = tmp;
@@ -3331,7 +3330,6 @@ static void brcm_show_subchannels(struct ast_cli_args *a, struct brcm_pvt *p)
 		ast_cli(a->fd, "  Channel state       : %s\n", state2str(sub->channel_state));
 		ast_cli(a->fd, "  Connection init     : %d\n", sub->connection_init);
 		ast_cli(a->fd, "  Codec used          : %s\n", brcm_get_codec_string(sub->codec));
-		ast_cli(a->fd, "  RTP sequence number : %d\n", sub->sequence_number);
 		ast_cli(a->fd, "  RTP SSRC            : %d\n", sub->ssrc);
 		ast_cli(a->fd, "  RTP timestamp       : %d\n", sub->time_stamp);
 		ast_cli(a->fd, "  CW Timer id         : %d\n", sub->cw_timer_id);
@@ -4093,13 +4091,13 @@ static void ubus_call_answer_rtp_stats(struct ubus_request *req, int type, struc
 
 	sub->rtp_stats.averageFarEndInterarrivalJitter = sub->jitter_count ? (sub->farEndInterrivalJitter / sub->jitter_count) : 0;
 
-	ast_log(LOG_DEBUG, "RTP stats received:\nlocalBurstDensity: %d\nremoteBurstDensity: %d\n"
-			"localBurstDuration: %d\nremoteBurstDuration: %d\nlocalGapDensity: %d\n"
-			"remoteGapDensity: %d\nlocalGapDuration: %d\nremoteGapDuration: %d\n"
-			"localJbRate: %d\nremoteJbRate: %d\nlocalJbMax: %d\nremoteJbMax: %d\n"
-			"localJbNominal: %d\nremoteJbNominal: %d\nlocalJbAbsMax: %d\n"
-			"remoteJbAbsMax: %d\ndiscarded: %d\nlost: %d\nrxpkts: %d\ntxpkts: %d\n"
-			"jbAvg: %d\njitter: %d\nuLossRate: %d\nmaxJitter: %d\naverageRoundTripDelay: %d\n"
+	ast_log(LOG_DEBUG, "RTP stats received:\nlocalBurstDensity: %d, remoteBurstDensity: %d, "
+			"localBurstDuration: %d, remoteBurstDuration: %d, localGapDensity: %d, "
+			"remoteGapDensity: %d, localGapDuration: %d, remoteGapDuration: %d, "
+			"localJbRate: %d, remoteJbRate: %d, localJbMax: %d, remoteJbMax: %d, "
+			"localJbNominal: %d remoteJbNominal: %d, localJbAbsMax: %d, "
+			"remoteJbAbsMax: %d, discarded: %d, lost: %d, rxpkts: %d, txpkts: %d, "
+			"jbAvg: %d, jitter: %d, uLossRate: %d, maxJitter: %d, averageRoundTripDelay: %d, "
 			"averageFarEndInterarrivalJitter: %d\n",
 			sub->rtp_stats.localBurstDensity, sub->rtp_stats.remoteBurstDensity, sub->rtp_stats.localBurstDuration,
 			sub->rtp_stats.remoteBurstDuration, sub->rtp_stats.localGapDensity, sub->rtp_stats.remoteGapDensity,
@@ -4734,7 +4732,7 @@ static int brcm_close_connection(struct brcm_subchannel *sub)
 
 
 /* Generate rtp payload, 12 bytes of header and 160 bytes of ulaw payload */
-static void brcm_generate_rtp_packet(struct brcm_subchannel *sub, uint8_t *packet_buf, int type, int marker, int dtmf_timestamp) {
+static void brcm_generate_rtp_packet(struct brcm_subchannel *sub, uint8_t *packet_buf, int type, int marker, int dtmf_timestamp, int seqno) {
 	unsigned short* packet_buf16 = (unsigned short*)packet_buf;
 	unsigned int*   packet_buf32 = (unsigned int*)packet_buf;
 
@@ -4745,8 +4743,7 @@ static void brcm_generate_rtp_packet(struct brcm_subchannel *sub, uint8_t *packe
 	//CSRC count 0
 	packet_buf[1] = type;
 	packet_buf[1] |= marker?0x80:0x00;
-	packet_buf16[1] = htons(sub->sequence_number++); //Add sequence number
-	if (sub->sequence_number > 0xFFFF) sub->sequence_number=0;
+	packet_buf16[1] = htons(seqno); //Add sequence number
 	packet_buf32[1] = htonl(sub->time_stamp);	//Add timestamp
 	sub->time_stamp += sub->period*8;
 	packet_buf32[2] = sub->ssrc;	//Random SSRC
diff --git a/channels/chan_brcm.h b/channels/chan_brcm.h
index e2df705cd9..84b5203ee9 100644
--- a/channels/chan_brcm.h
+++ b/channels/chan_brcm.h
@@ -118,7 +118,6 @@ struct brcm_subchannel {
 	enum CALL_DIRECTION call_direction;		// Direction of call for the subchannel : 0 = incoming, 1 = outgoing
 	unsigned int connection_init;	/* State for endpoint id connection initialization */
 	struct ast_frame fr;		/* Frame */
-	unsigned int sequence_number;	/* Endpoint RTP sequence number state */
 	unsigned int time_stamp;	/* Endpoint RTP time stamp state */
 	unsigned int period;		/* Endpoint RTP period */
 	unsigned int ssrc;		/* Endpoint RTP synchronization source */
-- 
GitLab