blob: f2facd47c2d1889a4529aab17efa10dcf27cb059 [file] [log] [blame]
pbos@webrtc.org9f79fe62014-12-04 15:34:061/*
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 04:47:3111#ifndef MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_
12#define MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_
pbos@webrtc.org9f79fe62014-12-04 15:34:0613
Niels Möllera12c42a2018-07-25 14:05:4814#include <stddef.h>
15#include <stdint.h>
pbos@webrtc.org9f79fe62014-12-04 15:34:0616
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/constructormagic.h"
pbos@webrtc.org9f79fe62014-12-04 15:34:0618
19namespace webrtc {
20
21// Helper class to compute the inter-arrival time delta and the size delta
22// between two timestamp groups. A timestamp is a 32 bit unsigned number with
23// a client defined rate.
24class InterArrival {
25 public:
stefan5e12d362016-07-11 08:44:0226 // After this many packet groups received out of order InterArrival will
27 // reset, assuming that clocks have made a jump.
28 static constexpr int kReorderedResetThreshold = 3;
29 static constexpr int64_t kArrivalTimeOffsetThresholdMs = 3000;
30
pbos@webrtc.org9f79fe62014-12-04 15:34:0631 // A timestamp group is defined as all packets with a timestamp which are at
stefan64c0a0a2015-11-27 09:02:3132 // most timestamp_group_length_ticks older than the first timestamp in that
pbos@webrtc.org9f79fe62014-12-04 15:34:0633 // group.
34 InterArrival(uint32_t timestamp_group_length_ticks,
35 double timestamp_to_ms_coeff,
36 bool enable_burst_grouping);
37
38 // This function returns true if a delta was computed, or false if the current
39 // group is still incomplete or if only one group has been completed.
40 // |timestamp| is the timestamp.
41 // |arrival_time_ms| is the local time at which the packet arrived.
stefan64c0a0a2015-11-27 09:02:3142 // |packet_size| is the size of the packet.
pbos@webrtc.org9f79fe62014-12-04 15:34:0643 // |timestamp_delta| (output) is the computed timestamp delta.
44 // |arrival_time_delta_ms| (output) is the computed arrival-time delta.
stefan64c0a0a2015-11-27 09:02:3145 // |packet_size_delta| (output) is the computed size delta.
pbos@webrtc.org9f79fe62014-12-04 15:34:0646 bool ComputeDeltas(uint32_t timestamp,
47 int64_t arrival_time_ms,
stefan5e12d362016-07-11 08:44:0248 int64_t system_time_ms,
stefan64c0a0a2015-11-27 09:02:3149 size_t packet_size,
pbos@webrtc.org9f79fe62014-12-04 15:34:0650 uint32_t* timestamp_delta,
stefan64c0a0a2015-11-27 09:02:3151 int64_t* arrival_time_delta_ms,
52 int* packet_size_delta);
pbos@webrtc.org9f79fe62014-12-04 15:34:0653
54 private:
55 struct TimestampGroup {
stefan64c0a0a2015-11-27 09:02:3156 TimestampGroup()
Sebastian Janssonbe20ef72018-09-06 10:32:3157 : size(0),
58 first_timestamp(0),
59 timestamp(0),
60 first_arrival_ms(-1),
61 complete_time_ms(-1) {}
pbos@webrtc.org9f79fe62014-12-04 15:34:0662
Yves Gerey665174f2018-06-19 13:03:0563 bool IsFirstPacket() const { return complete_time_ms == -1; }
pbos@webrtc.org9f79fe62014-12-04 15:34:0664
stefan64c0a0a2015-11-27 09:02:3165 size_t size;
pbos@webrtc.org9f79fe62014-12-04 15:34:0666 uint32_t first_timestamp;
67 uint32_t timestamp;
Sebastian Janssonbe20ef72018-09-06 10:32:3168 int64_t first_arrival_ms;
pbos@webrtc.org9f79fe62014-12-04 15:34:0669 int64_t complete_time_ms;
stefan5e12d362016-07-11 08:44:0270 int64_t last_system_time_ms;
pbos@webrtc.org9f79fe62014-12-04 15:34:0671 };
72
73 // Returns true if the packet with timestamp |timestamp| arrived in order.
74 bool PacketInOrder(uint32_t timestamp);
75
76 // Returns true if the last packet was the end of the current batch and the
77 // packet with |timestamp| is the first of a new batch.
78 bool NewTimestampGroup(int64_t arrival_time_ms, uint32_t timestamp) const;
79
80 bool BelongsToBurst(int64_t arrival_time_ms, uint32_t timestamp) const;
81
stefan5e12d362016-07-11 08:44:0282 void Reset();
83
pbos@webrtc.org9f79fe62014-12-04 15:34:0684 const uint32_t kTimestampGroupLengthTicks;
85 TimestampGroup current_timestamp_group_;
86 TimestampGroup prev_timestamp_group_;
87 double timestamp_to_ms_coeff_;
88 bool burst_grouping_;
stefan5e12d362016-07-11 08:44:0289 int num_consecutive_reordered_packets_;
pbos@webrtc.org9f79fe62014-12-04 15:34:0690
henrikg3c089d72015-09-16 12:37:4491 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(InterArrival);
pbos@webrtc.org9f79fe62014-12-04 15:34:0692};
93} // namespace webrtc
94
Mirko Bonadei92ea95e2017-09-15 04:47:3195#endif // MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_