OLD | NEW |
| (Empty) |
1 /* | |
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_ACM2_AUDIO_CODING_MODULE_IMPL_H_ | |
12 #define WEBRTC_MODULES_AUDIO_CODING_ACM2_AUDIO_CODING_MODULE_IMPL_H_ | |
13 | |
14 #include <memory> | |
15 #include <string> | |
16 #include <vector> | |
17 | |
18 #include "webrtc/base/buffer.h" | |
19 #include "webrtc/base/criticalsection.h" | |
20 #include "webrtc/base/thread_annotations.h" | |
21 #include "webrtc/common_types.h" | |
22 #include "webrtc/engine_configurations.h" | |
23 #include "webrtc/modules/audio_coding/acm2/acm_receiver.h" | |
24 #include "webrtc/modules/audio_coding/acm2/acm_resampler.h" | |
25 #include "webrtc/modules/audio_coding/acm2/codec_manager.h" | |
26 #include "webrtc/modules/audio_coding/codecs/audio_encoder.h" | |
27 | |
28 namespace webrtc { | |
29 | |
30 class AudioCodingImpl; | |
31 | |
32 namespace acm2 { | |
33 | |
34 struct EncoderFactory; | |
35 | |
36 class AudioCodingModuleImpl final : public AudioCodingModule { | |
37 public: | |
38 friend webrtc::AudioCodingImpl; | |
39 | |
40 explicit AudioCodingModuleImpl(const AudioCodingModule::Config& config); | |
41 ~AudioCodingModuleImpl() override; | |
42 | |
43 ///////////////////////////////////////// | |
44 // Sender | |
45 // | |
46 | |
47 // Can be called multiple times for Codec, CNG, RED. | |
48 int RegisterSendCodec(const CodecInst& send_codec) override; | |
49 | |
50 void RegisterExternalSendCodec( | |
51 AudioEncoder* external_speech_encoder) override; | |
52 | |
53 void ModifyEncoder( | |
54 FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) override; | |
55 | |
56 // Get current send codec. | |
57 rtc::Optional<CodecInst> SendCodec() const override; | |
58 | |
59 // Get current send frequency. | |
60 int SendFrequency() const override; | |
61 | |
62 // Sets the bitrate to the specified value in bits/sec. In case the codec does | |
63 // not support the requested value it will choose an appropriate value | |
64 // instead. | |
65 void SetBitRate(int bitrate_bps) override; | |
66 | |
67 // Register a transport callback which will be | |
68 // called to deliver the encoded buffers. | |
69 int RegisterTransportCallback(AudioPacketizationCallback* transport) override; | |
70 | |
71 // Add 10 ms of raw (PCM) audio data to the encoder. | |
72 int Add10MsData(const AudioFrame& audio_frame) override; | |
73 | |
74 ///////////////////////////////////////// | |
75 // (RED) Redundant Coding | |
76 // | |
77 | |
78 // Configure RED status i.e. on/off. | |
79 int SetREDStatus(bool enable_red) override; | |
80 | |
81 // Get RED status. | |
82 bool REDStatus() const override; | |
83 | |
84 ///////////////////////////////////////// | |
85 // (FEC) Forward Error Correction (codec internal) | |
86 // | |
87 | |
88 // Configure FEC status i.e. on/off. | |
89 int SetCodecFEC(bool enabled_codec_fec) override; | |
90 | |
91 // Get FEC status. | |
92 bool CodecFEC() const override; | |
93 | |
94 // Set target packet loss rate | |
95 int SetPacketLossRate(int loss_rate) override; | |
96 | |
97 ///////////////////////////////////////// | |
98 // (VAD) Voice Activity Detection | |
99 // and | |
100 // (CNG) Comfort Noise Generation | |
101 // | |
102 | |
103 int SetVAD(bool enable_dtx = true, | |
104 bool enable_vad = false, | |
105 ACMVADMode mode = VADNormal) override; | |
106 | |
107 int VAD(bool* dtx_enabled, | |
108 bool* vad_enabled, | |
109 ACMVADMode* mode) const override; | |
110 | |
111 int RegisterVADCallback(ACMVADCallback* vad_callback) override; | |
112 | |
113 ///////////////////////////////////////// | |
114 // Receiver | |
115 // | |
116 | |
117 // Initialize receiver, resets codec database etc. | |
118 int InitializeReceiver() override; | |
119 | |
120 // Get current receive frequency. | |
121 int ReceiveFrequency() const override; | |
122 | |
123 // Get current playout frequency. | |
124 int PlayoutFrequency() const override; | |
125 | |
126 int RegisterReceiveCodec(const CodecInst& receive_codec) override; | |
127 int RegisterReceiveCodec( | |
128 const CodecInst& receive_codec, | |
129 FunctionView<std::unique_ptr<AudioDecoder>()> isac_factory) override; | |
130 | |
131 int RegisterExternalReceiveCodec(int rtp_payload_type, | |
132 AudioDecoder* external_decoder, | |
133 int sample_rate_hz, | |
134 int num_channels, | |
135 const std::string& name) override; | |
136 | |
137 // Get current received codec. | |
138 int ReceiveCodec(CodecInst* current_codec) const override; | |
139 | |
140 // Incoming packet from network parsed and ready for decode. | |
141 int IncomingPacket(const uint8_t* incoming_payload, | |
142 const size_t payload_length, | |
143 const WebRtcRTPHeader& rtp_info) override; | |
144 | |
145 // Incoming payloads, without rtp-info, the rtp-info will be created in ACM. | |
146 // One usage for this API is when pre-encoded files are pushed in ACM. | |
147 int IncomingPayload(const uint8_t* incoming_payload, | |
148 const size_t payload_length, | |
149 uint8_t payload_type, | |
150 uint32_t timestamp) override; | |
151 | |
152 // Minimum playout delay. | |
153 int SetMinimumPlayoutDelay(int time_ms) override; | |
154 | |
155 // Maximum playout delay. | |
156 int SetMaximumPlayoutDelay(int time_ms) override; | |
157 | |
158 // Smallest latency NetEq will maintain. | |
159 int LeastRequiredDelayMs() const override; | |
160 | |
161 RTC_DEPRECATED int32_t PlayoutTimestamp(uint32_t* timestamp) override; | |
162 | |
163 rtc::Optional<uint32_t> PlayoutTimestamp() override; | |
164 | |
165 // Get 10 milliseconds of raw audio data to play out, and | |
166 // automatic resample to the requested frequency if > 0. | |
167 int PlayoutData10Ms(int desired_freq_hz, | |
168 AudioFrame* audio_frame, | |
169 bool* muted) override; | |
170 int PlayoutData10Ms(int desired_freq_hz, AudioFrame* audio_frame) override; | |
171 | |
172 ///////////////////////////////////////// | |
173 // Statistics | |
174 // | |
175 | |
176 int GetNetworkStatistics(NetworkStatistics* statistics) override; | |
177 | |
178 int SetOpusApplication(OpusApplicationMode application) override; | |
179 | |
180 // If current send codec is Opus, informs it about the maximum playback rate | |
181 // the receiver will render. | |
182 int SetOpusMaxPlaybackRate(int frequency_hz) override; | |
183 | |
184 int EnableOpusDtx() override; | |
185 | |
186 int DisableOpusDtx() override; | |
187 | |
188 int UnregisterReceiveCodec(uint8_t payload_type) override; | |
189 | |
190 int EnableNack(size_t max_nack_list_size) override; | |
191 | |
192 void DisableNack() override; | |
193 | |
194 std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const override; | |
195 | |
196 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const override; | |
197 | |
198 private: | |
199 struct InputData { | |
200 uint32_t input_timestamp; | |
201 const int16_t* audio; | |
202 size_t length_per_channel; | |
203 size_t audio_channel; | |
204 // If a re-mix is required (up or down), this buffer will store a re-mixed | |
205 // version of the input. | |
206 int16_t buffer[WEBRTC_10MS_PCM_AUDIO]; | |
207 }; | |
208 | |
209 // This member class writes values to the named UMA histogram, but only if | |
210 // the value has changed since the last time (and always for the first call). | |
211 class ChangeLogger { | |
212 public: | |
213 explicit ChangeLogger(const std::string& histogram_name) | |
214 : histogram_name_(histogram_name) {} | |
215 // Logs the new value if it is different from the last logged value, or if | |
216 // this is the first call. | |
217 void MaybeLog(int value); | |
218 | |
219 private: | |
220 int last_value_ = 0; | |
221 int first_time_ = true; | |
222 const std::string histogram_name_; | |
223 }; | |
224 | |
225 int RegisterReceiveCodecUnlocked( | |
226 const CodecInst& codec, | |
227 FunctionView<std::unique_ptr<AudioDecoder>()> isac_factory) | |
228 EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); | |
229 | |
230 int Add10MsDataInternal(const AudioFrame& audio_frame, InputData* input_data) | |
231 EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); | |
232 int Encode(const InputData& input_data) | |
233 EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); | |
234 | |
235 int InitializeReceiverSafe() EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); | |
236 | |
237 bool HaveValidEncoder(const char* caller_name) const | |
238 EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); | |
239 | |
240 // Preprocessing of input audio, including resampling and down-mixing if | |
241 // required, before pushing audio into encoder's buffer. | |
242 // | |
243 // in_frame: input audio-frame | |
244 // ptr_out: pointer to output audio_frame. If no preprocessing is required | |
245 // |ptr_out| will be pointing to |in_frame|, otherwise pointing to | |
246 // |preprocess_frame_|. | |
247 // | |
248 // Return value: | |
249 // -1: if encountering an error. | |
250 // 0: otherwise. | |
251 int PreprocessToAddData(const AudioFrame& in_frame, | |
252 const AudioFrame** ptr_out) | |
253 EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); | |
254 | |
255 // Change required states after starting to receive the codec corresponding | |
256 // to |index|. | |
257 int UpdateUponReceivingCodec(int index); | |
258 | |
259 rtc::CriticalSection acm_crit_sect_; | |
260 rtc::Buffer encode_buffer_ GUARDED_BY(acm_crit_sect_); | |
261 int id_; // TODO(henrik.lundin) Make const. | |
262 uint32_t expected_codec_ts_ GUARDED_BY(acm_crit_sect_); | |
263 uint32_t expected_in_ts_ GUARDED_BY(acm_crit_sect_); | |
264 ACMResampler resampler_ GUARDED_BY(acm_crit_sect_); | |
265 AcmReceiver receiver_; // AcmReceiver has it's own internal lock. | |
266 ChangeLogger bitrate_logger_ GUARDED_BY(acm_crit_sect_); | |
267 | |
268 std::unique_ptr<EncoderFactory> encoder_factory_ GUARDED_BY(acm_crit_sect_); | |
269 | |
270 // Current encoder stack, either obtained from | |
271 // encoder_factory_->rent_a_codec.RentEncoderStack or provided by a call to | |
272 // RegisterEncoder. | |
273 std::unique_ptr<AudioEncoder> encoder_stack_ GUARDED_BY(acm_crit_sect_); | |
274 | |
275 std::unique_ptr<AudioDecoder> isac_decoder_16k_ GUARDED_BY(acm_crit_sect_); | |
276 std::unique_ptr<AudioDecoder> isac_decoder_32k_ GUARDED_BY(acm_crit_sect_); | |
277 | |
278 // This is to keep track of CN instances where we can send DTMFs. | |
279 uint8_t previous_pltype_ GUARDED_BY(acm_crit_sect_); | |
280 | |
281 // Used when payloads are pushed into ACM without any RTP info | |
282 // One example is when pre-encoded bit-stream is pushed from | |
283 // a file. | |
284 // IMPORTANT: this variable is only used in IncomingPayload(), therefore, | |
285 // no lock acquired when interacting with this variable. If it is going to | |
286 // be used in other methods, locks need to be taken. | |
287 std::unique_ptr<WebRtcRTPHeader> aux_rtp_header_; | |
288 | |
289 bool receiver_initialized_ GUARDED_BY(acm_crit_sect_); | |
290 | |
291 AudioFrame preprocess_frame_ GUARDED_BY(acm_crit_sect_); | |
292 bool first_10ms_data_ GUARDED_BY(acm_crit_sect_); | |
293 | |
294 bool first_frame_ GUARDED_BY(acm_crit_sect_); | |
295 uint32_t last_timestamp_ GUARDED_BY(acm_crit_sect_); | |
296 uint32_t last_rtp_timestamp_ GUARDED_BY(acm_crit_sect_); | |
297 | |
298 rtc::CriticalSection callback_crit_sect_; | |
299 AudioPacketizationCallback* packetization_callback_ | |
300 GUARDED_BY(callback_crit_sect_); | |
301 ACMVADCallback* vad_callback_ GUARDED_BY(callback_crit_sect_); | |
302 | |
303 int codec_histogram_bins_log_[static_cast<size_t>( | |
304 AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes)]; | |
305 int number_of_consecutive_empty_packets_; | |
306 }; | |
307 | |
308 } // namespace acm2 | |
309 } // namespace webrtc | |
310 | |
311 #endif // WEBRTC_MODULES_AUDIO_CODING_ACM2_AUDIO_CODING_MODULE_IMPL_H_ | |
OLD | NEW |