inter_arrival.h (3424B)
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 11 #ifndef MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_ 12 #define MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 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: 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 29 // A timestamp group is defined as all packets with a timestamp which are at 30 // most timestamp_group_length_ticks older than the first timestamp in that 31 // group. 32 InterArrival(uint32_t timestamp_group_length_ticks, 33 double timestamp_to_ms_coeff); 34 35 InterArrival() = delete; 36 InterArrival(const InterArrival&) = delete; 37 InterArrival& operator=(const InterArrival&) = delete; 38 39 // This function returns true if a delta was computed, or false if the current 40 // group is still incomplete or if only one group has been completed. 41 // `timestamp` is the timestamp. 42 // `arrival_time_ms` is the local time at which the packet arrived. 43 // `packet_size` is the size of the packet. 44 // `timestamp_delta` (output) is the computed timestamp delta. 45 // `arrival_time_delta_ms` (output) is the computed arrival-time delta. 46 // `packet_size_delta` (output) is the computed size delta. 47 bool ComputeDeltas(uint32_t timestamp, 48 int64_t arrival_time_ms, 49 int64_t system_time_ms, 50 size_t packet_size, 51 uint32_t* timestamp_delta, 52 int64_t* arrival_time_delta_ms, 53 int* packet_size_delta); 54 55 private: 56 struct TimestampGroup { 57 TimestampGroup() 58 : size(0), 59 first_timestamp(0), 60 timestamp(0), 61 first_arrival_ms(-1), 62 complete_time_ms(-1) {} 63 64 bool IsFirstPacket() const { return complete_time_ms == -1; } 65 66 size_t size; 67 uint32_t first_timestamp; 68 uint32_t timestamp; 69 int64_t first_arrival_ms; 70 int64_t complete_time_ms; 71 int64_t last_system_time_ms; 72 }; 73 74 // Returns true if the packet with timestamp `timestamp` arrived in order. 75 bool PacketInOrder(uint32_t timestamp); 76 77 // Returns true if the last packet was the end of the current batch and the 78 // packet with `timestamp` is the first of a new batch. 79 bool NewTimestampGroup(int64_t arrival_time_ms, uint32_t timestamp) const; 80 81 bool BelongsToBurst(int64_t arrival_time_ms, uint32_t timestamp) const; 82 83 void Reset(); 84 85 const uint32_t kTimestampGroupLengthTicks; 86 TimestampGroup current_timestamp_group_; 87 TimestampGroup prev_timestamp_group_; 88 double timestamp_to_ms_coeff_; 89 int num_consecutive_reordered_packets_; 90 }; 91 } // namespace webrtc 92 93 #endif // MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_