blob: 56dde77669e6546477390da2b0b2bb606ca24a1d [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
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000170 // Get sequence number and timestamp of the latest RTP.
171 // This method is to facilitate NACK.
172 virtual int DecodedRtpInfo(int* sequence_number, uint32_t* timestamp);
173
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 private:
175 static const int kOutputSizeMs = 10;
176 static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz.
177 // TODO(hlundin): Provide a better value for kSyncBufferSize.
178 static const int kSyncBufferSize = 2 * kMaxFrameSize;
179
180 // Inserts a new packet into NetEq. This is used by the InsertPacket method
181 // above. Returns 0 on success, otherwise an error code.
182 // TODO(hlundin): Merge this with InsertPacket above?
183 int InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
184 const uint8_t* payload,
185 int length_bytes,
186 uint32_t receive_timestamp);
187
188
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000189 // Delivers 10 ms of audio data. The data is written to |output|, which can
190 // hold (at least) |max_length| elements. The number of channels that were
191 // written to the output is provided in the output variable |num_channels|,
192 // and each channel contains |samples_per_channel| elements. If more than one
193 // channel is written, the samples are interleaved.
194 // Returns 0 on success, otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000195 int GetAudioInternal(size_t max_length, int16_t* output,
196 int* samples_per_channel, int* num_channels);
197
198
199 // Provides a decision to the GetAudioInternal method. The decision what to
200 // do is written to |operation|. Packets to decode are written to
201 // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When
202 // DTMF should be played, |play_dtmf| is set to true by the method.
203 // Returns 0 on success, otherwise an error code.
204 int GetDecision(Operations* operation,
205 PacketList* packet_list,
206 DtmfEvent* dtmf_event,
207 bool* play_dtmf);
208
209 // Decodes the speech packets in |packet_list|, and writes the results to
210 // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
211 // elements. The length of the decoded data is written to |decoded_length|.
212 // The speech type -- speech or (codec-internal) comfort noise -- is written
213 // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389
214 // comfort noise, those are not decoded.
215 int Decode(PacketList* packet_list, Operations* operation,
216 int* decoded_length, AudioDecoder::SpeechType* speech_type);
217
218 // Sub-method to Decode(). Performs the actual decoding.
219 int DecodeLoop(PacketList* packet_list, Operations* operation,
220 AudioDecoder* decoder, int* decoded_length,
221 AudioDecoder::SpeechType* speech_type);
222
223 // Sub-method which calls the Normal class to perform the normal operation.
224 void DoNormal(const 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 Merge class to perform the merge operation.
229 void DoMerge(int16_t* decoded_buffer, size_t decoded_length,
230 AudioDecoder::SpeechType speech_type, bool play_dtmf,
231 AudioMultiVector<int16_t>* algorithm_buffer);
232
233 // Sub-method which calls the Expand class to perform the expand operation.
234 int DoExpand(bool play_dtmf, AudioMultiVector<int16_t>* algorithm_buffer);
235
236 // Sub-method which calls the Accelerate class to perform the accelerate
237 // operation.
238 int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
239 AudioDecoder::SpeechType speech_type, bool play_dtmf,
240 AudioMultiVector<int16_t>* algorithm_buffer);
241
242 // Sub-method which calls the PreemptiveExpand class to perform the
243 // preemtive expand operation.
244 int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length,
245 AudioDecoder::SpeechType speech_type, bool play_dtmf,
246 AudioMultiVector<int16_t>* algorithm_buffer);
247
248 // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort
249 // noise. |packet_list| can either contain one SID frame to update the
250 // noise parameters, or no payload at all, in which case the previously
251 // received parameters are used.
252 int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf,
253 AudioMultiVector<int16_t>* algorithm_buffer);
254
255 // Calls the audio decoder to generate codec-internal comfort noise when
256 // no packet was received.
257 void DoCodecInternalCng(AudioMultiVector<int16_t>* algorithm_buffer);
258
259 // Calls the DtmfToneGenerator class to generate DTMF tones.
260 int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf,
261 AudioMultiVector<int16_t>* algorithm_buffer);
262
263 // Produces packet-loss concealment using alternative methods. If the codec
264 // has an internal PLC, it is called to generate samples. Otherwise, the
265 // method performs zero-stuffing.
266 void DoAlternativePlc(bool increase_timestamp,
267 AudioMultiVector<int16_t>* algorithm_buffer);
268
269 // Overdub DTMF on top of |output|.
270 int DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
271 int16_t* output) const;
272
273 // Extracts packets from |packet_buffer_| to produce at least
274 // |required_samples| samples. The packets are inserted into |packet_list|.
275 // Returns the number of samples that the packets in the list will produce, or
276 // -1 in case of an error.
277 int ExtractPackets(int required_samples, PacketList* packet_list);
278
279 // Resets various variables and objects to new values based on the sample rate
280 // |fs_hz| and |channels| number audio channels.
281 void SetSampleRateAndChannels(int fs_hz, size_t channels);
282
283 // Returns the output type for the audio produced by the latest call to
284 // GetAudio().
285 NetEqOutputType LastOutputType();
286
287 BackgroundNoise* background_noise_;
288 scoped_ptr<BufferLevelFilter> buffer_level_filter_;
289 scoped_ptr<DecoderDatabase> decoder_database_;
290 scoped_ptr<DelayManager> delay_manager_;
291 scoped_ptr<DelayPeakDetector> delay_peak_detector_;
292 scoped_ptr<DtmfBuffer> dtmf_buffer_;
293 scoped_ptr<DtmfToneGenerator> dtmf_tone_generator_;
294 scoped_ptr<PacketBuffer> packet_buffer_;
295 scoped_ptr<PayloadSplitter> payload_splitter_;
296 scoped_ptr<TimestampScaler> timestamp_scaler_;
297 scoped_ptr<DecisionLogic> decision_logic_;
298 scoped_ptr<PostDecodeVad> vad_;
299 SyncBuffer* sync_buffer_;
300 Expand* expand_;
301 RandomVector random_vector_;
302 ComfortNoise* comfort_noise_;
303 Rtcp rtcp_;
304 StatisticsCalculator stats_;
305 int fs_hz_;
306 int fs_mult_;
307 int output_size_samples_;
308 int decoder_frame_length_;
309 Modes last_mode_;
310 scoped_array<int16_t> mute_factor_array_;
311 size_t decoded_buffer_length_;
312 scoped_array<int16_t> decoded_buffer_;
313 uint32_t playout_timestamp_;
314 bool new_codec_;
315 uint32_t timestamp_;
316 bool reset_decoder_;
317 uint8_t current_rtp_payload_type_;
318 uint8_t current_cng_rtp_payload_type_;
319 uint32_t ssrc_;
320 bool first_packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 int error_code_; // Store last error code.
322 int decoder_error_code_;
323 CriticalSectionWrapper* crit_sect_;
324
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000325 // These values are used by NACK module to estimate time-to-play of
326 // a missing packet. Occasionally, NetEq might decide to decode more
327 // than one packet. Therefore, these values store sequence number and
328 // timestamp of the first packet pulled from the packet buffer. In
329 // such cases, these values do not exactly represent the sequence number
330 // or timestamp associated with a 10ms audio pulled from NetEq. NACK
331 // module is designed to compensate for this.
332 int decoded_packet_sequence_number_;
333 uint32_t decoded_packet_timestamp_;
334
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
336};
337
338} // namespace webrtc
339#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_