blob: c0416809cafc16f09441cfd80013c41727627bef [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.
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000030class Accelerate;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031class BackgroundNoise;
32class BufferLevelFilter;
33class ComfortNoise;
34class CriticalSectionWrapper;
35class DecisionLogic;
36class DecoderDatabase;
37class DelayManager;
38class DelayPeakDetector;
39class DtmfBuffer;
40class DtmfToneGenerator;
41class Expand;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000042class Merge;
43class Normal;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044class PacketBuffer;
45class PayloadSplitter;
46class PostDecodeVad;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000047class PreemptiveExpand;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048class RandomVector;
49class SyncBuffer;
50class TimestampScaler;
51struct DtmfEvent;
52
53class NetEqImpl : public webrtc::NetEq {
54 public:
55 // Creates a new NetEqImpl object. The object will assume ownership of all
56 // injected dependencies, and will delete them when done.
57 NetEqImpl(int fs,
58 BufferLevelFilter* buffer_level_filter,
59 DecoderDatabase* decoder_database,
60 DelayManager* delay_manager,
61 DelayPeakDetector* delay_peak_detector,
62 DtmfBuffer* dtmf_buffer,
63 DtmfToneGenerator* dtmf_tone_generator,
64 PacketBuffer* packet_buffer,
65 PayloadSplitter* payload_splitter,
66 TimestampScaler* timestamp_scaler);
67
68 virtual ~NetEqImpl();
69
70 // Inserts a new packet into NetEq. The |receive_timestamp| is an indication
71 // of the time when the packet was received, and should be measured with
72 // the same tick rate as the RTP timestamp of the current payload.
73 // Returns 0 on success, -1 on failure.
74 virtual int InsertPacket(const WebRtcRTPHeader& rtp_header,
75 const uint8_t* payload,
76 int length_bytes,
77 uint32_t receive_timestamp);
78
79 // Instructs NetEq to deliver 10 ms of audio data. The data is written to
80 // |output_audio|, which can hold (at least) |max_length| elements.
81 // The number of channels that were written to the output is provided in
82 // the output variable |num_channels|, and each channel contains
83 // |samples_per_channel| elements. If more than one channel is written,
84 // the samples are interleaved.
85 // The speech type is written to |type|, if |type| is not NULL.
86 // Returns kOK on success, or kFail in case of an error.
87 virtual int GetAudio(size_t max_length, int16_t* output_audio,
88 int* samples_per_channel, int* num_channels,
89 NetEqOutputType* type);
90
91 // Associates |rtp_payload_type| with |codec| and stores the information in
92 // the codec database. Returns kOK on success, kFail on failure.
93 virtual int RegisterPayloadType(enum NetEqDecoder codec,
94 uint8_t rtp_payload_type);
95
96 // Provides an externally created decoder object |decoder| to insert in the
97 // decoder database. The decoder implements a decoder of type |codec| and
98 // associates it with |rtp_payload_type|. The decoder operates at the
99 // frequency |sample_rate_hz|. Returns kOK on success, kFail on failure.
100 virtual int RegisterExternalDecoder(AudioDecoder* decoder,
101 enum NetEqDecoder codec,
102 int sample_rate_hz,
103 uint8_t rtp_payload_type);
104
105 // Removes |rtp_payload_type| from the codec database. Returns 0 on success,
106 // -1 on failure.
107 virtual int RemovePayloadType(uint8_t rtp_payload_type);
108
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000109 virtual bool SetMinimumDelay(int delay_ms);
110
111 virtual bool SetMaximumDelay(int delay_ms);
112
113 virtual int LeastRequiredDelayMs() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114
115 virtual int SetTargetDelay() { return kNotImplemented; }
116
117 virtual int TargetDelay() { return kNotImplemented; }
118
119 virtual int CurrentDelay() { return kNotImplemented; }
120
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 // Sets the playout mode to |mode|.
122 virtual void SetPlayoutMode(NetEqPlayoutMode mode);
123
124 // Returns the current playout mode.
125 virtual NetEqPlayoutMode PlayoutMode() const;
126
127 // Writes the current network statistics to |stats|. The statistics are reset
128 // after the call.
129 virtual int NetworkStatistics(NetEqNetworkStatistics* stats);
130
131 // Writes the last packet waiting times (in ms) to |waiting_times|. The number
132 // of values written is no more than 100, but may be smaller if the interface
133 // is polled again before 100 packets has arrived.
134 virtual void WaitingTimes(std::vector<int>* waiting_times);
135
136 // Writes the current RTCP statistics to |stats|. The statistics are reset
137 // and a new report period is started with the call.
138 virtual void GetRtcpStatistics(RtcpStatistics* stats);
139
140 // Same as RtcpStatistics(), but does not reset anything.
141 virtual void GetRtcpStatisticsNoReset(RtcpStatistics* stats);
142
143 // Enables post-decode VAD. When enabled, GetAudio() will return
144 // kOutputVADPassive when the signal contains no speech.
145 virtual void EnableVad();
146
147 // Disables post-decode VAD.
148 virtual void DisableVad();
149
150 // Returns the RTP timestamp for the last sample delivered by GetAudio().
151 virtual uint32_t PlayoutTimestamp();
152
153 virtual int SetTargetNumberOfChannels() { return kNotImplemented; }
154
155 virtual int SetTargetSampleRate() { return kNotImplemented; }
156
157 // Returns the error code for the last occurred error. If no error has
158 // occurred, 0 is returned.
159 virtual int LastError();
160
161 // Returns the error code last returned by a decoder (audio or comfort noise).
162 // When LastError() returns kDecoderErrorCode or kComfortNoiseErrorCode, check
163 // this method to get the decoder's error code.
164 virtual int LastDecoderError();
165
166 // Flushes both the packet buffer and the sync buffer.
167 virtual void FlushBuffers();
168
turaj@webrtc.org7df97062013-08-02 18:07:13 +0000169 virtual void PacketBufferStatistics(int* current_num_packets,
170 int* max_num_packets,
171 int* current_memory_size_bytes,
172 int* max_memory_size_bytes) const;
173
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000174 // Get sequence number and timestamp of the latest RTP.
175 // This method is to facilitate NACK.
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000176 virtual int DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const;
177
178 // Sets background noise mode.
179 virtual void SetBackgroundNoiseMode(NetEqBackgroundNoiseMode mode);
180
181 // Gets background noise mode.
182 virtual NetEqBackgroundNoiseMode BackgroundNoiseMode() const;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000183
turaj@webrtc.org036b7432013-09-11 18:45:02 +0000184 virtual int InsertSyncPacket(const WebRtcRTPHeader& rtp_header,
185 uint32_t receive_timestamp);
186
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 private:
188 static const int kOutputSizeMs = 10;
189 static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz.
190 // TODO(hlundin): Provide a better value for kSyncBufferSize.
191 static const int kSyncBufferSize = 2 * kMaxFrameSize;
192
193 // Inserts a new packet into NetEq. This is used by the InsertPacket method
194 // above. Returns 0 on success, otherwise an error code.
195 // TODO(hlundin): Merge this with InsertPacket above?
196 int InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
197 const uint8_t* payload,
198 int length_bytes,
199 uint32_t receive_timestamp);
200
201
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000202 // Delivers 10 ms of audio data. The data is written to |output|, which can
203 // hold (at least) |max_length| elements. The number of channels that were
204 // written to the output is provided in the output variable |num_channels|,
205 // and each channel contains |samples_per_channel| elements. If more than one
206 // channel is written, the samples are interleaved.
207 // Returns 0 on success, otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208 int GetAudioInternal(size_t max_length, int16_t* output,
209 int* samples_per_channel, int* num_channels);
210
211
212 // Provides a decision to the GetAudioInternal method. The decision what to
213 // do is written to |operation|. Packets to decode are written to
214 // |packet_list|, and a DTMF event to play is written to |dtmf_event|. When
215 // DTMF should be played, |play_dtmf| is set to true by the method.
216 // Returns 0 on success, otherwise an error code.
217 int GetDecision(Operations* operation,
218 PacketList* packet_list,
219 DtmfEvent* dtmf_event,
220 bool* play_dtmf);
221
222 // Decodes the speech packets in |packet_list|, and writes the results to
223 // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
224 // elements. The length of the decoded data is written to |decoded_length|.
225 // The speech type -- speech or (codec-internal) comfort noise -- is written
226 // to |speech_type|. If |packet_list| contains any SID frames for RFC 3389
227 // comfort noise, those are not decoded.
228 int Decode(PacketList* packet_list, Operations* operation,
229 int* decoded_length, AudioDecoder::SpeechType* speech_type);
230
231 // Sub-method to Decode(). Performs the actual decoding.
232 int DecodeLoop(PacketList* packet_list, Operations* operation,
233 AudioDecoder* decoder, int* decoded_length,
234 AudioDecoder::SpeechType* speech_type);
235
236 // Sub-method which calls the Normal class to perform the normal operation.
237 void DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000238 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000239
240 // Sub-method which calls the Merge class to perform the merge operation.
241 void DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000242 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243
244 // Sub-method which calls the Expand class to perform the expand operation.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000245 int DoExpand(bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000246
247 // Sub-method which calls the Accelerate class to perform the accelerate
248 // operation.
249 int DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000250 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251
252 // Sub-method which calls the PreemptiveExpand class to perform the
253 // preemtive expand operation.
254 int DoPreemptiveExpand(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000255 AudioDecoder::SpeechType speech_type, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256
257 // Sub-method which calls the ComfortNoise class to generate RFC 3389 comfort
258 // noise. |packet_list| can either contain one SID frame to update the
259 // noise parameters, or no payload at all, in which case the previously
260 // received parameters are used.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000261 int DoRfc3389Cng(PacketList* packet_list, bool play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000262
263 // Calls the audio decoder to generate codec-internal comfort noise when
264 // no packet was received.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000265 void DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266
267 // Calls the DtmfToneGenerator class to generate DTMF tones.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000268 int DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269
270 // Produces packet-loss concealment using alternative methods. If the codec
271 // has an internal PLC, it is called to generate samples. Otherwise, the
272 // method performs zero-stuffing.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000273 void DoAlternativePlc(bool increase_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274
275 // Overdub DTMF on top of |output|.
276 int DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
277 int16_t* output) const;
278
279 // Extracts packets from |packet_buffer_| to produce at least
280 // |required_samples| samples. The packets are inserted into |packet_list|.
281 // Returns the number of samples that the packets in the list will produce, or
282 // -1 in case of an error.
283 int ExtractPackets(int required_samples, PacketList* packet_list);
284
285 // Resets various variables and objects to new values based on the sample rate
286 // |fs_hz| and |channels| number audio channels.
287 void SetSampleRateAndChannels(int fs_hz, size_t channels);
288
289 // Returns the output type for the audio produced by the latest call to
290 // GetAudio().
291 NetEqOutputType LastOutputType();
292
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000293 scoped_ptr<BackgroundNoise> background_noise_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 scoped_ptr<BufferLevelFilter> buffer_level_filter_;
295 scoped_ptr<DecoderDatabase> decoder_database_;
296 scoped_ptr<DelayManager> delay_manager_;
297 scoped_ptr<DelayPeakDetector> delay_peak_detector_;
298 scoped_ptr<DtmfBuffer> dtmf_buffer_;
299 scoped_ptr<DtmfToneGenerator> dtmf_tone_generator_;
300 scoped_ptr<PacketBuffer> packet_buffer_;
301 scoped_ptr<PayloadSplitter> payload_splitter_;
302 scoped_ptr<TimestampScaler> timestamp_scaler_;
303 scoped_ptr<DecisionLogic> decision_logic_;
304 scoped_ptr<PostDecodeVad> vad_;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000305 scoped_ptr<AudioMultiVector<int16_t> > algorithm_buffer_;
306 scoped_ptr<SyncBuffer> sync_buffer_;
307 scoped_ptr<Expand> expand_;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +0000308 scoped_ptr<Normal> normal_;
309 scoped_ptr<Merge> merge_;
310 scoped_ptr<Accelerate> accelerate_;
311 scoped_ptr<PreemptiveExpand> preemptive_expand_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 RandomVector random_vector_;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000313 scoped_ptr<ComfortNoise> comfort_noise_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314 Rtcp rtcp_;
315 StatisticsCalculator stats_;
316 int fs_hz_;
317 int fs_mult_;
318 int output_size_samples_;
319 int decoder_frame_length_;
320 Modes last_mode_;
321 scoped_array<int16_t> mute_factor_array_;
322 size_t decoded_buffer_length_;
323 scoped_array<int16_t> decoded_buffer_;
324 uint32_t playout_timestamp_;
325 bool new_codec_;
326 uint32_t timestamp_;
327 bool reset_decoder_;
328 uint8_t current_rtp_payload_type_;
329 uint8_t current_cng_rtp_payload_type_;
330 uint32_t ssrc_;
331 bool first_packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 int error_code_; // Store last error code.
333 int decoder_error_code_;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000334 scoped_ptr<CriticalSectionWrapper> crit_sect_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000336 // These values are used by NACK module to estimate time-to-play of
337 // a missing packet. Occasionally, NetEq might decide to decode more
338 // than one packet. Therefore, these values store sequence number and
339 // timestamp of the first packet pulled from the packet buffer. In
340 // such cases, these values do not exactly represent the sequence number
341 // or timestamp associated with a 10ms audio pulled from NetEq. NACK
342 // module is designed to compensate for this.
343 int decoded_packet_sequence_number_;
344 uint32_t decoded_packet_timestamp_;
345
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
347};
348
349} // namespace webrtc
350#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_NETEQ_IMPL_H_