blob: f9be8b40f4fbea062979f0460cc853602be8448a [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_INTERFACE_NETEQ_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_INTERFACE_NETEQ_H_
13
14#include <cstring> // Provide access to size_t.
15#include <vector>
16
17#include "webrtc/modules/audio_coding/neteq4/interface/audio_decoder.h"
18#include "webrtc/system_wrappers/interface/constructor_magic.h"
19#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
23// Forward declarations.
24struct WebRtcRTPHeader;
25
26// RTCP statistics.
27struct RtcpStatistics {
28 uint16_t fraction_lost;
29 uint32_t cumulative_lost;
30 uint32_t extended_max;
31 uint32_t jitter;
32};
33
34struct NetEqNetworkStatistics {
35 uint16_t current_buffer_size_ms; // Current jitter buffer size in ms.
36 uint16_t preferred_buffer_size_ms; // Target buffer size in ms.
37 uint16_t jitter_peaks_found; // 1 if adding extra delay due to peaky
38 // jitter; 0 otherwise.
39 uint16_t packet_loss_rate; // Loss rate (network + late) in Q14.
40 uint16_t packet_discard_rate; // Late loss rate in Q14.
41 uint16_t expand_rate; // Fraction (of original stream) of synthesized
42 // speech inserted through expansion (in Q14).
43 uint16_t preemptive_rate; // Fraction of data inserted through pre-emptive
44 // expansion (in Q14).
45 uint16_t accelerate_rate; // Fraction of data removed through acceleration
46 // (in Q14).
47 int32_t clockdrift_ppm; // Average clock-drift in parts-per-million
48 // (positive or negative).
49 int added_zero_samples; // Number of zero samples added in "off" mode.
50};
51
52enum NetEqOutputType {
53 kOutputNormal,
54 kOutputPLC,
55 kOutputCNG,
56 kOutputPLCtoCNG,
57 kOutputVADPassive
58};
59
60enum NetEqPlayoutMode {
61 kPlayoutOn,
62 kPlayoutOff,
63 kPlayoutFax,
64 kPlayoutStreaming
65};
66
67// This is the interface class for NetEq.
68class NetEq {
69 public:
70 enum ReturnCodes {
71 kOK = 0,
72 kFail = -1,
73 kNotImplemented = -2
74 };
75
76 enum ErrorCodes {
77 kNoError = 0,
78 kOtherError,
79 kInvalidRtpPayloadType,
80 kUnknownRtpPayloadType,
81 kCodecNotSupported,
82 kDecoderExists,
83 kDecoderNotFound,
84 kInvalidSampleRate,
85 kInvalidPointer,
86 kAccelerateError,
87 kPreemptiveExpandError,
88 kComfortNoiseErrorCode,
89 kDecoderErrorCode,
90 kOtherDecoderError,
91 kInvalidOperation,
92 kDtmfParameterError,
93 kDtmfParsingError,
94 kDtmfInsertError,
95 kStereoNotSupported,
96 kSampleUnderrun,
97 kDecodedTooMuch,
98 kFrameSplitError,
99 kRedundancySplitError,
100 kPacketBufferCorruption
101 };
102
103 static const int kMaxNumPacketsInBuffer = 240; // TODO(hlundin): Remove.
104 static const int kMaxBytesInBuffer = 113280; // TODO(hlundin): Remove.
105
106 // Creates a new NetEq object, starting at the sample rate |sample_rate_hz|.
107 // (Note that it will still change the sample rate depending on what payloads
108 // are being inserted; |sample_rate_hz| is just for startup configuration.)
109 static NetEq* Create(int sample_rate_hz);
110
111 virtual ~NetEq() {}
112
113 // Inserts a new packet into NetEq. The |receive_timestamp| is an indication
114 // of the time when the packet was received, and should be measured with
115 // the same tick rate as the RTP timestamp of the current payload.
116 // Returns 0 on success, -1 on failure.
117 virtual int InsertPacket(const WebRtcRTPHeader& rtp_header,
118 const uint8_t* payload,
119 int length_bytes,
120 uint32_t receive_timestamp) = 0;
121
122 // Instructs NetEq to deliver 10 ms of audio data. The data is written to
123 // |output_audio|, which can hold (at least) |max_length| elements.
124 // The number of channels that were written to the output is provided in
125 // the output variable |num_channels|, and each channel contains
126 // |samples_per_channel| elements. If more than one channel is written,
127 // the samples are interleaved.
128 // The speech type is written to |type|, if |type| is not NULL.
129 // Returns kOK on success, or kFail in case of an error.
130 virtual int GetAudio(size_t max_length, int16_t* output_audio,
131 int* samples_per_channel, int* num_channels,
132 NetEqOutputType* type) = 0;
133
134 // Associates |rtp_payload_type| with |codec| and stores the information in
135 // the codec database. Returns 0 on success, -1 on failure.
136 virtual int RegisterPayloadType(enum NetEqDecoder codec,
137 uint8_t rtp_payload_type) = 0;
138
139 // Provides an externally created decoder object |decoder| to insert in the
140 // decoder database. The decoder implements a decoder of type |codec| and
141 // associates it with |rtp_payload_type|. The decoder operates at the
142 // frequency |sample_rate_hz|. Returns kOK on success, kFail on failure.
143 virtual int RegisterExternalDecoder(AudioDecoder* decoder,
144 enum NetEqDecoder codec,
145 int sample_rate_hz,
146 uint8_t rtp_payload_type) = 0;
147
148 // Removes |rtp_payload_type| from the codec database. Returns 0 on success,
149 // -1 on failure.
150 virtual int RemovePayloadType(uint8_t rtp_payload_type) = 0;
151
152 // Sets the desired extra delay on top of what NetEq already applies due to
153 // current network situation. Used for synchronization with video. Returns
154 // true if successful, otherwise false.
155 virtual bool SetExtraDelay(int extra_delay_ms) = 0;
156
157 // Not implemented.
158 virtual int SetTargetDelay() = 0;
159
160 // Not implemented.
161 virtual int TargetDelay() = 0;
162
163 // Not implemented.
164 virtual int CurrentDelay() = 0;
165
166 // Enables playout of DTMF tones.
167 virtual int EnableDtmf() = 0;
168
169 // Sets the playout mode to |mode|.
170 virtual void SetPlayoutMode(NetEqPlayoutMode mode) = 0;
171
172 // Returns the current playout mode.
173 virtual NetEqPlayoutMode PlayoutMode() const = 0;
174
175 // Writes the current network statistics to |stats|. The statistics are reset
176 // after the call.
177 virtual int NetworkStatistics(NetEqNetworkStatistics* stats) = 0;
178
179 // Writes the last packet waiting times (in ms) to |waiting_times|. The number
180 // of values written is no more than 100, but may be smaller if the interface
181 // is polled again before 100 packets has arrived.
182 virtual void WaitingTimes(std::vector<int>* waiting_times) = 0;
183
184 // Writes the current RTCP statistics to |stats|. The statistics are reset
185 // and a new report period is started with the call.
186 virtual void GetRtcpStatistics(RtcpStatistics* stats) = 0;
187
188 // Same as RtcpStatistics(), but does not reset anything.
189 virtual void GetRtcpStatisticsNoReset(RtcpStatistics* stats) = 0;
190
191 // Enables post-decode VAD. When enabled, GetAudio() will return
192 // kOutputVADPassive when the signal contains no speech.
193 virtual void EnableVad() = 0;
194
195 // Disables post-decode VAD.
196 virtual void DisableVad() = 0;
197
198 // Returns the RTP timestamp for the last sample delivered by GetAudio().
199 virtual uint32_t PlayoutTimestamp() = 0;
200
201 // Not implemented.
202 virtual int SetTargetNumberOfChannels() = 0;
203
204 // Not implemented.
205 virtual int SetTargetSampleRate() = 0;
206
207 // Returns the error code for the last occurred error. If no error has
208 // occurred, 0 is returned.
209 virtual int LastError() = 0;
210
211 // Returns the error code last returned by a decoder (audio or comfort noise).
212 // When LastError() returns kDecoderErrorCode or kComfortNoiseErrorCode, check
213 // this method to get the decoder's error code.
214 virtual int LastDecoderError() = 0;
215
216 // Flushes both the packet buffer and the sync buffer.
217 virtual void FlushBuffers() = 0;
218
219 protected:
220 NetEq() {}
221
222 private:
223 DISALLOW_COPY_AND_ASSIGN(NetEq);
224};
225
226} // namespace webrtc
227#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_INTERFACE_NETEQ_H_