blob: ab5ebcd12ffc788d4c76ac7c2dae761aa823c42e [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) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200328 LOG_F(LS_ERROR) << "Failed to enable high pass filter.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200329 return -1;
330 }
331 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200332 LOG_F(LS_ERROR) << "Failed to disable drift compensation.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200333 return -1;
334 }
335 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200336 LOG_F(LS_ERROR) << "Failed to set noise suppression level: "
337 << kDefaultNsMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200338 return -1;
339 }
340 GainControl* agc = audioproc->gain_control();
341 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200342 LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: "
343 << kMinVolumeLevel << " and maximum: " << kMaxVolumeLevel;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200344 return -1;
345 }
346 if (agc->set_mode(kDefaultAgcMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200347 LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200348 return -1;
349 }
350 if (agc->Enable(kDefaultAgcState) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200351 LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200352 return -1;
353 }
354 shared_->SetLastError(0); // Clear error state.
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000355
niklase@google.com470e71d2011-07-07 08:21:25 +0000356#ifdef WEBRTC_VOICE_ENGINE_AGC
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200357 bool agc_enabled =
358 agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled();
359 if (shared_->audio_device()->SetAGC(agc_enabled) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200360 LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200361 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
362 // TODO(ajm): No error return here due to
363 // https://code.google.com/p/webrtc/issues/detail?id=1464
364 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000365#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200367 return shared_->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000368}
369
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200370int VoEBaseImpl::Terminate() {
371 CriticalSectionScoped cs(shared_->crit_sec());
372 return TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000373}
374
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000375int VoEBaseImpl::CreateChannel() {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200376 CriticalSectionScoped cs(shared_->crit_sec());
377 if (!shared_->statistics().Initialized()) {
378 shared_->SetLastError(VE_NOT_INITED, kTraceError);
379 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000380 }
381
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200382 voe::ChannelOwner channel_owner = shared_->channel_manager().CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000383 return InitializeChannel(&channel_owner);
384}
385
386int VoEBaseImpl::CreateChannel(const Config& config) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200387 CriticalSectionScoped cs(shared_->crit_sec());
388 if (!shared_->statistics().Initialized()) {
389 shared_->SetLastError(VE_NOT_INITED, kTraceError);
390 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000391 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200392 voe::ChannelOwner channel_owner =
393 shared_->channel_manager().CreateChannel(config);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000394 return InitializeChannel(&channel_owner);
395}
396
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200397int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) {
398 if (channel_owner->channel()->SetEngineInformation(
399 shared_->statistics(), *shared_->output_mixer(),
400 *shared_->transmit_mixer(), *shared_->process_thread(),
401 *shared_->audio_device(), voiceEngineObserverPtr_,
402 &callbackCritSect_) != 0) {
403 shared_->SetLastError(
404 VE_CHANNEL_NOT_CREATED, kTraceError,
405 "CreateChannel() failed to associate engine and channel."
406 " Destroying channel.");
407 shared_->channel_manager().DestroyChannel(
408 channel_owner->channel()->ChannelId());
409 return -1;
410 } else if (channel_owner->channel()->Init() != 0) {
411 shared_->SetLastError(
412 VE_CHANNEL_NOT_CREATED, kTraceError,
413 "CreateChannel() failed to initialize channel. Destroying"
414 " channel.");
415 shared_->channel_manager().DestroyChannel(
416 channel_owner->channel()->ChannelId());
417 return -1;
418 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200420 return channel_owner->channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000421}
422
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200423int VoEBaseImpl::DeleteChannel(int channel) {
424 CriticalSectionScoped cs(shared_->crit_sec());
425 if (!shared_->statistics().Initialized()) {
426 shared_->SetLastError(VE_NOT_INITED, kTraceError);
427 return -1;
428 }
429
430 {
431 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
432 voe::Channel* channelPtr = ch.channel();
433 if (channelPtr == nullptr) {
434 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
435 "DeleteChannel() failed to locate channel");
436 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000437 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200438 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200440 shared_->channel_manager().DestroyChannel(channel);
441 if (StopSend() != 0) {
442 return -1;
443 }
444 if (StopPlayout() != 0) {
445 return -1;
446 }
447 return 0;
448}
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200450int VoEBaseImpl::StartReceive(int channel) {
451 CriticalSectionScoped cs(shared_->crit_sec());
452 if (!shared_->statistics().Initialized()) {
453 shared_->SetLastError(VE_NOT_INITED, kTraceError);
454 return -1;
455 }
456 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
457 voe::Channel* channelPtr = ch.channel();
458 if (channelPtr == nullptr) {
459 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
460 "StartReceive() failed to locate channel");
461 return -1;
462 }
463 return channelPtr->StartReceiving();
464}
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200466int VoEBaseImpl::StopReceive(int channel) {
467 CriticalSectionScoped cs(shared_->crit_sec());
468 if (!shared_->statistics().Initialized()) {
469 shared_->SetLastError(VE_NOT_INITED, kTraceError);
470 return -1;
471 }
472 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
473 voe::Channel* channelPtr = ch.channel();
474 if (channelPtr == nullptr) {
475 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
476 "SetLocalReceiver() failed to locate channel");
477 return -1;
478 }
479 return channelPtr->StopReceiving();
480}
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200482int VoEBaseImpl::StartPlayout(int channel) {
483 CriticalSectionScoped cs(shared_->crit_sec());
484 if (!shared_->statistics().Initialized()) {
485 shared_->SetLastError(VE_NOT_INITED, kTraceError);
486 return -1;
487 }
488 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
489 voe::Channel* channelPtr = ch.channel();
490 if (channelPtr == nullptr) {
491 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
492 "StartPlayout() failed to locate channel");
493 return -1;
494 }
495 if (channelPtr->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000496 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200497 }
498 if (StartPlayout() != 0) {
499 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
500 "StartPlayout() failed to start playout");
501 return -1;
502 }
503 return channelPtr->StartPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000504}
505
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200506int VoEBaseImpl::StopPlayout(int channel) {
507 CriticalSectionScoped cs(shared_->crit_sec());
508 if (!shared_->statistics().Initialized()) {
509 shared_->SetLastError(VE_NOT_INITED, kTraceError);
510 return -1;
511 }
512 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
513 voe::Channel* channelPtr = ch.channel();
514 if (channelPtr == nullptr) {
515 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
516 "StopPlayout() failed to locate channel");
517 return -1;
518 }
519 if (channelPtr->StopPlayout() != 0) {
520 LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel "
521 << channel;
522 }
523 return StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000524}
525
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200526int VoEBaseImpl::StartSend(int channel) {
527 CriticalSectionScoped cs(shared_->crit_sec());
528 if (!shared_->statistics().Initialized()) {
529 shared_->SetLastError(VE_NOT_INITED, kTraceError);
530 return -1;
531 }
532 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
533 voe::Channel* channelPtr = ch.channel();
534 if (channelPtr == nullptr) {
535 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
536 "StartSend() failed to locate channel");
537 return -1;
538 }
539 if (channelPtr->Sending()) {
540 return 0;
541 }
542 if (StartSend() != 0) {
543 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
544 "StartSend() failed to start recording");
545 return -1;
546 }
547 return channelPtr->StartSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000548}
549
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200550int VoEBaseImpl::StopSend(int channel) {
551 CriticalSectionScoped cs(shared_->crit_sec());
552 if (!shared_->statistics().Initialized()) {
553 shared_->SetLastError(VE_NOT_INITED, kTraceError);
554 return -1;
555 }
556 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
557 voe::Channel* channelPtr = ch.channel();
558 if (channelPtr == nullptr) {
559 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
560 "StopSend() failed to locate channel");
561 return -1;
562 }
563 if (channelPtr->StopSend() != 0) {
564 LOG_F(LS_WARNING) << "StopSend() failed to stop sending for channel "
565 << channel;
566 }
567 return StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000568}
569
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200570int VoEBaseImpl::GetVersion(char version[1024]) {
571 assert(kVoiceEngineVersionMaxMessageSize == 1024);
niklase@google.com470e71d2011-07-07 08:21:25 +0000572
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200573 if (version == nullptr) {
574 shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
575 return (-1);
576 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000577
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200578 char versionBuf[kVoiceEngineVersionMaxMessageSize];
579 char* versionPtr = versionBuf;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200581 int32_t len = 0;
582 int32_t accLen = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000583
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200584 len = AddVoEVersion(versionPtr);
585 if (len == -1) {
586 return -1;
587 }
588 versionPtr += len;
589 accLen += len;
590 assert(accLen < kVoiceEngineVersionMaxMessageSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000591
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000592#ifdef WEBRTC_EXTERNAL_TRANSPORT
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200593 len = AddExternalTransportBuild(versionPtr);
594 if (len == -1) {
595 return -1;
596 }
597 versionPtr += len;
598 accLen += len;
599 assert(accLen < kVoiceEngineVersionMaxMessageSize);
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000600#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200602 memcpy(version, versionBuf, accLen);
603 version[accLen] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +0000604
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200605 // to avoid the truncation in the trace, split the string into parts
606 char partOfVersion[256];
607 for (int partStart = 0; partStart < accLen;) {
608 memset(partOfVersion, 0, sizeof(partOfVersion));
609 int partEnd = partStart + 180;
610 while (version[partEnd] != '\n' && version[partEnd] != '\0') {
611 partEnd--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000612 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200613 if (partEnd < accLen) {
614 memcpy(partOfVersion, &version[partStart], partEnd - partStart);
615 } else {
616 memcpy(partOfVersion, &version[partStart], accLen - partStart);
617 }
618 partStart = partEnd;
619 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000620
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200621 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000622}
623
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200624int32_t VoEBaseImpl::AddVoEVersion(char* str) const {
625 return sprintf(str, "VoiceEngine 4.1.0\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000626}
627
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000628#ifdef WEBRTC_EXTERNAL_TRANSPORT
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200629int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const {
630 return sprintf(str, "External transport build\n");
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000631}
632#endif
633
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200634int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000635
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200636int32_t VoEBaseImpl::StartPlayout() {
637 if (shared_->audio_device()->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000638 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200639 }
640 if (!shared_->ext_playout()) {
641 if (shared_->audio_device()->InitPlayout() != 0) {
642 LOG_F(LS_ERROR) << "Failed to initialize playout";
643 return -1;
644 }
645 if (shared_->audio_device()->StartPlayout() != 0) {
646 LOG_F(LS_ERROR) << "Failed to start playout";
647 return -1;
648 }
649 }
650 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000651}
652
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000653int32_t VoEBaseImpl::StopPlayout() {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000654 // Stop audio-device playing if no channel is playing out
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200655 if (shared_->NumOfPlayingChannels() == 0) {
656 if (shared_->audio_device()->StopPlayout() != 0) {
657 shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError,
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000658 "StopPlayout() failed to stop playout");
659 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000660 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000661 }
662 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000663}
664
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200665int32_t VoEBaseImpl::StartSend() {
666 if (shared_->audio_device()->Recording()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000667 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200668 }
669 if (!shared_->ext_recording()) {
670 if (shared_->audio_device()->InitRecording() != 0) {
671 LOG_F(LS_ERROR) << "Failed to initialize recording";
672 return -1;
673 }
674 if (shared_->audio_device()->StartRecording() != 0) {
675 LOG_F(LS_ERROR) << "Failed to start recording";
676 return -1;
677 }
678 }
679
680 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681}
682
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200683int32_t VoEBaseImpl::StopSend() {
684 if (shared_->NumOfSendingChannels() == 0 &&
685 !shared_->transmit_mixer()->IsRecordingMic()) {
686 // Stop audio-device recording if no channel is recording
687 if (shared_->audio_device()->StopRecording() != 0) {
688 shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
689 "StopSend() failed to stop recording");
690 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000691 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200692 shared_->transmit_mixer()->StopSend();
693 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000694
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200695 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200698int32_t VoEBaseImpl::TerminateInternal() {
699 // Delete any remaining channel objects
700 shared_->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200702 if (shared_->process_thread()) {
703 if (shared_->audio_device()) {
704 shared_->process_thread()->DeRegisterModule(shared_->audio_device());
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200706 shared_->process_thread()->Stop();
707 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000708
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200709 if (shared_->audio_device()) {
710 if (shared_->audio_device()->StopPlayout() != 0) {
711 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
712 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200714 if (shared_->audio_device()->StopRecording() != 0) {
715 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
716 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200718 if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) {
719 shared_->SetLastError(
720 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
721 "TerminateInternal() failed to de-register event observer "
722 "for the ADM");
723 }
724 if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) {
725 shared_->SetLastError(
726 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
727 "TerminateInternal() failed to de-register audio callback "
728 "for the ADM");
729 }
730 if (shared_->audio_device()->Terminate() != 0) {
731 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
732 "TerminateInternal() failed to terminate the ADM");
733 }
734 shared_->set_audio_device(nullptr);
735 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000736
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200737 if (shared_->audio_processing()) {
738 shared_->set_audio_processing(nullptr);
739 }
740
741 return shared_->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000744int VoEBaseImpl::ProcessRecordedDataWithAPM(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200745 const int voe_channels[], int number_of_voe_channels,
746 const void* audio_data, uint32_t sample_rate, uint8_t number_of_channels,
747 uint32_t number_of_frames, uint32_t audio_delay_milliseconds,
748 int32_t clock_drift, uint32_t volume, bool key_pressed) {
749 assert(shared_->transmit_mixer() != nullptr);
750 assert(shared_->audio_device() != nullptr);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000751
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000752 uint32_t max_volume = 0;
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000753 uint16_t voe_mic_level = 0;
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +0000754 // Check for zero to skip this calculation; the consumer may use this to
755 // indicate no volume is available.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000756 if (volume != 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000757 // Scale from ADM to VoE level range
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200758 if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000759 if (max_volume) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000760 voe_mic_level = static_cast<uint16_t>(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200761 (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) /
762 max_volume);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000763 }
764 }
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000765 // We learned that on certain systems (e.g Linux) the voe_mic_level
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000766 // can be greater than the maxVolumeLevel therefore
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000767 // we are going to cap the voe_mic_level to the maxVolumeLevel
768 // and change the maxVolume to volume if it turns out that
769 // the voe_mic_level is indeed greater than the maxVolumeLevel.
770 if (voe_mic_level > kMaxVolumeLevel) {
771 voe_mic_level = kMaxVolumeLevel;
772 max_volume = volume;
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000773 }
774 }
775
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000776 // Perform channel-independent operations
777 // (APM, mix with file, record to file, mute, etc.)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200778 shared_->transmit_mixer()->PrepareDemux(
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000779 audio_data, number_of_frames, number_of_channels, sample_rate,
780 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000781 voe_mic_level, key_pressed);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000782
783 // Copy the audio frame to each sending channel and perform
784 // channel-dependent operations (file mixing, mute, etc.), encode and
785 // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0,
786 // do the operations on all the existing VoE channels; otherwise the
787 // operations will be done on specific channels.
788 if (number_of_voe_channels == 0) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200789 shared_->transmit_mixer()->DemuxAndMix();
790 shared_->transmit_mixer()->EncodeAndSend();
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000791 } else {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200792 shared_->transmit_mixer()->DemuxAndMix(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000793 number_of_voe_channels);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200794 shared_->transmit_mixer()->EncodeAndSend(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000795 number_of_voe_channels);
796 }
797
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000798 // Scale from VoE to ADM level range.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200799 uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel();
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000800 if (new_voe_mic_level != voe_mic_level) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000801 // Return the new volume if AGC has changed the volume.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200802 return static_cast<int>((new_voe_mic_level * max_volume +
803 static_cast<int>(kMaxVolumeLevel / 2)) /
804 kMaxVolumeLevel);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000805 }
806
807 // Return 0 to indicate no change on the volume.
808 return 0;
809}
810
xians@webrtc.org56925312014-04-14 10:50:37 +0000811void VoEBaseImpl::GetPlayoutData(int sample_rate, int number_of_channels,
812 int number_of_frames, bool feed_data_to_apm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200813 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000814 int64_t* ntp_time_ms) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200815 assert(shared_->output_mixer() != nullptr);
xians@webrtc.org56925312014-04-14 10:50:37 +0000816
817 // TODO(andrew): if the device is running in mono, we should tell the mixer
818 // here so that it will only request mono from AudioCodingModule.
819 // Perform mixing of all active participants (channel-based mixing)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200820 shared_->output_mixer()->MixActiveChannels();
xians@webrtc.org56925312014-04-14 10:50:37 +0000821
822 // Additional operations on the combined signal
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200823 shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm);
xians@webrtc.org56925312014-04-14 10:50:37 +0000824
825 // Retrieve the final output mix (resampled to match the ADM)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200826 shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels,
827 &audioFrame_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000828
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200829 assert(number_of_frames == audioFrame_.samples_per_channel_);
830 assert(sample_rate == audioFrame_.sample_rate_hz_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000831
832 // Deliver audio (PCM) samples to the ADM
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200833 memcpy(audio_data, audioFrame_.data_,
xians@webrtc.org56925312014-04-14 10:50:37 +0000834 sizeof(int16_t) * number_of_frames * number_of_channels);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000835
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200836 *elapsed_time_ms = audioFrame_.elapsed_time_ms_;
837 *ntp_time_ms = audioFrame_.ntp_time_ms_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000838}
839
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000840} // namespace webrtc