blob: c17ff1e29c7f5be4879642cb091e3c4f85cb64c0 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
27namespace webrtc {
28
29// Forward declarations.
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000030class Accelerate;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031class BackgroundNoise;
32class BufferLevelFilter;
33class ComfortNoise;
34class CriticalSectionWrapper;
35class DecisionLogic;
36class DecoderDatabase;
37class DelayManager;
38class DelayPeakDetector;
39class DtmfBuffer;
40class DtmfToneGenerator;
41class Expand;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000042class Merge;
43class Normal;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044class PacketBuffer;
45class PayloadSplitter;
46class PostDecodeVad;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000047class PreemptiveExpand;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048class RandomVector;
49class SyncBuffer;
50class TimestampScaler;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000051struct AccelerateFactory;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052struct DtmfEvent;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000053struct ExpandFactory;
54struct PreemptiveExpandFactory;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055
56class NetEqImpl : public webrtc::NetEq {
57 public:
58 // Creates a new NetEqImpl object. The object will assume ownership of all
59 // injected dependencies, and will delete them when done.
60 NetEqImpl(int fs,
61 BufferLevelFilter* buffer_level_filter,
62 DecoderDatabase* decoder_database,
63 DelayManager* delay_manager,
64 DelayPeakDetector* delay_peak_detector,
65 DtmfBuffer* dtmf_buffer,
66 DtmfToneGenerator* dtmf_tone_generator,
67 PacketBuffer* packet_buffer,
68 PayloadSplitter* payload_splitter,
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000069 TimestampScaler* timestamp_scaler,
70 AccelerateFactory* accelerate_factory,
71 ExpandFactory* expand_factory,
72 PreemptiveExpandFactory* preemptive_expand_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073
74 virtual ~NetEqImpl();
75
76 // Inserts a new packet into NetEq. The |receive_timestamp| is an indication
77 // of the time when the packet was received, and should be measured with
78 // the same tick rate as the RTP timestamp of the current payload.
79 // Returns 0 on success, -1 on failure.
80 virtual int InsertPacket(const WebRtcRTPHeader& rtp_header,
81 const uint8_t* payload,
82 int length_bytes,
83 uint32_t receive_timestamp);
84
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000085 // Inserts a sync-packet into packet queue. Sync-packets are decoded to
86 // silence and are intended to keep AV-sync intact in an event of long packet
87 // losses when Video NACK is enabled but Audio NACK is not. Clients of NetEq
88 // might insert sync-packet when they observe that buffer level of NetEq is
89 // decreasing below a certain threshold, defined by the application.
90 // Sync-packets should have the same payload type as the last audio payload
91 // type, i.e. they cannot have DTMF or CNG payload type, nor a codec change
92 // can be implied by inserting a sync-packet.
93 // Returns kOk on success, kFail on failure.
94 virtual int InsertSyncPacket(const WebRtcRTPHeader& rtp_header,
95 uint32_t receive_timestamp);
96
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097 // Instructs NetEq to deliver 10 ms of audio data. The data is written to
98 // |output_audio|, which can hold (at least) |max_length| elements.
99 // The number of channels that were written to the output is provided in
100 // the output variable |num_channels|, and each channel contains
101 // |samples_per_channel| elements. If more than one channel is written,
102 // the samples are interleaved.
103 // The speech type is written to |type|, if |type| is not NULL.
104 // Returns kOK on success, or kFail in case of an error.
105 virtual int GetAudio(size_t max_length, int16_t* output_audio,
106 int* samples_per_channel, int* num_channels,
107 NetEqOutputType* type);
108
109 // Associates |rtp_payload_type| with |codec| and stores the information in
110 // the codec database. Returns kOK on success, kFail on failure.
111 virtual int RegisterPayloadType(enum NetEqDecoder codec,
112 uint8_t rtp_payload_type);
113
114 // Provides an externally created decoder object |decoder| to insert in the
115 // decoder database. The decoder implements a decoder of type |codec| and
116 // associates it with |rtp_payload_type|. The decoder operates at the
117 // frequency |sample_rate_hz|. Returns kOK on success, kFail on failure.
118 virtual int RegisterExternalDecoder(AudioDecoder* decoder,
119 enum NetEqDecoder codec,
120 int sample_rate_hz,
121 uint8_t rtp_payload_type);
122
123 // Removes |rtp_payload_type| from the codec database. Returns 0 on success,
124 // -1 on failure.
125 virtual int RemovePayloadType(uint8_t rtp_payload_type);
126
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000127 virtual bool SetMinimumDelay(int delay_ms);
128
129 virtual bool SetMaximumDelay(int delay_ms);
130
131 virtual int LeastRequiredDelayMs() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000132
133 virtual int SetTargetDelay() { return kNotImplemented; }
134
135 virtual int TargetDelay() { return kNotImplemented; }
136
137 virtual int CurrentDelay() { return kNotImplemented; }
138
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000139 // Sets the playout mode to |mode|.
140 virtual void SetPlayoutMode(NetEqPlayoutMode mode);
141
142 // Returns the current playout mode.
143 virtual NetEqPlayoutMode PlayoutMode() const;
144
145 // Writes the current network statistics to |stats|. The statistics are reset
146 // after the call.
147 virtual int NetworkStatistics(NetEqNetworkStatistics* stats);
148
149 // Writes the last packet waiting times (in ms) to |waiting_times|. The number
150 // of values written is no more than 100, but may be smaller if the interface
151 // is polled again before 100 packets has arrived.
152 virtual void WaitingTimes(std::vector<int>* waiting_times);
153
154 // Writes the current RTCP statistics to |stats|. The statistics are reset
155 // and a new report period is started with the call.
156 virtual void GetRtcpStatistics(RtcpStatistics* stats);
157
158 // Same as RtcpStatistics(), but does not reset anything.
159 virtual void GetRtcpStatisticsNoReset(RtcpStatistics* stats);
160
161 // Enables post-decode VAD. When enabled, GetAudio() will return
162 // kOutputVADPassive when the signal contains no speech.
163 virtual void EnableVad();
164
165 // Disables post-decode VAD.
166 virtual void DisableVad();
167
168 // Returns the RTP timestamp for the last sample delivered by GetAudio().
169 virtual uint32_t PlayoutTimestamp();
170
171 virtual int SetTargetNumberOfChannels() { return kNotImplemented; }
172
173 virtual int SetTargetSampleRate() { return kNotImplemented; }
174
175 // Returns the error code for the last occurred error. If no error has
176 // occurred, 0 is returned.
177 virtual int LastError();
178
179 // Returns the error code last returned by a decoder (audio or comfort noise).
180 // When LastError() returns kDecoderErrorCode or kComfortNoiseErrorCode, check
181 // this method to get the decoder's error code.
182 virtual int LastDecoderError();
183
184 // Flushes both the packet buffer and the sync buffer.
185 virtual void FlushBuffers();
186
turaj@webrtc.org7df97062013-08-02 18:07:13 +0000187 virtual void PacketBufferStatistics(int* current_num_packets,
188 int* max_num_packets,
189 int* current_memory_size_bytes,
190 int* max_memory_size_bytes) const;
191
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000192 // Get sequence number and timestamp of the latest RTP.
193 // This method is to facilitate NACK.
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000194 virtual int DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const;
195
196 // Sets background noise mode.
197 virtual void SetBackgroundNoiseMode(NetEqBackgroundNoiseMode mode);
198
199 // Gets background noise mode.
200 virtual NetEqBackgroundNoiseMode BackgroundNoiseMode() const;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000201
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 private:
203 static const int kOutputSizeMs = 10;
204 static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz.
205 // TODO(hlundin): Provide a better value for kSyncBufferSize.
206 static const int kSyncBufferSize = 2 * kMaxFrameSize;
207
208 // Inserts a new packet into NetEq. This is used by the InsertPacket method
209 // above. Returns 0 on success, otherwise an error code.
210 // TODO(hlundin): Merge this with InsertPacket above?
211 int InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
212 const uint8_t* payload,
213 int length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000214 uint32_t receive_timestamp,
215 bool is_sync_packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216
217
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000218 // Delivers 10 ms of audio data. The data is written to |output|, which can
219 // hold (at least) |max_length| elements. The number of channels that were
220 // written to the output is provided in the output variable |num_channels|,
221 // and each channel contains |samples_per_channel| elements. If more than one
222 // channel is written, the samples are interleaved.
223 // Returns 0 on success, otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224 int GetAudioInternal(size_t max_length, int16_t* output,
225 int* samples_per_channel, int* num_channels);
226
227
228 // Provides a decision to the GetAudioInternal method. The decision what to
229 // do is written to |operation|. Packets to decode are written to
230 // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When
231 // DTMF should be played, |play_dtmf| is set to true by the method.
232 // Returns 0 on success, otherwise an error code.
233 int GetDecision(Operations* operation,
234 PacketList* packet_list,
235 DtmfEvent* dtmf_event,
236 bool* play_dtmf);
237
238 // Decodes the speech packets in |packet_list|, and writes the results to
239 // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
240 // elements. The length of the decoded data is written to |decoded_length|.
241 // The speech type -- speech or (codec-internal) comfort noise -- is written
242 // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389
243 // comfort noise, those are not decoded.
244 int Decode(PacketList* packet_list, Operations* operation,
245 int* decoded_length, AudioDecoder::SpeechType* speech_type);
246
247 // Sub-method to Decode(). Performs the actual decoding.
248 int DecodeLoop(PacketList* packet_list, Operations* operation,
249 AudioDecoder* decoder, int* decoded_length,
250 AudioDecoder::SpeechType* speech_type);
251
252 // Sub-method which calls the Normal class to perform the normal operation.
253 void DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000254 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255
256 // Sub-method which calls the Merge class to perform the merge operation.
257 void DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000258 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259
260 // Sub-method which calls the Expand class to perform the expand operation.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000261 int DoExpand(bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000262
263 // Sub-method which calls the Accelerate class to perform the accelerate
264 // operation.
265 int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000266 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267
268 // Sub-method which calls the PreemptiveExpand class to perform the
269 // preemtive expand operation.
270 int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000271 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272
273 // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort
274 // noise. |packet_list| can either contain one SID frame to update the
275 // noise parameters, or no payload at all, in which case the previously
276 // received parameters are used.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000277 int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278
279 // Calls the audio decoder to generate codec-internal comfort noise when
280 // no packet was received.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000281 void DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282
283 // Calls the DtmfToneGenerator class to generate DTMF tones.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000284 int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285
286 // Produces packet-loss concealment using alternative methods. If the codec
287 // has an internal PLC, it is called to generate samples. Otherwise, the
288 // method performs zero-stuffing.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000289 void DoAlternativePlc(bool increase_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290
291 // Overdub DTMF on top of |output|.
292 int DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
293 int16_t* output) const;
294
295 // Extracts packets from |packet_buffer_| to produce at least
296 // |required_samples| samples. The packets are inserted into |packet_list|.
297 // Returns the number of samples that the packets in the list will produce, or
298 // -1 in case of an error.
299 int ExtractPackets(int required_samples, PacketList* packet_list);
300
301 // Resets various variables and objects to new values based on the sample rate
302 // |fs_hz| and |channels| number audio channels.
303 void SetSampleRateAndChannels(int fs_hz, size_t channels);
304
305 // Returns the output type for the audio produced by the latest call to
306 // GetAudio().
307 NetEqOutputType LastOutputType();
308
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000309 scoped_ptr<BackgroundNoise> background_noise_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310 scoped_ptr<BufferLevelFilter> buffer_level_filter_;
311 scoped_ptr<DecoderDatabase> decoder_database_;
312 scoped_ptr<DelayManager> delay_manager_;
313 scoped_ptr<DelayPeakDetector> delay_peak_detector_;
314 scoped_ptr<DtmfBuffer> dtmf_buffer_;
315 scoped_ptr<DtmfToneGenerator> dtmf_tone_generator_;
316 scoped_ptr<PacketBuffer> packet_buffer_;
317 scoped_ptr<PayloadSplitter> payload_splitter_;
318 scoped_ptr<TimestampScaler> timestamp_scaler_;
319 scoped_ptr<DecisionLogic> decision_logic_;
320 scoped_ptr<PostDecodeVad> vad_;
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +0000321 scoped_ptr<AudioMultiVector> algorithm_buffer_;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000322 scoped_ptr<SyncBuffer> sync_buffer_;
323 scoped_ptr<Expand> expand_;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000324 scoped_ptr<ExpandFactory> expand_factory_;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +0000325 scoped_ptr<Normal> normal_;
326 scoped_ptr<Merge> merge_;
327 scoped_ptr<Accelerate> accelerate_;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000328 scoped_ptr<AccelerateFactory> accelerate_factory_;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +0000329 scoped_ptr<PreemptiveExpand> preemptive_expand_;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000330 scoped_ptr<PreemptiveExpandFactory> preemptive_expand_factory_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331 RandomVector random_vector_;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000332 scoped_ptr<ComfortNoise> comfort_noise_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 Rtcp rtcp_;
334 StatisticsCalculator stats_;
335 int fs_hz_;
336 int fs_mult_;
337 int output_size_samples_;
338 int decoder_frame_length_;
339 Modes last_mode_;
340 scoped_array<int16_t> mute_factor_array_;
341 size_t decoded_buffer_length_;
342 scoped_array<int16_t> decoded_buffer_;
343 uint32_t playout_timestamp_;
344 bool new_codec_;
345 uint32_t timestamp_;
346 bool reset_decoder_;
347 uint8_t current_rtp_payload_type_;
348 uint8_t current_cng_rtp_payload_type_;
349 uint32_t ssrc_;
350 bool first_packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 int error_code_; // Store last error code.
352 int decoder_error_code_;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000353 scoped_ptr<CriticalSectionWrapper> crit_sect_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000355 // These values are used by NACK module to estimate time-to-play of
356 // a missing packet. Occasionally, NetEq might decide to decode more
357 // than one packet. Therefore, these values store sequence number and
358 // timestamp of the first packet pulled from the packet buffer. In
359 // such cases, these values do not exactly represent the sequence number
360 // or timestamp associated with a 10ms audio pulled from NetEq. NACK
361 // module is designed to compensate for this.
362 int decoded_packet_sequence_number_;
363 uint32_t decoded_packet_timestamp_;
364
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365 DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
366};
367
368} // namespace webrtc
369#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_