neteq_controller.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2019 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 API_NETEQ_NETEQ_CONTROLLER_H_
  11. #define API_NETEQ_NETEQ_CONTROLLER_H_
  12. #include <cstddef>
  13. #include <cstdint>
  14. #include <functional>
  15. #include <memory>
  16. #include "absl/types/optional.h"
  17. #include "api/neteq/neteq.h"
  18. #include "api/neteq/tick_timer.h"
  19. #include "system_wrappers/include/clock.h"
  20. namespace webrtc {
  21. // Decides the actions that NetEq should take. This affects the behavior of the
  22. // jitter buffer, and how it reacts to network conditions.
  23. // This class will undergo substantial refactoring in the near future, and the
  24. // API is expected to undergo significant changes. A target API is given below:
  25. //
  26. // class NetEqController {
  27. // public:
  28. // // Resets object to a clean state.
  29. // void Reset();
  30. // // Given NetEq status, make a decision.
  31. // Operation GetDecision(NetEqStatus neteq_status);
  32. // // Register every packet received.
  33. // void RegisterPacket(PacketInfo packet_info);
  34. // // Register empty packet.
  35. // void RegisterEmptyPacket();
  36. // // Register a codec switching.
  37. // void CodecSwithed();
  38. // // Sets the sample rate.
  39. // void SetSampleRate(int fs_hz);
  40. // // Sets the packet length in samples.
  41. // void SetPacketLengthSamples();
  42. // // Sets maximum delay.
  43. // void SetMaximumDelay(int delay_ms);
  44. // // Sets mininum delay.
  45. // void SetMinimumDelay(int delay_ms);
  46. // // Sets base mininum delay.
  47. // void SetBaseMinimumDelay(int delay_ms);
  48. // // Gets target buffer level.
  49. // int GetTargetBufferLevelMs() const;
  50. // // Gets filtered buffer level.
  51. // int GetFilteredBufferLevel() const;
  52. // // Gets base minimum delay.
  53. // int GetBaseMinimumDelay() const;
  54. // }
  55. class NetEqController {
  56. public:
  57. // This struct is used to create a NetEqController.
  58. struct Config {
  59. bool allow_time_stretching;
  60. bool enable_rtx_handling;
  61. int max_packets_in_buffer;
  62. int base_min_delay_ms;
  63. TickTimer* tick_timer;
  64. webrtc::Clock* clock = nullptr;
  65. };
  66. struct PacketInfo {
  67. uint32_t timestamp;
  68. bool is_dtx;
  69. bool is_cng;
  70. };
  71. struct PacketBufferInfo {
  72. bool dtx_or_cng;
  73. size_t num_samples;
  74. size_t span_samples;
  75. size_t span_samples_no_dtx;
  76. size_t num_packets;
  77. };
  78. struct NetEqStatus {
  79. uint32_t target_timestamp;
  80. int16_t expand_mutefactor;
  81. size_t last_packet_samples;
  82. absl::optional<PacketInfo> next_packet;
  83. NetEq::Mode last_mode;
  84. bool play_dtmf;
  85. size_t generated_noise_samples;
  86. PacketBufferInfo packet_buffer_info;
  87. size_t sync_buffer_samples;
  88. };
  89. virtual ~NetEqController() = default;
  90. // Resets object to a clean state.
  91. virtual void Reset() = 0;
  92. // Resets parts of the state. Typically done when switching codecs.
  93. virtual void SoftReset() = 0;
  94. // Given info about the latest received packet, and current jitter buffer
  95. // status, returns the operation. |target_timestamp| and |expand_mutefactor|
  96. // are provided for reference. |last_packet_samples| is the number of samples
  97. // obtained from the last decoded frame. If there is a packet available, it
  98. // should be supplied in |packet|. The mode resulting from the last call to
  99. // NetEqImpl::GetAudio is supplied in |last_mode|. If there is a DTMF event to
  100. // play, |play_dtmf| should be set to true. The output variable
  101. // |reset_decoder| will be set to true if a reset is required; otherwise it is
  102. // left unchanged (i.e., it can remain true if it was true before the call).
  103. virtual NetEq::Operation GetDecision(const NetEqStatus& status,
  104. bool* reset_decoder) = 0;
  105. // Inform NetEqController that an empty packet has arrived.
  106. virtual void RegisterEmptyPacket() = 0;
  107. // Sets the sample rate and the output block size.
  108. virtual void SetSampleRate(int fs_hz, size_t output_size_samples) = 0;
  109. // Sets a minimum or maximum delay in millisecond.
  110. // Returns true if the delay bound is successfully applied, otherwise false.
  111. virtual bool SetMaximumDelay(int delay_ms) = 0;
  112. virtual bool SetMinimumDelay(int delay_ms) = 0;
  113. // Sets a base minimum delay in milliseconds for packet buffer. The effective
  114. // minimum delay can't be lower than base minimum delay, even if a lower value
  115. // is set using SetMinimumDelay.
  116. // Returns true if the base minimum is successfully applied, otherwise false.
  117. virtual bool SetBaseMinimumDelay(int delay_ms) = 0;
  118. virtual int GetBaseMinimumDelay() const = 0;
  119. // These methods test the |cng_state_| for different conditions.
  120. virtual bool CngRfc3389On() const = 0;
  121. virtual bool CngOff() const = 0;
  122. // Resets the |cng_state_| to kCngOff.
  123. virtual void SetCngOff() = 0;
  124. // Reports back to DecisionLogic whether the decision to do expand remains or
  125. // not. Note that this is necessary, since an expand decision can be changed
  126. // to kNormal in NetEqImpl::GetDecision if there is still enough data in the
  127. // sync buffer.
  128. virtual void ExpandDecision(NetEq::Operation operation) = 0;
  129. // Adds |value| to |sample_memory_|.
  130. virtual void AddSampleMemory(int32_t value) = 0;
  131. // Returns the target buffer level in ms.
  132. virtual int TargetLevelMs() const = 0;
  133. // Notify the NetEqController that a packet has arrived. Returns the relative
  134. // arrival delay, if it can be computed.
  135. virtual absl::optional<int> PacketArrived(bool last_cng_or_dtmf,
  136. size_t packet_length_samples,
  137. bool should_update_stats,
  138. uint16_t main_sequence_number,
  139. uint32_t main_timestamp,
  140. int fs_hz) = 0;
  141. // Returns true if a peak was found.
  142. virtual bool PeakFound() const = 0;
  143. // Get the filtered buffer level in samples.
  144. virtual int GetFilteredBufferLevel() const = 0;
  145. // Accessors and mutators.
  146. virtual void set_sample_memory(int32_t value) = 0;
  147. virtual size_t noise_fast_forward() const = 0;
  148. virtual size_t packet_length_samples() const = 0;
  149. virtual void set_packet_length_samples(size_t value) = 0;
  150. virtual void set_prev_time_scale(bool value) = 0;
  151. };
  152. } // namespace webrtc
  153. #endif // API_NETEQ_NETEQ_CONTROLLER_H_