blob: 16ed43f0225c91bf3beded6ac1b4b782b58d06d0 [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"
turaj@webrtc.org03f33702013-11-13 00:02:48 +000014#include "webrtc/common.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
16#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
17#include "webrtc/modules/audio_device/audio_device_impl.h"
18#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
20#include "webrtc/system_wrappers/include/file_wrapper.h"
21#include "webrtc/system_wrappers/include/logging.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
Jelena Marusic2dd6a272015-04-14 09:47:00 +020050void VoEBaseImpl::OnErrorIsReported(ErrorCode error) {
51 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
Jelena Marusic2dd6a272015-04-14 09:47:00 +020066void VoEBaseImpl::OnWarningIsReported(WarningCode warning) {
67 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
pbos@webrtc.org6141e132013-04-09 10:09:10 +000082int32_t VoEBaseImpl::RecordedDataIsAvailable(
Peter Kastingdce40cf2015-08-24 14:52:23 -070083 const void* audioSamples, size_t nSamples, size_t nBytesPerSample,
Jelena Marusic2dd6a272015-04-14 09:47:00 +020084 uint8_t nChannels, uint32_t samplesPerSec, uint32_t totalDelayMS,
85 int32_t clockDrift, uint32_t micLevel, bool keyPressed,
86 uint32_t& newMicLevel) {
87 newMicLevel = static_cast<uint32_t>(ProcessRecordedDataWithAPM(
88 nullptr, 0, audioSamples, samplesPerSec, nChannels, nSamples,
89 totalDelayMS, clockDrift, micLevel, keyPressed));
90 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
Peter Kastingdce40cf2015-08-24 14:52:23 -070093int32_t VoEBaseImpl::NeedMorePlayData(size_t nSamples,
94 size_t nBytesPerSample,
Jelena Marusic2dd6a272015-04-14 09:47:00 +020095 uint8_t nChannels, uint32_t samplesPerSec,
Peter Kastingdce40cf2015-08-24 14:52:23 -070096 void* audioSamples, size_t& nSamplesOut,
Jelena Marusic2dd6a272015-04-14 09:47:00 +020097 int64_t* elapsed_time_ms,
98 int64_t* ntp_time_ms) {
99 GetPlayoutData(static_cast<int>(samplesPerSec), static_cast<int>(nChannels),
Peter Kastingdce40cf2015-08-24 14:52:23 -0700100 nSamples, true, audioSamples,
wu@webrtc.org94454b72014-06-05 20:34:08 +0000101 elapsed_time_ms, ntp_time_ms);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200102 nSamplesOut = audioFrame_.samples_per_channel_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000103 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
105
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000106int VoEBaseImpl::OnDataAvailable(const int voe_channels[],
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000107 int number_of_voe_channels,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200108 const int16_t* audio_data, int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700109 int number_of_channels,
110 size_t number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200111 int audio_delay_milliseconds, int volume,
112 bool key_pressed, bool need_audio_processing) {
113 if (number_of_voe_channels == 0) return 0;
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000114
115 if (need_audio_processing) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000116 return ProcessRecordedDataWithAPM(
117 voe_channels, number_of_voe_channels, audio_data, sample_rate,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200118 number_of_channels, number_of_frames, audio_delay_milliseconds, 0,
119 volume, key_pressed);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000120 }
121
122 // No need to go through the APM, demultiplex the data to each VoE channel,
123 // encode and send to the network.
124 for (int i = 0; i < number_of_voe_channels; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000125 // TODO(ajm): In the case where multiple channels are using the same codec
126 // rate, this path needlessly does extra conversions. We should convert once
127 // and share between channels.
xians@webrtc.org56925312014-04-14 10:50:37 +0000128 PushCaptureData(voe_channels[i], audio_data, 16, sample_rate,
129 number_of_channels, number_of_frames);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +0000130 }
131
132 // Return 0 to indicate no need to change the volume.
133 return 0;
134}
135
xians@webrtc.orgc1e28032014-02-02 15:30:20 +0000136void VoEBaseImpl::OnData(int voe_channel, const void* audio_data,
137 int bits_per_sample, int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 int number_of_channels, size_t number_of_frames) {
xians@webrtc.org56925312014-04-14 10:50:37 +0000139 PushCaptureData(voe_channel, audio_data, bits_per_sample, sample_rate,
140 number_of_channels, number_of_frames);
141}
142
143void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data,
144 int bits_per_sample, int sample_rate,
145 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 size_t number_of_frames) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200147 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel);
xians@webrtc.org07e51962014-01-29 13:54:02 +0000148 voe::Channel* channel_ptr = ch.channel();
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200149 if (!channel_ptr) return;
xians@webrtc.org07e51962014-01-29 13:54:02 +0000150
henrika@webrtc.org66803482014-04-17 10:45:01 +0000151 if (channel_ptr->Sending()) {
xians@webrtc.org07e51962014-01-29 13:54:02 +0000152 channel_ptr->Demultiplex(static_cast<const int16_t*>(audio_data),
153 sample_rate, number_of_frames, number_of_channels);
154 channel_ptr->PrepareEncodeAndSend(sample_rate);
155 channel_ptr->EncodeAndSend();
156 }
157}
158
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159void VoEBaseImpl::PullRenderData(int bits_per_sample,
160 int sample_rate,
161 int number_of_channels,
162 size_t number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200163 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000164 int64_t* ntp_time_ms) {
Jelena Marusic6fc2d2f2015-04-13 14:07:04 +0200165 assert(bits_per_sample == 16);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700166 assert(number_of_frames == static_cast<size_t>(sample_rate / 100));
xians@webrtc.org56925312014-04-14 10:50:37 +0000167
168 GetPlayoutData(sample_rate, number_of_channels, number_of_frames, false,
wu@webrtc.org94454b72014-06-05 20:34:08 +0000169 audio_data, elapsed_time_ms, ntp_time_ms);
xians@webrtc.org56925312014-04-14 10:50:37 +0000170}
171
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200172int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
173 CriticalSectionScoped cs(&callbackCritSect_);
174 if (voiceEngineObserverPtr_) {
175 shared_->SetLastError(
176 VE_INVALID_OPERATION, kTraceError,
177 "RegisterVoiceEngineObserver() observer already enabled");
178 return -1;
179 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200181 // Register the observer in all active channels
182 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
183 it.IsValid(); it.Increment()) {
184 it.GetChannel()->RegisterVoiceEngineObserver(observer);
185 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000186
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200187 shared_->transmit_mixer()->RegisterVoiceEngineObserver(observer);
188 voiceEngineObserverPtr_ = &observer;
189 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200192int VoEBaseImpl::DeRegisterVoiceEngineObserver() {
193 CriticalSectionScoped cs(&callbackCritSect_);
194 if (!voiceEngineObserverPtr_) {
195 shared_->SetLastError(
196 VE_INVALID_OPERATION, kTraceError,
197 "DeRegisterVoiceEngineObserver() observer already disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200199 }
200 voiceEngineObserverPtr_ = nullptr;
201
202 // Deregister the observer in all active channels
203 for (voe::ChannelManager::Iterator it(&shared_->channel_manager());
204 it.IsValid(); it.Increment()) {
205 it.GetChannel()->DeRegisterVoiceEngineObserver();
206 }
207
208 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209}
210
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000211int VoEBaseImpl::Init(AudioDeviceModule* external_adm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200212 AudioProcessing* audioproc) {
213 CriticalSectionScoped cs(shared_->crit_sec());
214 WebRtcSpl_Init();
215 if (shared_->statistics().Initialized()) {
216 return 0;
217 }
218 if (shared_->process_thread()) {
219 shared_->process_thread()->Start();
220 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200222 // Create an internal ADM if the user has not added an external
223 // ADM implementation as input to Init().
224 if (external_adm == nullptr) {
Tommi931e6582015-05-20 09:44:38 +0200225#if !defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
226 return -1;
227#else
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200228 // Create the internal ADM implementation.
229 shared_->set_audio_device(AudioDeviceModuleImpl::Create(
230 VoEId(shared_->instance_id(), -1), shared_->audio_device_layer()));
kma@webrtc.org0221b782012-09-08 00:09:26 +0000231
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200232 if (shared_->audio_device() == nullptr) {
233 shared_->SetLastError(VE_NO_MEMORY, kTraceCritical,
234 "Init() failed to create the ADM");
235 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 }
Tommi931e6582015-05-20 09:44:38 +0200237#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200238 } else {
239 // Use the already existing external ADM implementation.
240 shared_->set_audio_device(external_adm);
241 LOG_F(LS_INFO)
242 << "An external ADM implementation will be used in VoiceEngine";
243 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000244
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200245 // Register the ADM to the process thread, which will drive the error
246 // callback mechanism
247 if (shared_->process_thread()) {
248 shared_->process_thread()->RegisterModule(shared_->audio_device());
249 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200251 bool available = false;
andrew@webrtc.orgf4589162011-10-03 15:22:28 +0000252
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200253 // --------------------
254 // Reinitialize the ADM
henrika@google.com73d65512011-09-07 15:11:18 +0000255
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200256 // Register the AudioObserver implementation
257 if (shared_->audio_device()->RegisterEventObserver(this) != 0) {
258 shared_->SetLastError(
259 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
260 "Init() failed to register event observer for the ADM");
261 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000262
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200263 // Register the AudioTransport implementation
264 if (shared_->audio_device()->RegisterAudioCallback(this) != 0) {
265 shared_->SetLastError(
266 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
267 "Init() failed to register audio callback for the ADM");
268 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000269
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200270 // ADM initialization
271 if (shared_->audio_device()->Init() != 0) {
272 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
273 "Init() failed to initialize the ADM");
274 return -1;
275 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200277 // Initialize the default speaker
278 if (shared_->audio_device()->SetPlayoutDevice(
279 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
280 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo,
281 "Init() failed to set the default output device");
282 }
283 if (shared_->audio_device()->InitSpeaker() != 0) {
284 shared_->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo,
285 "Init() failed to initialize the speaker");
286 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000287
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200288 // Initialize the default microphone
289 if (shared_->audio_device()->SetRecordingDevice(
290 WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
291 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo,
292 "Init() failed to set the default input device");
293 }
294 if (shared_->audio_device()->InitMicrophone() != 0) {
295 shared_->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo,
296 "Init() failed to initialize the microphone");
297 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000298
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200299 // Set number of channels
300 if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
301 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
302 "Init() failed to query stereo playout mode");
303 }
304 if (shared_->audio_device()->SetStereoPlayout(available) != 0) {
305 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
306 "Init() failed to set mono/stereo playout mode");
307 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000308
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200309 // TODO(andrew): These functions don't tell us whether stereo recording
310 // is truly available. We simply set the AudioProcessing input to stereo
311 // here, because we have to wait until receiving the first frame to
312 // determine the actual number of channels anyway.
313 //
314 // These functions may be changed; tracked here:
315 // http://code.google.com/p/webrtc/issues/detail?id=204
316 shared_->audio_device()->StereoRecordingIsAvailable(&available);
317 if (shared_->audio_device()->SetStereoRecording(available) != 0) {
318 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
319 "Init() failed to set mono/stereo recording mode");
320 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000321
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200322 if (!audioproc) {
323 audioproc = AudioProcessing::Create();
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000324 if (!audioproc) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200325 LOG(LS_ERROR) << "Failed to create AudioProcessing.";
326 shared_->SetLastError(VE_NO_MEMORY);
327 return -1;
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000328 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200329 }
330 shared_->set_audio_processing(audioproc);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000331
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200332 // Set the error state for any failures in this block.
333 shared_->SetLastError(VE_APM_ERROR);
334 // Configure AudioProcessing components.
335 if (audioproc->high_pass_filter()->Enable(true) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200336 LOG_F(LS_ERROR) << "Failed to enable high pass filter.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200337 return -1;
338 }
339 if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200340 LOG_F(LS_ERROR) << "Failed to disable drift compensation.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200341 return -1;
342 }
343 if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200344 LOG_F(LS_ERROR) << "Failed to set noise suppression level: "
345 << kDefaultNsMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200346 return -1;
347 }
348 GainControl* agc = audioproc->gain_control();
349 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200350 LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: "
351 << kMinVolumeLevel << " and maximum: " << kMaxVolumeLevel;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200352 return -1;
353 }
354 if (agc->set_mode(kDefaultAgcMode) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200355 LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200356 return -1;
357 }
358 if (agc->Enable(kDefaultAgcState) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200359 LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200360 return -1;
361 }
362 shared_->SetLastError(0); // Clear error state.
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000363
niklase@google.com470e71d2011-07-07 08:21:25 +0000364#ifdef WEBRTC_VOICE_ENGINE_AGC
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200365 bool agc_enabled =
366 agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled();
367 if (shared_->audio_device()->SetAGC(agc_enabled) != 0) {
Jelena Marusicf353dd52015-05-06 15:04:22 +0200368 LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200369 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR);
370 // TODO(ajm): No error return here due to
371 // https://code.google.com/p/webrtc/issues/detail?id=1464
372 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000373#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200375 return shared_->statistics().SetInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000376}
377
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200378int VoEBaseImpl::Terminate() {
379 CriticalSectionScoped cs(shared_->crit_sec());
380 return TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000383int VoEBaseImpl::CreateChannel() {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200384 CriticalSectionScoped cs(shared_->crit_sec());
385 if (!shared_->statistics().Initialized()) {
386 shared_->SetLastError(VE_NOT_INITED, kTraceError);
387 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000388 }
389
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200390 voe::ChannelOwner channel_owner = shared_->channel_manager().CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000391 return InitializeChannel(&channel_owner);
392}
393
394int VoEBaseImpl::CreateChannel(const Config& config) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200395 CriticalSectionScoped cs(shared_->crit_sec());
396 if (!shared_->statistics().Initialized()) {
397 shared_->SetLastError(VE_NOT_INITED, kTraceError);
398 return -1;
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000399 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200400 voe::ChannelOwner channel_owner =
401 shared_->channel_manager().CreateChannel(config);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000402 return InitializeChannel(&channel_owner);
403}
404
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200405int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) {
406 if (channel_owner->channel()->SetEngineInformation(
407 shared_->statistics(), *shared_->output_mixer(),
408 *shared_->transmit_mixer(), *shared_->process_thread(),
409 *shared_->audio_device(), voiceEngineObserverPtr_,
410 &callbackCritSect_) != 0) {
411 shared_->SetLastError(
412 VE_CHANNEL_NOT_CREATED, kTraceError,
413 "CreateChannel() failed to associate engine and channel."
414 " Destroying channel.");
415 shared_->channel_manager().DestroyChannel(
416 channel_owner->channel()->ChannelId());
417 return -1;
418 } else if (channel_owner->channel()->Init() != 0) {
419 shared_->SetLastError(
420 VE_CHANNEL_NOT_CREATED, kTraceError,
421 "CreateChannel() failed to initialize channel. Destroying"
422 " channel.");
423 shared_->channel_manager().DestroyChannel(
424 channel_owner->channel()->ChannelId());
425 return -1;
426 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200427 return channel_owner->channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000428}
429
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200430int VoEBaseImpl::DeleteChannel(int channel) {
431 CriticalSectionScoped cs(shared_->crit_sec());
432 if (!shared_->statistics().Initialized()) {
433 shared_->SetLastError(VE_NOT_INITED, kTraceError);
434 return -1;
435 }
436
437 {
438 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
439 voe::Channel* channelPtr = ch.channel();
440 if (channelPtr == nullptr) {
441 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
442 "DeleteChannel() failed to locate channel");
443 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200445 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200447 shared_->channel_manager().DestroyChannel(channel);
448 if (StopSend() != 0) {
449 return -1;
450 }
451 if (StopPlayout() != 0) {
452 return -1;
453 }
454 return 0;
455}
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200457int VoEBaseImpl::StartReceive(int channel) {
458 CriticalSectionScoped cs(shared_->crit_sec());
459 if (!shared_->statistics().Initialized()) {
460 shared_->SetLastError(VE_NOT_INITED, kTraceError);
461 return -1;
462 }
463 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
464 voe::Channel* channelPtr = ch.channel();
465 if (channelPtr == nullptr) {
466 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
467 "StartReceive() failed to locate channel");
468 return -1;
469 }
470 return channelPtr->StartReceiving();
471}
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200473int VoEBaseImpl::StopReceive(int channel) {
474 CriticalSectionScoped cs(shared_->crit_sec());
475 if (!shared_->statistics().Initialized()) {
476 shared_->SetLastError(VE_NOT_INITED, kTraceError);
477 return -1;
478 }
479 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
480 voe::Channel* channelPtr = ch.channel();
481 if (channelPtr == nullptr) {
482 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
483 "SetLocalReceiver() failed to locate channel");
484 return -1;
485 }
486 return channelPtr->StopReceiving();
487}
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200489int VoEBaseImpl::StartPlayout(int channel) {
490 CriticalSectionScoped cs(shared_->crit_sec());
491 if (!shared_->statistics().Initialized()) {
492 shared_->SetLastError(VE_NOT_INITED, kTraceError);
493 return -1;
494 }
495 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
496 voe::Channel* channelPtr = ch.channel();
497 if (channelPtr == nullptr) {
498 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
499 "StartPlayout() failed to locate channel");
500 return -1;
501 }
502 if (channelPtr->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200504 }
505 if (StartPlayout() != 0) {
506 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
507 "StartPlayout() failed to start playout");
508 return -1;
509 }
510 return channelPtr->StartPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200513int VoEBaseImpl::StopPlayout(int channel) {
514 CriticalSectionScoped cs(shared_->crit_sec());
515 if (!shared_->statistics().Initialized()) {
516 shared_->SetLastError(VE_NOT_INITED, kTraceError);
517 return -1;
518 }
519 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
520 voe::Channel* channelPtr = ch.channel();
521 if (channelPtr == nullptr) {
522 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
523 "StopPlayout() failed to locate channel");
524 return -1;
525 }
526 if (channelPtr->StopPlayout() != 0) {
527 LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel "
528 << channel;
529 }
530 return StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000531}
532
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200533int VoEBaseImpl::StartSend(int channel) {
534 CriticalSectionScoped cs(shared_->crit_sec());
535 if (!shared_->statistics().Initialized()) {
536 shared_->SetLastError(VE_NOT_INITED, kTraceError);
537 return -1;
538 }
539 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
540 voe::Channel* channelPtr = ch.channel();
541 if (channelPtr == nullptr) {
542 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
543 "StartSend() failed to locate channel");
544 return -1;
545 }
546 if (channelPtr->Sending()) {
547 return 0;
548 }
549 if (StartSend() != 0) {
550 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
551 "StartSend() failed to start recording");
552 return -1;
553 }
554 return channelPtr->StartSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000555}
556
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200557int VoEBaseImpl::StopSend(int channel) {
558 CriticalSectionScoped cs(shared_->crit_sec());
559 if (!shared_->statistics().Initialized()) {
560 shared_->SetLastError(VE_NOT_INITED, kTraceError);
561 return -1;
562 }
563 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
564 voe::Channel* channelPtr = ch.channel();
565 if (channelPtr == nullptr) {
566 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
567 "StopSend() failed to locate channel");
568 return -1;
569 }
570 if (channelPtr->StopSend() != 0) {
571 LOG_F(LS_WARNING) << "StopSend() failed to stop sending for channel "
572 << channel;
573 }
574 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]) {
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200578 static_assert(kVoiceEngineVersionMaxMessageSize == 1024, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200580 if (version == nullptr) {
581 shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError);
582 return (-1);
583 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000584
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200585 char versionBuf[kVoiceEngineVersionMaxMessageSize];
586 char* versionPtr = versionBuf;
niklase@google.com470e71d2011-07-07 08:21:25 +0000587
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200588 int32_t len = 0;
589 int32_t accLen = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200591 len = AddVoEVersion(versionPtr);
592 if (len == -1) {
593 return -1;
594 }
595 versionPtr += len;
596 accLen += len;
597 assert(accLen < kVoiceEngineVersionMaxMessageSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000598
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000599#ifdef WEBRTC_EXTERNAL_TRANSPORT
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200600 len = AddExternalTransportBuild(versionPtr);
601 if (len == -1) {
602 return -1;
603 }
604 versionPtr += len;
605 accLen += len;
606 assert(accLen < kVoiceEngineVersionMaxMessageSize);
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000607#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000608
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200609 memcpy(version, versionBuf, accLen);
610 version[accLen] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +0000611
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200612 // to avoid the truncation in the trace, split the string into parts
613 char partOfVersion[256];
614 for (int partStart = 0; partStart < accLen;) {
615 memset(partOfVersion, 0, sizeof(partOfVersion));
616 int partEnd = partStart + 180;
617 while (version[partEnd] != '\n' && version[partEnd] != '\0') {
618 partEnd--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000619 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200620 if (partEnd < accLen) {
621 memcpy(partOfVersion, &version[partStart], partEnd - partStart);
622 } else {
623 memcpy(partOfVersion, &version[partStart], accLen - partStart);
624 }
625 partStart = partEnd;
626 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000627
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200628 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000629}
630
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200631int32_t VoEBaseImpl::AddVoEVersion(char* str) const {
632 return sprintf(str, "VoiceEngine 4.1.0\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000633}
634
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000635#ifdef WEBRTC_EXTERNAL_TRANSPORT
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200636int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const {
637 return sprintf(str, "External transport build\n");
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000638}
639#endif
640
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200641int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000642
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200643int32_t VoEBaseImpl::StartPlayout() {
solenberge313e022015-09-08 02:16:04 -0700644 if (!shared_->audio_device()->Playing()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200645 if (shared_->audio_device()->InitPlayout() != 0) {
646 LOG_F(LS_ERROR) << "Failed to initialize playout";
647 return -1;
648 }
649 if (shared_->audio_device()->StartPlayout() != 0) {
650 LOG_F(LS_ERROR) << "Failed to start playout";
651 return -1;
652 }
653 }
654 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000655}
656
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000657int32_t VoEBaseImpl::StopPlayout() {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000658 // Stop audio-device playing if no channel is playing out
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200659 if (shared_->NumOfPlayingChannels() == 0) {
660 if (shared_->audio_device()->StopPlayout() != 0) {
661 shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError,
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000662 "StopPlayout() failed to stop playout");
663 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000665 }
666 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000667}
668
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200669int32_t VoEBaseImpl::StartSend() {
solenberge313e022015-09-08 02:16:04 -0700670 if (!shared_->audio_device()->Recording()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200671 if (shared_->audio_device()->InitRecording() != 0) {
672 LOG_F(LS_ERROR) << "Failed to initialize recording";
673 return -1;
674 }
675 if (shared_->audio_device()->StartRecording() != 0) {
676 LOG_F(LS_ERROR) << "Failed to start recording";
677 return -1;
678 }
679 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200680 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681}
682
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200683int32_t VoEBaseImpl::StopSend() {
684 if (shared_->NumOfSendingChannels() == 0 &&
685 !shared_->transmit_mixer()->IsRecordingMic()) {
686 // Stop audio-device recording if no channel is recording
687 if (shared_->audio_device()->StopRecording() != 0) {
688 shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError,
689 "StopSend() failed to stop recording");
690 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000691 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200692 shared_->transmit_mixer()->StopSend();
693 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000694
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200695 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200698int32_t VoEBaseImpl::TerminateInternal() {
699 // Delete any remaining channel objects
700 shared_->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200702 if (shared_->process_thread()) {
703 if (shared_->audio_device()) {
704 shared_->process_thread()->DeRegisterModule(shared_->audio_device());
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200706 shared_->process_thread()->Stop();
707 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000708
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200709 if (shared_->audio_device()) {
710 if (shared_->audio_device()->StopPlayout() != 0) {
711 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
712 "TerminateInternal() failed to stop playout");
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200714 if (shared_->audio_device()->StopRecording() != 0) {
715 shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning,
716 "TerminateInternal() failed to stop recording");
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200718 if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) {
719 shared_->SetLastError(
720 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
721 "TerminateInternal() failed to de-register event observer "
722 "for the ADM");
723 }
724 if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) {
725 shared_->SetLastError(
726 VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning,
727 "TerminateInternal() failed to de-register audio callback "
728 "for the ADM");
729 }
730 if (shared_->audio_device()->Terminate() != 0) {
731 shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
732 "TerminateInternal() failed to terminate the ADM");
733 }
734 shared_->set_audio_device(nullptr);
735 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000736
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200737 if (shared_->audio_processing()) {
738 shared_->set_audio_processing(nullptr);
739 }
740
741 return shared_->statistics().SetUnInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000744int VoEBaseImpl::ProcessRecordedDataWithAPM(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200745 const int voe_channels[], int number_of_voe_channels,
746 const void* audio_data, uint32_t sample_rate, uint8_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700747 size_t number_of_frames, uint32_t audio_delay_milliseconds,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200748 int32_t clock_drift, uint32_t volume, bool key_pressed) {
749 assert(shared_->transmit_mixer() != nullptr);
750 assert(shared_->audio_device() != nullptr);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000751
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000752 uint32_t max_volume = 0;
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000753 uint16_t voe_mic_level = 0;
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +0000754 // Check for zero to skip this calculation; the consumer may use this to
755 // indicate no volume is available.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000756 if (volume != 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000757 // Scale from ADM to VoE level range
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200758 if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000759 if (max_volume) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000760 voe_mic_level = static_cast<uint16_t>(
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200761 (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) /
762 max_volume);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000763 }
764 }
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000765 // We learned that on certain systems (e.g Linux) the voe_mic_level
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000766 // can be greater than the maxVolumeLevel therefore
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000767 // we are going to cap the voe_mic_level to the maxVolumeLevel
768 // and change the maxVolume to volume if it turns out that
769 // the voe_mic_level is indeed greater than the maxVolumeLevel.
770 if (voe_mic_level > kMaxVolumeLevel) {
771 voe_mic_level = kMaxVolumeLevel;
772 max_volume = volume;
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000773 }
774 }
775
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000776 // Perform channel-independent operations
777 // (APM, mix with file, record to file, mute, etc.)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200778 shared_->transmit_mixer()->PrepareDemux(
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000779 audio_data, number_of_frames, number_of_channels, sample_rate,
780 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000781 voe_mic_level, key_pressed);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000782
783 // Copy the audio frame to each sending channel and perform
784 // channel-dependent operations (file mixing, mute, etc.), encode and
785 // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0,
786 // do the operations on all the existing VoE channels; otherwise the
787 // operations will be done on specific channels.
788 if (number_of_voe_channels == 0) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200789 shared_->transmit_mixer()->DemuxAndMix();
790 shared_->transmit_mixer()->EncodeAndSend();
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000791 } else {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200792 shared_->transmit_mixer()->DemuxAndMix(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000793 number_of_voe_channels);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200794 shared_->transmit_mixer()->EncodeAndSend(voe_channels,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000795 number_of_voe_channels);
796 }
797
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000798 // Scale from VoE to ADM level range.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200799 uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel();
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000800 if (new_voe_mic_level != voe_mic_level) {
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000801 // Return the new volume if AGC has changed the volume.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200802 return static_cast<int>((new_voe_mic_level * max_volume +
803 static_cast<int>(kMaxVolumeLevel / 2)) /
804 kMaxVolumeLevel);
xians@webrtc.org8fff1f02013-07-31 16:27:42 +0000805 }
806
807 // Return 0 to indicate no change on the volume.
808 return 0;
809}
810
xians@webrtc.org56925312014-04-14 10:50:37 +0000811void VoEBaseImpl::GetPlayoutData(int sample_rate, int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700812 size_t number_of_frames, bool feed_data_to_apm,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200813 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000814 int64_t* ntp_time_ms) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200815 assert(shared_->output_mixer() != nullptr);
xians@webrtc.org56925312014-04-14 10:50:37 +0000816
817 // TODO(andrew): if the device is running in mono, we should tell the mixer
818 // here so that it will only request mono from AudioCodingModule.
819 // Perform mixing of all active participants (channel-based mixing)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200820 shared_->output_mixer()->MixActiveChannels();
xians@webrtc.org56925312014-04-14 10:50:37 +0000821
822 // Additional operations on the combined signal
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200823 shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm);
xians@webrtc.org56925312014-04-14 10:50:37 +0000824
825 // Retrieve the final output mix (resampled to match the ADM)
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200826 shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels,
827 &audioFrame_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000828
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200829 assert(number_of_frames == audioFrame_.samples_per_channel_);
830 assert(sample_rate == audioFrame_.sample_rate_hz_);
xians@webrtc.org56925312014-04-14 10:50:37 +0000831
832 // Deliver audio (PCM) samples to the ADM
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200833 memcpy(audio_data, audioFrame_.data_,
xians@webrtc.org56925312014-04-14 10:50:37 +0000834 sizeof(int16_t) * number_of_frames * number_of_channels);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000835
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200836 *elapsed_time_ms = audioFrame_.elapsed_time_ms_;
837 *ntp_time_ms = audioFrame_.ntp_time_ms_;
xians@webrtc.org56925312014-04-14 10:50:37 +0000838}
839
Minyue2013aec2015-05-13 14:14:42 +0200840int VoEBaseImpl::AssociateSendChannel(int channel,
841 int accociate_send_channel) {
842 CriticalSectionScoped cs(shared_->crit_sec());
843
844 if (!shared_->statistics().Initialized()) {
845 shared_->SetLastError(VE_NOT_INITED, kTraceError);
846 return -1;
847 }
848
849 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
850 voe::Channel* channel_ptr = ch.channel();
851 if (channel_ptr == NULL) {
852 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
853 "AssociateSendChannel() failed to locate channel");
854 return -1;
855 }
856
857 ch = shared_->channel_manager().GetChannel(accociate_send_channel);
858 voe::Channel* accociate_send_channel_ptr = ch.channel();
859 if (accociate_send_channel_ptr == NULL) {
860 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
861 "AssociateSendChannel() failed to locate accociate_send_channel");
862 return -1;
863 }
864
865 channel_ptr->set_associate_send_channel(ch);
866 return 0;
867}
868
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000869} // namespace webrtc