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