blob: 9bdafdde3dcbb4cc6cb27e6d33bde2709c201b4b [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
turaj@webrtc.org036b7432013-09-11 18:45:02 +0000174 virtual int InsertSyncPacket(const WebRtcRTPHeader& rtp_header,
175 uint32_t receive_timestamp);
176
177 virtual void SetBackgroundNoiseMode(NetEqBackgroundNoiseMode mode);
178
179 virtual NetEqBackgroundNoiseMode BackgroundNoiseMode() const;
180
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 private:
182 static const int kOutputSizeMs = 10;
183 static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz.
184 // TODO(hlundin): Provide a better value for kSyncBufferSize.
185 static const int kSyncBufferSize = 2 * kMaxFrameSize;
186
187 // Inserts a new packet into NetEq. This is used by the InsertPacket method
188 // above. Returns 0 on success, otherwise an error code.
189 // TODO(hlundin): Merge this with InsertPacket above?
190 int InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
191 const uint8_t* payload,
192 int length_bytes,
193 uint32_t receive_timestamp);
194
195
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000196 // Delivers 10 ms of audio data. The data is written to |output|, which can
197 // hold (at least) |max_length| elements. The number of channels that were
198 // written to the output is provided in the output variable |num_channels|,
199 // and each channel contains |samples_per_channel| elements. If more than one
200 // channel is written, the samples are interleaved.
201 // Returns 0 on success, otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 int GetAudioInternal(size_t max_length, int16_t* output,
203 int* samples_per_channel, int* num_channels);
204
205
206 // Provides a decision to the GetAudioInternal method. The decision what to
207 // do is written to |operation|. Packets to decode are written to
208 // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When
209 // DTMF should be played, |play_dtmf| is set to true by the method.
210 // Returns 0 on success, otherwise an error code.
211 int GetDecision(Operations* operation,
212 PacketList* packet_list,
213 DtmfEvent* dtmf_event,
214 bool* play_dtmf);
215
216 // Decodes the speech packets in |packet_list|, and writes the results to
217 // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
218 // elements. The length of the decoded data is written to |decoded_length|.
219 // The speech type -- speech or (codec-internal) comfort noise -- is written
220 // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389
221 // comfort noise, those are not decoded.
222 int Decode(PacketList* packet_list, Operations* operation,
223 int* decoded_length, AudioDecoder::SpeechType* speech_type);
224
225 // Sub-method to Decode(). Performs the actual decoding.
226 int DecodeLoop(PacketList* packet_list, Operations* operation,
227 AudioDecoder* decoder, int* decoded_length,
228 AudioDecoder::SpeechType* speech_type);
229
230 // Sub-method which calls the Normal class to perform the normal operation.
231 void DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000232 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233
234 // Sub-method which calls the Merge class to perform the merge operation.
235 void DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000236 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237
238 // Sub-method which calls the Expand class to perform the expand operation.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000239 int DoExpand(bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000240
241 // Sub-method which calls the Accelerate class to perform the accelerate
242 // operation.
243 int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000244 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000245
246 // Sub-method which calls the PreemptiveExpand class to perform the
247 // preemtive expand operation.
248 int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000249 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000250
251 // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort
252 // noise. |packet_list| can either contain one SID frame to update the
253 // noise parameters, or no payload at all, in which case the previously
254 // received parameters are used.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000255 int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256
257 // Calls the audio decoder to generate codec-internal comfort noise when
258 // no packet was received.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000259 void DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000260
261 // Calls the DtmfToneGenerator class to generate DTMF tones.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000262 int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263
264 // Produces packet-loss concealment using alternative methods. If the codec
265 // has an internal PLC, it is called to generate samples. Otherwise, the
266 // method performs zero-stuffing.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000267 void DoAlternativePlc(bool increase_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268
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_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000299 AudioMultiVector<int16_t>* algorithm_buffer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300 SyncBuffer* sync_buffer_;
301 Expand* expand_;
302 RandomVector random_vector_;
303 ComfortNoise* comfort_noise_;
304 Rtcp rtcp_;
305 StatisticsCalculator stats_;
306 int fs_hz_;
307 int fs_mult_;
308 int output_size_samples_;
309 int decoder_frame_length_;
310 Modes last_mode_;
311 scoped_array<int16_t> mute_factor_array_;
312 size_t decoded_buffer_length_;
313 scoped_array<int16_t> decoded_buffer_;
314 uint32_t playout_timestamp_;
315 bool new_codec_;
316 uint32_t timestamp_;
317 bool reset_decoder_;
318 uint8_t current_rtp_payload_type_;
319 uint8_t current_cng_rtp_payload_type_;
320 uint32_t ssrc_;
321 bool first_packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 int error_code_; // Store last error code.
323 int decoder_error_code_;
324 CriticalSectionWrapper* crit_sect_;
325
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000326 // These values are used by NACK module to estimate time-to-play of
327 // a missing packet. Occasionally, NetEq might decide to decode more
328 // than one packet. Therefore, these values store sequence number and
329 // timestamp of the first packet pulled from the packet buffer. In
330 // such cases, these values do not exactly represent the sequence number
331 // or timestamp associated with a 10ms audio pulled from NetEq. NACK
332 // module is designed to compensate for this.
333 int decoded_packet_sequence_number_;
334 uint32_t decoded_packet_timestamp_;
335
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
337};
338
339} // namespace webrtc
340#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_