blob: d1ce934bcfb0d39d9d6ac5d2e5826dd3f0bce69e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * Implementation of RTCP statistics reporting.
13 */
14
15#include "rtcp.h"
16
17#include <string.h>
18
19#include "signal_processing_library.h"
20
pbos@webrtc.org0946a562013-04-09 00:28:06 +000021int WebRtcNetEQ_RTCPInit(WebRtcNetEQ_RTCP_t *RTCP_inst, uint16_t uw16_seqNo)
niklase@google.com470e71d2011-07-07 08:21:25 +000022{
23 /*
24 * Initialize everything to zero and then set the start values for the RTP packet stream.
25 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000026 WebRtcSpl_MemSetW16((int16_t*) RTCP_inst, 0,
27 sizeof(WebRtcNetEQ_RTCP_t) / sizeof(int16_t));
niklase@google.com470e71d2011-07-07 08:21:25 +000028 RTCP_inst->base_seq = uw16_seqNo;
29 RTCP_inst->max_seq = uw16_seqNo;
30 return 0;
31}
32
pbos@webrtc.org0946a562013-04-09 00:28:06 +000033int WebRtcNetEQ_RTCPUpdate(WebRtcNetEQ_RTCP_t *RTCP_inst, uint16_t uw16_seqNo,
34 uint32_t uw32_timeStamp, uint32_t uw32_recTime)
niklase@google.com470e71d2011-07-07 08:21:25 +000035{
pbos@webrtc.org0946a562013-04-09 00:28:06 +000036 int16_t w16_SeqDiff;
37 int32_t w32_TimeDiff;
38 int32_t w32_JitterDiff;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
40 /*
41 * Update number of received packets, and largest packet number received.
42 */
43 RTCP_inst->received++;
44 w16_SeqDiff = uw16_seqNo - RTCP_inst->max_seq;
45 if (w16_SeqDiff >= 0)
46 {
47 if (uw16_seqNo < RTCP_inst->max_seq)
48 {
49 /* Wrap around detected */
50 RTCP_inst->cycles++;
51 }
52 RTCP_inst->max_seq = uw16_seqNo;
53 }
54
55 /* Calculate Jitter, and update previous timestamps */
56 /* Note that the value in RTCP_inst->jitter is in Q4. */
57 if (RTCP_inst->received > 1)
58 {
59 w32_TimeDiff = (uw32_recTime - (uw32_timeStamp - RTCP_inst->transit));
60 w32_TimeDiff = WEBRTC_SPL_ABS_W32(w32_TimeDiff);
61 w32_JitterDiff = WEBRTC_SPL_LSHIFT_W16(w32_TimeDiff, 4) - RTCP_inst->jitter;
62 RTCP_inst->jitter = RTCP_inst->jitter + WEBRTC_SPL_RSHIFT_W32((w32_JitterDiff + 8), 4);
63 }
64 RTCP_inst->transit = (uw32_timeStamp - uw32_recTime);
65 return 0;
66}
67
68int WebRtcNetEQ_RTCPGetStats(WebRtcNetEQ_RTCP_t *RTCP_inst,
pbos@webrtc.org0946a562013-04-09 00:28:06 +000069 uint16_t *puw16_fraction_lost,
70 uint32_t *puw32_cum_lost, uint32_t *puw32_ext_max,
71 uint32_t *puw32_jitter, int16_t doNotReset)
niklase@google.com470e71d2011-07-07 08:21:25 +000072{
pbos@webrtc.org0946a562013-04-09 00:28:06 +000073 uint32_t uw32_exp_nr, uw32_exp_interval, uw32_rec_interval;
74 int32_t w32_lost;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
76 /* Extended highest sequence number received */
77 *puw32_ext_max
pbos@webrtc.org0946a562013-04-09 00:28:06 +000078 = (uint32_t) WEBRTC_SPL_LSHIFT_W32((uint32_t)RTCP_inst->cycles, 16)
niklase@google.com470e71d2011-07-07 08:21:25 +000079 + RTCP_inst->max_seq;
80
81 /*
82 * Calculate expected number of packets and compare it to the number of packets that
83 * were actually received => the cumulative number of packets lost can be extracted.
84 */
85 uw32_exp_nr = *puw32_ext_max - RTCP_inst->base_seq + 1;
86 if (RTCP_inst->received == 0)
87 {
88 /* no packets received, assume none lost */
89 *puw32_cum_lost = 0;
90 }
91 else if (uw32_exp_nr > RTCP_inst->received)
92 {
93 *puw32_cum_lost = uw32_exp_nr - RTCP_inst->received;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000094 if (*puw32_cum_lost > (uint32_t) 0xFFFFFF)
niklase@google.com470e71d2011-07-07 08:21:25 +000095 {
96 *puw32_cum_lost = 0xFFFFFF;
97 }
98 }
99 else
100 {
101 *puw32_cum_lost = 0;
102 }
103
104 /* Fraction lost (Since last report) */
105 uw32_exp_interval = uw32_exp_nr - RTCP_inst->exp_prior;
106 if (!doNotReset)
107 {
108 RTCP_inst->exp_prior = uw32_exp_nr;
109 }
110 uw32_rec_interval = RTCP_inst->received - RTCP_inst->rec_prior;
111 if (!doNotReset)
112 {
113 RTCP_inst->rec_prior = RTCP_inst->received;
114 }
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000115 w32_lost = (int32_t) (uw32_exp_interval - uw32_rec_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 if (uw32_exp_interval == 0 || w32_lost <= 0 || RTCP_inst->received == 0)
117 {
118 *puw16_fraction_lost = 0;
119 }
120 else
121 {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000122 *puw16_fraction_lost = (uint16_t) (WEBRTC_SPL_LSHIFT_W32(w32_lost, 8)
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 / uw32_exp_interval);
124 }
125 if (*puw16_fraction_lost > 0xFF)
126 {
127 *puw16_fraction_lost = 0xFF;
128 }
129
130 /* Inter-arrival jitter */
131 *puw32_jitter = (RTCP_inst->jitter) >> 4; /* scaling from Q4 */
132 return 0;
133}
134