blob: 3572471460c410a367ac23f418a427c121924805 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +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#include "webrtc/modules/audio_coding/neteq4/rtcp.h"
12
13#include <string.h>
14
15#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
16#include "webrtc/modules/interface/module_common_types.h"
17
18namespace webrtc {
19
20void Rtcp::Init(uint16_t start_sequence_number) {
21 cycles_ = 0;
22 max_seq_no_ = start_sequence_number;
23 base_seq_no_ = start_sequence_number;
24 received_packets_ = 0;
25 received_packets_prior_ = 0;
26 expected_prior_ = 0;
27 jitter_ = 0;
28 transit_ = 0;
29}
30
31void Rtcp::Update(const RTPHeader& rtp_header, uint32_t receive_timestamp) {
32 // Update number of received packets, and largest packet number received.
33 received_packets_++;
34 int16_t sn_diff = rtp_header.sequenceNumber - max_seq_no_;
35 if (sn_diff >= 0) {
36 if (rtp_header.sequenceNumber < max_seq_no_) {
37 // Wrap-around detected.
38 cycles_++;
39 }
40 max_seq_no_ = rtp_header.sequenceNumber;
41 }
42
43 // Calculate jitter according to RFC 3550, and update previous timestamps.
44 // Note that the value in |jitter_| is in Q4.
45 if (received_packets_ > 1) {
46 int32_t ts_diff = receive_timestamp - (rtp_header.timestamp - transit_);
47 ts_diff = WEBRTC_SPL_ABS_W32(ts_diff);
48 int32_t jitter_diff = (ts_diff << 4) - jitter_;
49 // Calculate 15 * jitter_ / 16 + jitter_diff / 16 (with proper rounding).
50 jitter_ = jitter_ + ((jitter_diff + 8) >> 4);
51 }
52 transit_ = rtp_header.timestamp - receive_timestamp;
53}
54
55void Rtcp::GetStatistics(bool no_reset, RtcpStatistics* stats) {
56 // Extended highest sequence number received.
57 stats->extended_max = (static_cast<int>(cycles_) << 16) + max_seq_no_;
58
59 // Calculate expected number of packets and compare it with the number of
60 // packets that were actually received. The cumulative number of lost packets
61 // can be extracted.
62 uint32_t expected_packets = stats->extended_max - base_seq_no_ + 1;
63 if (received_packets_ == 0) {
64 // No packets received, assume none lost.
65 stats->cumulative_lost = 0;
66 } else if (expected_packets > received_packets_) {
67 stats->cumulative_lost = expected_packets - received_packets_;
68 if (stats->cumulative_lost > 0xFFFFFF) {
69 stats->cumulative_lost = 0xFFFFFF;
70 }
71 } else {
72 stats->cumulative_lost = 0;
73 }
74
75 // Fraction lost since last report.
76 uint32_t expected_since_last = expected_packets - expected_prior_;
77 uint32_t received_since_last = received_packets_ - received_packets_prior_;
78 if (!no_reset) {
79 expected_prior_ = expected_packets;
80 received_packets_prior_ = received_packets_;
81 }
82 int32_t lost = expected_since_last - received_since_last;
83 if (expected_since_last == 0 || lost <= 0 || received_packets_ == 0) {
84 stats->fraction_lost = 0;
85 } else {
86 stats->fraction_lost = (lost << 8) / expected_since_last;
87 }
88 if (stats->fraction_lost > 0xFF) {
89 stats->fraction_lost = 0xFF;
90 }
91
92 stats->jitter = jitter_ >> 4; // Scaling from Q4.
93}
94
95} // namespace webrtc