blob: eff6115dccb27b2695e8683977c22097bf3d125f [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
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
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000182 // Delivers 10 ms of audio data. The data is written to |output|, which can
183 // hold (at least) |max_length| elements. The number of channels that were
184 // written to the output is provided in the output variable |num_channels|,
185 // and each channel contains |samples_per_channel| elements. If more than one
186 // channel is written, the samples are interleaved.
187 // Returns 0 on success, otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188 int GetAudioInternal(size_t max_length, int16_t* output,
189 int* samples_per_channel, int* num_channels);
190
191
192 // Provides a decision to the GetAudioInternal method. The decision what to
193 // do is written to |operation|. Packets to decode are written to
194 // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When
195 // DTMF should be played, |play_dtmf| is set to true by the method.
196 // Returns 0 on success, otherwise an error code.
197 int GetDecision(Operations* operation,
198 PacketList* packet_list,
199 DtmfEvent* dtmf_event,
200 bool* play_dtmf);
201
202 // Decodes the speech packets in |packet_list|, and writes the results to
203 // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
204 // elements. The length of the decoded data is written to |decoded_length|.
205 // The speech type -- speech or (codec-internal) comfort noise -- is written
206 // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389
207 // comfort noise, those are not decoded.
208 int Decode(PacketList* packet_list, Operations* operation,
209 int* decoded_length, AudioDecoder::SpeechType* speech_type);
210
211 // Sub-method to Decode(). Performs the actual decoding.
212 int DecodeLoop(PacketList* packet_list, Operations* operation,
213 AudioDecoder* decoder, int* decoded_length,
214 AudioDecoder::SpeechType* speech_type);
215
216 // Sub-method which calls the Normal class to perform the normal operation.
217 void DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
218 AudioDecoder::SpeechType speech_type, bool play_dtmf,
219 AudioMultiVector<int16_t>* algorithm_buffer);
220
221 // Sub-method which calls the Merge class to perform the merge operation.
222 void DoMerge(int16_t* decoded_buffer, size_t decoded_length,
223 AudioDecoder::SpeechType speech_type, bool play_dtmf,
224 AudioMultiVector<int16_t>* algorithm_buffer);
225
226 // Sub-method which calls the Expand class to perform the expand operation.
227 int DoExpand(bool play_dtmf, AudioMultiVector<int16_t>* algorithm_buffer);
228
229 // Sub-method which calls the Accelerate class to perform the accelerate
230 // operation.
231 int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
232 AudioDecoder::SpeechType speech_type, bool play_dtmf,
233 AudioMultiVector<int16_t>* algorithm_buffer);
234
235 // Sub-method which calls the PreemptiveExpand class to perform the
236 // preemtive expand operation.
237 int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length,
238 AudioDecoder::SpeechType speech_type, bool play_dtmf,
239 AudioMultiVector<int16_t>* algorithm_buffer);
240
241 // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort
242 // noise. |packet_list| can either contain one SID frame to update the
243 // noise parameters, or no payload at all, in which case the previously
244 // received parameters are used.
245 int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf,
246 AudioMultiVector<int16_t>* algorithm_buffer);
247
248 // Calls the audio decoder to generate codec-internal comfort noise when
249 // no packet was received.
250 void DoCodecInternalCng(AudioMultiVector<int16_t>* algorithm_buffer);
251
252 // Calls the DtmfToneGenerator class to generate DTMF tones.
253 int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf,
254 AudioMultiVector<int16_t>* algorithm_buffer);
255
256 // Produces packet-loss concealment using alternative methods. If the codec
257 // has an internal PLC, it is called to generate samples. Otherwise, the
258 // method performs zero-stuffing.
259 void DoAlternativePlc(bool increase_timestamp,
260 AudioMultiVector<int16_t>* algorithm_buffer);
261
262 // Overdub DTMF on top of |output|.
263 int DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
264 int16_t* output) const;
265
266 // Extracts packets from |packet_buffer_| to produce at least
267 // |required_samples| samples. The packets are inserted into |packet_list|.
268 // Returns the number of samples that the packets in the list will produce, or
269 // -1 in case of an error.
270 int ExtractPackets(int required_samples, PacketList* packet_list);
271
272 // Resets various variables and objects to new values based on the sample rate
273 // |fs_hz| and |channels| number audio channels.
274 void SetSampleRateAndChannels(int fs_hz, size_t channels);
275
276 // Returns the output type for the audio produced by the latest call to
277 // GetAudio().
278 NetEqOutputType LastOutputType();
279
280 BackgroundNoise* background_noise_;
281 scoped_ptr<BufferLevelFilter> buffer_level_filter_;
282 scoped_ptr<DecoderDatabase> decoder_database_;
283 scoped_ptr<DelayManager> delay_manager_;
284 scoped_ptr<DelayPeakDetector> delay_peak_detector_;
285 scoped_ptr<DtmfBuffer> dtmf_buffer_;
286 scoped_ptr<DtmfToneGenerator> dtmf_tone_generator_;
287 scoped_ptr<PacketBuffer> packet_buffer_;
288 scoped_ptr<PayloadSplitter> payload_splitter_;
289 scoped_ptr<TimestampScaler> timestamp_scaler_;
290 scoped_ptr<DecisionLogic> decision_logic_;
291 scoped_ptr<PostDecodeVad> vad_;
292 SyncBuffer* sync_buffer_;
293 Expand* expand_;
294 RandomVector random_vector_;
295 ComfortNoise* comfort_noise_;
296 Rtcp rtcp_;
297 StatisticsCalculator stats_;
298 int fs_hz_;
299 int fs_mult_;
300 int output_size_samples_;
301 int decoder_frame_length_;
302 Modes last_mode_;
303 scoped_array<int16_t> mute_factor_array_;
304 size_t decoded_buffer_length_;
305 scoped_array<int16_t> decoded_buffer_;
306 uint32_t playout_timestamp_;
307 bool new_codec_;
308 uint32_t timestamp_;
309 bool reset_decoder_;
310 uint8_t current_rtp_payload_type_;
311 uint8_t current_cng_rtp_payload_type_;
312 uint32_t ssrc_;
313 bool first_packet_;
314 bool dtmf_enabled_;
315 int error_code_; // Store last error code.
316 int decoder_error_code_;
317 CriticalSectionWrapper* crit_sect_;
318
319 DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
320};
321
322} // namespace webrtc
323#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_