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