blob: 18169dcc87905a7a4b8cbc5d59d1a250e52336d6 [file] [log] [blame]
henrik.lundin@webrtc.org9a400812013-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
105 // Sets the desired extra delay on top of what NetEq already applies due to
106 // current network situation. Used for synchronization with video. Returns
107 // true if successful, otherwise false.
108 virtual bool SetExtraDelay(int extra_delay_ms);
109
110 virtual int SetTargetDelay() { return kNotImplemented; }
111
112 virtual int TargetDelay() { return kNotImplemented; }
113
114 virtual int CurrentDelay() { return kNotImplemented; }
115
116 // Enables playout of DTMF tones.
117 virtual int EnableDtmf();
118
119 // Sets the playout mode to |mode|.
120 virtual void SetPlayoutMode(NetEqPlayoutMode mode);
121
122 // Returns the current playout mode.
123 virtual NetEqPlayoutMode PlayoutMode() const;
124
125 // Writes the current network statistics to |stats|. The statistics are reset
126 // after the call.
127 virtual int NetworkStatistics(NetEqNetworkStatistics* stats);
128
129 // Writes the last packet waiting times (in ms) to |waiting_times|. The number
130 // of values written is no more than 100, but may be smaller if the interface
131 // is polled again before 100 packets has arrived.
132 virtual void WaitingTimes(std::vector<int>* waiting_times);
133
134 // Writes the current RTCP statistics to |stats|. The statistics are reset
135 // and a new report period is started with the call.
136 virtual void GetRtcpStatistics(RtcpStatistics* stats);
137
138 // Same as RtcpStatistics(), but does not reset anything.
139 virtual void GetRtcpStatisticsNoReset(RtcpStatistics* stats);
140
141 // Enables post-decode VAD. When enabled, GetAudio() will return
142 // kOutputVADPassive when the signal contains no speech.
143 virtual void EnableVad();
144
145 // Disables post-decode VAD.
146 virtual void DisableVad();
147
148 // Returns the RTP timestamp for the last sample delivered by GetAudio().
149 virtual uint32_t PlayoutTimestamp();
150
151 virtual int SetTargetNumberOfChannels() { return kNotImplemented; }
152
153 virtual int SetTargetSampleRate() { return kNotImplemented; }
154
155 // Returns the error code for the last occurred error. If no error has
156 // occurred, 0 is returned.
157 virtual int LastError();
158
159 // Returns the error code last returned by a decoder (audio or comfort noise).
160 // When LastError() returns kDecoderErrorCode or kComfortNoiseErrorCode, check
161 // this method to get the decoder's error code.
162 virtual int LastDecoderError();
163
164 // Flushes both the packet buffer and the sync buffer.
165 virtual void FlushBuffers();
166
167 private:
168 static const int kOutputSizeMs = 10;
169 static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz.
170 // TODO(hlundin): Provide a better value for kSyncBufferSize.
171 static const int kSyncBufferSize = 2 * kMaxFrameSize;
172
173 // Inserts a new packet into NetEq. This is used by the InsertPacket method
174 // above. Returns 0 on success, otherwise an error code.
175 // TODO(hlundin): Merge this with InsertPacket above?
176 int InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
177 const uint8_t* payload,
178 int length_bytes,
179 uint32_t receive_timestamp);
180
181
182 // Delivers 10 ms of audio to |output|. The number of samples produced is
183 // written to |output_length|. Returns 0 on success, or an error code.
184 int GetAudioInternal(size_t max_length, int16_t* output,
185 int* samples_per_channel, int* num_channels);
186
187
188 // Provides a decision to the GetAudioInternal method. The decision what to
189 // do is written to |operation|. Packets to decode are written to
190 // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When
191 // DTMF should be played, |play_dtmf| is set to true by the method.
192 // Returns 0 on success, otherwise an error code.
193 int GetDecision(Operations* operation,
194 PacketList* packet_list,
195 DtmfEvent* dtmf_event,
196 bool* play_dtmf);
197
198 // Decodes the speech packets in |packet_list|, and writes the results to
199 // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
200 // elements. The length of the decoded data is written to |decoded_length|.
201 // The speech type -- speech or (codec-internal) comfort noise -- is written
202 // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389
203 // comfort noise, those are not decoded.
204 int Decode(PacketList* packet_list, Operations* operation,
205 int* decoded_length, AudioDecoder::SpeechType* speech_type);
206
207 // Sub-method to Decode(). Performs the actual decoding.
208 int DecodeLoop(PacketList* packet_list, Operations* operation,
209 AudioDecoder* decoder, int* decoded_length,
210 AudioDecoder::SpeechType* speech_type);
211
212 // Sub-method which calls the Normal class to perform the normal operation.
213 void DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
214 AudioDecoder::SpeechType speech_type, bool play_dtmf,
215 AudioMultiVector<int16_t>* algorithm_buffer);
216
217 // Sub-method which calls the Merge class to perform the merge operation.
218 void DoMerge(int16_t* decoded_buffer, size_t decoded_length,
219 AudioDecoder::SpeechType speech_type, bool play_dtmf,
220 AudioMultiVector<int16_t>* algorithm_buffer);
221
222 // Sub-method which calls the Expand class to perform the expand operation.
223 int DoExpand(bool play_dtmf, AudioMultiVector<int16_t>* algorithm_buffer);
224
225 // Sub-method which calls the Accelerate class to perform the accelerate
226 // operation.
227 int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
228 AudioDecoder::SpeechType speech_type, bool play_dtmf,
229 AudioMultiVector<int16_t>* algorithm_buffer);
230
231 // Sub-method which calls the PreemptiveExpand class to perform the
232 // preemtive expand operation.
233 int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length,
234 AudioDecoder::SpeechType speech_type, bool play_dtmf,
235 AudioMultiVector<int16_t>* algorithm_buffer);
236
237 // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort
238 // noise. |packet_list| can either contain one SID frame to update the
239 // noise parameters, or no payload at all, in which case the previously
240 // received parameters are used.
241 int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf,
242 AudioMultiVector<int16_t>* algorithm_buffer);
243
244 // Calls the audio decoder to generate codec-internal comfort noise when
245 // no packet was received.
246 void DoCodecInternalCng(AudioMultiVector<int16_t>* algorithm_buffer);
247
248 // Calls the DtmfToneGenerator class to generate DTMF tones.
249 int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf,
250 AudioMultiVector<int16_t>* algorithm_buffer);
251
252 // Produces packet-loss concealment using alternative methods. If the codec
253 // has an internal PLC, it is called to generate samples. Otherwise, the
254 // method performs zero-stuffing.
255 void DoAlternativePlc(bool increase_timestamp,
256 AudioMultiVector<int16_t>* algorithm_buffer);
257
258 // Overdub DTMF on top of |output|.
259 int DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
260 int16_t* output) const;
261
262 // Extracts packets from |packet_buffer_| to produce at least
263 // |required_samples| samples. The packets are inserted into |packet_list|.
264 // Returns the number of samples that the packets in the list will produce, or
265 // -1 in case of an error.
266 int ExtractPackets(int required_samples, PacketList* packet_list);
267
268 // Resets various variables and objects to new values based on the sample rate
269 // |fs_hz| and |channels| number audio channels.
270 void SetSampleRateAndChannels(int fs_hz, size_t channels);
271
272 // Returns the output type for the audio produced by the latest call to
273 // GetAudio().
274 NetEqOutputType LastOutputType();
275
276 BackgroundNoise* background_noise_;
277 scoped_ptr<BufferLevelFilter> buffer_level_filter_;
278 scoped_ptr<DecoderDatabase> decoder_database_;
279 scoped_ptr<DelayManager> delay_manager_;
280 scoped_ptr<DelayPeakDetector> delay_peak_detector_;
281 scoped_ptr<DtmfBuffer> dtmf_buffer_;
282 scoped_ptr<DtmfToneGenerator> dtmf_tone_generator_;
283 scoped_ptr<PacketBuffer> packet_buffer_;
284 scoped_ptr<PayloadSplitter> payload_splitter_;
285 scoped_ptr<TimestampScaler> timestamp_scaler_;
286 scoped_ptr<DecisionLogic> decision_logic_;
287 scoped_ptr<PostDecodeVad> vad_;
288 SyncBuffer* sync_buffer_;
289 Expand* expand_;
290 RandomVector random_vector_;
291 ComfortNoise* comfort_noise_;
292 Rtcp rtcp_;
293 StatisticsCalculator stats_;
294 int fs_hz_;
295 int fs_mult_;
296 int output_size_samples_;
297 int decoder_frame_length_;
298 Modes last_mode_;
299 scoped_array<int16_t> mute_factor_array_;
300 size_t decoded_buffer_length_;
301 scoped_array<int16_t> decoded_buffer_;
302 uint32_t playout_timestamp_;
303 bool new_codec_;
304 uint32_t timestamp_;
305 bool reset_decoder_;
306 uint8_t current_rtp_payload_type_;
307 uint8_t current_cng_rtp_payload_type_;
308 uint32_t ssrc_;
309 bool first_packet_;
310 bool dtmf_enabled_;
311 int error_code_; // Store last error code.
312 int decoder_error_code_;
313 CriticalSectionWrapper* crit_sect_;
314
315 DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
316};
317
318} // namespace webrtc
319#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_