pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_ |
| 12 | #define MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_ |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 13 | |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 16 | |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
| 19 | // Helper class to compute the inter-arrival time delta and the size delta |
| 20 | // between two timestamp groups. A timestamp is a 32 bit unsigned number with |
| 21 | // a client defined rate. |
| 22 | class InterArrival { |
| 23 | public: |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 24 | // After this many packet groups received out of order InterArrival will |
| 25 | // reset, assuming that clocks have made a jump. |
| 26 | static constexpr int kReorderedResetThreshold = 3; |
| 27 | static constexpr int64_t kArrivalTimeOffsetThresholdMs = 3000; |
| 28 | |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 29 | // A timestamp group is defined as all packets with a timestamp which are at |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 30 | // most timestamp_group_length_ticks older than the first timestamp in that |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 31 | // group. |
| 32 | InterArrival(uint32_t timestamp_group_length_ticks, |
| 33 | double timestamp_to_ms_coeff, |
| 34 | bool enable_burst_grouping); |
| 35 | |
Niels Möller | de95329 | 2020-09-29 09:46:21 +0200 | [diff] [blame^] | 36 | InterArrival() = delete; |
| 37 | InterArrival(const InterArrival&) = delete; |
| 38 | InterArrival& operator=(const InterArrival&) = delete; |
| 39 | |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 40 | // This function returns true if a delta was computed, or false if the current |
| 41 | // group is still incomplete or if only one group has been completed. |
| 42 | // |timestamp| is the timestamp. |
| 43 | // |arrival_time_ms| is the local time at which the packet arrived. |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 44 | // |packet_size| is the size of the packet. |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 45 | // |timestamp_delta| (output) is the computed timestamp delta. |
| 46 | // |arrival_time_delta_ms| (output) is the computed arrival-time delta. |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 47 | // |packet_size_delta| (output) is the computed size delta. |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 48 | bool ComputeDeltas(uint32_t timestamp, |
| 49 | int64_t arrival_time_ms, |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 50 | int64_t system_time_ms, |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 51 | size_t packet_size, |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 52 | uint32_t* timestamp_delta, |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 53 | int64_t* arrival_time_delta_ms, |
| 54 | int* packet_size_delta); |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | struct TimestampGroup { |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 58 | TimestampGroup() |
Sebastian Jansson | be20ef7 | 2018-09-06 12:32:31 +0200 | [diff] [blame] | 59 | : size(0), |
| 60 | first_timestamp(0), |
| 61 | timestamp(0), |
| 62 | first_arrival_ms(-1), |
| 63 | complete_time_ms(-1) {} |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 64 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 65 | bool IsFirstPacket() const { return complete_time_ms == -1; } |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 66 | |
stefan | 64c0a0a | 2015-11-27 01:02:31 -0800 | [diff] [blame] | 67 | size_t size; |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 68 | uint32_t first_timestamp; |
| 69 | uint32_t timestamp; |
Sebastian Jansson | be20ef7 | 2018-09-06 12:32:31 +0200 | [diff] [blame] | 70 | int64_t first_arrival_ms; |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 71 | int64_t complete_time_ms; |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 72 | int64_t last_system_time_ms; |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Returns true if the packet with timestamp |timestamp| arrived in order. |
| 76 | bool PacketInOrder(uint32_t timestamp); |
| 77 | |
| 78 | // Returns true if the last packet was the end of the current batch and the |
| 79 | // packet with |timestamp| is the first of a new batch. |
| 80 | bool NewTimestampGroup(int64_t arrival_time_ms, uint32_t timestamp) const; |
| 81 | |
| 82 | bool BelongsToBurst(int64_t arrival_time_ms, uint32_t timestamp) const; |
| 83 | |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 84 | void Reset(); |
| 85 | |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 86 | const uint32_t kTimestampGroupLengthTicks; |
| 87 | TimestampGroup current_timestamp_group_; |
| 88 | TimestampGroup prev_timestamp_group_; |
| 89 | double timestamp_to_ms_coeff_; |
| 90 | bool burst_grouping_; |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 91 | int num_consecutive_reordered_packets_; |
pbos@webrtc.org | 9f79fe6 | 2014-12-04 15:34:06 +0000 | [diff] [blame] | 92 | }; |
| 93 | } // namespace webrtc |
| 94 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 95 | #endif // MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_ |