decision_logic.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
  12. #include "api/neteq/neteq.h"
  13. #include "api/neteq/neteq_controller.h"
  14. #include "api/neteq/tick_timer.h"
  15. #include "modules/audio_coding/neteq/buffer_level_filter.h"
  16. #include "modules/audio_coding/neteq/delay_manager.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/experiments/field_trial_parser.h"
  19. namespace webrtc {
  20. // This is the class for the decision tree implementation.
  21. class DecisionLogic : public NetEqController {
  22. public:
  23. static const int kReinitAfterExpands = 100;
  24. static const int kMaxWaitForPacket = 10;
  25. // Constructor.
  26. DecisionLogic(NetEqController::Config config);
  27. ~DecisionLogic() override;
  28. // Resets object to a clean state.
  29. void Reset() override;
  30. // Resets parts of the state. Typically done when switching codecs.
  31. void SoftReset() override;
  32. // Sets the sample rate and the output block size.
  33. void SetSampleRate(int fs_hz, size_t output_size_samples) override;
  34. // Given info about the latest received packet, and current jitter buffer
  35. // status, returns the operation. |target_timestamp| and |expand_mutefactor|
  36. // are provided for reference. |last_packet_samples| is the number of samples
  37. // obtained from the last decoded frame. If there is a packet available, it
  38. // should be supplied in |packet|; otherwise it should be NULL. The mode
  39. // resulting from the last call to NetEqImpl::GetAudio is supplied in
  40. // |last_mode|. If there is a DTMF event to play, |play_dtmf| should be set to
  41. // true. The output variable |reset_decoder| will be set to true if a reset is
  42. // required; otherwise it is left unchanged (i.e., it can remain true if it
  43. // was true before the call).
  44. NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status,
  45. bool* reset_decoder) override;
  46. // These methods test the |cng_state_| for different conditions.
  47. bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; }
  48. bool CngOff() const override { return cng_state_ == kCngOff; }
  49. // Resets the |cng_state_| to kCngOff.
  50. void SetCngOff() override { cng_state_ = kCngOff; }
  51. // Reports back to DecisionLogic whether the decision to do expand remains or
  52. // not. Note that this is necessary, since an expand decision can be changed
  53. // to kNormal in NetEqImpl::GetDecision if there is still enough data in the
  54. // sync buffer.
  55. void ExpandDecision(NetEq::Operation operation) override;
  56. // Adds |value| to |sample_memory_|.
  57. void AddSampleMemory(int32_t value) override { sample_memory_ += value; }
  58. int TargetLevelMs() const override {
  59. return ((delay_manager_->TargetLevel() * packet_length_samples_) >> 8) /
  60. rtc::CheckedDivExact(sample_rate_, 1000);
  61. }
  62. absl::optional<int> PacketArrived(bool last_cng_or_dtmf,
  63. size_t packet_length_samples,
  64. bool should_update_stats,
  65. uint16_t main_sequence_number,
  66. uint32_t main_timestamp,
  67. int fs_hz) override;
  68. void RegisterEmptyPacket() override { delay_manager_->RegisterEmptyPacket(); }
  69. bool SetMaximumDelay(int delay_ms) override {
  70. return delay_manager_->SetMaximumDelay(delay_ms);
  71. }
  72. bool SetMinimumDelay(int delay_ms) override {
  73. return delay_manager_->SetMinimumDelay(delay_ms);
  74. }
  75. bool SetBaseMinimumDelay(int delay_ms) override {
  76. return delay_manager_->SetBaseMinimumDelay(delay_ms);
  77. }
  78. int GetBaseMinimumDelay() const override {
  79. return delay_manager_->GetBaseMinimumDelay();
  80. }
  81. bool PeakFound() const override { return false; }
  82. int GetFilteredBufferLevel() const override {
  83. return buffer_level_filter_.filtered_current_level();
  84. }
  85. // Accessors and mutators.
  86. void set_sample_memory(int32_t value) override { sample_memory_ = value; }
  87. size_t noise_fast_forward() const override { return noise_fast_forward_; }
  88. size_t packet_length_samples() const override {
  89. return packet_length_samples_;
  90. }
  91. void set_packet_length_samples(size_t value) override {
  92. packet_length_samples_ = value;
  93. }
  94. void set_prev_time_scale(bool value) override { prev_time_scale_ = value; }
  95. private:
  96. // The value 5 sets maximum time-stretch rate to about 100 ms/s.
  97. static const int kMinTimescaleInterval = 5;
  98. enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
  99. // Updates the |buffer_level_filter_| with the current buffer level
  100. // |buffer_size_packets|.
  101. void FilterBufferLevel(size_t buffer_size_packets);
  102. // Returns the operation given that the next available packet is a comfort
  103. // noise payload (RFC 3389 only, not codec-internal).
  104. virtual NetEq::Operation CngOperation(NetEq::Mode prev_mode,
  105. uint32_t target_timestamp,
  106. uint32_t available_timestamp,
  107. size_t generated_noise_samples);
  108. // Returns the operation given that no packets are available (except maybe
  109. // a DTMF event, flagged by setting |play_dtmf| true).
  110. virtual NetEq::Operation NoPacket(bool play_dtmf);
  111. // Returns the operation to do given that the expected packet is available.
  112. virtual NetEq::Operation ExpectedPacketAvailable(NetEq::Mode prev_mode,
  113. bool play_dtmf);
  114. // Returns the operation to do given that the expected packet is not
  115. // available, but a packet further into the future is at hand.
  116. virtual NetEq::Operation FuturePacketAvailable(
  117. size_t decoder_frame_length,
  118. NetEq::Mode prev_mode,
  119. uint32_t target_timestamp,
  120. uint32_t available_timestamp,
  121. bool play_dtmf,
  122. size_t generated_noise_samples,
  123. size_t span_samples_in_packet_buffer,
  124. size_t num_packets_in_packet_buffer);
  125. // Checks if enough time has elapsed since the last successful timescale
  126. // operation was done (i.e., accelerate or preemptive expand).
  127. bool TimescaleAllowed() const {
  128. return !timescale_countdown_ || timescale_countdown_->Finished();
  129. }
  130. // Checks if the current (filtered) buffer level is under the target level.
  131. bool UnderTargetLevel() const;
  132. // Checks if |timestamp_leap| is so long into the future that a reset due
  133. // to exceeding kReinitAfterExpands will be done.
  134. bool ReinitAfterExpands(uint32_t timestamp_leap) const;
  135. // Checks if we still have not done enough expands to cover the distance from
  136. // the last decoded packet to the next available packet, the distance beeing
  137. // conveyed in |timestamp_leap|.
  138. bool PacketTooEarly(uint32_t timestamp_leap) const;
  139. // Checks if num_consecutive_expands_ >= kMaxWaitForPacket.
  140. bool MaxWaitForPacket() const;
  141. std::unique_ptr<DelayManager> delay_manager_;
  142. BufferLevelFilter buffer_level_filter_;
  143. const TickTimer* tick_timer_;
  144. int sample_rate_;
  145. size_t output_size_samples_;
  146. CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by
  147. // other event (e.g., DTMF).
  148. size_t noise_fast_forward_ = 0;
  149. size_t packet_length_samples_ = 0;
  150. int sample_memory_ = 0;
  151. bool prev_time_scale_ = false;
  152. bool disallow_time_stretching_;
  153. std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
  154. int num_consecutive_expands_ = 0;
  155. int time_stretched_cn_samples_ = 0;
  156. FieldTrialParameter<bool> estimate_dtx_delay_;
  157. FieldTrialParameter<bool> time_stretch_cn_;
  158. FieldTrialConstrained<int> target_level_window_ms_;
  159. RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
  160. };
  161. } // namespace webrtc
  162. #endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_