inter_arrival.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_
  11. #define MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. namespace webrtc {
  15. // Helper class to compute the inter-arrival time delta and the size delta
  16. // between two timestamp groups. A timestamp is a 32 bit unsigned number with
  17. // a client defined rate.
  18. class InterArrival {
  19. public:
  20. // After this many packet groups received out of order InterArrival will
  21. // reset, assuming that clocks have made a jump.
  22. static constexpr int kReorderedResetThreshold = 3;
  23. static constexpr int64_t kArrivalTimeOffsetThresholdMs = 3000;
  24. // A timestamp group is defined as all packets with a timestamp which are at
  25. // most timestamp_group_length_ticks older than the first timestamp in that
  26. // group.
  27. InterArrival(uint32_t timestamp_group_length_ticks,
  28. double timestamp_to_ms_coeff,
  29. bool enable_burst_grouping);
  30. InterArrival() = delete;
  31. InterArrival(const InterArrival&) = delete;
  32. InterArrival& operator=(const InterArrival&) = delete;
  33. // This function returns true if a delta was computed, or false if the current
  34. // group is still incomplete or if only one group has been completed.
  35. // |timestamp| is the timestamp.
  36. // |arrival_time_ms| is the local time at which the packet arrived.
  37. // |packet_size| is the size of the packet.
  38. // |timestamp_delta| (output) is the computed timestamp delta.
  39. // |arrival_time_delta_ms| (output) is the computed arrival-time delta.
  40. // |packet_size_delta| (output) is the computed size delta.
  41. bool ComputeDeltas(uint32_t timestamp,
  42. int64_t arrival_time_ms,
  43. int64_t system_time_ms,
  44. size_t packet_size,
  45. uint32_t* timestamp_delta,
  46. int64_t* arrival_time_delta_ms,
  47. int* packet_size_delta);
  48. private:
  49. struct TimestampGroup {
  50. TimestampGroup()
  51. : size(0),
  52. first_timestamp(0),
  53. timestamp(0),
  54. first_arrival_ms(-1),
  55. complete_time_ms(-1) {}
  56. bool IsFirstPacket() const { return complete_time_ms == -1; }
  57. size_t size;
  58. uint32_t first_timestamp;
  59. uint32_t timestamp;
  60. int64_t first_arrival_ms;
  61. int64_t complete_time_ms;
  62. int64_t last_system_time_ms;
  63. };
  64. // Returns true if the packet with timestamp |timestamp| arrived in order.
  65. bool PacketInOrder(uint32_t timestamp);
  66. // Returns true if the last packet was the end of the current batch and the
  67. // packet with |timestamp| is the first of a new batch.
  68. bool NewTimestampGroup(int64_t arrival_time_ms, uint32_t timestamp) const;
  69. bool BelongsToBurst(int64_t arrival_time_ms, uint32_t timestamp) const;
  70. void Reset();
  71. const uint32_t kTimestampGroupLengthTicks;
  72. TimestampGroup current_timestamp_group_;
  73. TimestampGroup prev_timestamp_group_;
  74. double timestamp_to_ms_coeff_;
  75. bool burst_grouping_;
  76. int num_consecutive_reordered_packets_;
  77. };
  78. } // namespace webrtc
  79. #endif // MODULES_REMOTE_BITRATE_ESTIMATOR_INTER_ARRIVAL_H_