blob: b8e683b89a6f2efc2f39580394c136309dd675ab [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
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000013#include <cassert>
14
Yves Gerey988cc082018-10-23 12:03:01 +020015#include "modules/include/module_common_types_public.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/logging.h"
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000017
18namespace webrtc {
19
stefan1112b2b2016-04-14 08:08:15 -070020static const int kBurstDeltaThresholdMs = 5;
Sebastian Janssonbe20ef72018-09-06 12:32:31 +020021static const int kMaxBurstDurationMs = 100;
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;
Sebastian Janssonbe20ef72018-09-06 12:32:31 +020049 current_timestamp_group_.first_arrival_ms = arrival_time_ms;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000050 } else if (!PacketInOrder(timestamp)) {
51 return false;
52 } else if (NewTimestampGroup(arrival_time_ms, timestamp)) {
53 // First packet of a later frame, the previous frame sample is ready.
54 if (prev_timestamp_group_.complete_time_ms >= 0) {
Yves Gerey665174f2018-06-19 15:03:05 +020055 *timestamp_delta =
56 current_timestamp_group_.timestamp - prev_timestamp_group_.timestamp;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000057 *arrival_time_delta_ms = current_timestamp_group_.complete_time_ms -
58 prev_timestamp_group_.complete_time_ms;
stefan5e12d362016-07-11 01:44:02 -070059 // Check system time differences to see if we have an unproportional jump
60 // in arrival time. In that case reset the inter-arrival computations.
61 int64_t system_time_delta_ms =
62 current_timestamp_group_.last_system_time_ms -
63 prev_timestamp_group_.last_system_time_ms;
64 if (*arrival_time_delta_ms - system_time_delta_ms >=
65 kArrivalTimeOffsetThresholdMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010066 RTC_LOG(LS_WARNING)
67 << "The arrival time clock offset has changed (diff = "
68 << *arrival_time_delta_ms - system_time_delta_ms
69 << " ms), resetting.";
stefan5e12d362016-07-11 01:44:02 -070070 Reset();
71 return false;
72 }
stefancfd5f962015-07-24 03:26:46 -070073 if (*arrival_time_delta_ms < 0) {
74 // The group of packets has been reordered since receiving its local
75 // arrival timestamp.
stefan5e12d362016-07-11 01:44:02 -070076 ++num_consecutive_reordered_packets_;
77 if (num_consecutive_reordered_packets_ >= kReorderedResetThreshold) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010078 RTC_LOG(LS_WARNING)
79 << "Packets are being reordered on the path from the "
80 "socket to the bandwidth estimator. Ignoring this "
81 "packet for bandwidth estimation, resetting.";
stefan5e12d362016-07-11 01:44:02 -070082 Reset();
83 }
stefancfd5f962015-07-24 03:26:46 -070084 return false;
stefan5e12d362016-07-11 01:44:02 -070085 } else {
86 num_consecutive_reordered_packets_ = 0;
stefancfd5f962015-07-24 03:26:46 -070087 }
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000088 assert(*arrival_time_delta_ms >= 0);
stefan64c0a0a2015-11-27 01:02:31 -080089 *packet_size_delta = static_cast<int>(current_timestamp_group_.size) -
Yves Gerey665174f2018-06-19 15:03:05 +020090 static_cast<int>(prev_timestamp_group_.size);
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +000091 calculated_deltas = true;
92 }
93 prev_timestamp_group_ = current_timestamp_group_;
94 // The new timestamp is now the current frame.
95 current_timestamp_group_.first_timestamp = timestamp;
96 current_timestamp_group_.timestamp = timestamp;
Sebastian Janssonbe20ef72018-09-06 12:32:31 +020097 current_timestamp_group_.first_arrival_ms = arrival_time_ms;
stefan64c0a0a2015-11-27 01:02:31 -080098 current_timestamp_group_.size = 0;
terelius8f09f172015-12-15 00:51:54 -080099 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200100 current_timestamp_group_.timestamp =
101 LatestTimestamp(current_timestamp_group_.timestamp, timestamp);
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000102 }
stefan64c0a0a2015-11-27 01:02:31 -0800103 // Accumulate the frame size.
104 current_timestamp_group_.size += packet_size;
105 current_timestamp_group_.complete_time_ms = arrival_time_ms;
stefan5e12d362016-07-11 01:44:02 -0700106 current_timestamp_group_.last_system_time_ms = system_time_ms;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000107
108 return calculated_deltas;
109}
110
111bool InterArrival::PacketInOrder(uint32_t timestamp) {
112 if (current_timestamp_group_.IsFirstPacket()) {
113 return true;
114 } else {
115 // Assume that a diff which is bigger than half the timestamp interval
116 // (32 bits) must be due to reordering. This code is almost identical to
117 // that in IsNewerTimestamp() in module_common_types.h.
Yves Gerey665174f2018-06-19 15:03:05 +0200118 uint32_t timestamp_diff =
119 timestamp - current_timestamp_group_.first_timestamp;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000120 return timestamp_diff < 0x80000000;
121 }
122}
123
124// Assumes that |timestamp| is not reordered compared to
125// |current_timestamp_group_|.
126bool InterArrival::NewTimestampGroup(int64_t arrival_time_ms,
127 uint32_t timestamp) const {
128 if (current_timestamp_group_.IsFirstPacket()) {
129 return false;
130 } else if (BelongsToBurst(arrival_time_ms, timestamp)) {
131 return false;
132 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200133 uint32_t timestamp_diff =
134 timestamp - current_timestamp_group_.first_timestamp;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000135 return timestamp_diff > kTimestampGroupLengthTicks;
136 }
137}
138
139bool InterArrival::BelongsToBurst(int64_t arrival_time_ms,
140 uint32_t timestamp) const {
141 if (!burst_grouping_) {
142 return false;
143 }
144 assert(current_timestamp_group_.complete_time_ms >= 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200145 int64_t arrival_time_delta_ms =
146 arrival_time_ms - current_timestamp_group_.complete_time_ms;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000147 uint32_t timestamp_diff = timestamp - current_timestamp_group_.timestamp;
148 int64_t ts_delta_ms = timestamp_to_ms_coeff_ * timestamp_diff + 0.5;
149 if (ts_delta_ms == 0)
150 return true;
151 int propagation_delta_ms = arrival_time_delta_ms - ts_delta_ms;
Sebastian Janssonbe20ef72018-09-06 12:32:31 +0200152 if (propagation_delta_ms < 0 &&
153 arrival_time_delta_ms <= kBurstDeltaThresholdMs &&
154 arrival_time_ms - current_timestamp_group_.first_arrival_ms <
155 kMaxBurstDurationMs)
156 return true;
157 return false;
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000158}
stefan5e12d362016-07-11 01:44:02 -0700159
160void InterArrival::Reset() {
161 num_consecutive_reordered_packets_ = 0;
162 current_timestamp_group_ = TimestampGroup();
163 prev_timestamp_group_ = TimestampGroup();
164}
pbos@webrtc.org9f79fe62014-12-04 15:34:06 +0000165} // namespace webrtc