blob: ef3ca8b624c2560a91ce14e580870a12ae41a913 [file] [log] [blame]
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/remote_bitrate_estimator/inter_arrival.h"
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000012
13#include <algorithm>
14#include <cassert>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/include/module_common_types.h"
17#include "rtc_base/logging.h"
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000018
19namespace webrtc {
20
stefan1112b2b2016-04-14 08:08:15 -070021static const int kBurstDeltaThresholdMs = 5;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000022
23InterArrival::InterArrival(uint32_t timestamp_group_length_ticks,
24 double timestamp_to_ms_coeff,
25 bool enable_burst_grouping)
26 : kTimestampGroupLengthTicks(timestamp_group_length_ticks),
27 current_timestamp_group_(),
28 prev_timestamp_group_(),
29 timestamp_to_ms_coeff_(timestamp_to_ms_coeff),
stefan5e12d362016-07-11 01:44:02 -070030 burst_grouping_(enable_burst_grouping),
31 num_consecutive_reordered_packets_(0) {}
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000032
33bool InterArrival::ComputeDeltas(uint32_t timestamp,
34 int64_t arrival_time_ms,
stefan5e12d362016-07-11 01:44:02 -070035 int64_t system_time_ms,
stefan64c0a0a2015-11-27 01:02:31 -080036 size_t packet_size,
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000037 uint32_t* timestamp_delta,
stefan64c0a0a2015-11-27 01:02:31 -080038 int64_t* arrival_time_delta_ms,
39 int* packet_size_delta) {
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000040 assert(timestamp_delta != NULL);
41 assert(arrival_time_delta_ms != NULL);
stefan64c0a0a2015-11-27 01:02:31 -080042 assert(packet_size_delta != NULL);
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000043 bool calculated_deltas = false;
44 if (current_timestamp_group_.IsFirstPacket()) {
45 // We don't have enough data to update the filter, so we store it until we
46 // have two frames of data to process.
47 current_timestamp_group_.timestamp = timestamp;
48 current_timestamp_group_.first_timestamp = timestamp;
49 } else if (!PacketInOrder(timestamp)) {
50 return false;
51 } else if (NewTimestampGroup(arrival_time_ms, timestamp)) {
52 // First packet of a later frame, the previous frame sample is ready.
53 if (prev_timestamp_group_.complete_time_ms >= 0) {
54 *timestamp_delta = current_timestamp_group_.timestamp -
55 prev_timestamp_group_.timestamp;
56 *arrival_time_delta_ms = current_timestamp_group_.complete_time_ms -
57 prev_timestamp_group_.complete_time_ms;
stefan5e12d362016-07-11 01:44:02 -070058 // Check system time differences to see if we have an unproportional jump
59 // in arrival time. In that case reset the inter-arrival computations.
60 int64_t system_time_delta_ms =
61 current_timestamp_group_.last_system_time_ms -
62 prev_timestamp_group_.last_system_time_ms;
63 if (*arrival_time_delta_ms - system_time_delta_ms >=
64 kArrivalTimeOffsetThresholdMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG(LS_WARNING)
66 << "The arrival time clock offset has changed (diff = "
67 << *arrival_time_delta_ms - system_time_delta_ms
68 << " ms), resetting.";
stefan5e12d362016-07-11 01:44:02 -070069 Reset();
70 return false;
71 }
stefancfd5f962015-07-24 03:26:46 -070072 if (*arrival_time_delta_ms < 0) {
73 // The group of packets has been reordered since receiving its local
74 // arrival timestamp.
stefan5e12d362016-07-11 01:44:02 -070075 ++num_consecutive_reordered_packets_;
76 if (num_consecutive_reordered_packets_ >= kReorderedResetThreshold) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_WARNING)
78 << "Packets are being reordered on the path from the "
79 "socket to the bandwidth estimator. Ignoring this "
80 "packet for bandwidth estimation, resetting.";
stefan5e12d362016-07-11 01:44:02 -070081 Reset();
82 }
stefancfd5f962015-07-24 03:26:46 -070083 return false;
stefan5e12d362016-07-11 01:44:02 -070084 } else {
85 num_consecutive_reordered_packets_ = 0;
stefancfd5f962015-07-24 03:26:46 -070086 }
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000087 assert(*arrival_time_delta_ms >= 0);
stefan64c0a0a2015-11-27 01:02:31 -080088 *packet_size_delta = static_cast<int>(current_timestamp_group_.size) -
89 static_cast<int>(prev_timestamp_group_.size);
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000090 calculated_deltas = true;
91 }
92 prev_timestamp_group_ = current_timestamp_group_;
93 // The new timestamp is now the current frame.
94 current_timestamp_group_.first_timestamp = timestamp;
95 current_timestamp_group_.timestamp = timestamp;
stefan64c0a0a2015-11-27 01:02:31 -080096 current_timestamp_group_.size = 0;
terelius8f09f172015-12-15 00:51:54 -080097 } else {
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000098 current_timestamp_group_.timestamp = LatestTimestamp(
99 current_timestamp_group_.timestamp, timestamp);
100 }
stefan64c0a0a2015-11-27 01:02:31 -0800101 // Accumulate the frame size.
102 current_timestamp_group_.size += packet_size;
103 current_timestamp_group_.complete_time_ms = arrival_time_ms;
stefan5e12d362016-07-11 01:44:02 -0700104 current_timestamp_group_.last_system_time_ms = system_time_ms;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000105
106 return calculated_deltas;
107}
108
109bool InterArrival::PacketInOrder(uint32_t timestamp) {
110 if (current_timestamp_group_.IsFirstPacket()) {
111 return true;
112 } else {
113 // Assume that a diff which is bigger than half the timestamp interval
114 // (32 bits) must be due to reordering. This code is almost identical to
115 // that in IsNewerTimestamp() in module_common_types.h.
116 uint32_t timestamp_diff = timestamp -
117 current_timestamp_group_.first_timestamp;
118 return timestamp_diff < 0x80000000;
119 }
120}
121
122// Assumes that |timestamp| is not reordered compared to
123// |current_timestamp_group_|.
124bool InterArrival::NewTimestampGroup(int64_t arrival_time_ms,
125 uint32_t timestamp) const {
126 if (current_timestamp_group_.IsFirstPacket()) {
127 return false;
128 } else if (BelongsToBurst(arrival_time_ms, timestamp)) {
129 return false;
130 } else {
131 uint32_t timestamp_diff = timestamp -
132 current_timestamp_group_.first_timestamp;
133 return timestamp_diff > kTimestampGroupLengthTicks;
134 }
135}
136
137bool InterArrival::BelongsToBurst(int64_t arrival_time_ms,
138 uint32_t timestamp) const {
139 if (!burst_grouping_) {
140 return false;
141 }
142 assert(current_timestamp_group_.complete_time_ms >= 0);
143 int64_t arrival_time_delta_ms = arrival_time_ms -
144 current_timestamp_group_.complete_time_ms;
145 uint32_t timestamp_diff = timestamp - current_timestamp_group_.timestamp;
146 int64_t ts_delta_ms = timestamp_to_ms_coeff_ * timestamp_diff + 0.5;
147 if (ts_delta_ms == 0)
148 return true;
149 int propagation_delta_ms = arrival_time_delta_ms - ts_delta_ms;
150 return propagation_delta_ms < 0 &&
151 arrival_time_delta_ms <= kBurstDeltaThresholdMs;
152}
stefan5e12d362016-07-11 01:44:02 -0700153
154void InterArrival::Reset() {
155 num_consecutive_reordered_packets_ = 0;
156 current_timestamp_group_ = TimestampGroup();
157 prev_timestamp_group_ = TimestampGroup();
158}
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000159} // namespace webrtc