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