blob: b53aa1889c0a58e0e2ae0acb59c61a28c5ee3bb2 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org79af7342012-01-31 12:22:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000011#include "webrtc/voice_engine/voe_base_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
turaj@webrtc.org03f33702013-11-13 00:02:48 +000013#include "webrtc/common.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000014#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
15#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
16#include "webrtc/modules/audio_device/audio_device_impl.h"
17#include "webrtc/modules/audio_processing/include/audio_processing.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/file_wrapper.h"
Jelena Marusic9e5e4212015-04-13 13:41:56 +020020#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000021#include "webrtc/voice_engine/channel.h"
22#include "webrtc/voice_engine/include/voe_errors.h"
23#include "webrtc/voice_engine/output_mixer.h"
24#include "webrtc/voice_engine/transmit_mixer.h"
25#include "webrtc/voice_engine/utility.h"
26#include "webrtc/voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
Jelena Marusic2dd6a272015-04-14 09:47:00 +020028namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000029
Jelena Marusic2dd6a272015-04-14 09:47:00 +020030VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine) {
31 if (nullptr == voiceEngine) {
32 return nullptr;
33 }
34 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
35 s->AddRef();
36 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
Jelena Marusic2dd6a272015-04-14 09:47:00 +020039VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared)
40 : voiceEngineObserverPtr_(nullptr),
41 callbackCritSect_(*CriticalSectionWrapper::CreateCriticalSection()),
42 shared_(shared) {}
43
44VoEBaseImpl::~VoEBaseImpl() {
45 TerminateInternal();
46 delete &callbackCritSect_;
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
Jelena Marusic2dd6a272015-04-14 09:47:00 +020049void VoEBaseImpl::OnErrorIsReported(ErrorCode error) {
50 CriticalSectionScoped cs(&callbackCritSect_);
51 int errCode = 0;
52 if (error == AudioDeviceObserver::kRecordingError) {
53 errCode = VE_RUNTIME_REC_ERROR;
54 LOG_F(LS_ERROR) << "VE_RUNTIME_REC_ERROR";
55 } else if (error == AudioDeviceObserver::kPlayoutError) {
56 errCode = VE_RUNTIME_PLAY_ERROR;
57 LOG_F(LS_ERROR) << "VE_RUNTIME_PLAY_ERROR";
58 }
59 if (voiceEngineObserverPtr_) {
60 // Deliver callback (-1 <=> no channel dependency)
61 voiceEngineObserverPtr_->CallbackOnError(-1, errCode);
62 }
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
Jelena Marusic2dd6a272015-04-14 09:47:00 +020065void VoEBaseImpl::OnWarningIsReported(WarningCode warning) {
66 CriticalSectionScoped cs(&callbackCritSect_);
67 int warningCode = 0;
68 if (warning == AudioDeviceObserver::kRecordingWarning) {
69 warningCode = VE_RUNTIME_REC_WARNING;
70 LOG_F(LS_WARNING) << "VE_RUNTIME_REC_WARNING";
71 } else if (warning == AudioDeviceObserver::kPlayoutWarning) {
72 warningCode = VE_RUNTIME_PLAY_WARNING;
73 LOG_F(LS_WARNING) << "VE_RUNTIME_PLAY_WARNING";
74 }
75 if (voiceEngineObserverPtr_) {
76 // Deliver callback (-1 <=> no channel dependency)
77 voiceEngineObserverPtr_->CallbackOnError(-1, warningCode);
78 }
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
pbos@webrtc.org6141e132013-04-09 10:09:10 +000081int32_t VoEBaseImpl::RecordedDataIsAvailable(
Jelena Marusic2dd6a272015-04-14 09:47:00 +020082 const void* audioSamples, uint32_t nSamples, uint8_t nBytesPerSample,
83 uint8_t nChannels, uint32_t samplesPerSec, uint32_t totalDelayMS,
84 int32_t clockDrift, uint32_t micLevel, bool keyPressed,
85 uint32_t& newMicLevel) {
86 newMicLevel = static_cast<uint32_t>(ProcessRecordedDataWithAPM(
87 nullptr, 0, audioSamples, samplesPerSec, nChannels, nSamples,
88 totalDelayMS, clockDrift, micLevel, keyPressed));
89 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
Jelena Marusic2dd6a272015-04-14 09:47:00 +020092int32_t VoEBaseImpl::NeedMorePlayData(uint32_t nSamples,
93 uint8_t nBytesPerSample,
94 uint8_t nChannels, uint32_t samplesPerSec,
95 void* audioSamples, uint32_t& nSamplesOut,
96 int64_t* elapsed_time_ms,
97 int64_t* ntp_time_ms) {
98 GetPlayoutData(static_cast<int>(samplesPerSec), static_cast<int>(nChannels),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +000099 static_cast<int>(nSamples), true, audioSamples,
wu@webrtc.org94454b72014-06-05 20:34:08 +0000100 elapsed_time_ms, ntp_time_ms);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200101 nSamplesOut = audioFrame_.samples_per_channel_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000102 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103}
104
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000105int VoEBaseImpl::OnDataAvailable(const int voe_channels[],
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000106 int number_of_voe_channels,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200107 const int16_t* audio_data, int sample_rate,
108 int number_of_channels, int number_of_frames,
109 int audio_delay_milliseconds, int volume,
110 bool key_pressed, bool need_audio_processing) {
111 if (number_of_voe_channels == 0) return 0;
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000112
113 if (need_audio_processing) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000114 return ProcessRecordedDataWithAPM(
115 voe_channels, number_of_voe_channels, audio_data, sample_rate,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200116 number_of_channels, number_of_frames, audio_delay_milliseconds, 0,
117 volume, key_pressed);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000118 }
119
120 // No need to go through the APM, demultiplex the data to each VoE channel,
121 // encode and send to the network.
122 for (int i = 0; i < number_of_voe_channels; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000123 // TODO(ajm): In the case where multiple channels are using the same codec
124 // rate, this path needlessly does extra conversions. We should convert once
125 // and share between channels.
xians@webrtc.org56925312014-04-14 10:50:37 +0000126 PushCaptureData(voe_channels[i], audio_data, 16, sample_rate,
127 number_of_channels, number_of_frames);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000128 }
129
130 // Return 0 to indicate no need to change the volume.
131 return 0;
132}
133
xians@webrtc.orgc1e28032014-02-02 15:30:20 +0000134void VoEBaseImpl::OnData(int voe_channel, const void* audio_data,
135 int bits_per_sample, int sample_rate,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200136 int number_of_channels, int number_of_frames) {
xians@webrtc.org56925312014-04-14 10:50:37 +0000137 PushCaptureData(voe_channel, audio_data, bits_per_sample, sample_rate,
138 number_of_channels, number_of_frames);
139}
140
141void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data,
142 int bits_per_sample, int sample_rate,
143 int number_of_channels,
144 int number_of_frames) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200145 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel);
xians@webrtc.org07e51962014-01-29 13:54:02 +0000146 voe::Channel* channel_ptr = ch.channel();
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200147 if (!channel_ptr) return;
xians@webrtc.org07e51962014-01-29 13:54:02 +0000148
henrika@webrtc.org66803482014-04-17 10:45:01 +0000149 if (channel_ptr->Sending()) {
xians@webrtc.org07e51962014-01-29 13:54:02 +0000150 channel_ptr->Demultiplex(static_cast<const int16_t*>(audio_data),
151 sample_rate, number_of_frames, number_of_channels);
152 channel_ptr->PrepareEncodeAndSend(sample_rate);
153 channel_ptr->EncodeAndSend();
154 }
155}
156
xians@webrtc.org56925312014-04-14 10:50:37 +0000157void VoEBaseImpl::PullRenderData(int bits_per_sample, int sample_rate,
158 int number_of_channels, int number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200159 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000160 int64_t* ntp_time_ms) {
Jelena Marusic6fc2d2f2015-04-13 14:07:04 +0200161 assert(bits_per_sample == 16);
162 assert(number_of_frames == static_cast<int>(sample_rate / 100));
xians@webrtc.org56925312014-04-14 10:50:37 +0000163
164 GetPlayoutData(sample_rate, number_of_channels, number_of_frames, false,
wu@webrtc.org94454b72014-06-05 20:34:08 +0000165 audio_data, elapsed_time_ms, ntp_time_ms);
xians@webrtc.org56925312014-04-14 10:50:37 +0000166}
167
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200168int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
169 CriticalSectionScoped cs(&callbackCritSect_);
170 if (voiceEngineObserverPtr_) {
171 shared_->SetLastError(
172 VE_INVALID_OPERATION, kTraceError,
173 "RegisterVoiceEngineObserver() observer already enabled");
174 return -1;
175 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200177 // Register the observer in all active channels
178 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
179 it.IsValid(); it.Increment()) {
180 it.GetChannel()->RegisterVoiceEngineObserver(observer);
181 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000182
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200183 shared_->transmit_mixer()->RegisterVoiceEngineObserver(observer);
184 voiceEngineObserverPtr_ = &observer;
185 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200188int VoEBaseImpl::DeRegisterVoiceEngineObserver() {
189 CriticalSectionScoped cs(&callbackCritSect_);
190 if (!voiceEngineObserverPtr_) {
191 shared_->SetLastError(
192 VE_INVALID_OPERATION, kTraceError,
193 "DeRegisterVoiceEngineObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200195 }
196 voiceEngineObserverPtr_ = nullptr;
197
198 // Deregister the observer in all active channels
199 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
200 it.IsValid(); it.Increment()) {
201 it.GetChannel()->DeRegisterVoiceEngineObserver();
202 }
203
204 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000207int VoEBaseImpl::Init(AudioDeviceModule* external_adm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200208 AudioProcessing* audioproc) {
209 CriticalSectionScoped cs(shared_->crit_sec());
210 WebRtcSpl_Init();
211 if (shared_->statistics().Initialized()) {
212 return 0;
213 }
214 if (shared_->process_thread()) {
215 shared_->process_thread()->Start();
216 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200218 // Create an internal ADM if the user has not added an external
219 // ADM implementation as input to Init().
220 if (external_adm == nullptr) {
221 // Create the internal ADM implementation.
222 shared_->set_audio_device(AudioDeviceModuleImpl::Create(
223 VoEId(shared_->instance_id(), -1), shared_->audio_device_layer()));
kma@webrtc.org0221b782012-09-08 00:09:26 +0000224
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200225 if (shared_->audio_device() == nullptr) {
226 shared_->SetLastError(VE_NO_MEMORY, kTraceCritical,
227 "Init() failed to create the ADM");
228 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200230 } else {
231 // Use the already existing external ADM implementation.
232 shared_->set_audio_device(external_adm);
233 LOG_F(LS_INFO)
234 << "An external ADM implementation will be used in VoiceEngine";
235 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200237 // Register the ADM to the process thread, which will drive the error
238 // callback mechanism
239 if (shared_->process_thread()) {
240 shared_->process_thread()->RegisterModule(shared_->audio_device());
241 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200243 bool available = false;
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000244
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200245 // --------------------
246 // Reinitialize the ADM
henrika@google.com73d65512011-09-07 15:11:18 +0000247
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200248 // Register the AudioObserver implementation
249 if (shared_->audio_device()->RegisterEventObserver(this) != 0) {
250 shared_->SetLastError(
251 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
252 "Init() failed to register event observer for the ADM");
253 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200255 // Register the AudioTransport implementation
256 if (shared_->audio_device()->RegisterAudioCallback(this) != 0) {
257 shared_->SetLastError(
258 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
259 "Init() failed to register audio callback for the ADM");
260 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200262 // ADM initialization
263 if (shared_->audio_device()->Init() != 0) {
264 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
265 "Init() failed to initialize the ADM");
266 return -1;
267 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200269 // Initialize the default speaker
270 if (shared_->audio_device()->SetPlayoutDevice(
271 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
272 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo,
273 "Init() failed to set the default output device");
274 }
275 if (shared_->audio_device()->InitSpeaker() != 0) {
276 shared_->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
277 "Init() failed to initialize the speaker");
278 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000279
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200280 // Initialize the default microphone
281 if (shared_->audio_device()->SetRecordingDevice(
282 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
283 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo,
284 "Init() failed to set the default input device");
285 }
286 if (shared_->audio_device()->InitMicrophone() != 0) {
287 shared_->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
288 "Init() failed to initialize the microphone");
289 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200291 // Set number of channels
292 if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
293 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
294 "Init() failed to query stereo playout mode");
295 }
296 if (shared_->audio_device()->SetStereoPlayout(available) != 0) {
297 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
298 "Init() failed to set mono/stereo playout mode");
299 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200301 // TODO(andrew): These functions don't tell us whether stereo recording
302 // is truly available. We simply set the AudioProcessing input to stereo
303 // here, because we have to wait until receiving the first frame to
304 // determine the actual number of channels anyway.
305 //
306 // These functions may be changed; tracked here:
307 // http://code.google.com/p/webrtc/issues/detail?id=204
308 shared_->audio_device()->StereoRecordingIsAvailable(&available);
309 if (shared_->audio_device()->SetStereoRecording(available) != 0) {
310 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
311 "Init() failed to set mono/stereo recording mode");
312 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000313
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200314 if (!audioproc) {
315 audioproc = AudioProcessing::Create();
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000316 if (!audioproc) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200317 LOG(LS_ERROR) << "Failed to create AudioProcessing.";
318 shared_->SetLastError(VE_NO_MEMORY);
319 return -1;
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000320 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200321 }
322 shared_->set_audio_processing(audioproc);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000323
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200324 // Set the error state for any failures in this block.
325 shared_->SetLastError(VE_APM_ERROR);
326 // Configure AudioProcessing components.
327 if (audioproc->high_pass_filter()->Enable(true) != 0) {
328 LOG_FERR1(LS_ERROR, high_pass_filter()->Enable, true);
329 return -1;
330 }
331 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
332 LOG_FERR1(LS_ERROR, enable_drift_compensation, false);
333 return -1;
334 }
335 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
336 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
337 return -1;
338 }
339 GainControl* agc = audioproc->gain_control();
340 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
341 LOG_FERR2(LS_ERROR, agc->set_analog_level_limits, kMinVolumeLevel,
342 kMaxVolumeLevel);
343 return -1;
344 }
345 if (agc->set_mode(kDefaultAgcMode) != 0) {
346 LOG_FERR1(LS_ERROR, agc->set_mode, kDefaultAgcMode);
347 return -1;
348 }
349 if (agc->Enable(kDefaultAgcState) != 0) {
350 LOG_FERR1(LS_ERROR, agc->Enable, kDefaultAgcState);
351 return -1;
352 }
353 shared_->SetLastError(0); // Clear error state.
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000354
niklase@google.com470e71d2011-07-07 08:21:25 +0000355#ifdef WEBRTC_VOICE_ENGINE_AGC
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200356 bool agc_enabled =
357 agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled();
358 if (shared_->audio_device()->SetAGC(agc_enabled) != 0) {
359 LOG_FERR1(LS_ERROR, audio_device()->SetAGC, agc_enabled);
360 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
361 // TODO(ajm): No error return here due to
362 // https://code.google.com/p/webrtc/issues/detail?id=1464
363 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000364#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200366 return shared_->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000367}
368
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200369int VoEBaseImpl::Terminate() {
370 CriticalSectionScoped cs(shared_->crit_sec());
371 return TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000372}
373
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000374int VoEBaseImpl::CreateChannel() {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200375 CriticalSectionScoped cs(shared_->crit_sec());
376 if (!shared_->statistics().Initialized()) {
377 shared_->SetLastError(VE_NOT_INITED, kTraceError);
378 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000379 }
380
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200381 voe::ChannelOwner channel_owner = shared_->channel_manager().CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000382 return InitializeChannel(&channel_owner);
383}
384
385int VoEBaseImpl::CreateChannel(const Config& config) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200386 CriticalSectionScoped cs(shared_->crit_sec());
387 if (!shared_->statistics().Initialized()) {
388 shared_->SetLastError(VE_NOT_INITED, kTraceError);
389 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000390 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200391 voe::ChannelOwner channel_owner =
392 shared_->channel_manager().CreateChannel(config);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000393 return InitializeChannel(&channel_owner);
394}
395
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200396int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) {
397 if (channel_owner->channel()->SetEngineInformation(
398 shared_->statistics(), *shared_->output_mixer(),
399 *shared_->transmit_mixer(), *shared_->process_thread(),
400 *shared_->audio_device(), voiceEngineObserverPtr_,
401 &callbackCritSect_) != 0) {
402 shared_->SetLastError(
403 VE_CHANNEL_NOT_CREATED, kTraceError,
404 "CreateChannel() failed to associate engine and channel."
405 " Destroying channel.");
406 shared_->channel_manager().DestroyChannel(
407 channel_owner->channel()->ChannelId());
408 return -1;
409 } else if (channel_owner->channel()->Init() != 0) {
410 shared_->SetLastError(
411 VE_CHANNEL_NOT_CREATED, kTraceError,
412 "CreateChannel() failed to initialize channel. Destroying"
413 " channel.");
414 shared_->channel_manager().DestroyChannel(
415 channel_owner->channel()->ChannelId());
416 return -1;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200419 return channel_owner->channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000420}
421
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200422int VoEBaseImpl::DeleteChannel(int channel) {
423 CriticalSectionScoped cs(shared_->crit_sec());
424 if (!shared_->statistics().Initialized()) {
425 shared_->SetLastError(VE_NOT_INITED, kTraceError);
426 return -1;
427 }
428
429 {
430 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
431 voe::Channel* channelPtr = ch.channel();
432 if (channelPtr == nullptr) {
433 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
434 "DeleteChannel() failed to locate channel");
435 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200437 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200439 shared_->channel_manager().DestroyChannel(channel);
440 if (StopSend() != 0) {
441 return -1;
442 }
443 if (StopPlayout() != 0) {
444 return -1;
445 }
446 return 0;
447}
niklase@google.com470e71d2011-07-07 08:21:25 +0000448
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200449int VoEBaseImpl::StartReceive(int channel) {
450 CriticalSectionScoped cs(shared_->crit_sec());
451 if (!shared_->statistics().Initialized()) {
452 shared_->SetLastError(VE_NOT_INITED, kTraceError);
453 return -1;
454 }
455 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
456 voe::Channel* channelPtr = ch.channel();
457 if (channelPtr == nullptr) {
458 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
459 "StartReceive() failed to locate channel");
460 return -1;
461 }
462 return channelPtr->StartReceiving();
463}
niklase@google.com470e71d2011-07-07 08:21:25 +0000464
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200465int VoEBaseImpl::StopReceive(int channel) {
466 CriticalSectionScoped cs(shared_->crit_sec());
467 if (!shared_->statistics().Initialized()) {
468 shared_->SetLastError(VE_NOT_INITED, kTraceError);
469 return -1;
470 }
471 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
472 voe::Channel* channelPtr = ch.channel();
473 if (channelPtr == nullptr) {
474 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
475 "SetLocalReceiver() failed to locate channel");
476 return -1;
477 }
478 return channelPtr->StopReceiving();
479}
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200481int VoEBaseImpl::StartPlayout(int channel) {
482 CriticalSectionScoped cs(shared_->crit_sec());
483 if (!shared_->statistics().Initialized()) {
484 shared_->SetLastError(VE_NOT_INITED, kTraceError);
485 return -1;
486 }
487 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
488 voe::Channel* channelPtr = ch.channel();
489 if (channelPtr == nullptr) {
490 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
491 "StartPlayout() failed to locate channel");
492 return -1;
493 }
494 if (channelPtr->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200496 }
497 if (StartPlayout() != 0) {
498 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
499 "StartPlayout() failed to start playout");
500 return -1;
501 }
502 return channelPtr->StartPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200505int VoEBaseImpl::StopPlayout(int channel) {
506 CriticalSectionScoped cs(shared_->crit_sec());
507 if (!shared_->statistics().Initialized()) {
508 shared_->SetLastError(VE_NOT_INITED, kTraceError);
509 return -1;
510 }
511 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
512 voe::Channel* channelPtr = ch.channel();
513 if (channelPtr == nullptr) {
514 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
515 "StopPlayout() failed to locate channel");
516 return -1;
517 }
518 if (channelPtr->StopPlayout() != 0) {
519 LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel "
520 << channel;
521 }
522 return StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000523}
524
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200525int VoEBaseImpl::StartSend(int channel) {
526 CriticalSectionScoped cs(shared_->crit_sec());
527 if (!shared_->statistics().Initialized()) {
528 shared_->SetLastError(VE_NOT_INITED, kTraceError);
529 return -1;
530 }
531 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
532 voe::Channel* channelPtr = ch.channel();
533 if (channelPtr == nullptr) {
534 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
535 "StartSend() failed to locate channel");
536 return -1;
537 }
538 if (channelPtr->Sending()) {
539 return 0;
540 }
541 if (StartSend() != 0) {
542 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
543 "StartSend() failed to start recording");
544 return -1;
545 }
546 return channelPtr->StartSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000547}
548
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200549int VoEBaseImpl::StopSend(int channel) {
550 CriticalSectionScoped cs(shared_->crit_sec());
551 if (!shared_->statistics().Initialized()) {
552 shared_->SetLastError(VE_NOT_INITED, kTraceError);
553 return -1;
554 }
555 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
556 voe::Channel* channelPtr = ch.channel();
557 if (channelPtr == nullptr) {
558 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
559 "StopSend() failed to locate channel");
560 return -1;
561 }
562 if (channelPtr->StopSend() != 0) {
563 LOG_F(LS_WARNING) << "StopSend() failed to stop sending for channel "
564 << channel;
565 }
566 return StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000567}
568
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200569int VoEBaseImpl::GetVersion(char version[1024]) {
570 assert(kVoiceEngineVersionMaxMessageSize == 1024);
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200572 if (version == nullptr) {
573 shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
574 return (-1);
575 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200577 char versionBuf[kVoiceEngineVersionMaxMessageSize];
578 char* versionPtr = versionBuf;
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200580 int32_t len = 0;
581 int32_t accLen = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000582
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200583 len = AddVoEVersion(versionPtr);
584 if (len == -1) {
585 return -1;
586 }
587 versionPtr += len;
588 accLen += len;
589 assert(accLen < kVoiceEngineVersionMaxMessageSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000591#ifdef WEBRTC_EXTERNAL_TRANSPORT
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200592 len = AddExternalTransportBuild(versionPtr);
593 if (len == -1) {
594 return -1;
595 }
596 versionPtr += len;
597 accLen += len;
598 assert(accLen < kVoiceEngineVersionMaxMessageSize);
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000599#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000600
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200601 memcpy(version, versionBuf, accLen);
602 version[accLen] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +0000603
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200604 // to avoid the truncation in the trace, split the string into parts
605 char partOfVersion[256];
606 for (int partStart = 0; partStart < accLen;) {
607 memset(partOfVersion, 0, sizeof(partOfVersion));
608 int partEnd = partStart + 180;
609 while (version[partEnd] != '\n' && version[partEnd] != '\0') {
610 partEnd--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200612 if (partEnd < accLen) {
613 memcpy(partOfVersion, &version[partStart], partEnd - partStart);
614 } else {
615 memcpy(partOfVersion, &version[partStart], accLen - partStart);
616 }
617 partStart = partEnd;
618 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000619
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200620 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000621}
622
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200623int32_t VoEBaseImpl::AddVoEVersion(char* str) const {
624 return sprintf(str, "VoiceEngine 4.1.0\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000625}
626
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000627#ifdef WEBRTC_EXTERNAL_TRANSPORT
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200628int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const {
629 return sprintf(str, "External transport build\n");
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000630}
631#endif
632
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200633int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000634
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200635int32_t VoEBaseImpl::StartPlayout() {
636 if (shared_->audio_device()->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000637 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200638 }
639 if (!shared_->ext_playout()) {
640 if (shared_->audio_device()->InitPlayout() != 0) {
641 LOG_F(LS_ERROR) << "Failed to initialize playout";
642 return -1;
643 }
644 if (shared_->audio_device()->StartPlayout() != 0) {
645 LOG_F(LS_ERROR) << "Failed to start playout";
646 return -1;
647 }
648 }
649 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000650}
651
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000652int32_t VoEBaseImpl::StopPlayout() {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000653 // Stop audio-device playing if no channel is playing out
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200654 if (shared_->NumOfPlayingChannels() == 0) {
655 if (shared_->audio_device()->StopPlayout() != 0) {
656 shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError,
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000657 "StopPlayout() failed to stop playout");
658 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000659 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000660 }
661 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662}
663
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200664int32_t VoEBaseImpl::StartSend() {
665 if (shared_->audio_device()->Recording()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000666 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200667 }
668 if (!shared_->ext_recording()) {
669 if (shared_->audio_device()->InitRecording() != 0) {
670 LOG_F(LS_ERROR) << "Failed to initialize recording";
671 return -1;
672 }
673 if (shared_->audio_device()->StartRecording() != 0) {
674 LOG_F(LS_ERROR) << "Failed to start recording";
675 return -1;
676 }
677 }
678
679 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000680}
681
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200682int32_t VoEBaseImpl::StopSend() {
683 if (shared_->NumOfSendingChannels() == 0 &&
684 !shared_->transmit_mixer()->IsRecordingMic()) {
685 // Stop audio-device recording if no channel is recording
686 if (shared_->audio_device()->StopRecording() != 0) {
687 shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
688 "StopSend() failed to stop recording");
689 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200691 shared_->transmit_mixer()->StopSend();
692 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000693
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200694 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000695}
696
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200697int32_t VoEBaseImpl::TerminateInternal() {
698 // Delete any remaining channel objects
699 shared_->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200701 if (shared_->process_thread()) {
702 if (shared_->audio_device()) {
703 shared_->process_thread()->DeRegisterModule(shared_->audio_device());
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200705 shared_->process_thread()->Stop();
706 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000707
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200708 if (shared_->audio_device()) {
709 if (shared_->audio_device()->StopPlayout() != 0) {
710 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
711 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200713 if (shared_->audio_device()->StopRecording() != 0) {
714 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
715 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000716 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200717 if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) {
718 shared_->SetLastError(
719 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
720 "TerminateInternal() failed to de-register event observer "
721 "for the ADM");
722 }
723 if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) {
724 shared_->SetLastError(
725 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
726 "TerminateInternal() failed to de-register audio callback "
727 "for the ADM");
728 }
729 if (shared_->audio_device()->Terminate() != 0) {
730 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
731 "TerminateInternal() failed to terminate the ADM");
732 }
733 shared_->set_audio_device(nullptr);
734 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200736 if (shared_->audio_processing()) {
737 shared_->set_audio_processing(nullptr);
738 }
739
740 return shared_->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000741}
742
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000743int VoEBaseImpl::ProcessRecordedDataWithAPM(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200744 const int voe_channels[], int number_of_voe_channels,
745 const void* audio_data, uint32_t sample_rate, uint8_t number_of_channels,
746 uint32_t number_of_frames, uint32_t audio_delay_milliseconds,
747 int32_t clock_drift, uint32_t volume, bool key_pressed) {
748 assert(shared_->transmit_mixer() != nullptr);
749 assert(shared_->audio_device() != nullptr);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000750
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000751 uint32_t max_volume = 0;
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000752 uint16_t voe_mic_level = 0;
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +0000753 // Check for zero to skip this calculation; the consumer may use this to
754 // indicate no volume is available.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000755 if (volume != 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000756 // Scale from ADM to VoE level range
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200757 if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000758 if (max_volume) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000759 voe_mic_level = static_cast<uint16_t>(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200760 (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) /
761 max_volume);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000762 }
763 }
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000764 // We learned that on certain systems (e.g Linux) the voe_mic_level
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000765 // can be greater than the maxVolumeLevel therefore
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000766 // we are going to cap the voe_mic_level to the maxVolumeLevel
767 // and change the maxVolume to volume if it turns out that
768 // the voe_mic_level is indeed greater than the maxVolumeLevel.
769 if (voe_mic_level > kMaxVolumeLevel) {
770 voe_mic_level = kMaxVolumeLevel;
771 max_volume = volume;
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000772 }
773 }
774
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000775 // Perform channel-independent operations
776 // (APM, mix with file, record to file, mute, etc.)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200777 shared_->transmit_mixer()->PrepareDemux(
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000778 audio_data, number_of_frames, number_of_channels, sample_rate,
779 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000780 voe_mic_level, key_pressed);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000781
782 // Copy the audio frame to each sending channel and perform
783 // channel-dependent operations (file mixing, mute, etc.), encode and
784 // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0,
785 // do the operations on all the existing VoE channels; otherwise the
786 // operations will be done on specific channels.
787 if (number_of_voe_channels == 0) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200788 shared_->transmit_mixer()->DemuxAndMix();
789 shared_->transmit_mixer()->EncodeAndSend();
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000790 } else {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200791 shared_->transmit_mixer()->DemuxAndMix(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000792 number_of_voe_channels);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200793 shared_->transmit_mixer()->EncodeAndSend(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000794 number_of_voe_channels);
795 }
796
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000797 // Scale from VoE to ADM level range.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200798 uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel();
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000799 if (new_voe_mic_level != voe_mic_level) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000800 // Return the new volume if AGC has changed the volume.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200801 return static_cast<int>((new_voe_mic_level * max_volume +
802 static_cast<int>(kMaxVolumeLevel / 2)) /
803 kMaxVolumeLevel);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000804 }
805
806 // Return 0 to indicate no change on the volume.
807 return 0;
808}
809
xians@webrtc.org56925312014-04-14 10:50:37 +0000810void VoEBaseImpl::GetPlayoutData(int sample_rate, int number_of_channels,
811 int number_of_frames, bool feed_data_to_apm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200812 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000813 int64_t* ntp_time_ms) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200814 assert(shared_->output_mixer() != nullptr);
xians@webrtc.org56925312014-04-14 10:50:37 +0000815
816 // TODO(andrew): if the device is running in mono, we should tell the mixer
817 // here so that it will only request mono from AudioCodingModule.
818 // Perform mixing of all active participants (channel-based mixing)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200819 shared_->output_mixer()->MixActiveChannels();
xians@webrtc.org56925312014-04-14 10:50:37 +0000820
821 // Additional operations on the combined signal
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200822 shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm);
xians@webrtc.org56925312014-04-14 10:50:37 +0000823
824 // Retrieve the final output mix (resampled to match the ADM)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200825 shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels,
826 &audioFrame_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000827
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200828 assert(number_of_frames == audioFrame_.samples_per_channel_);
829 assert(sample_rate == audioFrame_.sample_rate_hz_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000830
831 // Deliver audio (PCM) samples to the ADM
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200832 memcpy(audio_data, audioFrame_.data_,
xians@webrtc.org56925312014-04-14 10:50:37 +0000833 sizeof(int16_t) * number_of_frames * number_of_channels);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000834
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200835 *elapsed_time_ms = audioFrame_.elapsed_time_ms_;
836 *ntp_time_ms = audioFrame_.ntp_time_ms_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000837}
838
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000839} // namespace webrtc