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