blob: 3e5cfbb14b7945d819b3a58aae90cc562c0dc6c1 [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
Peter Kastingdce40cf2015-08-24 14:52:23 -070013#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080014#include "webrtc/base/logging.h"
turaj@webrtc.org03f33702013-11-13 00:02:48 +000015#include "webrtc/common.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000016#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kjellander3e6db232015-11-26 04:44:54 -080017#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000018#include "webrtc/modules/audio_device/audio_device_impl.h"
19#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
21#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),
42 callbackCritSect_(*CriticalSectionWrapper::CreateCriticalSection()),
43 shared_(shared) {}
44
45VoEBaseImpl::~VoEBaseImpl() {
46 TerminateInternal();
47 delete &callbackCritSect_;
niklase@google.com470e71d2011-07-07 08:21:25 +000048}
49
solenberg13725082015-11-25 08:16:52 -080050void VoEBaseImpl::OnErrorIsReported(const ErrorCode error) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +020051 CriticalSectionScoped cs(&callbackCritSect_);
52 int errCode = 0;
53 if (error == AudioDeviceObserver::kRecordingError) {
54 errCode = VE_RUNTIME_REC_ERROR;
55 LOG_F(LS_ERROR) << "VE_RUNTIME_REC_ERROR";
56 } else if (error == AudioDeviceObserver::kPlayoutError) {
57 errCode = VE_RUNTIME_PLAY_ERROR;
58 LOG_F(LS_ERROR) << "VE_RUNTIME_PLAY_ERROR";
59 }
60 if (voiceEngineObserverPtr_) {
61 // Deliver callback (-1 <=> no channel dependency)
62 voiceEngineObserverPtr_->CallbackOnError(-1, errCode);
63 }
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
solenberg13725082015-11-25 08:16:52 -080066void VoEBaseImpl::OnWarningIsReported(const WarningCode warning) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +020067 CriticalSectionScoped cs(&callbackCritSect_);
68 int warningCode = 0;
69 if (warning == AudioDeviceObserver::kRecordingWarning) {
70 warningCode = VE_RUNTIME_REC_WARNING;
71 LOG_F(LS_WARNING) << "VE_RUNTIME_REC_WARNING";
72 } else if (warning == AudioDeviceObserver::kPlayoutWarning) {
73 warningCode = VE_RUNTIME_PLAY_WARNING;
74 LOG_F(LS_WARNING) << "VE_RUNTIME_PLAY_WARNING";
75 }
76 if (voiceEngineObserverPtr_) {
77 // Deliver callback (-1 <=> no channel dependency)
78 voiceEngineObserverPtr_->CallbackOnError(-1, warningCode);
79 }
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
solenberg13725082015-11-25 08:16:52 -080082int32_t VoEBaseImpl::RecordedDataIsAvailable(const void* audioSamples,
83 const size_t nSamples,
84 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080085 const size_t nChannels,
solenberg13725082015-11-25 08:16:52 -080086 const uint32_t samplesPerSec,
87 const uint32_t totalDelayMS,
88 const int32_t clockDrift,
89 const uint32_t currentMicLevel,
90 const bool keyPressed,
91 uint32_t& newMicLevel) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +020092 newMicLevel = static_cast<uint32_t>(ProcessRecordedDataWithAPM(
93 nullptr, 0, audioSamples, samplesPerSec, nChannels, nSamples,
solenberg13725082015-11-25 08:16:52 -080094 totalDelayMS, clockDrift, currentMicLevel, keyPressed));
Jelena Marusic2dd6a272015-04-14 09:47:00 +020095 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
solenberg13725082015-11-25 08:16:52 -080098int32_t VoEBaseImpl::NeedMorePlayData(const size_t nSamples,
99 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -0800100 const size_t nChannels,
solenberg13725082015-11-25 08:16:52 -0800101 const uint32_t samplesPerSec,
102 void* audioSamples,
103 size_t& nSamplesOut,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200104 int64_t* elapsed_time_ms,
105 int64_t* ntp_time_ms) {
Peter Kasting69558702016-01-12 16:26:35 -0800106 GetPlayoutData(static_cast<int>(samplesPerSec), nChannels, nSamples, true,
107 audioSamples, elapsed_time_ms, ntp_time_ms);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200108 nSamplesOut = audioFrame_.samples_per_channel_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000109 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000110}
111
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000112int VoEBaseImpl::OnDataAvailable(const int voe_channels[],
Peter Kasting69558702016-01-12 16:26:35 -0800113 size_t number_of_voe_channels,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200114 const int16_t* audio_data, int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800115 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 size_t number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200117 int audio_delay_milliseconds, int volume,
118 bool key_pressed, bool need_audio_processing) {
119 if (number_of_voe_channels == 0) return 0;
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000120
121 if (need_audio_processing) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000122 return ProcessRecordedDataWithAPM(
123 voe_channels, number_of_voe_channels, audio_data, sample_rate,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200124 number_of_channels, number_of_frames, audio_delay_milliseconds, 0,
125 volume, key_pressed);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000126 }
127
128 // No need to go through the APM, demultiplex the data to each VoE channel,
129 // encode and send to the network.
Peter Kasting69558702016-01-12 16:26:35 -0800130 for (size_t i = 0; i < number_of_voe_channels; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000131 // TODO(ajm): In the case where multiple channels are using the same codec
132 // rate, this path needlessly does extra conversions. We should convert once
133 // and share between channels.
xians@webrtc.org56925312014-04-14 10:50:37 +0000134 PushCaptureData(voe_channels[i], audio_data, 16, sample_rate,
135 number_of_channels, number_of_frames);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000136 }
137
138 // Return 0 to indicate no need to change the volume.
139 return 0;
140}
141
xians@webrtc.orgc1e28032014-02-02 15:30:20 +0000142void VoEBaseImpl::OnData(int voe_channel, const void* audio_data,
143 int bits_per_sample, int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800144 size_t number_of_channels, size_t number_of_frames) {
xians@webrtc.org56925312014-04-14 10:50:37 +0000145 PushCaptureData(voe_channel, audio_data, bits_per_sample, sample_rate,
146 number_of_channels, number_of_frames);
147}
148
149void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data,
150 int bits_per_sample, int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800151 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 size_t number_of_frames) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200153 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel);
xians@webrtc.org07e51962014-01-29 13:54:02 +0000154 voe::Channel* channel_ptr = ch.channel();
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200155 if (!channel_ptr) return;
xians@webrtc.org07e51962014-01-29 13:54:02 +0000156
henrika@webrtc.org66803482014-04-17 10:45:01 +0000157 if (channel_ptr->Sending()) {
xians@webrtc.org07e51962014-01-29 13:54:02 +0000158 channel_ptr->Demultiplex(static_cast<const int16_t*>(audio_data),
159 sample_rate, number_of_frames, number_of_channels);
160 channel_ptr->PrepareEncodeAndSend(sample_rate);
161 channel_ptr->EncodeAndSend();
162 }
163}
164
Peter Kastingdce40cf2015-08-24 14:52:23 -0700165void VoEBaseImpl::PullRenderData(int bits_per_sample,
166 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800167 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700168 size_t number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200169 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000170 int64_t* ntp_time_ms) {
Jelena Marusic6fc2d2f2015-04-13 14:07:04 +0200171 assert(bits_per_sample == 16);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700172 assert(number_of_frames == static_cast<size_t>(sample_rate / 100));
xians@webrtc.org56925312014-04-14 10:50:37 +0000173
174 GetPlayoutData(sample_rate, number_of_channels, number_of_frames, false,
wu@webrtc.org94454b72014-06-05 20:34:08 +0000175 audio_data, elapsed_time_ms, ntp_time_ms);
xians@webrtc.org56925312014-04-14 10:50:37 +0000176}
177
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200178int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
179 CriticalSectionScoped cs(&callbackCritSect_);
180 if (voiceEngineObserverPtr_) {
181 shared_->SetLastError(
182 VE_INVALID_OPERATION, kTraceError,
183 "RegisterVoiceEngineObserver() observer already enabled");
184 return -1;
185 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200187 // Register the observer in all active channels
188 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
189 it.IsValid(); it.Increment()) {
190 it.GetChannel()->RegisterVoiceEngineObserver(observer);
191 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000192
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200193 shared_->transmit_mixer()->RegisterVoiceEngineObserver(observer);
194 voiceEngineObserverPtr_ = &observer;
195 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200198int VoEBaseImpl::DeRegisterVoiceEngineObserver() {
199 CriticalSectionScoped cs(&callbackCritSect_);
200 if (!voiceEngineObserverPtr_) {
201 shared_->SetLastError(
202 VE_INVALID_OPERATION, kTraceError,
203 "DeRegisterVoiceEngineObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200205 }
206 voiceEngineObserverPtr_ = nullptr;
207
208 // Deregister the observer in all active channels
209 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
210 it.IsValid(); it.Increment()) {
211 it.GetChannel()->DeRegisterVoiceEngineObserver();
212 }
213
214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000217int VoEBaseImpl::Init(AudioDeviceModule* external_adm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200218 AudioProcessing* audioproc) {
219 CriticalSectionScoped cs(shared_->crit_sec());
220 WebRtcSpl_Init();
221 if (shared_->statistics().Initialized()) {
222 return 0;
223 }
224 if (shared_->process_thread()) {
225 shared_->process_thread()->Start();
226 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200228 // Create an internal ADM if the user has not added an external
229 // ADM implementation as input to Init().
230 if (external_adm == nullptr) {
Tommi931e6582015-05-20 09:44:38 +0200231#if !defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
232 return -1;
233#else
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200234 // Create the internal ADM implementation.
235 shared_->set_audio_device(AudioDeviceModuleImpl::Create(
236 VoEId(shared_->instance_id(), -1), shared_->audio_device_layer()));
kma@webrtc.org0221b782012-09-08 00:09:26 +0000237
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200238 if (shared_->audio_device() == nullptr) {
239 shared_->SetLastError(VE_NO_MEMORY, kTraceCritical,
240 "Init() failed to create the ADM");
241 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 }
Tommi931e6582015-05-20 09:44:38 +0200243#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200244 } else {
245 // Use the already existing external ADM implementation.
246 shared_->set_audio_device(external_adm);
247 LOG_F(LS_INFO)
248 << "An external ADM implementation will be used in VoiceEngine";
249 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200251 // Register the ADM to the process thread, which will drive the error
252 // callback mechanism
253 if (shared_->process_thread()) {
254 shared_->process_thread()->RegisterModule(shared_->audio_device());
255 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000256
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200257 bool available = false;
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000258
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200259 // --------------------
260 // Reinitialize the ADM
henrika@google.com73d65512011-09-07 15:11:18 +0000261
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200262 // Register the AudioObserver implementation
263 if (shared_->audio_device()->RegisterEventObserver(this) != 0) {
264 shared_->SetLastError(
265 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
266 "Init() failed to register event observer for the ADM");
267 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200269 // Register the AudioTransport implementation
270 if (shared_->audio_device()->RegisterAudioCallback(this) != 0) {
271 shared_->SetLastError(
272 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
273 "Init() failed to register audio callback for the ADM");
274 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200276 // ADM initialization
277 if (shared_->audio_device()->Init() != 0) {
278 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
279 "Init() failed to initialize the ADM");
280 return -1;
281 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000282
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200283 // Initialize the default speaker
284 if (shared_->audio_device()->SetPlayoutDevice(
285 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
286 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo,
287 "Init() failed to set the default output device");
288 }
289 if (shared_->audio_device()->InitSpeaker() != 0) {
290 shared_->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
291 "Init() failed to initialize the speaker");
292 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000293
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200294 // Initialize the default microphone
295 if (shared_->audio_device()->SetRecordingDevice(
296 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
297 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo,
298 "Init() failed to set the default input device");
299 }
300 if (shared_->audio_device()->InitMicrophone() != 0) {
301 shared_->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
302 "Init() failed to initialize the microphone");
303 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000304
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200305 // Set number of channels
306 if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
307 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
308 "Init() failed to query stereo playout mode");
309 }
310 if (shared_->audio_device()->SetStereoPlayout(available) != 0) {
311 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
312 "Init() failed to set mono/stereo playout mode");
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200315 // TODO(andrew): These functions don't tell us whether stereo recording
316 // is truly available. We simply set the AudioProcessing input to stereo
317 // here, because we have to wait until receiving the first frame to
318 // determine the actual number of channels anyway.
319 //
320 // These functions may be changed; tracked here:
321 // http://code.google.com/p/webrtc/issues/detail?id=204
322 shared_->audio_device()->StereoRecordingIsAvailable(&available);
323 if (shared_->audio_device()->SetStereoRecording(available) != 0) {
324 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
325 "Init() failed to set mono/stereo recording mode");
326 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200328 if (!audioproc) {
329 audioproc = AudioProcessing::Create();
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000330 if (!audioproc) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200331 LOG(LS_ERROR) << "Failed to create AudioProcessing.";
332 shared_->SetLastError(VE_NO_MEMORY);
333 return -1;
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000334 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200335 }
336 shared_->set_audio_processing(audioproc);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000337
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200338 // Set the error state for any failures in this block.
339 shared_->SetLastError(VE_APM_ERROR);
340 // Configure AudioProcessing components.
341 if (audioproc->high_pass_filter()->Enable(true) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200342 LOG_F(LS_ERROR) << "Failed to enable high pass filter.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200343 return -1;
344 }
345 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200346 LOG_F(LS_ERROR) << "Failed to disable drift compensation.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200347 return -1;
348 }
349 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200350 LOG_F(LS_ERROR) << "Failed to set noise suppression level: "
351 << kDefaultNsMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200352 return -1;
353 }
354 GainControl* agc = audioproc->gain_control();
355 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200356 LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: "
357 << kMinVolumeLevel << " and maximum: " << kMaxVolumeLevel;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200358 return -1;
359 }
360 if (agc->set_mode(kDefaultAgcMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200361 LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200362 return -1;
363 }
364 if (agc->Enable(kDefaultAgcState) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200365 LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200366 return -1;
367 }
368 shared_->SetLastError(0); // Clear error state.
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000369
niklase@google.com470e71d2011-07-07 08:21:25 +0000370#ifdef WEBRTC_VOICE_ENGINE_AGC
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200371 bool agc_enabled =
372 agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled();
373 if (shared_->audio_device()->SetAGC(agc_enabled) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200374 LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200375 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
376 // TODO(ajm): No error return here due to
377 // https://code.google.com/p/webrtc/issues/detail?id=1464
378 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000379#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000380
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200381 return shared_->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000382}
383
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200384int VoEBaseImpl::Terminate() {
385 CriticalSectionScoped cs(shared_->crit_sec());
386 return TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000387}
388
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000389int VoEBaseImpl::CreateChannel() {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200390 CriticalSectionScoped cs(shared_->crit_sec());
391 if (!shared_->statistics().Initialized()) {
392 shared_->SetLastError(VE_NOT_INITED, kTraceError);
393 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000394 }
395
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200396 voe::ChannelOwner channel_owner = shared_->channel_manager().CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000397 return InitializeChannel(&channel_owner);
398}
399
400int VoEBaseImpl::CreateChannel(const Config& config) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200401 CriticalSectionScoped cs(shared_->crit_sec());
402 if (!shared_->statistics().Initialized()) {
403 shared_->SetLastError(VE_NOT_INITED, kTraceError);
404 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000405 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200406 voe::ChannelOwner channel_owner =
407 shared_->channel_manager().CreateChannel(config);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000408 return InitializeChannel(&channel_owner);
409}
410
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200411int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) {
412 if (channel_owner->channel()->SetEngineInformation(
413 shared_->statistics(), *shared_->output_mixer(),
414 *shared_->transmit_mixer(), *shared_->process_thread(),
415 *shared_->audio_device(), voiceEngineObserverPtr_,
416 &callbackCritSect_) != 0) {
417 shared_->SetLastError(
418 VE_CHANNEL_NOT_CREATED, kTraceError,
419 "CreateChannel() failed to associate engine and channel."
420 " Destroying channel.");
421 shared_->channel_manager().DestroyChannel(
422 channel_owner->channel()->ChannelId());
423 return -1;
424 } else if (channel_owner->channel()->Init() != 0) {
425 shared_->SetLastError(
426 VE_CHANNEL_NOT_CREATED, kTraceError,
427 "CreateChannel() failed to initialize channel. Destroying"
428 " channel.");
429 shared_->channel_manager().DestroyChannel(
430 channel_owner->channel()->ChannelId());
431 return -1;
432 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200433 return channel_owner->channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000434}
435
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200436int VoEBaseImpl::DeleteChannel(int channel) {
437 CriticalSectionScoped cs(shared_->crit_sec());
438 if (!shared_->statistics().Initialized()) {
439 shared_->SetLastError(VE_NOT_INITED, kTraceError);
440 return -1;
441 }
442
443 {
444 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
445 voe::Channel* channelPtr = ch.channel();
446 if (channelPtr == nullptr) {
447 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
448 "DeleteChannel() failed to locate channel");
449 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200451 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200453 shared_->channel_manager().DestroyChannel(channel);
454 if (StopSend() != 0) {
455 return -1;
456 }
457 if (StopPlayout() != 0) {
458 return -1;
459 }
460 return 0;
461}
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200463int VoEBaseImpl::StartReceive(int channel) {
464 CriticalSectionScoped cs(shared_->crit_sec());
465 if (!shared_->statistics().Initialized()) {
466 shared_->SetLastError(VE_NOT_INITED, kTraceError);
467 return -1;
468 }
469 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
470 voe::Channel* channelPtr = ch.channel();
471 if (channelPtr == nullptr) {
472 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
473 "StartReceive() failed to locate channel");
474 return -1;
475 }
476 return channelPtr->StartReceiving();
477}
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200479int VoEBaseImpl::StopReceive(int channel) {
480 CriticalSectionScoped cs(shared_->crit_sec());
481 if (!shared_->statistics().Initialized()) {
482 shared_->SetLastError(VE_NOT_INITED, kTraceError);
483 return -1;
484 }
485 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
486 voe::Channel* channelPtr = ch.channel();
487 if (channelPtr == nullptr) {
488 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
489 "SetLocalReceiver() failed to locate channel");
490 return -1;
491 }
492 return channelPtr->StopReceiving();
493}
niklase@google.com470e71d2011-07-07 08:21:25 +0000494
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200495int VoEBaseImpl::StartPlayout(int channel) {
496 CriticalSectionScoped cs(shared_->crit_sec());
497 if (!shared_->statistics().Initialized()) {
498 shared_->SetLastError(VE_NOT_INITED, kTraceError);
499 return -1;
500 }
501 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
502 voe::Channel* channelPtr = ch.channel();
503 if (channelPtr == nullptr) {
504 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
505 "StartPlayout() failed to locate channel");
506 return -1;
507 }
508 if (channelPtr->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000509 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200510 }
511 if (StartPlayout() != 0) {
512 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
513 "StartPlayout() failed to start playout");
514 return -1;
515 }
516 return channelPtr->StartPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000517}
518
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200519int VoEBaseImpl::StopPlayout(int channel) {
520 CriticalSectionScoped cs(shared_->crit_sec());
521 if (!shared_->statistics().Initialized()) {
522 shared_->SetLastError(VE_NOT_INITED, kTraceError);
523 return -1;
524 }
525 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
526 voe::Channel* channelPtr = ch.channel();
527 if (channelPtr == nullptr) {
528 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
529 "StopPlayout() failed to locate channel");
530 return -1;
531 }
532 if (channelPtr->StopPlayout() != 0) {
533 LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel "
534 << channel;
535 }
536 return StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000537}
538
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200539int VoEBaseImpl::StartSend(int channel) {
540 CriticalSectionScoped cs(shared_->crit_sec());
541 if (!shared_->statistics().Initialized()) {
542 shared_->SetLastError(VE_NOT_INITED, kTraceError);
543 return -1;
544 }
545 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
546 voe::Channel* channelPtr = ch.channel();
547 if (channelPtr == nullptr) {
548 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
549 "StartSend() failed to locate channel");
550 return -1;
551 }
552 if (channelPtr->Sending()) {
553 return 0;
554 }
555 if (StartSend() != 0) {
556 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
557 "StartSend() failed to start recording");
558 return -1;
559 }
560 return channelPtr->StartSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000561}
562
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200563int VoEBaseImpl::StopSend(int channel) {
564 CriticalSectionScoped cs(shared_->crit_sec());
565 if (!shared_->statistics().Initialized()) {
566 shared_->SetLastError(VE_NOT_INITED, kTraceError);
567 return -1;
568 }
569 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
570 voe::Channel* channelPtr = ch.channel();
571 if (channelPtr == nullptr) {
572 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
573 "StopSend() failed to locate channel");
574 return -1;
575 }
576 if (channelPtr->StopSend() != 0) {
577 LOG_F(LS_WARNING) << "StopSend() failed to stop sending for channel "
578 << channel;
579 }
580 return StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000581}
582
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200583int VoEBaseImpl::GetVersion(char version[1024]) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200584 if (version == nullptr) {
585 shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200586 return -1;
587 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000588
solenberg2515af22015-12-02 06:19:36 -0800589 std::string versionString = VoiceEngine::GetVersionString();
590 RTC_DCHECK_GT(1024u, versionString.size() + 1);
591 char* end = std::copy(versionString.cbegin(), versionString.cend(), version);
592 end[0] = '\n';
593 end[1] = '\0';
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200594 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595}
596
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200597int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000598
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200599int32_t VoEBaseImpl::StartPlayout() {
solenberge313e022015-09-08 02:16:04 -0700600 if (!shared_->audio_device()->Playing()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200601 if (shared_->audio_device()->InitPlayout() != 0) {
602 LOG_F(LS_ERROR) << "Failed to initialize playout";
603 return -1;
604 }
605 if (shared_->audio_device()->StartPlayout() != 0) {
606 LOG_F(LS_ERROR) << "Failed to start playout";
607 return -1;
608 }
609 }
610 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611}
612
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000613int32_t VoEBaseImpl::StopPlayout() {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000614 // Stop audio-device playing if no channel is playing out
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200615 if (shared_->NumOfPlayingChannels() == 0) {
616 if (shared_->audio_device()->StopPlayout() != 0) {
617 shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError,
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000618 "StopPlayout() failed to stop playout");
619 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000620 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000621 }
622 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000623}
624
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200625int32_t VoEBaseImpl::StartSend() {
solenberge313e022015-09-08 02:16:04 -0700626 if (!shared_->audio_device()->Recording()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200627 if (shared_->audio_device()->InitRecording() != 0) {
628 LOG_F(LS_ERROR) << "Failed to initialize recording";
629 return -1;
630 }
631 if (shared_->audio_device()->StartRecording() != 0) {
632 LOG_F(LS_ERROR) << "Failed to start recording";
633 return -1;
634 }
635 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200636 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000637}
638
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200639int32_t VoEBaseImpl::StopSend() {
640 if (shared_->NumOfSendingChannels() == 0 &&
641 !shared_->transmit_mixer()->IsRecordingMic()) {
642 // Stop audio-device recording if no channel is recording
643 if (shared_->audio_device()->StopRecording() != 0) {
644 shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
645 "StopSend() failed to stop recording");
646 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000647 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200648 shared_->transmit_mixer()->StopSend();
649 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000650
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200651 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000652}
653
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200654int32_t VoEBaseImpl::TerminateInternal() {
655 // Delete any remaining channel objects
656 shared_->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000657
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200658 if (shared_->process_thread()) {
659 if (shared_->audio_device()) {
660 shared_->process_thread()->DeRegisterModule(shared_->audio_device());
niklase@google.com470e71d2011-07-07 08:21:25 +0000661 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200662 shared_->process_thread()->Stop();
663 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000664
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200665 if (shared_->audio_device()) {
666 if (shared_->audio_device()->StopPlayout() != 0) {
667 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
668 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000669 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200670 if (shared_->audio_device()->StopRecording() != 0) {
671 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
672 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000673 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200674 if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) {
675 shared_->SetLastError(
676 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
677 "TerminateInternal() failed to de-register event observer "
678 "for the ADM");
679 }
680 if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) {
681 shared_->SetLastError(
682 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
683 "TerminateInternal() failed to de-register audio callback "
684 "for the ADM");
685 }
686 if (shared_->audio_device()->Terminate() != 0) {
687 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
688 "TerminateInternal() failed to terminate the ADM");
689 }
690 shared_->set_audio_device(nullptr);
691 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000692
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200693 if (shared_->audio_processing()) {
694 shared_->set_audio_processing(nullptr);
695 }
696
697 return shared_->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000700int VoEBaseImpl::ProcessRecordedDataWithAPM(
Peter Kasting69558702016-01-12 16:26:35 -0800701 const int voe_channels[], size_t number_of_voe_channels,
702 const void* audio_data, uint32_t sample_rate, size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 size_t number_of_frames, uint32_t audio_delay_milliseconds,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200704 int32_t clock_drift, uint32_t volume, bool key_pressed) {
705 assert(shared_->transmit_mixer() != nullptr);
706 assert(shared_->audio_device() != nullptr);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000707
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000708 uint32_t max_volume = 0;
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000709 uint16_t voe_mic_level = 0;
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +0000710 // Check for zero to skip this calculation; the consumer may use this to
711 // indicate no volume is available.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000712 if (volume != 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000713 // Scale from ADM to VoE level range
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200714 if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000715 if (max_volume) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000716 voe_mic_level = static_cast<uint16_t>(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200717 (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) /
718 max_volume);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000719 }
720 }
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000721 // We learned that on certain systems (e.g Linux) the voe_mic_level
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000722 // can be greater than the maxVolumeLevel therefore
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000723 // we are going to cap the voe_mic_level to the maxVolumeLevel
724 // and change the maxVolume to volume if it turns out that
725 // the voe_mic_level is indeed greater than the maxVolumeLevel.
726 if (voe_mic_level > kMaxVolumeLevel) {
727 voe_mic_level = kMaxVolumeLevel;
728 max_volume = volume;
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000729 }
730 }
731
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000732 // Perform channel-independent operations
733 // (APM, mix with file, record to file, mute, etc.)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200734 shared_->transmit_mixer()->PrepareDemux(
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000735 audio_data, number_of_frames, number_of_channels, sample_rate,
736 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000737 voe_mic_level, key_pressed);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000738
739 // Copy the audio frame to each sending channel and perform
740 // channel-dependent operations (file mixing, mute, etc.), encode and
741 // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0,
742 // do the operations on all the existing VoE channels; otherwise the
743 // operations will be done on specific channels.
744 if (number_of_voe_channels == 0) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200745 shared_->transmit_mixer()->DemuxAndMix();
746 shared_->transmit_mixer()->EncodeAndSend();
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000747 } else {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200748 shared_->transmit_mixer()->DemuxAndMix(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000749 number_of_voe_channels);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200750 shared_->transmit_mixer()->EncodeAndSend(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000751 number_of_voe_channels);
752 }
753
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000754 // Scale from VoE to ADM level range.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200755 uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel();
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000756 if (new_voe_mic_level != voe_mic_level) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000757 // Return the new volume if AGC has changed the volume.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200758 return static_cast<int>((new_voe_mic_level * max_volume +
759 static_cast<int>(kMaxVolumeLevel / 2)) /
760 kMaxVolumeLevel);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000761 }
762
763 // Return 0 to indicate no change on the volume.
764 return 0;
765}
766
Peter Kasting69558702016-01-12 16:26:35 -0800767void VoEBaseImpl::GetPlayoutData(int sample_rate, size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700768 size_t number_of_frames, bool feed_data_to_apm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200769 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000770 int64_t* ntp_time_ms) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200771 assert(shared_->output_mixer() != nullptr);
xians@webrtc.org56925312014-04-14 10:50:37 +0000772
773 // TODO(andrew): if the device is running in mono, we should tell the mixer
774 // here so that it will only request mono from AudioCodingModule.
775 // Perform mixing of all active participants (channel-based mixing)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200776 shared_->output_mixer()->MixActiveChannels();
xians@webrtc.org56925312014-04-14 10:50:37 +0000777
778 // Additional operations on the combined signal
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200779 shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm);
xians@webrtc.org56925312014-04-14 10:50:37 +0000780
781 // Retrieve the final output mix (resampled to match the ADM)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200782 shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels,
783 &audioFrame_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000784
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200785 assert(number_of_frames == audioFrame_.samples_per_channel_);
786 assert(sample_rate == audioFrame_.sample_rate_hz_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000787
788 // Deliver audio (PCM) samples to the ADM
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200789 memcpy(audio_data, audioFrame_.data_,
xians@webrtc.org56925312014-04-14 10:50:37 +0000790 sizeof(int16_t) * number_of_frames * number_of_channels);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000791
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200792 *elapsed_time_ms = audioFrame_.elapsed_time_ms_;
793 *ntp_time_ms = audioFrame_.ntp_time_ms_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000794}
795
Minyue2013aec2015-05-13 14:14:42 +0200796int VoEBaseImpl::AssociateSendChannel(int channel,
797 int accociate_send_channel) {
798 CriticalSectionScoped cs(shared_->crit_sec());
799
800 if (!shared_->statistics().Initialized()) {
801 shared_->SetLastError(VE_NOT_INITED, kTraceError);
802 return -1;
803 }
804
805 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
806 voe::Channel* channel_ptr = ch.channel();
807 if (channel_ptr == NULL) {
808 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
809 "AssociateSendChannel() failed to locate channel");
810 return -1;
811 }
812
813 ch = shared_->channel_manager().GetChannel(accociate_send_channel);
814 voe::Channel* accociate_send_channel_ptr = ch.channel();
815 if (accociate_send_channel_ptr == NULL) {
816 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
817 "AssociateSendChannel() failed to locate accociate_send_channel");
818 return -1;
819 }
820
821 channel_ptr->set_associate_send_channel(ch);
822 return 0;
823}
824
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000825} // namespace webrtc