blob: 35f73da847d68d3d64670d66e86768ba60d7a674 [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
21int WebRtcNetEQ_RTCPInit(WebRtcNetEQ_RTCP_t *RTCP_inst, WebRtc_UWord16 uw16_seqNo)
22{
23 /*
24 * Initialize everything to zero and then set the start values for the RTP packet stream.
25 */
26 WebRtcSpl_MemSetW16((WebRtc_Word16*) RTCP_inst, 0,
27 sizeof(WebRtcNetEQ_RTCP_t) / sizeof(WebRtc_Word16));
28 RTCP_inst->base_seq = uw16_seqNo;
29 RTCP_inst->max_seq = uw16_seqNo;
30 return 0;
31}
32
33int WebRtcNetEQ_RTCPUpdate(WebRtcNetEQ_RTCP_t *RTCP_inst, WebRtc_UWord16 uw16_seqNo,
34 WebRtc_UWord32 uw32_timeStamp, WebRtc_UWord32 uw32_recTime)
35{
36 WebRtc_Word16 w16_SeqDiff;
37 WebRtc_Word32 w32_TimeDiff;
38 WebRtc_Word32 w32_JitterDiff;
39
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,
69 WebRtc_UWord16 *puw16_fraction_lost,
70 WebRtc_UWord32 *puw32_cum_lost, WebRtc_UWord32 *puw32_ext_max,
71 WebRtc_UWord32 *puw32_jitter, WebRtc_Word16 doNotReset)
72{
73 WebRtc_UWord32 uw32_exp_nr, uw32_exp_interval, uw32_rec_interval;
74 WebRtc_Word32 w32_lost;
75
76 /* Extended highest sequence number received */
77 *puw32_ext_max
78 = (WebRtc_UWord32) WEBRTC_SPL_LSHIFT_W32((WebRtc_UWord32)RTCP_inst->cycles, 16)
79 + 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;
94 if (*puw32_cum_lost > (WebRtc_UWord32) 0xFFFFFF)
95 {
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 }
115 w32_lost = (WebRtc_Word32) (uw32_exp_interval - uw32_rec_interval);
116 if (uw32_exp_interval == 0 || w32_lost <= 0 || RTCP_inst->received == 0)
117 {
118 *puw16_fraction_lost = 0;
119 }
120 else
121 {
122 *puw16_fraction_lost = (WebRtc_UWord16) (WEBRTC_SPL_LSHIFT_W32(w32_lost, 8)
123 / 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