delay_manager.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2012 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_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
  12. #include <string.h> // Provide access to size_t.
  13. #include <deque>
  14. #include <memory>
  15. #include "absl/types/optional.h"
  16. #include "api/neteq/tick_timer.h"
  17. #include "modules/audio_coding/neteq/histogram.h"
  18. #include "rtc_base/constructor_magic.h"
  19. namespace webrtc {
  20. class DelayManager {
  21. public:
  22. DelayManager(size_t max_packets_in_buffer,
  23. int base_minimum_delay_ms,
  24. int histogram_quantile,
  25. bool enable_rtx_handling,
  26. const TickTimer* tick_timer,
  27. std::unique_ptr<Histogram> histogram);
  28. // Create a DelayManager object. Notify the delay manager that the packet
  29. // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
  30. // is the number of packet slots in the buffer) and that the target delay
  31. // should be greater than or equal to |base_minimum_delay_ms|. Supply a
  32. // PeakDetector object to the DelayManager.
  33. static std::unique_ptr<DelayManager> Create(size_t max_packets_in_buffer,
  34. int base_minimum_delay_ms,
  35. bool enable_rtx_handling,
  36. const TickTimer* tick_timer);
  37. virtual ~DelayManager();
  38. // Updates the delay manager with a new incoming packet, with
  39. // |sequence_number| and |timestamp| from the RTP header. This updates the
  40. // inter-arrival time histogram and other statistics, as well as the
  41. // associated DelayPeakDetector. A new target buffer level is calculated.
  42. // Returns the relative delay if it can be calculated.
  43. virtual absl::optional<int> Update(uint16_t sequence_number,
  44. uint32_t timestamp,
  45. int sample_rate_hz);
  46. // Calculates a new target buffer level. Called from the Update() method.
  47. // Sets target_level_ (in Q8) and returns the same value. Also calculates
  48. // and updates base_target_level_, which is the target buffer level before
  49. // taking delay peaks into account.
  50. virtual int CalculateTargetLevel();
  51. // Notifies the DelayManager of how much audio data is carried in each packet.
  52. // The method updates the DelayPeakDetector too, and resets the inter-arrival
  53. // time counter. Returns 0 on success, -1 on failure.
  54. virtual int SetPacketAudioLength(int length_ms);
  55. // Resets the DelayManager and the associated DelayPeakDetector.
  56. virtual void Reset();
  57. // Reset the inter-arrival time counter to 0.
  58. virtual void ResetPacketIatCount();
  59. // Writes the lower and higher limits which the buffer level should stay
  60. // within to the corresponding pointers. The values are in (fractions of)
  61. // packets in Q8.
  62. virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
  63. virtual void BufferLimits(int target_level,
  64. int* lower_limit,
  65. int* higher_limit) const;
  66. // Gets the target buffer level, in (fractions of) packets in Q8.
  67. virtual int TargetLevel() const;
  68. // Informs the delay manager whether or not the last decoded packet contained
  69. // speech.
  70. virtual void LastDecodedWasCngOrDtmf(bool it_was);
  71. // Notify the delay manager that empty packets have been received. These are
  72. // packets that are part of the sequence number series, so that an empty
  73. // packet will shift the sequence numbers for the following packets.
  74. virtual void RegisterEmptyPacket();
  75. // Accessors and mutators.
  76. // Assuming |delay| is in valid range.
  77. virtual bool SetMinimumDelay(int delay_ms);
  78. virtual bool SetMaximumDelay(int delay_ms);
  79. virtual bool SetBaseMinimumDelay(int delay_ms);
  80. virtual int GetBaseMinimumDelay() const;
  81. virtual int base_target_level() const;
  82. virtual int last_pack_cng_or_dtmf() const;
  83. virtual void set_last_pack_cng_or_dtmf(int value);
  84. // This accessor is only intended for testing purposes.
  85. int effective_minimum_delay_ms_for_test() const {
  86. return effective_minimum_delay_ms_;
  87. }
  88. // These accessors are only intended for testing purposes.
  89. int histogram_quantile() const { return histogram_quantile_; }
  90. Histogram* histogram() const { return histogram_.get(); }
  91. private:
  92. // Provides value which minimum delay can't exceed based on current buffer
  93. // size and given |maximum_delay_ms_|. Lower bound is a constant 0.
  94. int MinimumDelayUpperBound() const;
  95. // Provides 75% of currently possible maximum buffer size in milliseconds.
  96. int MaxBufferTimeQ75() const;
  97. // Updates |delay_history_|.
  98. void UpdateDelayHistory(int iat_delay_ms,
  99. uint32_t timestamp,
  100. int sample_rate_hz);
  101. // Calculate relative packet arrival delay from |delay_history_|.
  102. int CalculateRelativePacketArrivalDelay() const;
  103. // Updates |effective_minimum_delay_ms_| delay based on current
  104. // |minimum_delay_ms_|, |base_minimum_delay_ms_| and |maximum_delay_ms_|
  105. // and buffer size.
  106. void UpdateEffectiveMinimumDelay();
  107. // Makes sure that |target_level_| is not too large, taking
  108. // |max_packets_in_buffer_| into account. This method is called by Update().
  109. void LimitTargetLevel();
  110. // Makes sure that |delay_ms| is less than maximum delay, if any maximum
  111. // is set. Also, if possible check |delay_ms| to be less than 75% of
  112. // |max_packets_in_buffer_|.
  113. bool IsValidMinimumDelay(int delay_ms) const;
  114. bool IsValidBaseMinimumDelay(int delay_ms) const;
  115. bool first_packet_received_;
  116. const size_t max_packets_in_buffer_; // Capacity of the packet buffer.
  117. std::unique_ptr<Histogram> histogram_;
  118. const int histogram_quantile_;
  119. const TickTimer* tick_timer_;
  120. int base_minimum_delay_ms_;
  121. // Provides delay which is used by LimitTargetLevel as lower bound on target
  122. // delay.
  123. int effective_minimum_delay_ms_;
  124. // Time elapsed since last packet.
  125. std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
  126. int base_target_level_; // Currently preferred buffer level before peak
  127. // detection and streaming mode (Q0).
  128. // TODO(turajs) change the comment according to the implementation of
  129. // minimum-delay.
  130. int target_level_; // Currently preferred buffer level in (fractions)
  131. // of packets (Q8), before adding any extra delay.
  132. int packet_len_ms_; // Length of audio in each incoming packet [ms].
  133. uint16_t last_seq_no_; // Sequence number for last received packet.
  134. uint32_t last_timestamp_; // Timestamp for the last received packet.
  135. int minimum_delay_ms_; // Externally set minimum delay.
  136. int maximum_delay_ms_; // Externally set maximum allowed delay.
  137. int last_pack_cng_or_dtmf_;
  138. const bool enable_rtx_handling_;
  139. int num_reordered_packets_ = 0; // Number of consecutive reordered packets.
  140. struct PacketDelay {
  141. int iat_delay_ms;
  142. uint32_t timestamp;
  143. };
  144. std::deque<PacketDelay> delay_history_;
  145. RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
  146. };
  147. } // namespace webrtc
  148. #endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_