blob: c76e06deede194e4739a79a20466193bc506b57b [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
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000013#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
14#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
15#include "webrtc/modules/audio_device/audio_device_impl.h"
16#include "webrtc/modules/audio_processing/include/audio_processing.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18#include "webrtc/system_wrappers/interface/file_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
20#include "webrtc/voice_engine/channel.h"
21#include "webrtc/voice_engine/include/voe_errors.h"
22#include "webrtc/voice_engine/output_mixer.h"
23#include "webrtc/voice_engine/transmit_mixer.h"
24#include "webrtc/voice_engine/utility.h"
25#include "webrtc/voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27#if (defined(_WIN32) && defined(_DLL) && (_MSC_VER == 1400))
28// Fix for VS 2005 MD/MDd link problem
29#include <stdio.h>
30extern "C"
31 { FILE _iob[3] = { __iob_func()[0], __iob_func()[1], __iob_func()[2]}; }
32#endif
33
34namespace webrtc
35{
36
37VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine)
38{
39 if (NULL == voiceEngine)
40 {
41 return NULL;
42 }
tommi@webrtc.org0989fb72013-02-15 15:07:32 +000043 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000044 s->AddRef();
45 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
tommi@webrtc.org851becd2012-04-04 14:57:19 +000048VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared) :
niklase@google.com470e71d2011-07-07 08:21:25 +000049 _voiceEngineObserverPtr(NULL),
50 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
tommi@webrtc.org851becd2012-04-04 14:57:19 +000051 _voiceEngineObserver(false), _oldVoEMicLevel(0), _oldMicLevel(0),
52 _shared(shared)
niklase@google.com470e71d2011-07-07 08:21:25 +000053{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000054 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000055 "VoEBaseImpl() - ctor");
56}
57
58VoEBaseImpl::~VoEBaseImpl()
59{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000060 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000061 "~VoEBaseImpl() - dtor");
62
63 TerminateInternal();
64
65 delete &_callbackCritSect;
66}
67
pbos@webrtc.org92135212013-05-14 08:31:39 +000068void VoEBaseImpl::OnErrorIsReported(ErrorCode error)
niklase@google.com470e71d2011-07-07 08:21:25 +000069{
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +000070 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000071 if (_voiceEngineObserver)
72 {
73 if (_voiceEngineObserverPtr)
74 {
75 int errCode(0);
76 if (error == AudioDeviceObserver::kRecordingError)
77 {
78 errCode = VE_RUNTIME_REC_ERROR;
tommi@webrtc.org851becd2012-04-04 14:57:19 +000079 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
80 VoEId(_shared->instance_id(), -1),
81 "VoEBaseImpl::OnErrorIsReported() => VE_RUNTIME_REC_ERROR");
niklase@google.com470e71d2011-07-07 08:21:25 +000082 }
83 else if (error == AudioDeviceObserver::kPlayoutError)
84 {
85 errCode = VE_RUNTIME_PLAY_ERROR;
tommi@webrtc.org851becd2012-04-04 14:57:19 +000086 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
87 VoEId(_shared->instance_id(), -1),
88 "VoEBaseImpl::OnErrorIsReported() => "
89 "VE_RUNTIME_PLAY_ERROR");
niklase@google.com470e71d2011-07-07 08:21:25 +000090 }
91 // Deliver callback (-1 <=> no channel dependency)
92 _voiceEngineObserverPtr->CallbackOnError(-1, errCode);
93 }
94 }
95}
96
pbos@webrtc.org92135212013-05-14 08:31:39 +000097void VoEBaseImpl::OnWarningIsReported(WarningCode warning)
niklase@google.com470e71d2011-07-07 08:21:25 +000098{
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +000099 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 if (_voiceEngineObserver)
101 {
102 if (_voiceEngineObserverPtr)
103 {
104 int warningCode(0);
105 if (warning == AudioDeviceObserver::kRecordingWarning)
106 {
107 warningCode = VE_RUNTIME_REC_WARNING;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000108 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
109 VoEId(_shared->instance_id(), -1),
110 "VoEBaseImpl::OnErrorIsReported() => "
111 "VE_RUNTIME_REC_WARNING");
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 }
113 else if (warning == AudioDeviceObserver::kPlayoutWarning)
114 {
115 warningCode = VE_RUNTIME_PLAY_WARNING;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000116 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
117 VoEId(_shared->instance_id(), -1),
118 "VoEBaseImpl::OnErrorIsReported() => "
119 "VE_RUNTIME_PLAY_WARNING");
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 }
121 // Deliver callback (-1 <=> no channel dependency)
122 _voiceEngineObserverPtr->CallbackOnError(-1, warningCode);
123 }
124 }
125}
126
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000127int32_t VoEBaseImpl::RecordedDataIsAvailable(
henrika@webrtc.org907bc552012-03-09 08:59:19 +0000128 const void* audioSamples,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000129 uint32_t nSamples,
130 uint8_t nBytesPerSample,
131 uint8_t nChannels,
132 uint32_t samplesPerSec,
133 uint32_t totalDelayMS,
134 int32_t clockDrift,
135 uint32_t currentMicLevel,
136 bool keyPressed,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000137 uint32_t& newMicLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +0000138{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000139 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 "VoEBaseImpl::RecordedDataIsAvailable(nSamples=%u, "
141 "nBytesPerSample=%u, nChannels=%u, samplesPerSec=%u, "
142 "totalDelayMS=%u, clockDrift=%d, currentMicLevel=%u)",
143 nSamples, nBytesPerSample, nChannels, samplesPerSec,
144 totalDelayMS, clockDrift, currentMicLevel);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000145 newMicLevel = static_cast<uint32_t>(ProcessRecordedDataWithAPM(
146 NULL, 0, audioSamples, samplesPerSec, nChannels, nSamples,
147 totalDelayMS, clockDrift, currentMicLevel, keyPressed));
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
149 return 0;
150}
151
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000152int32_t VoEBaseImpl::NeedMorePlayData(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000153 uint32_t nSamples,
154 uint8_t nBytesPerSample,
155 uint8_t nChannels,
156 uint32_t samplesPerSec,
henrika@webrtc.org907bc552012-03-09 08:59:19 +0000157 void* audioSamples,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000158 uint32_t& nSamplesOut)
niklase@google.com470e71d2011-07-07 08:21:25 +0000159{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000160 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 "VoEBaseImpl::NeedMorePlayData(nSamples=%u, "
162 "nBytesPerSample=%d, nChannels=%d, samplesPerSec=%u)",
163 nSamples, nBytesPerSample, nChannels, samplesPerSec);
164
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000165 assert(_shared->output_mixer() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000167 // TODO(andrew): if the device is running in mono, we should tell the mixer
168 // here so that it will only request mono from AudioCodingModule.
niklase@google.com470e71d2011-07-07 08:21:25 +0000169 // Perform mixing of all active participants (channel-based mixing)
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000170 _shared->output_mixer()->MixActiveChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
172 // Additional operations on the combined signal
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000173 _shared->output_mixer()->DoOperationsOnCombinedSignal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
175 // Retrieve the final output mix (resampled to match the ADM)
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000176 _shared->output_mixer()->GetMixedAudio(samplesPerSec, nChannels,
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000177 &_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +0000179 assert(static_cast<int>(nSamples) == _audioFrame.samples_per_channel_);
xians@google.com0b0665a2011-08-08 08:18:44 +0000180 assert(samplesPerSec ==
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000181 static_cast<uint32_t>(_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
183 // Deliver audio (PCM) samples to the ADM
184 memcpy(
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000185 (int16_t*) audioSamples,
186 (const int16_t*) _audioFrame.data_,
187 sizeof(int16_t) * (_audioFrame.samples_per_channel_
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000188 * _audioFrame.num_channels_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000190 nSamplesOut = _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
192 return 0;
193}
194
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000195int VoEBaseImpl::OnDataAvailable(const int voe_channels[],
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000196 int number_of_voe_channels,
197 const int16_t* audio_data,
198 int sample_rate,
199 int number_of_channels,
200 int number_of_frames,
201 int audio_delay_milliseconds,
202 int current_volume,
203 bool key_pressed,
204 bool need_audio_processing) {
205 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
206 "VoEBaseImpl::OnDataAvailable(number_of_voe_channels=%d, "
207 "sample_rate=%d, number_of_channels=%d, number_of_frames=%d, "
208 "audio_delay_milliseconds=%d, current_volume=%d, "
209 "key_pressed=%d, need_audio_processing=%d)",
210 number_of_voe_channels, sample_rate, number_of_channels,
211 number_of_frames, audio_delay_milliseconds, current_volume,
212 key_pressed, need_audio_processing);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000213 if (number_of_voe_channels == 0)
214 return 0;
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000215
216 if (need_audio_processing) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000217 return ProcessRecordedDataWithAPM(
218 voe_channels, number_of_voe_channels, audio_data, sample_rate,
219 number_of_channels, number_of_frames, audio_delay_milliseconds,
220 0, current_volume, key_pressed);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000221 }
222
223 // No need to go through the APM, demultiplex the data to each VoE channel,
224 // encode and send to the network.
225 for (int i = 0; i < number_of_voe_channels; ++i) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000226 voe::ChannelOwner ch =
227 _shared->channel_manager().GetChannel(voe_channels[i]);
228 voe::Channel* channel_ptr = ch.channel();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000229 if (!channel_ptr)
230 continue;
231
232 if (channel_ptr->InputIsOnHold()) {
233 channel_ptr->UpdateLocalTimeStamp();
234 } else if (channel_ptr->Sending()) {
235 channel_ptr->Demultiplex(audio_data, sample_rate, number_of_frames,
236 number_of_channels);
237 channel_ptr->PrepareEncodeAndSend(sample_rate);
238 channel_ptr->EncodeAndSend();
239 }
240 }
241
242 // Return 0 to indicate no need to change the volume.
243 return 0;
244}
245
niklase@google.com470e71d2011-07-07 08:21:25 +0000246int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
247{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000248 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 "RegisterVoiceEngineObserver(observer=0x%d)", &observer);
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000250 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 if (_voiceEngineObserverPtr)
252 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000253 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
254 "RegisterVoiceEngineObserver() observer already enabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000255 return -1;
256 }
257
258 // Register the observer in all active channels
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000259 for (voe::ChannelManager::Iterator it(&_shared->channel_manager());
260 it.IsValid();
261 it.Increment()) {
262 it.GetChannel()->RegisterVoiceEngineObserver(observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000264
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000265 _shared->transmit_mixer()->RegisterVoiceEngineObserver(observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000266
267 _voiceEngineObserverPtr = &observer;
268 _voiceEngineObserver = true;
269
270 return 0;
271}
272
273int VoEBaseImpl::DeRegisterVoiceEngineObserver()
274{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000275 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 "DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000277 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000278 if (!_voiceEngineObserverPtr)
279 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000280 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000281 "DeRegisterVoiceEngineObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000282 return 0;
283 }
284
285 _voiceEngineObserver = false;
286 _voiceEngineObserverPtr = NULL;
287
288 // Deregister the observer in all active channels
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000289 for (voe::ChannelManager::Iterator it(&_shared->channel_manager());
290 it.IsValid();
291 it.Increment()) {
292 it.GetChannel()->DeRegisterVoiceEngineObserver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000293 }
294
295 return 0;
296}
297
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000298int VoEBaseImpl::Init(AudioDeviceModule* external_adm,
299 AudioProcessing* audioproc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000300{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000301 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
henrika@google.com73d65512011-09-07 15:11:18 +0000302 "Init(external_adm=0x%p)", external_adm);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000303 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000304
kma@webrtc.org0221b782012-09-08 00:09:26 +0000305 WebRtcSpl_Init();
306
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000307 if (_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 {
309 return 0;
310 }
311
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000312 if (_shared->process_thread())
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000314 if (_shared->process_thread()->Start() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000315 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000316 _shared->SetLastError(VE_THREAD_ERROR, kTraceError,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000317 "Init() failed to start module process thread");
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 return -1;
319 }
320 }
321
andrew@webrtc.org3192d652011-12-21 18:00:59 +0000322 // Create an internal ADM if the user has not added an external
henrika@google.com73d65512011-09-07 15:11:18 +0000323 // ADM implementation as input to Init().
324 if (external_adm == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000325 {
henrika@google.com73d65512011-09-07 15:11:18 +0000326 // Create the internal ADM implementation.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000327 _shared->set_audio_device(AudioDeviceModuleImpl::Create(
328 VoEId(_shared->instance_id(), -1), _shared->audio_device_layer()));
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000329
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000330 if (_shared->audio_device() == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000331 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000332 _shared->SetLastError(VE_NO_MEMORY, kTraceCritical,
333 "Init() failed to create the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000334 return -1;
335 }
336 }
henrika@google.com73d65512011-09-07 15:11:18 +0000337 else
338 {
339 // Use the already existing external ADM implementation.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000340 _shared->set_audio_device(external_adm);
341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
henrika@google.com73d65512011-09-07 15:11:18 +0000342 "An external ADM implementation will be used in VoiceEngine");
343 }
344
niklase@google.com470e71d2011-07-07 08:21:25 +0000345 // Register the ADM to the process thread, which will drive the error
346 // callback mechanism
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000347 if (_shared->process_thread() &&
348 _shared->process_thread()->RegisterModule(_shared->audio_device()) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000349 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000350 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
351 "Init() failed to register the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 return -1;
353 }
354
355 bool available(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000356
357 // --------------------
358 // Reinitialize the ADM
359
360 // Register the AudioObserver implementation
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000361 if (_shared->audio_device()->RegisterEventObserver(this) != 0) {
362 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
363 "Init() failed to register event observer for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +0000364 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
366 // Register the AudioTransport implementation
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000367 if (_shared->audio_device()->RegisterAudioCallback(this) != 0) {
368 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
369 "Init() failed to register audio callback for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +0000370 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
372 // ADM initialization
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000373 if (_shared->audio_device()->Init() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000374 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000375 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
376 "Init() failed to initialize the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000377 return -1;
378 }
379
380 // Initialize the default speaker
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000381 if (_shared->audio_device()->SetPlayoutDevice(
382 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000383 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000384 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000385 "Init() failed to set the default output device");
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000387 if (_shared->audio_device()->SpeakerIsAvailable(&available) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000388 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000389 _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000390 "Init() failed to check speaker availability, trying to "
391 "initialize speaker anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000392 }
393 else if (!available)
394 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000395 _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000396 "Init() speaker not available, trying to initialize speaker "
397 "anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000398 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000399 if (_shared->audio_device()->InitSpeaker() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000401 _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000402 "Init() failed to initialize the speaker");
niklase@google.com470e71d2011-07-07 08:21:25 +0000403 }
404
405 // Initialize the default microphone
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000406 if (_shared->audio_device()->SetRecordingDevice(
407 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000408 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000409 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000410 "Init() failed to set the default input device");
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000412 if (_shared->audio_device()->MicrophoneIsAvailable(&available) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000414 _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000415 "Init() failed to check microphone availability, trying to "
416 "initialize microphone anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 }
418 else if (!available)
419 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000420 _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000421 "Init() microphone not available, trying to initialize "
422 "microphone anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000424 if (_shared->audio_device()->InitMicrophone() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000425 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000426 _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000427 "Init() failed to initialize the microphone");
niklase@google.com470e71d2011-07-07 08:21:25 +0000428 }
429
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000430 // Set number of channels
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000431 if (_shared->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
432 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
433 "Init() failed to query stereo playout mode");
xians@webrtc.org79af7342012-01-31 12:22:14 +0000434 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000435 if (_shared->audio_device()->SetStereoPlayout(available) != 0)
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000436 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000437 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000438 "Init() failed to set mono/stereo playout mode");
439 }
andrew@webrtc.org3192d652011-12-21 18:00:59 +0000440
441 // TODO(andrew): These functions don't tell us whether stereo recording
442 // is truly available. We simply set the AudioProcessing input to stereo
443 // here, because we have to wait until receiving the first frame to
444 // determine the actual number of channels anyway.
445 //
446 // These functions may be changed; tracked here:
447 // http://code.google.com/p/webrtc/issues/detail?id=204
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000448 _shared->audio_device()->StereoRecordingIsAvailable(&available);
449 if (_shared->audio_device()->SetStereoRecording(available) != 0)
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000450 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000451 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000452 "Init() failed to set mono/stereo recording mode");
453 }
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000454
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000455 if (!audioproc) {
456 audioproc = AudioProcessing::Create(VoEId(_shared->instance_id(), -1));
457 if (!audioproc) {
458 LOG(LS_ERROR) << "Failed to create AudioProcessing.";
459 _shared->SetLastError(VE_NO_MEMORY);
460 return -1;
461 }
462 }
463 _shared->set_audio_processing(audioproc);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000464
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000465 // Set the error state for any failures in this block.
466 _shared->SetLastError(VE_APM_ERROR);
467 if (audioproc->echo_cancellation()->set_device_sample_rate_hz(48000)) {
468 LOG_FERR1(LS_ERROR, set_device_sample_rate_hz, 48000);
469 return -1;
470 }
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000471
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +0000472 // Configure AudioProcessing components.
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000473 if (audioproc->high_pass_filter()->Enable(true) != 0) {
474 LOG_FERR1(LS_ERROR, high_pass_filter()->Enable, true);
475 return -1;
476 }
477 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
478 LOG_FERR1(LS_ERROR, enable_drift_compensation, false);
479 return -1;
480 }
481 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
482 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
483 return -1;
484 }
485 GainControl* agc = audioproc->gain_control();
486 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
487 LOG_FERR2(LS_ERROR, agc->set_analog_level_limits, kMinVolumeLevel,
488 kMaxVolumeLevel);
489 return -1;
490 }
491 if (agc->set_mode(kDefaultAgcMode) != 0) {
492 LOG_FERR1(LS_ERROR, agc->set_mode, kDefaultAgcMode);
493 return -1;
494 }
495 if (agc->Enable(kDefaultAgcState) != 0) {
496 LOG_FERR1(LS_ERROR, agc->Enable, kDefaultAgcState);
497 return -1;
498 }
499 _shared->SetLastError(0); // Clear error state.
500
niklase@google.com470e71d2011-07-07 08:21:25 +0000501#ifdef WEBRTC_VOICE_ENGINE_AGC
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000502 bool agc_enabled = agc->mode() == GainControl::kAdaptiveAnalog &&
503 agc->is_enabled();
504 if (_shared->audio_device()->SetAGC(agc_enabled) != 0) {
505 LOG_FERR1(LS_ERROR, audio_device()->SetAGC, agc_enabled);
506 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
andrew@webrtc.orga9a1df02013-03-05 23:36:10 +0000507 // TODO(ajm): No error return here due to
508 // https://code.google.com/p/webrtc/issues/detail?id=1464
niklase@google.com470e71d2011-07-07 08:21:25 +0000509 }
510#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000511
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000512 return _shared->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
515int VoEBaseImpl::Terminate()
516{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000517 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 "Terminate()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000519 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 return TerminateInternal();
521}
522
niklase@google.com470e71d2011-07-07 08:21:25 +0000523int VoEBaseImpl::CreateChannel()
524{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000525 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000526 "CreateChannel()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000527 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000528
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000529 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000531 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000532 return -1;
533 }
534
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000535 voe::ChannelOwner channel_owner =
536 _shared->channel_manager().CreateChannel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000537
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000538 if (channel_owner.channel()->SetEngineInformation(
539 _shared->statistics(),
540 *_shared->output_mixer(),
541 *_shared->transmit_mixer(),
542 *_shared->process_thread(),
543 *_shared->audio_device(),
544 _voiceEngineObserverPtr,
545 &_callbackCritSect) != 0) {
546 _shared->SetLastError(
547 VE_CHANNEL_NOT_CREATED,
548 kTraceError,
549 "CreateChannel() failed to associate engine and channel."
550 " Destroying channel.");
551 _shared->channel_manager()
552 .DestroyChannel(channel_owner.channel()->ChannelId());
553 return -1;
554 } else if (channel_owner.channel()->Init() != 0) {
555 _shared->SetLastError(
556 VE_CHANNEL_NOT_CREATED,
557 kTraceError,
558 "CreateChannel() failed to initialize channel. Destroying"
559 " channel.");
560 _shared->channel_manager()
561 .DestroyChannel(channel_owner.channel()->ChannelId());
562 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000563 }
564
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000565 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
566 VoEId(_shared->instance_id(), -1),
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000567 "CreateChannel() => %d", channel_owner.channel()->ChannelId());
568 return channel_owner.channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000569}
570
571int VoEBaseImpl::DeleteChannel(int channel)
572{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000573 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000574 "DeleteChannel(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000575 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000577 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000578 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000579 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 return -1;
581 }
582
583 {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000584 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
585 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000586 if (channelPtr == NULL)
587 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000588 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
589 "DeleteChannel() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000590 return -1;
591 }
592 }
593
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000594 _shared->channel_manager().DestroyChannel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
596 if (StopSend() != 0)
597 {
598 return -1;
599 }
600
601 if (StopPlayout() != 0)
602 {
603 return -1;
604 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000605
niklase@google.com470e71d2011-07-07 08:21:25 +0000606 return 0;
607}
608
niklase@google.com470e71d2011-07-07 08:21:25 +0000609int VoEBaseImpl::StartReceive(int channel)
610{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000611 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000612 "StartReceive(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000613 CriticalSectionScoped cs(_shared->crit_sec());
614 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000615 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000616 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 return -1;
618 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000619 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
620 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000621 if (channelPtr == NULL)
622 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000623 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
624 "StartReceive() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 return -1;
626 }
627 return channelPtr->StartReceiving();
628}
629
630int VoEBaseImpl::StopReceive(int channel)
631{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000632 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000633 "StopListen(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000634 CriticalSectionScoped cs(_shared->crit_sec());
635 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000636 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000637 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000638 return -1;
639 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000640 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
641 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000642 if (channelPtr == NULL)
643 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000644 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
645 "SetLocalReceiver() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000646 return -1;
647 }
648 return channelPtr->StopReceiving();
649}
650
651int VoEBaseImpl::StartPlayout(int channel)
652{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000653 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000654 "StartPlayout(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000655 CriticalSectionScoped cs(_shared->crit_sec());
656 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000657 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000658 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000659 return -1;
660 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000661 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
662 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000663 if (channelPtr == NULL)
664 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000665 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
666 "StartPlayout() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000667 return -1;
668 }
669 if (channelPtr->Playing())
670 {
671 return 0;
672 }
673 if (StartPlayout() != 0)
674 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000675 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
676 "StartPlayout() failed to start playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000677 return -1;
678 }
679 return channelPtr->StartPlayout();
680}
681
682int VoEBaseImpl::StopPlayout(int channel)
683{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000684 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000685 "StopPlayout(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000686 CriticalSectionScoped cs(_shared->crit_sec());
687 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000689 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 return -1;
691 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000692 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
693 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 if (channelPtr == NULL)
695 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000696 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
697 "StopPlayout() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000698 return -1;
699 }
700 if (channelPtr->StopPlayout() != 0)
701 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000702 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
703 VoEId(_shared->instance_id(), -1),
704 "StopPlayout() failed to stop playout for channel %d", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
706 return StopPlayout();
707}
708
709int VoEBaseImpl::StartSend(int channel)
710{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000711 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 "StartSend(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000713 CriticalSectionScoped cs(_shared->crit_sec());
714 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000716 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 return -1;
718 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000719 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
720 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000721 if (channelPtr == NULL)
722 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000723 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
724 "StartSend() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 return -1;
726 }
727 if (channelPtr->Sending())
728 {
729 return 0;
730 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 if (StartSend() != 0)
732 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000733 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
734 "StartSend() failed to start recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 return -1;
736 }
737 return channelPtr->StartSend();
738}
739
740int VoEBaseImpl::StopSend(int channel)
741{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000742 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 "StopSend(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000744 CriticalSectionScoped cs(_shared->crit_sec());
745 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000746 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000747 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 return -1;
749 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000750 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
751 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000752 if (channelPtr == NULL)
753 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000754 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
755 "StopSend() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000756 return -1;
757 }
758 if (channelPtr->StopSend() != 0)
759 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000760 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
761 VoEId(_shared->instance_id(), -1),
762 "StopSend() failed to stop sending for channel %d", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 }
764 return StopSend();
765}
766
767int VoEBaseImpl::GetVersion(char version[1024])
768{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000769 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000770 "GetVersion(version=?)");
771 assert(kVoiceEngineVersionMaxMessageSize == 1024);
772
773 if (version == NULL)
774 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000775 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 return (-1);
777 }
778
779 char versionBuf[kVoiceEngineVersionMaxMessageSize];
780 char* versionPtr = versionBuf;
781
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000782 int32_t len = 0;
783 int32_t accLen = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000784
785 len = AddVoEVersion(versionPtr);
786 if (len == -1)
787 {
788 return -1;
789 }
790 versionPtr += len;
791 accLen += len;
792 assert(accLen < kVoiceEngineVersionMaxMessageSize);
793
794 len = AddBuildInfo(versionPtr);
795 if (len == -1)
796 {
797 return -1;
798 }
799 versionPtr += len;
800 accLen += len;
801 assert(accLen < kVoiceEngineVersionMaxMessageSize);
802
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000803#ifdef WEBRTC_EXTERNAL_TRANSPORT
804 len = AddExternalTransportBuild(versionPtr);
805 if (len == -1)
806 {
807 return -1;
808 }
809 versionPtr += len;
810 accLen += len;
811 assert(accLen < kVoiceEngineVersionMaxMessageSize);
812#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000813#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
814 len = AddExternalRecAndPlayoutBuild(versionPtr);
815 if (len == -1)
816 {
817 return -1;
818 }
819 versionPtr += len;
820 accLen += len;
821 assert(accLen < kVoiceEngineVersionMaxMessageSize);
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000822 #endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000823
824 memcpy(version, versionBuf, accLen);
825 version[accLen] = '\0';
826
827 // to avoid the truncation in the trace, split the string into parts
828 char partOfVersion[256];
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000829 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
830 VoEId(_shared->instance_id(), -1), "GetVersion() =>");
niklase@google.com470e71d2011-07-07 08:21:25 +0000831 for (int partStart = 0; partStart < accLen;)
832 {
833 memset(partOfVersion, 0, sizeof(partOfVersion));
834 int partEnd = partStart + 180;
835 while (version[partEnd] != '\n' && version[partEnd] != '\0')
836 {
837 partEnd--;
838 }
839 if (partEnd < accLen)
840 {
841 memcpy(partOfVersion, &version[partStart], partEnd - partStart);
842 }
843 else
844 {
845 memcpy(partOfVersion, &version[partStart], accLen - partStart);
846 }
847 partStart = partEnd;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000848 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
849 VoEId(_shared->instance_id(), -1), "%s", partOfVersion);
niklase@google.com470e71d2011-07-07 08:21:25 +0000850 }
851
852 return 0;
853}
854
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000855int32_t VoEBaseImpl::AddBuildInfo(char* str) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000856{
leozwang@webrtc.org48a5df62012-04-24 14:50:50 +0000857 return sprintf(str, "Build: svn:%s %s\n", WEBRTC_SVNREVISION, BUILDINFO);
niklase@google.com470e71d2011-07-07 08:21:25 +0000858}
859
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000860int32_t VoEBaseImpl::AddVoEVersion(char* str) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000861{
862 return sprintf(str, "VoiceEngine 4.1.0\n");
863}
864
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000865#ifdef WEBRTC_EXTERNAL_TRANSPORT
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000866int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000867{
868 return sprintf(str, "External transport build\n");
869}
870#endif
871
niklase@google.com470e71d2011-07-07 08:21:25 +0000872#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000873int32_t VoEBaseImpl::AddExternalRecAndPlayoutBuild(char* str) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000874{
875 return sprintf(str, "External recording and playout build\n");
876}
877#endif
878
niklase@google.com470e71d2011-07-07 08:21:25 +0000879int VoEBaseImpl::LastError()
880{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000881 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000882 "LastError()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000883 return (_shared->statistics().LastError());
niklase@google.com470e71d2011-07-07 08:21:25 +0000884}
885
886
887int VoEBaseImpl::SetNetEQPlayoutMode(int channel, NetEqModes mode)
888{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000889 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000890 "SetNetEQPlayoutMode(channel=%i, mode=%i)", channel, mode);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000891 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000893 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 return -1;
895 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000896 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
897 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 if (channelPtr == NULL)
899 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000900 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
901 "SetNetEQPlayoutMode() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 return -1;
903 }
904 return channelPtr->SetNetEQPlayoutMode(mode);
905}
906
907int VoEBaseImpl::GetNetEQPlayoutMode(int channel, NetEqModes& mode)
908{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000909 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000910 "GetNetEQPlayoutMode(channel=%i, mode=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000911 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000912 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000913 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 return -1;
915 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000916 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
917 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 if (channelPtr == NULL)
919 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000920 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
921 "GetNetEQPlayoutMode() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000922 return -1;
923 }
924 return channelPtr->GetNetEQPlayoutMode(mode);
925}
926
niklase@google.com470e71d2011-07-07 08:21:25 +0000927int VoEBaseImpl::SetOnHoldStatus(int channel, bool enable, OnHoldModes mode)
928{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000929 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000930 "SetOnHoldStatus(channel=%d, enable=%d, mode=%d)", channel,
931 enable, mode);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000932 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000933 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000934 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000935 return -1;
936 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000937 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
938 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000939 if (channelPtr == NULL)
940 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000941 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
942 "SetOnHoldStatus() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000943 return -1;
944 }
945 return channelPtr->SetOnHoldStatus(enable, mode);
946}
947
948int VoEBaseImpl::GetOnHoldStatus(int channel, bool& enabled, OnHoldModes& mode)
949{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000950 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000951 "GetOnHoldStatus(channel=%d, enabled=?, mode=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000952 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000953 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000954 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000955 return -1;
956 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000957 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
958 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000959 if (channelPtr == NULL)
960 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000961 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
962 "GetOnHoldStatus() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000963 return -1;
964 }
965 return channelPtr->GetOnHoldStatus(enabled, mode);
966}
967
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000968int32_t VoEBaseImpl::StartPlayout()
niklase@google.com470e71d2011-07-07 08:21:25 +0000969{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000970 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000971 "VoEBaseImpl::StartPlayout()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000972 if (_shared->audio_device()->Playing())
niklase@google.com470e71d2011-07-07 08:21:25 +0000973 {
974 return 0;
975 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000976 if (!_shared->ext_playout())
niklase@google.com470e71d2011-07-07 08:21:25 +0000977 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000978 if (_shared->audio_device()->InitPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000979 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000980 WEBRTC_TRACE(kTraceError, kTraceVoice,
981 VoEId(_shared->instance_id(), -1),
982 "StartPlayout() failed to initialize playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000983 return -1;
984 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000985 if (_shared->audio_device()->StartPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000986 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000987 WEBRTC_TRACE(kTraceError, kTraceVoice,
988 VoEId(_shared->instance_id(), -1),
989 "StartPlayout() failed to start playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000990 return -1;
991 }
992 }
993 return 0;
994}
995
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000996int32_t VoEBaseImpl::StopPlayout() {
997 WEBRTC_TRACE(kTraceInfo,
998 kTraceVoice,
999 VoEId(_shared->instance_id(), -1),
1000 "VoEBaseImpl::StopPlayout()");
1001 // Stop audio-device playing if no channel is playing out
xians@webrtc.org675e2602013-10-17 16:15:34 +00001002 if (_shared->NumOfPlayingChannels() == 0) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +00001003 if (_shared->audio_device()->StopPlayout() != 0) {
1004 _shared->SetLastError(VE_CANNOT_STOP_PLAYOUT,
1005 kTraceError,
1006 "StopPlayout() failed to stop playout");
1007 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001008 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +00001009 }
1010 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001011}
1012
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001013int32_t VoEBaseImpl::StartSend()
niklase@google.com470e71d2011-07-07 08:21:25 +00001014{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001015 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001016 "VoEBaseImpl::StartSend()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001017 if (_shared->audio_device()->Recording())
niklase@google.com470e71d2011-07-07 08:21:25 +00001018 {
1019 return 0;
1020 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001021 if (!_shared->ext_recording())
niklase@google.com470e71d2011-07-07 08:21:25 +00001022 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001023 if (_shared->audio_device()->InitRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001024 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001025 WEBRTC_TRACE(kTraceError, kTraceVoice,
1026 VoEId(_shared->instance_id(), -1),
1027 "StartSend() failed to initialize recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001028 return -1;
1029 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001030 if (_shared->audio_device()->StartRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001031 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001032 WEBRTC_TRACE(kTraceError, kTraceVoice,
1033 VoEId(_shared->instance_id(), -1),
1034 "StartSend() failed to start recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001035 return -1;
1036 }
1037 }
1038
1039 return 0;
1040}
1041
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001042int32_t VoEBaseImpl::StopSend()
niklase@google.com470e71d2011-07-07 08:21:25 +00001043{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001044 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001045 "VoEBaseImpl::StopSend()");
1046
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001047 if (_shared->NumOfSendingChannels() == 0 &&
1048 !_shared->transmit_mixer()->IsRecordingMic())
niklase@google.com470e71d2011-07-07 08:21:25 +00001049 {
1050 // Stop audio-device recording if no channel is recording
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001051 if (_shared->audio_device()->StopRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001053 _shared->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
1054 "StopSend() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 return -1;
1056 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001057 _shared->transmit_mixer()->StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 }
1059
1060 return 0;
1061}
1062
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001063int32_t VoEBaseImpl::TerminateInternal()
niklase@google.com470e71d2011-07-07 08:21:25 +00001064{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001065 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 "VoEBaseImpl::TerminateInternal()");
1067
1068 // Delete any remaining channel objects
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +00001069 _shared->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +00001070
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001071 if (_shared->process_thread())
niklase@google.com470e71d2011-07-07 08:21:25 +00001072 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001073 if (_shared->audio_device())
niklase@google.com470e71d2011-07-07 08:21:25 +00001074 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001075 if (_shared->process_thread()->
1076 DeRegisterModule(_shared->audio_device()) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001077 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001078 _shared->SetLastError(VE_THREAD_ERROR, kTraceError,
1079 "TerminateInternal() failed to deregister ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +00001080 }
1081 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001082 if (_shared->process_thread()->Stop() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001084 _shared->SetLastError(VE_THREAD_ERROR, kTraceError,
1085 "TerminateInternal() failed to stop module process thread");
niklase@google.com470e71d2011-07-07 08:21:25 +00001086 }
1087 }
1088
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +00001089 if (_shared->audio_device())
niklase@google.com470e71d2011-07-07 08:21:25 +00001090 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001091 if (_shared->audio_device()->StopPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001092 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001093 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
1094 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +00001095 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001096 if (_shared->audio_device()->StopRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001097 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001098 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
1099 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001100 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001101 if (_shared->audio_device()->RegisterEventObserver(NULL) != 0) {
1102 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
1103 "TerminateInternal() failed to de-register event observer "
1104 "for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +00001105 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001106 if (_shared->audio_device()->RegisterAudioCallback(NULL) != 0) {
1107 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
1108 "TerminateInternal() failed to de-register audio callback "
1109 "for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +00001110 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001111 if (_shared->audio_device()->Terminate() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001112 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001113 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
1114 "TerminateInternal() failed to terminate the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +00001115 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001116 _shared->set_audio_device(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +00001117 }
1118
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +00001119 if (_shared->audio_processing()) {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001120 _shared->set_audio_processing(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +00001121 }
1122
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001123 return _shared->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +00001124}
1125
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00001126int VoEBaseImpl::ProcessRecordedDataWithAPM(
1127 const int voe_channels[],
1128 int number_of_voe_channels,
1129 const void* audio_data,
1130 uint32_t sample_rate,
1131 uint8_t number_of_channels,
1132 uint32_t number_of_frames,
1133 uint32_t audio_delay_milliseconds,
1134 int32_t clock_drift,
1135 uint32_t current_volume,
1136 bool key_pressed) {
1137 assert(_shared->transmit_mixer() != NULL);
1138 assert(_shared->audio_device() != NULL);
1139
1140 bool is_analog_agc(false);
1141 if (_shared->audio_processing() &&
1142 _shared->audio_processing()->gain_control()->mode() ==
1143 GainControl::kAdaptiveAnalog) {
1144 is_analog_agc = true;
1145 }
1146
1147 // Only deal with the volume in adaptive analog mode.
1148 uint32_t max_volume = 0;
1149 uint16_t current_voe_mic_level = 0;
1150 if (is_analog_agc) {
1151 // Scale from ADM to VoE level range
1152 if (_shared->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
1153 if (max_volume) {
1154 current_voe_mic_level = static_cast<uint16_t>(
1155 (current_volume * kMaxVolumeLevel +
1156 static_cast<int>(max_volume / 2)) / max_volume);
1157 }
1158 }
1159 // We learned that on certain systems (e.g Linux) the current_voe_mic_level
1160 // can be greater than the maxVolumeLevel therefore
1161 // we are going to cap the current_voe_mic_level to the maxVolumeLevel
1162 // and change the maxVolume to current_volume if it turns out that
1163 // the current_voe_mic_level is indeed greater than the maxVolumeLevel.
1164 if (current_voe_mic_level > kMaxVolumeLevel) {
1165 current_voe_mic_level = kMaxVolumeLevel;
1166 max_volume = current_volume;
1167 }
1168 }
1169
1170 // Keep track if the MicLevel has been changed by the AGC, if not,
1171 // use the old value AGC returns to let AGC continue its trend,
1172 // so eventually the AGC is able to change the mic level. This handles
1173 // issues with truncation introduced by the scaling.
1174 if (_oldMicLevel == current_volume)
1175 current_voe_mic_level = static_cast<uint16_t>(_oldVoEMicLevel);
1176
1177 // Perform channel-independent operations
1178 // (APM, mix with file, record to file, mute, etc.)
1179 _shared->transmit_mixer()->PrepareDemux(
1180 audio_data, number_of_frames, number_of_channels, sample_rate,
1181 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
1182 current_voe_mic_level, key_pressed);
1183
1184 // Copy the audio frame to each sending channel and perform
1185 // channel-dependent operations (file mixing, mute, etc.), encode and
1186 // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0,
1187 // do the operations on all the existing VoE channels; otherwise the
1188 // operations will be done on specific channels.
1189 if (number_of_voe_channels == 0) {
1190 _shared->transmit_mixer()->DemuxAndMix();
1191 _shared->transmit_mixer()->EncodeAndSend();
1192 } else {
1193 _shared->transmit_mixer()->DemuxAndMix(voe_channels,
1194 number_of_voe_channels);
1195 _shared->transmit_mixer()->EncodeAndSend(voe_channels,
1196 number_of_voe_channels);
1197 }
1198
1199 if (!is_analog_agc)
1200 return 0;
1201
1202 // Scale from VoE to ADM level range.
1203 uint32_t new_voe_mic_level = _shared->transmit_mixer()->CaptureLevel();
1204
1205 // Keep track of the value AGC returns.
1206 _oldVoEMicLevel = new_voe_mic_level;
1207 _oldMicLevel = current_volume;
1208
1209 if (new_voe_mic_level != current_voe_mic_level) {
1210 // Return the new volume if AGC has changed the volume.
1211 return static_cast<int>(
1212 (new_voe_mic_level * max_volume +
1213 static_cast<int>(kMaxVolumeLevel / 2)) / kMaxVolumeLevel);
1214 }
1215
1216 // Return 0 to indicate no change on the volume.
1217 return 0;
1218}
1219
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001220} // namespace webrtc