henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
| 11 | #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_ |
| 13 | |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "webrtc/modules/audio_coding/neteq4/audio_multi_vector.h" |
| 17 | #include "webrtc/modules/audio_coding/neteq4/defines.h" |
| 18 | #include "webrtc/modules/audio_coding/neteq4/interface/neteq.h" |
| 19 | #include "webrtc/modules/audio_coding/neteq4/packet.h" // Declare PacketList. |
| 20 | #include "webrtc/modules/audio_coding/neteq4/random_vector.h" |
| 21 | #include "webrtc/modules/audio_coding/neteq4/rtcp.h" |
| 22 | #include "webrtc/modules/audio_coding/neteq4/statistics_calculator.h" |
| 23 | #include "webrtc/system_wrappers/interface/constructor_magic.h" |
| 24 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 25 | #include "webrtc/typedefs.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | // Forward declarations. |
henrik.lundin@webrtc.org | 671d90b | 2013-09-18 12:19:50 +0000 | [diff] [blame^] | 30 | class Accelerate; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 31 | class BackgroundNoise; |
| 32 | class BufferLevelFilter; |
| 33 | class ComfortNoise; |
| 34 | class CriticalSectionWrapper; |
| 35 | class DecisionLogic; |
| 36 | class DecoderDatabase; |
| 37 | class DelayManager; |
| 38 | class DelayPeakDetector; |
| 39 | class DtmfBuffer; |
| 40 | class DtmfToneGenerator; |
| 41 | class Expand; |
henrik.lundin@webrtc.org | 671d90b | 2013-09-18 12:19:50 +0000 | [diff] [blame^] | 42 | class Merge; |
| 43 | class Normal; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | class PacketBuffer; |
| 45 | class PayloadSplitter; |
| 46 | class PostDecodeVad; |
henrik.lundin@webrtc.org | 671d90b | 2013-09-18 12:19:50 +0000 | [diff] [blame^] | 47 | class PreemptiveExpand; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | class RandomVector; |
| 49 | class SyncBuffer; |
| 50 | class TimestampScaler; |
| 51 | struct DtmfEvent; |
| 52 | |
| 53 | class NetEqImpl : public webrtc::NetEq { |
| 54 | public: |
| 55 | // Creates a new NetEqImpl object. The object will assume ownership of all |
| 56 | // injected dependencies, and will delete them when done. |
| 57 | NetEqImpl(int fs, |
| 58 | BufferLevelFilter* buffer_level_filter, |
| 59 | DecoderDatabase* decoder_database, |
| 60 | DelayManager* delay_manager, |
| 61 | DelayPeakDetector* delay_peak_detector, |
| 62 | DtmfBuffer* dtmf_buffer, |
| 63 | DtmfToneGenerator* dtmf_tone_generator, |
| 64 | PacketBuffer* packet_buffer, |
| 65 | PayloadSplitter* payload_splitter, |
| 66 | TimestampScaler* timestamp_scaler); |
| 67 | |
| 68 | virtual ~NetEqImpl(); |
| 69 | |
| 70 | // Inserts a new packet into NetEq. The |receive_timestamp| is an indication |
| 71 | // of the time when the packet was received, and should be measured with |
| 72 | // the same tick rate as the RTP timestamp of the current payload. |
| 73 | // Returns 0 on success, -1 on failure. |
| 74 | virtual int InsertPacket(const WebRtcRTPHeader& rtp_header, |
| 75 | const uint8_t* payload, |
| 76 | int length_bytes, |
| 77 | uint32_t receive_timestamp); |
| 78 | |
| 79 | // Instructs NetEq to deliver 10 ms of audio data. The data is written to |
| 80 | // |output_audio|, which can hold (at least) |max_length| elements. |
| 81 | // The number of channels that were written to the output is provided in |
| 82 | // the output variable |num_channels|, and each channel contains |
| 83 | // |samples_per_channel| elements. If more than one channel is written, |
| 84 | // the samples are interleaved. |
| 85 | // The speech type is written to |type|, if |type| is not NULL. |
| 86 | // Returns kOK on success, or kFail in case of an error. |
| 87 | virtual int GetAudio(size_t max_length, int16_t* output_audio, |
| 88 | int* samples_per_channel, int* num_channels, |
| 89 | NetEqOutputType* type); |
| 90 | |
| 91 | // Associates |rtp_payload_type| with |codec| and stores the information in |
| 92 | // the codec database. Returns kOK on success, kFail on failure. |
| 93 | virtual int RegisterPayloadType(enum NetEqDecoder codec, |
| 94 | uint8_t rtp_payload_type); |
| 95 | |
| 96 | // Provides an externally created decoder object |decoder| to insert in the |
| 97 | // decoder database. The decoder implements a decoder of type |codec| and |
| 98 | // associates it with |rtp_payload_type|. The decoder operates at the |
| 99 | // frequency |sample_rate_hz|. Returns kOK on success, kFail on failure. |
| 100 | virtual int RegisterExternalDecoder(AudioDecoder* decoder, |
| 101 | enum NetEqDecoder codec, |
| 102 | int sample_rate_hz, |
| 103 | uint8_t rtp_payload_type); |
| 104 | |
| 105 | // Removes |rtp_payload_type| from the codec database. Returns 0 on success, |
| 106 | // -1 on failure. |
| 107 | virtual int RemovePayloadType(uint8_t rtp_payload_type); |
| 108 | |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 109 | virtual bool SetMinimumDelay(int delay_ms); |
| 110 | |
| 111 | virtual bool SetMaximumDelay(int delay_ms); |
| 112 | |
| 113 | virtual int LeastRequiredDelayMs() const; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 114 | |
| 115 | virtual int SetTargetDelay() { return kNotImplemented; } |
| 116 | |
| 117 | virtual int TargetDelay() { return kNotImplemented; } |
| 118 | |
| 119 | virtual int CurrentDelay() { return kNotImplemented; } |
| 120 | |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | // Sets the playout mode to |mode|. |
| 122 | virtual void SetPlayoutMode(NetEqPlayoutMode mode); |
| 123 | |
| 124 | // Returns the current playout mode. |
| 125 | virtual NetEqPlayoutMode PlayoutMode() const; |
| 126 | |
| 127 | // Writes the current network statistics to |stats|. The statistics are reset |
| 128 | // after the call. |
| 129 | virtual int NetworkStatistics(NetEqNetworkStatistics* stats); |
| 130 | |
| 131 | // Writes the last packet waiting times (in ms) to |waiting_times|. The number |
| 132 | // of values written is no more than 100, but may be smaller if the interface |
| 133 | // is polled again before 100 packets has arrived. |
| 134 | virtual void WaitingTimes(std::vector<int>* waiting_times); |
| 135 | |
| 136 | // Writes the current RTCP statistics to |stats|. The statistics are reset |
| 137 | // and a new report period is started with the call. |
| 138 | virtual void GetRtcpStatistics(RtcpStatistics* stats); |
| 139 | |
| 140 | // Same as RtcpStatistics(), but does not reset anything. |
| 141 | virtual void GetRtcpStatisticsNoReset(RtcpStatistics* stats); |
| 142 | |
| 143 | // Enables post-decode VAD. When enabled, GetAudio() will return |
| 144 | // kOutputVADPassive when the signal contains no speech. |
| 145 | virtual void EnableVad(); |
| 146 | |
| 147 | // Disables post-decode VAD. |
| 148 | virtual void DisableVad(); |
| 149 | |
| 150 | // Returns the RTP timestamp for the last sample delivered by GetAudio(). |
| 151 | virtual uint32_t PlayoutTimestamp(); |
| 152 | |
| 153 | virtual int SetTargetNumberOfChannels() { return kNotImplemented; } |
| 154 | |
| 155 | virtual int SetTargetSampleRate() { return kNotImplemented; } |
| 156 | |
| 157 | // Returns the error code for the last occurred error. If no error has |
| 158 | // occurred, 0 is returned. |
| 159 | virtual int LastError(); |
| 160 | |
| 161 | // Returns the error code last returned by a decoder (audio or comfort noise). |
| 162 | // When LastError() returns kDecoderErrorCode or kComfortNoiseErrorCode, check |
| 163 | // this method to get the decoder's error code. |
| 164 | virtual int LastDecoderError(); |
| 165 | |
| 166 | // Flushes both the packet buffer and the sync buffer. |
| 167 | virtual void FlushBuffers(); |
| 168 | |
turaj@webrtc.org | 4b8077b | 2013-08-02 18:07:13 +0000 | [diff] [blame] | 169 | virtual void PacketBufferStatistics(int* current_num_packets, |
| 170 | int* max_num_packets, |
| 171 | int* current_memory_size_bytes, |
| 172 | int* max_memory_size_bytes) const; |
| 173 | |
minyue@webrtc.org | 42758b3 | 2013-08-29 00:58:14 +0000 | [diff] [blame] | 174 | // Get sequence number and timestamp of the latest RTP. |
| 175 | // This method is to facilitate NACK. |
| 176 | virtual int DecodedRtpInfo(int* sequence_number, uint32_t* timestamp); |
| 177 | |
turaj@webrtc.org | 66dbbd9 | 2013-09-11 18:45:02 +0000 | [diff] [blame] | 178 | virtual int InsertSyncPacket(const WebRtcRTPHeader& rtp_header, |
| 179 | uint32_t receive_timestamp); |
| 180 | |
| 181 | virtual void SetBackgroundNoiseMode(NetEqBackgroundNoiseMode mode); |
| 182 | |
| 183 | virtual NetEqBackgroundNoiseMode BackgroundNoiseMode() const; |
| 184 | |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 185 | private: |
| 186 | static const int kOutputSizeMs = 10; |
| 187 | static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz. |
| 188 | // TODO(hlundin): Provide a better value for kSyncBufferSize. |
| 189 | static const int kSyncBufferSize = 2 * kMaxFrameSize; |
| 190 | |
| 191 | // Inserts a new packet into NetEq. This is used by the InsertPacket method |
| 192 | // above. Returns 0 on success, otherwise an error code. |
| 193 | // TODO(hlundin): Merge this with InsertPacket above? |
| 194 | int InsertPacketInternal(const WebRtcRTPHeader& rtp_header, |
| 195 | const uint8_t* payload, |
| 196 | int length_bytes, |
| 197 | uint32_t receive_timestamp); |
| 198 | |
| 199 | |
henrik.lundin@webrtc.org | c340881 | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 200 | // Delivers 10 ms of audio data. The data is written to |output|, which can |
| 201 | // hold (at least) |max_length| elements. The number of channels that were |
| 202 | // written to the output is provided in the output variable |num_channels|, |
| 203 | // and each channel contains |samples_per_channel| elements. If more than one |
| 204 | // channel is written, the samples are interleaved. |
| 205 | // Returns 0 on success, otherwise an error code. |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 206 | int GetAudioInternal(size_t max_length, int16_t* output, |
| 207 | int* samples_per_channel, int* num_channels); |
| 208 | |
| 209 | |
| 210 | // Provides a decision to the GetAudioInternal method. The decision what to |
| 211 | // do is written to |operation|. Packets to decode are written to |
| 212 | // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When |
| 213 | // DTMF should be played, |play_dtmf| is set to true by the method. |
| 214 | // Returns 0 on success, otherwise an error code. |
| 215 | int GetDecision(Operations* operation, |
| 216 | PacketList* packet_list, |
| 217 | DtmfEvent* dtmf_event, |
| 218 | bool* play_dtmf); |
| 219 | |
| 220 | // Decodes the speech packets in |packet_list|, and writes the results to |
| 221 | // |decoded_buffer|, which is allocated to hold |decoded_buffer_length| |
| 222 | // elements. The length of the decoded data is written to |decoded_length|. |
| 223 | // The speech type -- speech or (codec-internal) comfort noise -- is written |
| 224 | // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389 |
| 225 | // comfort noise, those are not decoded. |
| 226 | int Decode(PacketList* packet_list, Operations* operation, |
| 227 | int* decoded_length, AudioDecoder::SpeechType* speech_type); |
| 228 | |
| 229 | // Sub-method to Decode(). Performs the actual decoding. |
| 230 | int DecodeLoop(PacketList* packet_list, Operations* operation, |
| 231 | AudioDecoder* decoder, int* decoded_length, |
| 232 | AudioDecoder::SpeechType* speech_type); |
| 233 | |
| 234 | // Sub-method which calls the Normal class to perform the normal operation. |
| 235 | void DoNormal(const int16_t* decoded_buffer, size_t decoded_length, |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 236 | AudioDecoder::SpeechType speech_type, bool play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 237 | |
| 238 | // Sub-method which calls the Merge class to perform the merge operation. |
| 239 | void DoMerge(int16_t* decoded_buffer, size_t decoded_length, |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 240 | AudioDecoder::SpeechType speech_type, bool play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 241 | |
| 242 | // Sub-method which calls the Expand class to perform the expand operation. |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 243 | int DoExpand(bool play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 244 | |
| 245 | // Sub-method which calls the Accelerate class to perform the accelerate |
| 246 | // operation. |
| 247 | int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length, |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 248 | AudioDecoder::SpeechType speech_type, bool play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 249 | |
| 250 | // Sub-method which calls the PreemptiveExpand class to perform the |
| 251 | // preemtive expand operation. |
| 252 | int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length, |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 253 | AudioDecoder::SpeechType speech_type, bool play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 254 | |
| 255 | // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort |
| 256 | // noise. |packet_list| can either contain one SID frame to update the |
| 257 | // noise parameters, or no payload at all, in which case the previously |
| 258 | // received parameters are used. |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 259 | int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 260 | |
| 261 | // Calls the audio decoder to generate codec-internal comfort noise when |
| 262 | // no packet was received. |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 263 | void DoCodecInternalCng(); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 264 | |
| 265 | // Calls the DtmfToneGenerator class to generate DTMF tones. |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 266 | int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 267 | |
| 268 | // Produces packet-loss concealment using alternative methods. If the codec |
| 269 | // has an internal PLC, it is called to generate samples. Otherwise, the |
| 270 | // method performs zero-stuffing. |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 271 | void DoAlternativePlc(bool increase_timestamp); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 272 | |
| 273 | // Overdub DTMF on top of |output|. |
| 274 | int DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels, |
| 275 | int16_t* output) const; |
| 276 | |
| 277 | // Extracts packets from |packet_buffer_| to produce at least |
| 278 | // |required_samples| samples. The packets are inserted into |packet_list|. |
| 279 | // Returns the number of samples that the packets in the list will produce, or |
| 280 | // -1 in case of an error. |
| 281 | int ExtractPackets(int required_samples, PacketList* packet_list); |
| 282 | |
| 283 | // Resets various variables and objects to new values based on the sample rate |
| 284 | // |fs_hz| and |channels| number audio channels. |
| 285 | void SetSampleRateAndChannels(int fs_hz, size_t channels); |
| 286 | |
| 287 | // Returns the output type for the audio produced by the latest call to |
| 288 | // GetAudio(). |
| 289 | NetEqOutputType LastOutputType(); |
| 290 | |
| 291 | BackgroundNoise* background_noise_; |
| 292 | scoped_ptr<BufferLevelFilter> buffer_level_filter_; |
| 293 | scoped_ptr<DecoderDatabase> decoder_database_; |
| 294 | scoped_ptr<DelayManager> delay_manager_; |
| 295 | scoped_ptr<DelayPeakDetector> delay_peak_detector_; |
| 296 | scoped_ptr<DtmfBuffer> dtmf_buffer_; |
| 297 | scoped_ptr<DtmfToneGenerator> dtmf_tone_generator_; |
| 298 | scoped_ptr<PacketBuffer> packet_buffer_; |
| 299 | scoped_ptr<PayloadSplitter> payload_splitter_; |
| 300 | scoped_ptr<TimestampScaler> timestamp_scaler_; |
| 301 | scoped_ptr<DecisionLogic> decision_logic_; |
| 302 | scoped_ptr<PostDecodeVad> vad_; |
henrik.lundin@webrtc.org | 797eb64 | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 303 | AudioMultiVector<int16_t>* algorithm_buffer_; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 304 | SyncBuffer* sync_buffer_; |
| 305 | Expand* expand_; |
henrik.lundin@webrtc.org | 671d90b | 2013-09-18 12:19:50 +0000 | [diff] [blame^] | 306 | scoped_ptr<Normal> normal_; |
| 307 | scoped_ptr<Merge> merge_; |
| 308 | scoped_ptr<Accelerate> accelerate_; |
| 309 | scoped_ptr<PreemptiveExpand> preemptive_expand_; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 310 | RandomVector random_vector_; |
| 311 | ComfortNoise* comfort_noise_; |
| 312 | Rtcp rtcp_; |
| 313 | StatisticsCalculator stats_; |
| 314 | int fs_hz_; |
| 315 | int fs_mult_; |
| 316 | int output_size_samples_; |
| 317 | int decoder_frame_length_; |
| 318 | Modes last_mode_; |
| 319 | scoped_array<int16_t> mute_factor_array_; |
| 320 | size_t decoded_buffer_length_; |
| 321 | scoped_array<int16_t> decoded_buffer_; |
| 322 | uint32_t playout_timestamp_; |
| 323 | bool new_codec_; |
| 324 | uint32_t timestamp_; |
| 325 | bool reset_decoder_; |
| 326 | uint8_t current_rtp_payload_type_; |
| 327 | uint8_t current_cng_rtp_payload_type_; |
| 328 | uint32_t ssrc_; |
| 329 | bool first_packet_; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 330 | int error_code_; // Store last error code. |
| 331 | int decoder_error_code_; |
| 332 | CriticalSectionWrapper* crit_sect_; |
| 333 | |
minyue@webrtc.org | 42758b3 | 2013-08-29 00:58:14 +0000 | [diff] [blame] | 334 | // These values are used by NACK module to estimate time-to-play of |
| 335 | // a missing packet. Occasionally, NetEq might decide to decode more |
| 336 | // than one packet. Therefore, these values store sequence number and |
| 337 | // timestamp of the first packet pulled from the packet buffer. In |
| 338 | // such cases, these values do not exactly represent the sequence number |
| 339 | // or timestamp associated with a 10ms audio pulled from NetEq. NACK |
| 340 | // module is designed to compensate for this. |
| 341 | int decoded_packet_sequence_number_; |
| 342 | uint32_t decoded_packet_timestamp_; |
| 343 | |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 344 | DISALLOW_COPY_AND_ASSIGN(NetEqImpl); |
| 345 | }; |
| 346 | |
| 347 | } // namespace webrtc |
| 348 | #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_ |