Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 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 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 11 | #ifndef API_NETEQ_NETEQ_CONTROLLER_H_ |
| 12 | #define API_NETEQ_NETEQ_CONTROLLER_H_ |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 13 | |
| 14 | #include <cstddef> |
| 15 | #include <cstdint> |
| 16 | |
| 17 | #include <functional> |
| 18 | #include <memory> |
| 19 | |
| 20 | #include "absl/types/optional.h" |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 21 | #include "api/neteq/neteq.h" |
| 22 | #include "api/neteq/tick_timer.h" |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 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 | }; |
| 71 | |
| 72 | struct PacketInfo { |
| 73 | uint32_t timestamp; |
| 74 | bool is_dtx; |
| 75 | bool is_cng; |
| 76 | }; |
| 77 | |
| 78 | struct PacketBufferInfo { |
| 79 | bool dtx_or_cng; |
| 80 | size_t num_samples; |
| 81 | size_t span_samples; |
| 82 | size_t span_samples_no_dtx; |
| 83 | size_t num_packets; |
| 84 | }; |
| 85 | |
| 86 | struct NetEqStatus { |
| 87 | uint32_t target_timestamp; |
| 88 | int16_t expand_mutefactor; |
| 89 | size_t last_packet_samples; |
| 90 | absl::optional<PacketInfo> next_packet; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 91 | NetEq::Mode last_mode; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 92 | bool play_dtmf; |
| 93 | size_t generated_noise_samples; |
| 94 | PacketBufferInfo packet_buffer_info; |
| 95 | }; |
| 96 | |
| 97 | virtual ~NetEqController() = default; |
| 98 | |
| 99 | // Resets object to a clean state. |
| 100 | virtual void Reset() = 0; |
| 101 | |
| 102 | // Resets parts of the state. Typically done when switching codecs. |
| 103 | virtual void SoftReset() = 0; |
| 104 | |
| 105 | // Given info about the latest received packet, and current jitter buffer |
| 106 | // status, returns the operation. |target_timestamp| and |expand_mutefactor| |
| 107 | // are provided for reference. |last_packet_samples| is the number of samples |
| 108 | // obtained from the last decoded frame. If there is a packet available, it |
| 109 | // should be supplied in |packet|. The mode resulting from the last call to |
| 110 | // NetEqImpl::GetAudio is supplied in |last_mode|. If there is a DTMF event to |
| 111 | // play, |play_dtmf| should be set to true. The output variable |
| 112 | // |reset_decoder| will be set to true if a reset is required; otherwise it is |
| 113 | // left unchanged (i.e., it can remain true if it was true before the call). |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 114 | virtual NetEq::Operation GetDecision(const NetEqStatus& status, |
| 115 | bool* reset_decoder) = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 116 | |
| 117 | // Inform NetEqController that an empty packet has arrived. |
| 118 | virtual void RegisterEmptyPacket() = 0; |
| 119 | |
| 120 | // Sets the sample rate and the output block size. |
| 121 | virtual void SetSampleRate(int fs_hz, size_t output_size_samples) = 0; |
| 122 | |
| 123 | // Sets a minimum or maximum delay in millisecond. |
| 124 | // Returns true if the delay bound is successfully applied, otherwise false. |
| 125 | virtual bool SetMaximumDelay(int delay_ms) = 0; |
| 126 | virtual bool SetMinimumDelay(int delay_ms) = 0; |
| 127 | |
| 128 | // Sets a base minimum delay in milliseconds for packet buffer. The effective |
| 129 | // minimum delay can't be lower than base minimum delay, even if a lower value |
| 130 | // is set using SetMinimumDelay. |
| 131 | // Returns true if the base minimum is successfully applied, otherwise false. |
| 132 | virtual bool SetBaseMinimumDelay(int delay_ms) = 0; |
| 133 | virtual int GetBaseMinimumDelay() const = 0; |
| 134 | |
| 135 | // These methods test the |cng_state_| for different conditions. |
| 136 | virtual bool CngRfc3389On() const = 0; |
| 137 | virtual bool CngOff() const = 0; |
| 138 | |
| 139 | // Resets the |cng_state_| to kCngOff. |
| 140 | virtual void SetCngOff() = 0; |
| 141 | |
| 142 | // Reports back to DecisionLogic whether the decision to do expand remains or |
| 143 | // not. Note that this is necessary, since an expand decision can be changed |
| 144 | // to kNormal in NetEqImpl::GetDecision if there is still enough data in the |
| 145 | // sync buffer. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 146 | virtual void ExpandDecision(NetEq::Operation operation) = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 147 | |
| 148 | // Adds |value| to |sample_memory_|. |
| 149 | virtual void AddSampleMemory(int32_t value) = 0; |
| 150 | |
| 151 | // Returns the target buffer level in ms. |
| 152 | virtual int TargetLevelMs() = 0; |
| 153 | |
| 154 | // Notify the NetEqController that a packet has arrived. Returns the relative |
| 155 | // arrival delay, if it can be computed. |
| 156 | virtual absl::optional<int> PacketArrived(bool last_cng_or_dtmf, |
| 157 | size_t packet_length_samples, |
| 158 | bool should_update_stats, |
| 159 | uint16_t main_sequence_number, |
| 160 | uint32_t main_timestamp, |
| 161 | int fs_hz) = 0; |
| 162 | |
| 163 | // Returns true if a peak was found. |
| 164 | virtual bool PeakFound() const = 0; |
| 165 | |
| 166 | // Get the filtered buffer level in samples. |
| 167 | virtual int GetFilteredBufferLevel() const = 0; |
| 168 | |
| 169 | // Accessors and mutators. |
| 170 | virtual void set_sample_memory(int32_t value) = 0; |
| 171 | virtual size_t noise_fast_forward() const = 0; |
| 172 | virtual size_t packet_length_samples() const = 0; |
| 173 | virtual void set_packet_length_samples(size_t value) = 0; |
| 174 | virtual void set_prev_time_scale(bool value) = 0; |
| 175 | }; |
| 176 | |
| 177 | } // namespace webrtc |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 178 | #endif // API_NETEQ_NETEQ_CONTROLLER_H_ |