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