turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
kjellander | 3e6db23 | 2015-11-26 04:44:54 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 13 | |
jmarusic@webrtc.org | a4bef3e | 2015-03-23 11:19:35 +0000 | [diff] [blame] | 14 | #include <map> |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 16 | #include <string> |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 19 | #include "webrtc/base/array_view.h" |
Tommi | 9090e0b | 2016-01-20 13:39:36 +0100 | [diff] [blame] | 20 | #include "webrtc/base/criticalsection.h" |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 21 | #include "webrtc/base/optional.h" |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 22 | #include "webrtc/base/thread_annotations.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 23 | #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
| 24 | #include "webrtc/engine_configurations.h" |
kjellander | 3e6db23 | 2015-11-26 04:44:54 -0800 | [diff] [blame] | 25 | #include "webrtc/modules/audio_coding/acm2/acm_resampler.h" |
| 26 | #include "webrtc/modules/audio_coding/acm2/call_statistics.h" |
| 27 | #include "webrtc/modules/audio_coding/acm2/initial_delay_manager.h" |
Tommi | 9090e0b | 2016-01-20 13:39:36 +0100 | [diff] [blame] | 28 | #include "webrtc/modules/audio_coding/include/audio_coding_module.h" |
Henrik Kjellander | 7464089 | 2015-10-29 11:31:02 +0100 | [diff] [blame] | 29 | #include "webrtc/modules/audio_coding/neteq/include/neteq.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 30 | #include "webrtc/modules/include/module_common_types.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 31 | #include "webrtc/typedefs.h" |
| 32 | |
| 33 | namespace webrtc { |
| 34 | |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 35 | struct CodecInst; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 36 | class NetEq; |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 37 | |
| 38 | namespace acm2 { |
| 39 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 40 | class AcmReceiver { |
| 41 | public: |
| 42 | struct Decoder { |
jmarusic@webrtc.org | a4bef3e | 2015-03-23 11:19:35 +0000 | [diff] [blame] | 43 | int acm_codec_id; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 44 | uint8_t payload_type; |
| 45 | // This field is meaningful for codecs where both mono and |
| 46 | // stereo versions are registered under the same ID. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 47 | size_t channels; |
Karl Wiberg | d8399e6 | 2015-05-25 14:39:56 +0200 | [diff] [blame] | 48 | int sample_rate_hz; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // Constructor of the class |
henrik.lundin@webrtc.org | 0bc9b5a | 2014-04-29 08:09:31 +0000 | [diff] [blame] | 52 | explicit AcmReceiver(const AudioCodingModule::Config& config); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 53 | |
| 54 | // Destructor of the class. |
| 55 | ~AcmReceiver(); |
| 56 | |
| 57 | // |
| 58 | // Inserts a payload with its associated RTP-header into NetEq. |
| 59 | // |
| 60 | // Input: |
| 61 | // - rtp_header : RTP header for the incoming payload containing |
| 62 | // information about payload type, sequence number, |
| 63 | // timestamp, SSRC and marker bit. |
| 64 | // - incoming_payload : Incoming audio payload. |
| 65 | // - length_payload : Length of incoming audio payload in bytes. |
| 66 | // |
| 67 | // Return value : 0 if OK. |
| 68 | // <0 if NetEq returned an error. |
| 69 | // |
| 70 | int InsertPacket(const WebRtcRTPHeader& rtp_header, |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 71 | rtc::ArrayView<const uint8_t> incoming_payload); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 72 | |
| 73 | // |
| 74 | // Asks NetEq for 10 milliseconds of decoded audio. |
| 75 | // |
| 76 | // Input: |
| 77 | // -desired_freq_hz : specifies the sampling rate [Hz] of the output |
| 78 | // audio. If set -1 indicates to resampling is |
| 79 | // is required and the audio returned at the |
| 80 | // sampling rate of the decoder. |
| 81 | // |
| 82 | // Output: |
| 83 | // -audio_frame : an audio frame were output data and |
| 84 | // associated parameters are written to. |
| 85 | // |
| 86 | // Return value : 0 if OK. |
| 87 | // -1 if NetEq returned an error. |
| 88 | // |
| 89 | int GetAudio(int desired_freq_hz, AudioFrame* audio_frame); |
| 90 | |
| 91 | // |
| 92 | // Adds a new codec to the NetEq codec database. |
| 93 | // |
| 94 | // Input: |
kwiberg | 4e14f09 | 2015-08-24 05:27:22 -0700 | [diff] [blame] | 95 | // - acm_codec_id : ACM codec ID; -1 means external decoder. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 96 | // - payload_type : payload type. |
Karl Wiberg | d8399e6 | 2015-05-25 14:39:56 +0200 | [diff] [blame] | 97 | // - sample_rate_hz : sample rate. |
kwiberg | 4e14f09 | 2015-08-24 05:27:22 -0700 | [diff] [blame] | 98 | // - audio_decoder : pointer to a decoder object. If it's null, then |
| 99 | // NetEq will internally create a decoder object |
| 100 | // based on the value of |acm_codec_id| (which |
| 101 | // mustn't be -1). Otherwise, NetEq will use the |
| 102 | // given decoder for the given payload type. NetEq |
| 103 | // won't take ownership of the decoder; it's up to |
| 104 | // the caller to delete it when it's no longer |
| 105 | // needed. |
| 106 | // |
| 107 | // Providing an existing decoder object here is |
| 108 | // necessary for external decoders, but may also be |
| 109 | // used for built-in decoders if NetEq doesn't have |
| 110 | // all the info it needs to construct them properly |
| 111 | // (e.g. iSAC, where the decoder needs to be paired |
| 112 | // with an encoder). |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 113 | // |
| 114 | // Return value : 0 if OK. |
| 115 | // <0 if NetEq returned an error. |
| 116 | // |
| 117 | int AddCodec(int acm_codec_id, |
| 118 | uint8_t payload_type, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 119 | size_t channels, |
Karl Wiberg | d8399e6 | 2015-05-25 14:39:56 +0200 | [diff] [blame] | 120 | int sample_rate_hz, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 121 | AudioDecoder* audio_decoder, |
| 122 | const std::string& name); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 123 | |
| 124 | // |
| 125 | // Sets a minimum delay for packet buffer. The given delay is maintained, |
| 126 | // unless channel condition dictates a higher delay. |
| 127 | // |
| 128 | // Input: |
| 129 | // - delay_ms : minimum delay in milliseconds. |
| 130 | // |
| 131 | // Return value : 0 if OK. |
| 132 | // <0 if NetEq returned an error. |
| 133 | // |
| 134 | int SetMinimumDelay(int delay_ms); |
| 135 | |
| 136 | // |
| 137 | // Sets a maximum delay [ms] for the packet buffer. The target delay does not |
| 138 | // exceed the given value, even if channel condition requires so. |
| 139 | // |
| 140 | // Input: |
| 141 | // - delay_ms : maximum delay in milliseconds. |
| 142 | // |
| 143 | // Return value : 0 if OK. |
| 144 | // <0 if NetEq returned an error. |
| 145 | // |
| 146 | int SetMaximumDelay(int delay_ms); |
| 147 | |
| 148 | // |
| 149 | // Get least required delay computed based on channel conditions. Note that |
| 150 | // this is before applying any user-defined limits (specified by calling |
| 151 | // (SetMinimumDelay() and/or SetMaximumDelay()). |
| 152 | // |
| 153 | int LeastRequiredDelayMs() const; |
| 154 | |
| 155 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 156 | // Resets the initial delay to zero. |
| 157 | // |
| 158 | void ResetInitialDelay(); |
| 159 | |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 160 | // Returns the sample rate of the decoder associated with the last incoming |
| 161 | // packet. If no packet of a registered non-CNG codec has been received, the |
| 162 | // return value is empty. Also, if the decoder was unregistered since the last |
| 163 | // packet was inserted, the return value is empty. |
| 164 | rtc::Optional<int> last_packet_sample_rate_hz() const; |
| 165 | |
henrik.lundin | d89814b | 2015-11-23 06:49:25 -0800 | [diff] [blame] | 166 | // Returns last_output_sample_rate_hz from the NetEq instance. |
| 167 | int last_output_sample_rate_hz() const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 168 | |
| 169 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 170 | // Get the current network statistics from NetEq. |
| 171 | // |
| 172 | // Output: |
| 173 | // - statistics : The current network statistics. |
| 174 | // |
minyue@webrtc.org | c0bd7be | 2015-02-18 15:24:13 +0000 | [diff] [blame] | 175 | void GetNetworkStatistics(NetworkStatistics* statistics); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 176 | |
| 177 | // |
| 178 | // Enable post-decoding VAD. |
| 179 | // |
| 180 | void EnableVad(); |
| 181 | |
| 182 | // |
| 183 | // Disable post-decoding VAD. |
| 184 | // |
| 185 | void DisableVad(); |
| 186 | |
| 187 | // |
| 188 | // Returns whether post-decoding VAD is enabled (true) or disabled (false). |
| 189 | // |
| 190 | bool vad_enabled() const { return vad_enabled_; } |
| 191 | |
| 192 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 193 | // Flushes the NetEq packet and speech buffers. |
| 194 | // |
| 195 | void FlushBuffers(); |
| 196 | |
| 197 | // |
| 198 | // Removes a payload-type from the NetEq codec database. |
| 199 | // |
| 200 | // Input: |
| 201 | // - payload_type : the payload-type to be removed. |
| 202 | // |
| 203 | // Return value : 0 if OK. |
| 204 | // -1 if an error occurred. |
| 205 | // |
| 206 | int RemoveCodec(uint8_t payload_type); |
| 207 | |
| 208 | // |
| 209 | // Remove all registered codecs. |
| 210 | // |
| 211 | int RemoveAllCodecs(); |
| 212 | |
| 213 | // |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 214 | // Gets the RTP timestamp of the last sample delivered by GetAudio(). |
| 215 | // Returns true if the RTP timestamp is valid, otherwise false. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 216 | // |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 217 | bool GetPlayoutTimestamp(uint32_t* timestamp); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 218 | |
| 219 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 220 | // Get the audio codec associated with the last non-CNG/non-DTMF received |
| 221 | // payload. If no non-CNG/non-DTMF packet is received -1 is returned, |
| 222 | // otherwise return 0. |
| 223 | // |
| 224 | int LastAudioCodec(CodecInst* codec) const; |
| 225 | |
| 226 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 227 | // Get a decoder given its registered payload-type. |
| 228 | // |
| 229 | // Input: |
| 230 | // -payload_type : the payload-type of the codec to be retrieved. |
| 231 | // |
| 232 | // Output: |
| 233 | // -codec : codec associated with the given payload-type. |
| 234 | // |
| 235 | // Return value : 0 if succeeded. |
| 236 | // -1 if failed, e.g. given payload-type is not |
| 237 | // registered. |
| 238 | // |
| 239 | int DecoderByPayloadType(uint8_t payload_type, |
| 240 | CodecInst* codec) const; |
| 241 | |
| 242 | // |
| 243 | // Enable NACK and set the maximum size of the NACK list. If NACK is already |
| 244 | // enabled then the maximum NACK list size is modified accordingly. |
| 245 | // |
| 246 | // Input: |
| 247 | // -max_nack_list_size : maximum NACK list size |
| 248 | // should be positive (none zero) and less than or |
| 249 | // equal to |Nack::kNackListSizeLimit| |
| 250 | // Return value |
| 251 | // : 0 if succeeded. |
| 252 | // -1 if failed |
| 253 | // |
| 254 | int EnableNack(size_t max_nack_list_size); |
| 255 | |
| 256 | // Disable NACK. |
| 257 | void DisableNack(); |
| 258 | |
| 259 | // |
| 260 | // Get a list of packets to be retransmitted. |
| 261 | // |
| 262 | // Input: |
| 263 | // -round_trip_time_ms : estimate of the round-trip-time (in milliseconds). |
| 264 | // Return value : list of packets to be retransmitted. |
| 265 | // |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 266 | std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 267 | |
| 268 | // |
wu@webrtc.org | 24301a6 | 2013-12-13 19:17:43 +0000 | [diff] [blame] | 269 | // Get statistics of calls to GetAudio(). |
| 270 | void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const; |
| 271 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 272 | private: |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 273 | const Decoder* RtpHeaderToDecoder(const RTPHeader& rtp_header, |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 274 | uint8_t payload_type) const |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 275 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 276 | |
| 277 | uint32_t NowInTimestamp(int decoder_sampling_rate) const; |
| 278 | |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 279 | rtc::CriticalSection crit_sect_; |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 280 | const Decoder* last_audio_decoder_ GUARDED_BY(crit_sect_); |
henrik.lundin@webrtc.org | a90abde | 2014-06-09 18:35:11 +0000 | [diff] [blame] | 281 | AudioFrame::VADActivity previous_audio_activity_ GUARDED_BY(crit_sect_); |
henrik.lundin@webrtc.org | a90abde | 2014-06-09 18:35:11 +0000 | [diff] [blame] | 282 | ACMResampler resampler_ GUARDED_BY(crit_sect_); |
| 283 | // Used in GetAudio, declared as member to avoid allocating every 10ms. |
| 284 | // TODO(henrik.lundin) Stack-allocate in GetAudio instead? |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 285 | std::unique_ptr<int16_t[]> audio_buffer_ GUARDED_BY(crit_sect_); |
| 286 | std::unique_ptr<int16_t[]> last_audio_buffer_ GUARDED_BY(crit_sect_); |
henrik.lundin@webrtc.org | a90abde | 2014-06-09 18:35:11 +0000 | [diff] [blame] | 287 | CallStatistics call_stats_ GUARDED_BY(crit_sect_); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 288 | NetEq* neteq_; |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 289 | // Decoders map is keyed by payload type |
| 290 | std::map<uint8_t, Decoder> decoders_ GUARDED_BY(crit_sect_); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 291 | bool vad_enabled_; |
henrik.lundin@webrtc.org | a90abde | 2014-06-09 18:35:11 +0000 | [diff] [blame] | 292 | Clock* clock_; // TODO(henrik.lundin) Make const if possible. |
henrik.lundin@webrtc.org | 913f7b8 | 2014-10-21 06:54:23 +0000 | [diff] [blame] | 293 | bool resampled_last_output_frame_ GUARDED_BY(crit_sect_); |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 294 | rtc::Optional<int> last_packet_sample_rate_hz_ GUARDED_BY(crit_sect_); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 297 | } // namespace acm2 |
| 298 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 299 | } // namespace webrtc |
| 300 | |
kjellander | 3e6db23 | 2015-11-26 04:44:54 -0800 | [diff] [blame] | 301 | #endif // WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |