blob: 8072cc86d91fed6ffbe8f99598266c4b40b8580c [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
kwiberg087bd342017-02-10 08:15:44 -080013#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
Peter Kastingdce40cf2015-08-24 14:52:23 -070014#include "webrtc/base/format_macros.h"
tommidea489f2017-03-03 03:20:24 -080015#include "webrtc/base/location.h"
pbosad856222015-11-27 09:48:36 -080016#include "webrtc/base/logging.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000017#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kjellander3e6db232015-11-26 04:44:54 -080018#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000019#include "webrtc/modules/audio_device/audio_device_impl.h"
20#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000022#include "webrtc/voice_engine/channel.h"
23#include "webrtc/voice_engine/include/voe_errors.h"
24#include "webrtc/voice_engine/output_mixer.h"
25#include "webrtc/voice_engine/transmit_mixer.h"
26#include "webrtc/voice_engine/utility.h"
27#include "webrtc/voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
Jelena Marusic2dd6a272015-04-14 09:47:00 +020029namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000030
Jelena Marusic2dd6a272015-04-14 09:47:00 +020031VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine) {
32 if (nullptr == voiceEngine) {
33 return nullptr;
34 }
35 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
36 s->AddRef();
37 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
Jelena Marusic2dd6a272015-04-14 09:47:00 +020040VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared)
41 : voiceEngineObserverPtr_(nullptr),
Jelena Marusic2dd6a272015-04-14 09:47:00 +020042 shared_(shared) {}
43
44VoEBaseImpl::~VoEBaseImpl() {
45 TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
solenberg13725082015-11-25 08:16:52 -080048void VoEBaseImpl::OnErrorIsReported(const ErrorCode error) {
tommi31fc21f2016-01-21 10:37:37 -080049 rtc::CritScope cs(&callbackCritSect_);
Jelena Marusic2dd6a272015-04-14 09:47:00 +020050 int errCode = 0;
51 if (error == AudioDeviceObserver::kRecordingError) {
52 errCode = VE_RUNTIME_REC_ERROR;
53 LOG_F(LS_ERROR) << "VE_RUNTIME_REC_ERROR";
54 } else if (error == AudioDeviceObserver::kPlayoutError) {
55 errCode = VE_RUNTIME_PLAY_ERROR;
56 LOG_F(LS_ERROR) << "VE_RUNTIME_PLAY_ERROR";
57 }
58 if (voiceEngineObserverPtr_) {
59 // Deliver callback (-1 <=> no channel dependency)
60 voiceEngineObserverPtr_->CallbackOnError(-1, errCode);
61 }
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
solenberg13725082015-11-25 08:16:52 -080064void VoEBaseImpl::OnWarningIsReported(const WarningCode warning) {
tommi31fc21f2016-01-21 10:37:37 -080065 rtc::CritScope cs(&callbackCritSect_);
Jelena Marusic2dd6a272015-04-14 09:47:00 +020066 int warningCode = 0;
67 if (warning == AudioDeviceObserver::kRecordingWarning) {
68 warningCode = VE_RUNTIME_REC_WARNING;
69 LOG_F(LS_WARNING) << "VE_RUNTIME_REC_WARNING";
70 } else if (warning == AudioDeviceObserver::kPlayoutWarning) {
71 warningCode = VE_RUNTIME_PLAY_WARNING;
72 LOG_F(LS_WARNING) << "VE_RUNTIME_PLAY_WARNING";
73 }
74 if (voiceEngineObserverPtr_) {
75 // Deliver callback (-1 <=> no channel dependency)
76 voiceEngineObserverPtr_->CallbackOnError(-1, warningCode);
77 }
niklase@google.com470e71d2011-07-07 08:21:25 +000078}
79
henrikaec6fbd22017-03-31 05:43:36 -070080int32_t VoEBaseImpl::RecordedDataIsAvailable(
81 const void* audio_data,
82 const size_t number_of_frames,
83 const size_t bytes_per_sample,
84 const size_t number_of_channels,
85 const uint32_t sample_rate,
86 const uint32_t audio_delay_milliseconds,
87 const int32_t clock_drift,
88 const uint32_t volume,
89 const bool key_pressed,
90 uint32_t& new_mic_volume) {
91 RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample);
92 RTC_DCHECK(shared_->transmit_mixer() != nullptr);
93 RTC_DCHECK(shared_->audio_device() != nullptr);
94
95 uint32_t max_volume = 0;
96 uint16_t voe_mic_level = 0;
97 // Check for zero to skip this calculation; the consumer may use this to
98 // indicate no volume is available.
99 if (volume != 0) {
100 // Scale from ADM to VoE level range
101 if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
102 if (max_volume) {
103 voe_mic_level = static_cast<uint16_t>(
104 (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) /
105 max_volume);
106 }
107 }
108 // We learned that on certain systems (e.g Linux) the voe_mic_level
109 // can be greater than the maxVolumeLevel therefore
110 // we are going to cap the voe_mic_level to the maxVolumeLevel
111 // and change the maxVolume to volume if it turns out that
112 // the voe_mic_level is indeed greater than the maxVolumeLevel.
113 if (voe_mic_level > kMaxVolumeLevel) {
114 voe_mic_level = kMaxVolumeLevel;
115 max_volume = volume;
116 }
117 }
118
119 // Perform channel-independent operations
120 // (APM, mix with file, record to file, mute, etc.)
121 shared_->transmit_mixer()->PrepareDemux(
122 audio_data, number_of_frames, number_of_channels, sample_rate,
123 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
124 voe_mic_level, key_pressed);
125
126 // Copy the audio frame to each sending channel and perform
127 // channel-dependent operations (file mixing, mute, etc.), encode and
128 // packetize+transmit the RTP packet.
129 shared_->transmit_mixer()->ProcessAndEncodeAudio();
130
131 // Scale from VoE to ADM level range.
132 uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel();
133 if (new_voe_mic_level != voe_mic_level) {
134 // Return the new volume if AGC has changed the volume.
135 return static_cast<int>((new_voe_mic_level * max_volume +
136 static_cast<int>(kMaxVolumeLevel / 2)) /
137 kMaxVolumeLevel);
138 }
139
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200140 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
solenberg13725082015-11-25 08:16:52 -0800143int32_t VoEBaseImpl::NeedMorePlayData(const size_t nSamples,
144 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -0800145 const size_t nChannels,
solenberg13725082015-11-25 08:16:52 -0800146 const uint32_t samplesPerSec,
147 void* audioSamples,
148 size_t& nSamplesOut,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200149 int64_t* elapsed_time_ms,
150 int64_t* ntp_time_ms) {
Peter Kasting69558702016-01-12 16:26:35 -0800151 GetPlayoutData(static_cast<int>(samplesPerSec), nChannels, nSamples, true,
152 audioSamples, elapsed_time_ms, ntp_time_ms);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200153 nSamplesOut = audioFrame_.samples_per_channel_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000154 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
xians@webrtc.org56925312014-04-14 10:50:37 +0000157void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data,
158 int bits_per_sample, int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800159 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 size_t number_of_frames) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200161 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel);
henrikaec6fbd22017-03-31 05:43:36 -0700162 voe::Channel* channel = ch.channel();
163 if (!channel)
164 return;
165 if (channel->Sending()) {
166 // Send the audio to each channel directly without using the APM in the
167 // transmit mixer.
168 channel->ProcessAndEncodeAudio(static_cast<const int16_t*>(audio_data),
169 sample_rate, number_of_frames,
170 number_of_channels);
xians@webrtc.org07e51962014-01-29 13:54:02 +0000171 }
172}
173
Peter Kastingdce40cf2015-08-24 14:52:23 -0700174void VoEBaseImpl::PullRenderData(int bits_per_sample,
175 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800176 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700177 size_t number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200178 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000179 int64_t* ntp_time_ms) {
Jelena Marusic6fc2d2f2015-04-13 14:07:04 +0200180 assert(bits_per_sample == 16);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700181 assert(number_of_frames == static_cast<size_t>(sample_rate / 100));
xians@webrtc.org56925312014-04-14 10:50:37 +0000182
183 GetPlayoutData(sample_rate, number_of_channels, number_of_frames, false,
wu@webrtc.org94454b72014-06-05 20:34:08 +0000184 audio_data, elapsed_time_ms, ntp_time_ms);
xians@webrtc.org56925312014-04-14 10:50:37 +0000185}
186
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200187int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
tommi31fc21f2016-01-21 10:37:37 -0800188 rtc::CritScope cs(&callbackCritSect_);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200189 if (voiceEngineObserverPtr_) {
190 shared_->SetLastError(
191 VE_INVALID_OPERATION, kTraceError,
192 "RegisterVoiceEngineObserver() observer already enabled");
193 return -1;
194 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200196 // Register the observer in all active channels
197 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
198 it.IsValid(); it.Increment()) {
199 it.GetChannel()->RegisterVoiceEngineObserver(observer);
200 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000201
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200202 shared_->transmit_mixer()->RegisterVoiceEngineObserver(observer);
203 voiceEngineObserverPtr_ = &observer;
204 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200207int VoEBaseImpl::DeRegisterVoiceEngineObserver() {
tommi31fc21f2016-01-21 10:37:37 -0800208 rtc::CritScope cs(&callbackCritSect_);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200209 if (!voiceEngineObserverPtr_) {
210 shared_->SetLastError(
211 VE_INVALID_OPERATION, kTraceError,
212 "DeRegisterVoiceEngineObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000213 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200214 }
215 voiceEngineObserverPtr_ = nullptr;
216
217 // Deregister the observer in all active channels
218 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
219 it.IsValid(); it.Increment()) {
220 it.GetChannel()->DeRegisterVoiceEngineObserver();
221 }
222
223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
ossu5f7cfa52016-05-30 08:11:28 -0700226int VoEBaseImpl::Init(
227 AudioDeviceModule* external_adm,
228 AudioProcessing* audioproc,
229 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
tommi31fc21f2016-01-21 10:37:37 -0800230 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200231 WebRtcSpl_Init();
232 if (shared_->statistics().Initialized()) {
233 return 0;
234 }
235 if (shared_->process_thread()) {
236 shared_->process_thread()->Start();
237 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200239 // Create an internal ADM if the user has not added an external
240 // ADM implementation as input to Init().
241 if (external_adm == nullptr) {
Tommi931e6582015-05-20 09:44:38 +0200242#if !defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
243 return -1;
244#else
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200245 // Create the internal ADM implementation.
Peter Boström4adbbcf2016-05-03 15:51:26 -0400246 shared_->set_audio_device(AudioDeviceModule::Create(
solenberg5b3e49a2017-03-15 08:08:07 -0700247 VoEId(shared_->instance_id(), -1),
248 AudioDeviceModule::kPlatformDefaultAudio));
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200249 if (shared_->audio_device() == nullptr) {
250 shared_->SetLastError(VE_NO_MEMORY, kTraceCritical,
251 "Init() failed to create the ADM");
252 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253 }
Tommi931e6582015-05-20 09:44:38 +0200254#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200255 } else {
256 // Use the already existing external ADM implementation.
257 shared_->set_audio_device(external_adm);
258 LOG_F(LS_INFO)
259 << "An external ADM implementation will be used in VoiceEngine";
260 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200262 // Register the ADM to the process thread, which will drive the error
263 // callback mechanism
264 if (shared_->process_thread()) {
tommidea489f2017-03-03 03:20:24 -0800265 shared_->process_thread()->RegisterModule(shared_->audio_device(),
266 RTC_FROM_HERE);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200267 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200269 bool available = false;
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000270
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200271 // --------------------
272 // Reinitialize the ADM
henrika@google.com73d65512011-09-07 15:11:18 +0000273
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200274 // Register the AudioObserver implementation
275 if (shared_->audio_device()->RegisterEventObserver(this) != 0) {
276 shared_->SetLastError(
277 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
278 "Init() failed to register event observer for the ADM");
279 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200281 // Register the AudioTransport implementation
282 if (shared_->audio_device()->RegisterAudioCallback(this) != 0) {
283 shared_->SetLastError(
284 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
285 "Init() failed to register audio callback for the ADM");
286 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000287
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200288 // ADM initialization
289 if (shared_->audio_device()->Init() != 0) {
290 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
291 "Init() failed to initialize the ADM");
292 return -1;
293 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000294
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200295 // Initialize the default speaker
296 if (shared_->audio_device()->SetPlayoutDevice(
297 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
298 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo,
299 "Init() failed to set the default output device");
300 }
301 if (shared_->audio_device()->InitSpeaker() != 0) {
302 shared_->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
303 "Init() failed to initialize the speaker");
304 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000305
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200306 // Initialize the default microphone
307 if (shared_->audio_device()->SetRecordingDevice(
308 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
309 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo,
310 "Init() failed to set the default input device");
311 }
312 if (shared_->audio_device()->InitMicrophone() != 0) {
313 shared_->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
314 "Init() failed to initialize the microphone");
315 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000316
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200317 // Set number of channels
318 if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
319 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
320 "Init() failed to query stereo playout mode");
321 }
322 if (shared_->audio_device()->SetStereoPlayout(available) != 0) {
323 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
324 "Init() failed to set mono/stereo playout mode");
325 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200327 // TODO(andrew): These functions don't tell us whether stereo recording
328 // is truly available. We simply set the AudioProcessing input to stereo
329 // here, because we have to wait until receiving the first frame to
330 // determine the actual number of channels anyway.
331 //
332 // These functions may be changed; tracked here:
333 // http://code.google.com/p/webrtc/issues/detail?id=204
334 shared_->audio_device()->StereoRecordingIsAvailable(&available);
335 if (shared_->audio_device()->SetStereoRecording(available) != 0) {
336 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
337 "Init() failed to set mono/stereo recording mode");
338 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200340 if (!audioproc) {
341 audioproc = AudioProcessing::Create();
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000342 if (!audioproc) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200343 LOG(LS_ERROR) << "Failed to create AudioProcessing.";
344 shared_->SetLastError(VE_NO_MEMORY);
345 return -1;
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000346 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200347 }
348 shared_->set_audio_processing(audioproc);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000349
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200350 // Set the error state for any failures in this block.
351 shared_->SetLastError(VE_APM_ERROR);
352 // Configure AudioProcessing components.
353 if (audioproc->high_pass_filter()->Enable(true) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200354 LOG_F(LS_ERROR) << "Failed to enable high pass filter.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200355 return -1;
356 }
357 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200358 LOG_F(LS_ERROR) << "Failed to disable drift compensation.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200359 return -1;
360 }
361 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200362 LOG_F(LS_ERROR) << "Failed to set noise suppression level: "
363 << kDefaultNsMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200364 return -1;
365 }
366 GainControl* agc = audioproc->gain_control();
367 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200368 LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: "
369 << kMinVolumeLevel << " and maximum: " << kMaxVolumeLevel;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200370 return -1;
371 }
372 if (agc->set_mode(kDefaultAgcMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200373 LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200374 return -1;
375 }
376 if (agc->Enable(kDefaultAgcState) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200377 LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200378 return -1;
379 }
380 shared_->SetLastError(0); // Clear error state.
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000381
niklase@google.com470e71d2011-07-07 08:21:25 +0000382#ifdef WEBRTC_VOICE_ENGINE_AGC
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200383 bool agc_enabled =
384 agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled();
385 if (shared_->audio_device()->SetAGC(agc_enabled) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200386 LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200387 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
388 // TODO(ajm): No error return here due to
389 // https://code.google.com/p/webrtc/issues/detail?id=1464
390 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000391#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000392
ossu5f7cfa52016-05-30 08:11:28 -0700393 if (decoder_factory)
394 decoder_factory_ = decoder_factory;
395 else
396 decoder_factory_ = CreateBuiltinAudioDecoderFactory();
397
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200398 return shared_->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000399}
400
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200401int VoEBaseImpl::Terminate() {
tommi31fc21f2016-01-21 10:37:37 -0800402 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200403 return TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000406int VoEBaseImpl::CreateChannel() {
solenberg88499ec2016-09-07 07:34:41 -0700407 return CreateChannel(ChannelConfig());
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000408}
409
solenberg88499ec2016-09-07 07:34:41 -0700410int VoEBaseImpl::CreateChannel(const ChannelConfig& config) {
tommi31fc21f2016-01-21 10:37:37 -0800411 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200412 if (!shared_->statistics().Initialized()) {
413 shared_->SetLastError(VE_NOT_INITED, kTraceError);
414 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000415 }
solenberg88499ec2016-09-07 07:34:41 -0700416
417 ChannelConfig config_copy(config);
418 config_copy.acm_config.decoder_factory = decoder_factory_;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200419 voe::ChannelOwner channel_owner =
solenberg88499ec2016-09-07 07:34:41 -0700420 shared_->channel_manager().CreateChannel(config_copy);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000421 return InitializeChannel(&channel_owner);
422}
423
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200424int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) {
425 if (channel_owner->channel()->SetEngineInformation(
426 shared_->statistics(), *shared_->output_mixer(),
solenberg796b8f92017-03-01 17:02:23 -0800427 *shared_->process_thread(), *shared_->audio_device(),
henrikaec6fbd22017-03-31 05:43:36 -0700428 voiceEngineObserverPtr_, &callbackCritSect_,
429 shared_->encoder_queue()) != 0) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200430 shared_->SetLastError(
431 VE_CHANNEL_NOT_CREATED, kTraceError,
432 "CreateChannel() failed to associate engine and channel."
433 " Destroying channel.");
434 shared_->channel_manager().DestroyChannel(
435 channel_owner->channel()->ChannelId());
436 return -1;
437 } else if (channel_owner->channel()->Init() != 0) {
438 shared_->SetLastError(
439 VE_CHANNEL_NOT_CREATED, kTraceError,
440 "CreateChannel() failed to initialize channel. Destroying"
441 " channel.");
442 shared_->channel_manager().DestroyChannel(
443 channel_owner->channel()->ChannelId());
444 return -1;
445 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200446 return channel_owner->channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200449int VoEBaseImpl::DeleteChannel(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800450 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200451 if (!shared_->statistics().Initialized()) {
452 shared_->SetLastError(VE_NOT_INITED, kTraceError);
453 return -1;
454 }
455
456 {
457 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
458 voe::Channel* channelPtr = ch.channel();
459 if (channelPtr == nullptr) {
460 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
461 "DeleteChannel() failed to locate channel");
462 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000463 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200464 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200466 shared_->channel_manager().DestroyChannel(channel);
467 if (StopSend() != 0) {
468 return -1;
469 }
470 if (StopPlayout() != 0) {
471 return -1;
472 }
473 return 0;
474}
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200476int VoEBaseImpl::StartReceive(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800477 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200478 if (!shared_->statistics().Initialized()) {
479 shared_->SetLastError(VE_NOT_INITED, kTraceError);
480 return -1;
481 }
482 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
483 voe::Channel* channelPtr = ch.channel();
484 if (channelPtr == nullptr) {
485 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
486 "StartReceive() failed to locate channel");
487 return -1;
488 }
solenberge566ac72016-10-31 12:52:33 -0700489 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200490}
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200492int VoEBaseImpl::StartPlayout(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800493 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200494 if (!shared_->statistics().Initialized()) {
495 shared_->SetLastError(VE_NOT_INITED, kTraceError);
496 return -1;
497 }
498 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
499 voe::Channel* channelPtr = ch.channel();
500 if (channelPtr == nullptr) {
501 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
502 "StartPlayout() failed to locate channel");
503 return -1;
504 }
505 if (channelPtr->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000506 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200507 }
508 if (StartPlayout() != 0) {
509 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
510 "StartPlayout() failed to start playout");
511 return -1;
512 }
513 return channelPtr->StartPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000514}
515
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200516int VoEBaseImpl::StopPlayout(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800517 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200518 if (!shared_->statistics().Initialized()) {
519 shared_->SetLastError(VE_NOT_INITED, kTraceError);
520 return -1;
521 }
522 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
523 voe::Channel* channelPtr = ch.channel();
524 if (channelPtr == nullptr) {
525 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
526 "StopPlayout() failed to locate channel");
527 return -1;
528 }
529 if (channelPtr->StopPlayout() != 0) {
530 LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel "
531 << channel;
532 }
533 return StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000534}
535
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200536int VoEBaseImpl::StartSend(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800537 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200538 if (!shared_->statistics().Initialized()) {
539 shared_->SetLastError(VE_NOT_INITED, kTraceError);
540 return -1;
541 }
542 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
543 voe::Channel* channelPtr = ch.channel();
544 if (channelPtr == nullptr) {
545 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
546 "StartSend() failed to locate channel");
547 return -1;
548 }
549 if (channelPtr->Sending()) {
550 return 0;
551 }
552 if (StartSend() != 0) {
553 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
554 "StartSend() failed to start recording");
555 return -1;
556 }
557 return channelPtr->StartSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000558}
559
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200560int VoEBaseImpl::StopSend(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800561 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200562 if (!shared_->statistics().Initialized()) {
563 shared_->SetLastError(VE_NOT_INITED, kTraceError);
564 return -1;
565 }
566 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
567 voe::Channel* channelPtr = ch.channel();
568 if (channelPtr == nullptr) {
569 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
570 "StopSend() failed to locate channel");
571 return -1;
572 }
henrikaec6fbd22017-03-31 05:43:36 -0700573 channelPtr->StopSend();
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200574 return StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000575}
576
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200577int VoEBaseImpl::GetVersion(char version[1024]) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200578 if (version == nullptr) {
579 shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200580 return -1;
581 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000582
solenberg2515af22015-12-02 06:19:36 -0800583 std::string versionString = VoiceEngine::GetVersionString();
kwibergaf476c72016-11-28 15:21:39 -0800584 RTC_DCHECK_GT(1024, versionString.size() + 1);
solenberg2515af22015-12-02 06:19:36 -0800585 char* end = std::copy(versionString.cbegin(), versionString.cend(), version);
586 end[0] = '\n';
587 end[1] = '\0';
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200588 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000589}
590
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200591int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000592
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200593int32_t VoEBaseImpl::StartPlayout() {
solenberge313e022015-09-08 02:16:04 -0700594 if (!shared_->audio_device()->Playing()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200595 if (shared_->audio_device()->InitPlayout() != 0) {
596 LOG_F(LS_ERROR) << "Failed to initialize playout";
597 return -1;
598 }
599 if (shared_->audio_device()->StartPlayout() != 0) {
600 LOG_F(LS_ERROR) << "Failed to start playout";
601 return -1;
602 }
603 }
604 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000605}
606
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000607int32_t VoEBaseImpl::StopPlayout() {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000608 // Stop audio-device playing if no channel is playing out
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200609 if (shared_->NumOfPlayingChannels() == 0) {
610 if (shared_->audio_device()->StopPlayout() != 0) {
611 shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError,
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000612 "StopPlayout() failed to stop playout");
613 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000615 }
616 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200619int32_t VoEBaseImpl::StartSend() {
solenbergd53a3f92016-04-14 13:56:37 -0700620 if (!shared_->audio_device()->RecordingIsInitialized() &&
621 !shared_->audio_device()->Recording()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200622 if (shared_->audio_device()->InitRecording() != 0) {
623 LOG_F(LS_ERROR) << "Failed to initialize recording";
624 return -1;
625 }
solenbergd53a3f92016-04-14 13:56:37 -0700626 }
627 if (!shared_->audio_device()->Recording()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200628 if (shared_->audio_device()->StartRecording() != 0) {
629 LOG_F(LS_ERROR) << "Failed to start recording";
630 return -1;
631 }
632 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200633 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634}
635
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200636int32_t VoEBaseImpl::StopSend() {
637 if (shared_->NumOfSendingChannels() == 0 &&
638 !shared_->transmit_mixer()->IsRecordingMic()) {
639 // Stop audio-device recording if no channel is recording
640 if (shared_->audio_device()->StopRecording() != 0) {
641 shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
642 "StopSend() failed to stop recording");
643 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000644 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200645 shared_->transmit_mixer()->StopSend();
646 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000647
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200648 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000649}
650
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200651int32_t VoEBaseImpl::TerminateInternal() {
652 // Delete any remaining channel objects
653 shared_->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000654
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200655 if (shared_->process_thread()) {
656 if (shared_->audio_device()) {
657 shared_->process_thread()->DeRegisterModule(shared_->audio_device());
niklase@google.com470e71d2011-07-07 08:21:25 +0000658 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200659 shared_->process_thread()->Stop();
660 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000661
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200662 if (shared_->audio_device()) {
663 if (shared_->audio_device()->StopPlayout() != 0) {
664 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
665 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000666 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200667 if (shared_->audio_device()->StopRecording() != 0) {
668 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
669 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000670 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200671 if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) {
672 shared_->SetLastError(
673 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
674 "TerminateInternal() failed to de-register event observer "
675 "for the ADM");
676 }
677 if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) {
678 shared_->SetLastError(
679 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
680 "TerminateInternal() failed to de-register audio callback "
681 "for the ADM");
682 }
683 if (shared_->audio_device()->Terminate() != 0) {
684 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
685 "TerminateInternal() failed to terminate the ADM");
686 }
687 shared_->set_audio_device(nullptr);
688 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000689
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200690 if (shared_->audio_processing()) {
691 shared_->set_audio_processing(nullptr);
692 }
693
694 return shared_->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000695}
696
Peter Kasting69558702016-01-12 16:26:35 -0800697void VoEBaseImpl::GetPlayoutData(int sample_rate, size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700698 size_t number_of_frames, bool feed_data_to_apm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200699 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000700 int64_t* ntp_time_ms) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200701 assert(shared_->output_mixer() != nullptr);
xians@webrtc.org56925312014-04-14 10:50:37 +0000702
703 // TODO(andrew): if the device is running in mono, we should tell the mixer
704 // here so that it will only request mono from AudioCodingModule.
705 // Perform mixing of all active participants (channel-based mixing)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200706 shared_->output_mixer()->MixActiveChannels();
xians@webrtc.org56925312014-04-14 10:50:37 +0000707
708 // Additional operations on the combined signal
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200709 shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm);
xians@webrtc.org56925312014-04-14 10:50:37 +0000710
711 // Retrieve the final output mix (resampled to match the ADM)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200712 shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels,
713 &audioFrame_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000714
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200715 assert(number_of_frames == audioFrame_.samples_per_channel_);
716 assert(sample_rate == audioFrame_.sample_rate_hz_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000717
718 // Deliver audio (PCM) samples to the ADM
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200719 memcpy(audio_data, audioFrame_.data_,
xians@webrtc.org56925312014-04-14 10:50:37 +0000720 sizeof(int16_t) * number_of_frames * number_of_channels);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000721
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200722 *elapsed_time_ms = audioFrame_.elapsed_time_ms_;
723 *ntp_time_ms = audioFrame_.ntp_time_ms_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000724}
725
Minyue2013aec2015-05-13 14:14:42 +0200726int VoEBaseImpl::AssociateSendChannel(int channel,
727 int accociate_send_channel) {
tommi31fc21f2016-01-21 10:37:37 -0800728 rtc::CritScope cs(shared_->crit_sec());
Minyue2013aec2015-05-13 14:14:42 +0200729
730 if (!shared_->statistics().Initialized()) {
731 shared_->SetLastError(VE_NOT_INITED, kTraceError);
732 return -1;
733 }
734
735 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
736 voe::Channel* channel_ptr = ch.channel();
737 if (channel_ptr == NULL) {
738 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
739 "AssociateSendChannel() failed to locate channel");
740 return -1;
741 }
742
743 ch = shared_->channel_manager().GetChannel(accociate_send_channel);
744 voe::Channel* accociate_send_channel_ptr = ch.channel();
745 if (accociate_send_channel_ptr == NULL) {
746 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
747 "AssociateSendChannel() failed to locate accociate_send_channel");
748 return -1;
749 }
750
751 channel_ptr->set_associate_send_channel(ch);
752 return 0;
753}
754
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000755} // namespace webrtc