blob: 0aed543d20bd2b0a3323715b2e7dbce119ba7d60 [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
11#include "voe_base_impl.h"
12
henrika@google.com73d65512011-09-07 15:11:18 +000013#include "audio_coding_module.h"
henrika@google.com73d65512011-09-07 15:11:18 +000014#include "audio_processing.h"
15#include "channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016#include "critical_section_wrapper.h"
17#include "file_wrapper.h"
andrew@webrtc.org236d5d32012-09-21 20:46:40 +000018#include "modules/audio_device/audio_device_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019#include "output_mixer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#include "signal_processing_library.h"
henrika@google.com73d65512011-09-07 15:11:18 +000021#include "trace.h"
22#include "transmit_mixer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023#include "utility.h"
henrika@google.com73d65512011-09-07 15:11:18 +000024#include "voe_errors.h"
25#include "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
niklase@google.com470e71d2011-07-07 08:21:25 +000068void VoEBaseImpl::OnErrorIsReported(const ErrorCode error)
69{
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
97void VoEBaseImpl::OnWarningIsReported(const WarningCode warning)
98{
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.org6141e132013-04-09 10:09:10 +0000129 const uint32_t nSamples,
130 const uint8_t nBytesPerSample,
131 const uint8_t nChannels,
132 const uint32_t samplesPerSec,
133 const uint32_t totalDelayMS,
134 const int32_t clockDrift,
135 const uint32_t currentMicLevel,
136 uint32_t& newMicLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +0000137{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000138 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 "VoEBaseImpl::RecordedDataIsAvailable(nSamples=%u, "
140 "nBytesPerSample=%u, nChannels=%u, samplesPerSec=%u, "
141 "totalDelayMS=%u, clockDrift=%d, currentMicLevel=%u)",
142 nSamples, nBytesPerSample, nChannels, samplesPerSec,
143 totalDelayMS, clockDrift, currentMicLevel);
144
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000145 assert(_shared->transmit_mixer() != NULL);
146 assert(_shared->audio_device() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 bool isAnalogAGC(false);
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000149 uint32_t maxVolume(0);
150 uint16_t currentVoEMicLevel(0);
151 uint32_t newVoEMicLevel(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000153 if (_shared->audio_processing() &&
154 (_shared->audio_processing()->gain_control()->mode()
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 == GainControl::kAdaptiveAnalog))
156 {
157 isAnalogAGC = true;
158 }
159
160 // Will only deal with the volume in adaptive analog mode
161 if (isAnalogAGC)
162 {
163 // Scale from ADM to VoE level range
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000164 if (_shared->audio_device()->MaxMicrophoneVolume(&maxVolume) == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000165 {
166 if (0 != maxVolume)
167 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000168 currentVoEMicLevel = (uint16_t) ((currentMicLevel
niklase@google.com470e71d2011-07-07 08:21:25 +0000169 * kMaxVolumeLevel + (int) (maxVolume / 2))
170 / (maxVolume));
171 }
172 }
punyabrata@webrtc.org6b6d0812011-09-28 17:45:03 +0000173 // We learned that on certain systems (e.g Linux) the currentVoEMicLevel
174 // can be greater than the maxVolumeLevel therefore
175 // we are going to cap the currentVoEMicLevel to the maxVolumeLevel
xians@webrtc.org3ab6dda2012-02-16 18:15:54 +0000176 // and change the maxVolume to currentMicLevel if it turns out that
177 // the currentVoEMicLevel is indeed greater than the maxVolumeLevel.
punyabrata@webrtc.org6b6d0812011-09-28 17:45:03 +0000178 if (currentVoEMicLevel > kMaxVolumeLevel)
179 {
180 currentVoEMicLevel = kMaxVolumeLevel;
xians@webrtc.org3ab6dda2012-02-16 18:15:54 +0000181 maxVolume = currentMicLevel;
punyabrata@webrtc.org6b6d0812011-09-28 17:45:03 +0000182 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 }
184
185 // Keep track if the MicLevel has been changed by the AGC, if not,
186 // use the old value AGC returns to let AGC continue its trend,
187 // so eventually the AGC is able to change the mic level. This handles
188 // issues with truncation introduced by the scaling.
189 if (_oldMicLevel == currentMicLevel)
190 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000191 currentVoEMicLevel = (uint16_t) _oldVoEMicLevel;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192 }
193
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 // Perform channel-independent operations
195 // (APM, mix with file, record to file, mute, etc.)
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000196 _shared->transmit_mixer()->PrepareDemux(audioSamples, nSamples, nChannels,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000197 samplesPerSec, static_cast<uint16_t>(totalDelayMS), clockDrift,
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000198 currentVoEMicLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
200 // Copy the audio frame to each sending channel and perform
201 // channel-dependent operations (file mixing, mute, etc.) to prepare
202 // for encoding.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000203 _shared->transmit_mixer()->DemuxAndMix();
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 // Do the encoding and packetize+transmit the RTP packet when encoding
205 // is done.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000206 _shared->transmit_mixer()->EncodeAndSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
208 // Will only deal with the volume in adaptive analog mode
209 if (isAnalogAGC)
210 {
211 // Scale from VoE to ADM level range
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000212 newVoEMicLevel = _shared->transmit_mixer()->CaptureLevel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000213 if (newVoEMicLevel != currentVoEMicLevel)
214 {
215 // Add (kMaxVolumeLevel/2) to round the value
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000216 newMicLevel = (uint32_t) ((newVoEMicLevel * maxVolume
niklase@google.com470e71d2011-07-07 08:21:25 +0000217 + (int) (kMaxVolumeLevel / 2)) / (kMaxVolumeLevel));
218 }
219 else
220 {
221 // Pass zero if the level is unchanged
222 newMicLevel = 0;
223 }
224
225 // Keep track of the value AGC returns
226 _oldVoEMicLevel = newVoEMicLevel;
227 _oldMicLevel = currentMicLevel;
228 }
229
230 return 0;
231}
232
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000233int32_t VoEBaseImpl::NeedMorePlayData(
234 const uint32_t nSamples,
235 const uint8_t nBytesPerSample,
236 const uint8_t nChannels,
237 const uint32_t samplesPerSec,
henrika@webrtc.org907bc552012-03-09 08:59:19 +0000238 void* audioSamples,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000239 uint32_t& nSamplesOut)
niklase@google.com470e71d2011-07-07 08:21:25 +0000240{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000241 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 "VoEBaseImpl::NeedMorePlayData(nSamples=%u, "
243 "nBytesPerSample=%d, nChannels=%d, samplesPerSec=%u)",
244 nSamples, nBytesPerSample, nChannels, samplesPerSec);
245
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000246 assert(_shared->output_mixer() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000248 // TODO(andrew): if the device is running in mono, we should tell the mixer
249 // here so that it will only request mono from AudioCodingModule.
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 // Perform mixing of all active participants (channel-based mixing)
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000251 _shared->output_mixer()->MixActiveChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
253 // Additional operations on the combined signal
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000254 _shared->output_mixer()->DoOperationsOnCombinedSignal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
256 // Retrieve the final output mix (resampled to match the ADM)
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000257 _shared->output_mixer()->GetMixedAudio(samplesPerSec, nChannels,
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000258 &_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000259
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +0000260 assert(static_cast<int>(nSamples) == _audioFrame.samples_per_channel_);
xians@google.com0b0665a2011-08-08 08:18:44 +0000261 assert(samplesPerSec ==
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000262 static_cast<uint32_t>(_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
264 // Deliver audio (PCM) samples to the ADM
265 memcpy(
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000266 (int16_t*) audioSamples,
267 (const int16_t*) _audioFrame.data_,
268 sizeof(int16_t) * (_audioFrame.samples_per_channel_
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000269 * _audioFrame.num_channels_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000271 nSamplesOut = _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272
273 return 0;
274}
275
276int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
277{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000278 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000279 "RegisterVoiceEngineObserver(observer=0x%d)", &observer);
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000280 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 if (_voiceEngineObserverPtr)
282 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000283 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
284 "RegisterVoiceEngineObserver() observer already enabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000285 return -1;
286 }
287
288 // Register the observer in all active channels
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000289 voe::ScopedChannel sc(_shared->channel_manager());
niklase@google.com470e71d2011-07-07 08:21:25 +0000290 void* iterator(NULL);
291 voe::Channel* channelPtr = sc.GetFirstChannel(iterator);
292 while (channelPtr != NULL)
293 {
294 channelPtr->RegisterVoiceEngineObserver(observer);
295 channelPtr = sc.GetNextChannel(iterator);
296 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000297 _shared->transmit_mixer()->RegisterVoiceEngineObserver(observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000298
299 _voiceEngineObserverPtr = &observer;
300 _voiceEngineObserver = true;
301
302 return 0;
303}
304
305int VoEBaseImpl::DeRegisterVoiceEngineObserver()
306{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000307 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 "DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000309 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000310 if (!_voiceEngineObserverPtr)
311 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000312 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000313 "DeRegisterVoiceEngineObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 return 0;
315 }
316
317 _voiceEngineObserver = false;
318 _voiceEngineObserverPtr = NULL;
319
320 // Deregister the observer in all active channels
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000321 voe::ScopedChannel sc(_shared->channel_manager());
niklase@google.com470e71d2011-07-07 08:21:25 +0000322 void* iterator(NULL);
323 voe::Channel* channelPtr = sc.GetFirstChannel(iterator);
324 while (channelPtr != NULL)
325 {
326 channelPtr->DeRegisterVoiceEngineObserver();
327 channelPtr = sc.GetNextChannel(iterator);
328 }
329
330 return 0;
331}
332
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000333int VoEBaseImpl::Init(AudioDeviceModule* external_adm,
334 AudioProcessing* audioproc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000335{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000336 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
henrika@google.com73d65512011-09-07 15:11:18 +0000337 "Init(external_adm=0x%p)", external_adm);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000338 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
kma@webrtc.org0221b782012-09-08 00:09:26 +0000340 WebRtcSpl_Init();
341
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000342 if (_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000343 {
344 return 0;
345 }
346
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000347 if (_shared->process_thread())
niklase@google.com470e71d2011-07-07 08:21:25 +0000348 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000349 if (_shared->process_thread()->Start() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000350 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000351 _shared->SetLastError(VE_THREAD_ERROR, kTraceError,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000352 "Init() failed to start module process thread");
niklase@google.com470e71d2011-07-07 08:21:25 +0000353 return -1;
354 }
355 }
356
andrew@webrtc.org3192d652011-12-21 18:00:59 +0000357 // Create an internal ADM if the user has not added an external
henrika@google.com73d65512011-09-07 15:11:18 +0000358 // ADM implementation as input to Init().
359 if (external_adm == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000360 {
henrika@google.com73d65512011-09-07 15:11:18 +0000361 // Create the internal ADM implementation.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000362 _shared->set_audio_device(AudioDeviceModuleImpl::Create(
363 VoEId(_shared->instance_id(), -1), _shared->audio_device_layer()));
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000364
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000365 if (_shared->audio_device() == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000366 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000367 _shared->SetLastError(VE_NO_MEMORY, kTraceCritical,
368 "Init() failed to create the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000369 return -1;
370 }
371 }
henrika@google.com73d65512011-09-07 15:11:18 +0000372 else
373 {
374 // Use the already existing external ADM implementation.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000375 _shared->set_audio_device(external_adm);
376 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
henrika@google.com73d65512011-09-07 15:11:18 +0000377 "An external ADM implementation will be used in VoiceEngine");
378 }
379
niklase@google.com470e71d2011-07-07 08:21:25 +0000380 // Register the ADM to the process thread, which will drive the error
381 // callback mechanism
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000382 if (_shared->process_thread() &&
383 _shared->process_thread()->RegisterModule(_shared->audio_device()) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000385 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
386 "Init() failed to register the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 return -1;
388 }
389
390 bool available(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000391
392 // --------------------
393 // Reinitialize the ADM
394
395 // Register the AudioObserver implementation
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000396 if (_shared->audio_device()->RegisterEventObserver(this) != 0) {
397 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
398 "Init() failed to register event observer for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +0000399 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
401 // Register the AudioTransport implementation
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000402 if (_shared->audio_device()->RegisterAudioCallback(this) != 0) {
403 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
404 "Init() failed to register audio callback for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +0000405 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
407 // ADM initialization
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000408 if (_shared->audio_device()->Init() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000409 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000410 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
411 "Init() failed to initialize the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 return -1;
413 }
414
415 // Initialize the default speaker
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000416 if (_shared->audio_device()->SetPlayoutDevice(
417 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000418 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000419 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000420 "Init() failed to set the default output device");
niklase@google.com470e71d2011-07-07 08:21:25 +0000421 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000422 if (_shared->audio_device()->SpeakerIsAvailable(&available) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000424 _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000425 "Init() failed to check speaker availability, trying to "
426 "initialize speaker anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 }
428 else if (!available)
429 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000430 _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000431 "Init() speaker not available, trying to initialize speaker "
432 "anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000434 if (_shared->audio_device()->InitSpeaker() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000435 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000436 _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000437 "Init() failed to initialize the speaker");
niklase@google.com470e71d2011-07-07 08:21:25 +0000438 }
439
440 // Initialize the default microphone
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000441 if (_shared->audio_device()->SetRecordingDevice(
442 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000443 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000444 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000445 "Init() failed to set the default input device");
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000447 if (_shared->audio_device()->MicrophoneIsAvailable(&available) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000448 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000449 _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000450 "Init() failed to check microphone availability, trying to "
451 "initialize microphone anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000452 }
453 else if (!available)
454 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000455 _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000456 "Init() microphone not available, trying to initialize "
457 "microphone anyway");
niklase@google.com470e71d2011-07-07 08:21:25 +0000458 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000459 if (_shared->audio_device()->InitMicrophone() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000460 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000461 _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000462 "Init() failed to initialize the microphone");
niklase@google.com470e71d2011-07-07 08:21:25 +0000463 }
464
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000465 // Set number of channels
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000466 if (_shared->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
467 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
468 "Init() failed to query stereo playout mode");
xians@webrtc.org79af7342012-01-31 12:22:14 +0000469 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000470 if (_shared->audio_device()->SetStereoPlayout(available) != 0)
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000471 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000472 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000473 "Init() failed to set mono/stereo playout mode");
474 }
andrew@webrtc.org3192d652011-12-21 18:00:59 +0000475
476 // TODO(andrew): These functions don't tell us whether stereo recording
477 // is truly available. We simply set the AudioProcessing input to stereo
478 // here, because we have to wait until receiving the first frame to
479 // determine the actual number of channels anyway.
480 //
481 // These functions may be changed; tracked here:
482 // http://code.google.com/p/webrtc/issues/detail?id=204
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000483 _shared->audio_device()->StereoRecordingIsAvailable(&available);
484 if (_shared->audio_device()->SetStereoRecording(available) != 0)
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000485 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000486 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000487 "Init() failed to set mono/stereo recording mode");
488 }
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000489
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000490 if (!audioproc) {
491 audioproc = AudioProcessing::Create(VoEId(_shared->instance_id(), -1));
492 if (!audioproc) {
493 LOG(LS_ERROR) << "Failed to create AudioProcessing.";
494 _shared->SetLastError(VE_NO_MEMORY);
495 return -1;
496 }
497 }
498 _shared->set_audio_processing(audioproc);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000499
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000500 // Set the error state for any failures in this block.
501 _shared->SetLastError(VE_APM_ERROR);
502 if (audioproc->echo_cancellation()->set_device_sample_rate_hz(48000)) {
503 LOG_FERR1(LS_ERROR, set_device_sample_rate_hz, 48000);
504 return -1;
505 }
506 // Assume 16 kHz mono until the audio frames are received from the capture
507 // device, at which point this can be updated.
508 if (audioproc->set_sample_rate_hz(16000)) {
509 LOG_FERR1(LS_ERROR, set_sample_rate_hz, 16000);
510 return -1;
511 }
512 if (audioproc->set_num_channels(1, 1) != 0) {
513 LOG_FERR2(LS_ERROR, set_num_channels, 1, 1);
514 return -1;
515 }
516 if (audioproc->set_num_reverse_channels(1) != 0) {
517 LOG_FERR1(LS_ERROR, set_num_reverse_channels, 1);
518 return -1;
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000519 }
520
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000521 // Configure AudioProcessing components. All are disabled by default.
522 if (audioproc->high_pass_filter()->Enable(true) != 0) {
523 LOG_FERR1(LS_ERROR, high_pass_filter()->Enable, true);
524 return -1;
525 }
526 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
527 LOG_FERR1(LS_ERROR, enable_drift_compensation, false);
528 return -1;
529 }
530 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
531 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
532 return -1;
533 }
534 GainControl* agc = audioproc->gain_control();
535 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
536 LOG_FERR2(LS_ERROR, agc->set_analog_level_limits, kMinVolumeLevel,
537 kMaxVolumeLevel);
538 return -1;
539 }
540 if (agc->set_mode(kDefaultAgcMode) != 0) {
541 LOG_FERR1(LS_ERROR, agc->set_mode, kDefaultAgcMode);
542 return -1;
543 }
544 if (agc->Enable(kDefaultAgcState) != 0) {
545 LOG_FERR1(LS_ERROR, agc->Enable, kDefaultAgcState);
546 return -1;
547 }
548 _shared->SetLastError(0); // Clear error state.
549
niklase@google.com470e71d2011-07-07 08:21:25 +0000550#ifdef WEBRTC_VOICE_ENGINE_AGC
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000551 bool agc_enabled = agc->mode() == GainControl::kAdaptiveAnalog &&
552 agc->is_enabled();
553 if (_shared->audio_device()->SetAGC(agc_enabled) != 0) {
554 LOG_FERR1(LS_ERROR, audio_device()->SetAGC, agc_enabled);
555 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
andrew@webrtc.orga9a1df02013-03-05 23:36:10 +0000556 // TODO(ajm): No error return here due to
557 // https://code.google.com/p/webrtc/issues/detail?id=1464
niklase@google.com470e71d2011-07-07 08:21:25 +0000558 }
559#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000560
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000561 return _shared->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000562}
563
564int VoEBaseImpl::Terminate()
565{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000566 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000567 "Terminate()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000568 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000569 return TerminateInternal();
570}
571
572int VoEBaseImpl::MaxNumOfChannels()
573{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000574 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000575 "MaxNumOfChannels()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000576 int32_t maxNumOfChannels =
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000577 _shared->channel_manager().MaxNumOfChannels();
578 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
579 VoEId(_shared->instance_id(), -1),
580 "MaxNumOfChannels() => %d", maxNumOfChannels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000581 return (maxNumOfChannels);
582}
583
584int VoEBaseImpl::CreateChannel()
585{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000586 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000587 "CreateChannel()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000588 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000589
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000590 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000592 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000593 return -1;
594 }
595
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000596 int32_t channelId = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000597
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000598 if (!_shared->channel_manager().CreateChannel(channelId))
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000600 _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError,
601 "CreateChannel() failed to allocate memory for channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000602 return -1;
603 }
604
605 bool destroyChannel(false);
606 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000607 voe::ScopedChannel sc(_shared->channel_manager(), channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000608 voe::Channel* channelPtr = sc.ChannelPtr();
609 if (channelPtr == NULL)
610 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000611 _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError,
612 "CreateChannel() failed to allocate memory for channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000613 return -1;
614 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000615 else if (channelPtr->SetEngineInformation(_shared->statistics(),
616 *_shared->output_mixer(),
617 *_shared->transmit_mixer(),
618 *_shared->process_thread(),
619 *_shared->audio_device(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000620 _voiceEngineObserverPtr,
621 &_callbackCritSect) != 0)
622 {
623 destroyChannel = true;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000624 _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError,
625 "CreateChannel() failed to associate engine and channel."
626 " Destroying channel.");
niklase@google.com470e71d2011-07-07 08:21:25 +0000627 }
628 else if (channelPtr->Init() != 0)
629 {
630 destroyChannel = true;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000631 _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError,
632 "CreateChannel() failed to initialize channel. Destroying"
633 " channel.");
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 }
635 }
636 if (destroyChannel)
637 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000638 _shared->channel_manager().DestroyChannel(channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000639 return -1;
640 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000641 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
642 VoEId(_shared->instance_id(), -1),
643 "CreateChannel() => %d", channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000644 return channelId;
645}
646
647int VoEBaseImpl::DeleteChannel(int channel)
648{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000649 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000650 "DeleteChannel(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000651 CriticalSectionScoped cs(_shared->crit_sec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000652
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000653 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000654 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000655 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000656 return -1;
657 }
658
659 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000660 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000661 voe::Channel* channelPtr = sc.ChannelPtr();
662 if (channelPtr == NULL)
663 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000664 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
665 "DeleteChannel() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000666 return -1;
667 }
668 }
669
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000670 if (_shared->channel_manager().DestroyChannel(channel) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000671 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000672 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
673 "DeleteChannel() failed to destroy channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000674 return -1;
675 }
676
677 if (StopSend() != 0)
678 {
679 return -1;
680 }
681
682 if (StopPlayout() != 0)
683 {
684 return -1;
685 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000686
niklase@google.com470e71d2011-07-07 08:21:25 +0000687 return 0;
688}
689
niklase@google.com470e71d2011-07-07 08:21:25 +0000690int VoEBaseImpl::StartReceive(int channel)
691{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000692 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000693 "StartReceive(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000694 CriticalSectionScoped cs(_shared->crit_sec());
695 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000696 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000697 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000698 return -1;
699 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000700 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000701 voe::Channel* channelPtr = sc.ChannelPtr();
702 if (channelPtr == NULL)
703 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000704 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
705 "StartReceive() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 return -1;
707 }
708 return channelPtr->StartReceiving();
709}
710
711int VoEBaseImpl::StopReceive(int channel)
712{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000713 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 "StopListen(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000715 CriticalSectionScoped cs(_shared->crit_sec());
716 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000718 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 return -1;
720 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000721 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 voe::Channel* channelPtr = sc.ChannelPtr();
723 if (channelPtr == NULL)
724 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000725 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
726 "SetLocalReceiver() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 return -1;
728 }
729 return channelPtr->StopReceiving();
730}
731
732int VoEBaseImpl::StartPlayout(int channel)
733{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000734 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 "StartPlayout(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000736 CriticalSectionScoped cs(_shared->crit_sec());
737 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000738 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000739 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000740 return -1;
741 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000742 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 voe::Channel* channelPtr = sc.ChannelPtr();
744 if (channelPtr == NULL)
745 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000746 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
747 "StartPlayout() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 return -1;
749 }
750 if (channelPtr->Playing())
751 {
752 return 0;
753 }
754 if (StartPlayout() != 0)
755 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000756 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
757 "StartPlayout() failed to start playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 return -1;
759 }
760 return channelPtr->StartPlayout();
761}
762
763int VoEBaseImpl::StopPlayout(int channel)
764{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000765 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 "StopPlayout(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000767 CriticalSectionScoped cs(_shared->crit_sec());
768 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000770 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 return -1;
772 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000773 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 voe::Channel* channelPtr = sc.ChannelPtr();
775 if (channelPtr == NULL)
776 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000777 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
778 "StopPlayout() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000779 return -1;
780 }
781 if (channelPtr->StopPlayout() != 0)
782 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000783 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
784 VoEId(_shared->instance_id(), -1),
785 "StopPlayout() failed to stop playout for channel %d", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 }
787 return StopPlayout();
788}
789
790int VoEBaseImpl::StartSend(int channel)
791{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000792 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000793 "StartSend(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000794 CriticalSectionScoped cs(_shared->crit_sec());
795 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000796 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000797 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 return -1;
799 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000800 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 voe::Channel* channelPtr = sc.ChannelPtr();
802 if (channelPtr == NULL)
803 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000804 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
805 "StartSend() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000806 return -1;
807 }
808 if (channelPtr->Sending())
809 {
810 return 0;
811 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000812 if (StartSend() != 0)
813 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000814 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
815 "StartSend() failed to start recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000816 return -1;
817 }
818 return channelPtr->StartSend();
819}
820
821int VoEBaseImpl::StopSend(int channel)
822{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000823 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000824 "StopSend(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000825 CriticalSectionScoped cs(_shared->crit_sec());
826 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000827 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000828 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000829 return -1;
830 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000831 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 voe::Channel* channelPtr = sc.ChannelPtr();
833 if (channelPtr == NULL)
834 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000835 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
836 "StopSend() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000837 return -1;
838 }
839 if (channelPtr->StopSend() != 0)
840 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000841 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
842 VoEId(_shared->instance_id(), -1),
843 "StopSend() failed to stop sending for channel %d", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000844 }
845 return StopSend();
846}
847
848int VoEBaseImpl::GetVersion(char version[1024])
849{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000850 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000851 "GetVersion(version=?)");
852 assert(kVoiceEngineVersionMaxMessageSize == 1024);
853
854 if (version == NULL)
855 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000856 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 return (-1);
858 }
859
860 char versionBuf[kVoiceEngineVersionMaxMessageSize];
861 char* versionPtr = versionBuf;
862
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000863 int32_t len = 0;
864 int32_t accLen = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000865
866 len = AddVoEVersion(versionPtr);
867 if (len == -1)
868 {
869 return -1;
870 }
871 versionPtr += len;
872 accLen += len;
873 assert(accLen < kVoiceEngineVersionMaxMessageSize);
874
875 len = AddBuildInfo(versionPtr);
876 if (len == -1)
877 {
878 return -1;
879 }
880 versionPtr += len;
881 accLen += len;
882 assert(accLen < kVoiceEngineVersionMaxMessageSize);
883
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000884#ifdef WEBRTC_EXTERNAL_TRANSPORT
885 len = AddExternalTransportBuild(versionPtr);
886 if (len == -1)
887 {
888 return -1;
889 }
890 versionPtr += len;
891 accLen += len;
892 assert(accLen < kVoiceEngineVersionMaxMessageSize);
893#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000894#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
895 len = AddExternalRecAndPlayoutBuild(versionPtr);
896 if (len == -1)
897 {
898 return -1;
899 }
900 versionPtr += len;
901 accLen += len;
902 assert(accLen < kVoiceEngineVersionMaxMessageSize);
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000903 #endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000904
905 memcpy(version, versionBuf, accLen);
906 version[accLen] = '\0';
907
908 // to avoid the truncation in the trace, split the string into parts
909 char partOfVersion[256];
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000910 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
911 VoEId(_shared->instance_id(), -1), "GetVersion() =>");
niklase@google.com470e71d2011-07-07 08:21:25 +0000912 for (int partStart = 0; partStart < accLen;)
913 {
914 memset(partOfVersion, 0, sizeof(partOfVersion));
915 int partEnd = partStart + 180;
916 while (version[partEnd] != '\n' && version[partEnd] != '\0')
917 {
918 partEnd--;
919 }
920 if (partEnd < accLen)
921 {
922 memcpy(partOfVersion, &version[partStart], partEnd - partStart);
923 }
924 else
925 {
926 memcpy(partOfVersion, &version[partStart], accLen - partStart);
927 }
928 partStart = partEnd;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000929 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
930 VoEId(_shared->instance_id(), -1), "%s", partOfVersion);
niklase@google.com470e71d2011-07-07 08:21:25 +0000931 }
932
933 return 0;
934}
935
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000936int32_t VoEBaseImpl::AddBuildInfo(char* str) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000937{
leozwang@webrtc.org48a5df62012-04-24 14:50:50 +0000938 return sprintf(str, "Build: svn:%s %s\n", WEBRTC_SVNREVISION, BUILDINFO);
niklase@google.com470e71d2011-07-07 08:21:25 +0000939}
940
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000941int32_t VoEBaseImpl::AddVoEVersion(char* str) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000942{
943 return sprintf(str, "VoiceEngine 4.1.0\n");
944}
945
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000946#ifdef WEBRTC_EXTERNAL_TRANSPORT
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000947int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000948{
949 return sprintf(str, "External transport build\n");
950}
951#endif
952
niklase@google.com470e71d2011-07-07 08:21:25 +0000953#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000954int32_t VoEBaseImpl::AddExternalRecAndPlayoutBuild(char* str) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000955{
956 return sprintf(str, "External recording and playout build\n");
957}
958#endif
959
niklase@google.com470e71d2011-07-07 08:21:25 +0000960int VoEBaseImpl::LastError()
961{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000962 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000963 "LastError()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000964 return (_shared->statistics().LastError());
niklase@google.com470e71d2011-07-07 08:21:25 +0000965}
966
967
968int VoEBaseImpl::SetNetEQPlayoutMode(int channel, NetEqModes mode)
969{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000970 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000971 "SetNetEQPlayoutMode(channel=%i, mode=%i)", channel, mode);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000972 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000973 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000974 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000975 return -1;
976 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000977 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000978 voe::Channel* channelPtr = sc.ChannelPtr();
979 if (channelPtr == NULL)
980 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000981 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
982 "SetNetEQPlayoutMode() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000983 return -1;
984 }
985 return channelPtr->SetNetEQPlayoutMode(mode);
986}
987
988int VoEBaseImpl::GetNetEQPlayoutMode(int channel, NetEqModes& mode)
989{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000990 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000991 "GetNetEQPlayoutMode(channel=%i, mode=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000992 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000993 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000994 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000995 return -1;
996 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000997 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000998 voe::Channel* channelPtr = sc.ChannelPtr();
999 if (channelPtr == NULL)
1000 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001001 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
1002 "GetNetEQPlayoutMode() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +00001003 return -1;
1004 }
1005 return channelPtr->GetNetEQPlayoutMode(mode);
1006}
1007
niklase@google.com470e71d2011-07-07 08:21:25 +00001008int VoEBaseImpl::SetOnHoldStatus(int channel, bool enable, OnHoldModes mode)
1009{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001010 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001011 "SetOnHoldStatus(channel=%d, enable=%d, mode=%d)", channel,
1012 enable, mode);
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001013 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +00001014 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001015 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +00001016 return -1;
1017 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001018 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +00001019 voe::Channel* channelPtr = sc.ChannelPtr();
1020 if (channelPtr == NULL)
1021 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001022 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
1023 "SetOnHoldStatus() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +00001024 return -1;
1025 }
1026 return channelPtr->SetOnHoldStatus(enable, mode);
1027}
1028
1029int VoEBaseImpl::GetOnHoldStatus(int channel, bool& enabled, OnHoldModes& mode)
1030{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001031 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001032 "GetOnHoldStatus(channel=%d, enabled=?, mode=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001033 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +00001034 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001035 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +00001036 return -1;
1037 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001038 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +00001039 voe::Channel* channelPtr = sc.ChannelPtr();
1040 if (channelPtr == NULL)
1041 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001042 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
1043 "GetOnHoldStatus() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +00001044 return -1;
1045 }
1046 return channelPtr->GetOnHoldStatus(enabled, mode);
1047}
1048
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001049int32_t VoEBaseImpl::StartPlayout()
niklase@google.com470e71d2011-07-07 08:21:25 +00001050{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001051 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 "VoEBaseImpl::StartPlayout()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001053 if (_shared->audio_device()->Playing())
niklase@google.com470e71d2011-07-07 08:21:25 +00001054 {
1055 return 0;
1056 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001057 if (!_shared->ext_playout())
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001059 if (_shared->audio_device()->InitPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001061 WEBRTC_TRACE(kTraceError, kTraceVoice,
1062 VoEId(_shared->instance_id(), -1),
1063 "StartPlayout() failed to initialize playout");
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 return -1;
1065 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001066 if (_shared->audio_device()->StartPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001068 WEBRTC_TRACE(kTraceError, kTraceVoice,
1069 VoEId(_shared->instance_id(), -1),
1070 "StartPlayout() failed to start playout");
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 return -1;
1072 }
1073 }
1074 return 0;
1075}
1076
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001077int32_t VoEBaseImpl::StopPlayout()
niklase@google.com470e71d2011-07-07 08:21:25 +00001078{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001079 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001080 "VoEBaseImpl::StopPlayout()");
1081
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001082 int32_t numOfChannels = _shared->channel_manager().NumOfChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 if (numOfChannels <= 0)
1084 {
1085 return 0;
1086 }
1087
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001088 uint16_t nChannelsPlaying(0);
1089 int32_t* channelsArray = new int32_t[numOfChannels];
niklase@google.com470e71d2011-07-07 08:21:25 +00001090
1091 // Get number of playing channels
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001092 _shared->channel_manager().GetChannelIds(channelsArray, numOfChannels);
niklase@google.com470e71d2011-07-07 08:21:25 +00001093 for (int i = 0; i < numOfChannels; i++)
1094 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001095 voe::ScopedChannel sc(_shared->channel_manager(), channelsArray[i]);
niklase@google.com470e71d2011-07-07 08:21:25 +00001096 voe::Channel* chPtr = sc.ChannelPtr();
1097 if (chPtr)
1098 {
1099 if (chPtr->Playing())
1100 {
1101 nChannelsPlaying++;
1102 }
1103 }
1104 }
1105 delete[] channelsArray;
1106
1107 // Stop audio-device playing if no channel is playing out
1108 if (nChannelsPlaying == 0)
1109 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001110 if (_shared->audio_device()->StopPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001111 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001112 _shared->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError,
1113 "StopPlayout() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 return -1;
1115 }
1116 }
1117 return 0;
1118}
1119
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001120int32_t VoEBaseImpl::StartSend()
niklase@google.com470e71d2011-07-07 08:21:25 +00001121{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001122 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 "VoEBaseImpl::StartSend()");
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001124 if (_shared->audio_device()->Recording())
niklase@google.com470e71d2011-07-07 08:21:25 +00001125 {
1126 return 0;
1127 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001128 if (!_shared->ext_recording())
niklase@google.com470e71d2011-07-07 08:21:25 +00001129 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001130 if (_shared->audio_device()->InitRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001132 WEBRTC_TRACE(kTraceError, kTraceVoice,
1133 VoEId(_shared->instance_id(), -1),
1134 "StartSend() failed to initialize recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001135 return -1;
1136 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001137 if (_shared->audio_device()->StartRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001138 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001139 WEBRTC_TRACE(kTraceError, kTraceVoice,
1140 VoEId(_shared->instance_id(), -1),
1141 "StartSend() failed to start recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 return -1;
1143 }
1144 }
1145
1146 return 0;
1147}
1148
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001149int32_t VoEBaseImpl::StopSend()
niklase@google.com470e71d2011-07-07 08:21:25 +00001150{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001151 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001152 "VoEBaseImpl::StopSend()");
1153
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001154 if (_shared->NumOfSendingChannels() == 0 &&
1155 !_shared->transmit_mixer()->IsRecordingMic())
niklase@google.com470e71d2011-07-07 08:21:25 +00001156 {
1157 // Stop audio-device recording if no channel is recording
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001158 if (_shared->audio_device()->StopRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001159 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001160 _shared->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
1161 "StopSend() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 return -1;
1163 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001164 _shared->transmit_mixer()->StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 }
1166
1167 return 0;
1168}
1169
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001170int32_t VoEBaseImpl::TerminateInternal()
niklase@google.com470e71d2011-07-07 08:21:25 +00001171{
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001172 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +00001173 "VoEBaseImpl::TerminateInternal()");
1174
1175 // Delete any remaining channel objects
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001176 int32_t numOfChannels = _shared->channel_manager().NumOfChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 if (numOfChannels > 0)
1178 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001179 int32_t* channelsArray = new int32_t[numOfChannels];
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001180 _shared->channel_manager().GetChannelIds(channelsArray, numOfChannels);
niklase@google.com470e71d2011-07-07 08:21:25 +00001181 for (int i = 0; i < numOfChannels; i++)
1182 {
1183 DeleteChannel(channelsArray[i]);
1184 }
1185 delete[] channelsArray;
1186 }
1187
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001188 if (_shared->process_thread())
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001190 if (_shared->audio_device())
niklase@google.com470e71d2011-07-07 08:21:25 +00001191 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001192 if (_shared->process_thread()->
1193 DeRegisterModule(_shared->audio_device()) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001194 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001195 _shared->SetLastError(VE_THREAD_ERROR, kTraceError,
1196 "TerminateInternal() failed to deregister ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +00001197 }
1198 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001199 if (_shared->process_thread()->Stop() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001200 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001201 _shared->SetLastError(VE_THREAD_ERROR, kTraceError,
1202 "TerminateInternal() failed to stop module process thread");
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 }
1204 }
1205
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +00001206 if (_shared->audio_device())
niklase@google.com470e71d2011-07-07 08:21:25 +00001207 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001208 if (_shared->audio_device()->StopPlayout() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001209 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001210 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
1211 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +00001212 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001213 if (_shared->audio_device()->StopRecording() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001214 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001215 _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
1216 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00001217 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001218 if (_shared->audio_device()->RegisterEventObserver(NULL) != 0) {
1219 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
1220 "TerminateInternal() failed to de-register event observer "
1221 "for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +00001222 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001223 if (_shared->audio_device()->RegisterAudioCallback(NULL) != 0) {
1224 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
1225 "TerminateInternal() failed to de-register audio callback "
1226 "for the ADM");
xians@webrtc.org79af7342012-01-31 12:22:14 +00001227 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001228 if (_shared->audio_device()->Terminate() != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001229 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001230 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
1231 "TerminateInternal() failed to terminate the ADM");
niklase@google.com470e71d2011-07-07 08:21:25 +00001232 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001233 _shared->set_audio_device(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +00001234 }
1235
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +00001236 if (_shared->audio_processing()) {
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001237 _shared->set_audio_processing(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +00001238 }
1239
tommi@webrtc.org851becd2012-04-04 14:57:19 +00001240 return _shared->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +00001241}
1242
1243} // namespace webrtc