niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/voe_base_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 13 | #include "webrtc/common.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 14 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 15 | #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h" |
| 16 | #include "webrtc/modules/audio_device/audio_device_impl.h" |
| 17 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 18 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 19 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
Jelena Marusic | 9e5e421 | 2015-04-13 13:41:56 +0200 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/logging.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 21 | #include "webrtc/voice_engine/channel.h" |
| 22 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 23 | #include "webrtc/voice_engine/output_mixer.h" |
| 24 | #include "webrtc/voice_engine/transmit_mixer.h" |
| 25 | #include "webrtc/voice_engine/utility.h" |
| 26 | #include "webrtc/voice_engine/voice_engine_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 28 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 30 | VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine) { |
| 31 | if (nullptr == voiceEngine) { |
| 32 | return nullptr; |
| 33 | } |
| 34 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 35 | s->AddRef(); |
| 36 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 39 | VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared) |
| 40 | : voiceEngineObserverPtr_(nullptr), |
| 41 | callbackCritSect_(*CriticalSectionWrapper::CreateCriticalSection()), |
| 42 | shared_(shared) {} |
| 43 | |
| 44 | VoEBaseImpl::~VoEBaseImpl() { |
| 45 | TerminateInternal(); |
| 46 | delete &callbackCritSect_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 49 | void VoEBaseImpl::OnErrorIsReported(ErrorCode error) { |
| 50 | CriticalSectionScoped cs(&callbackCritSect_); |
| 51 | int errCode = 0; |
| 52 | if (error == AudioDeviceObserver::kRecordingError) { |
| 53 | errCode = VE_RUNTIME_REC_ERROR; |
| 54 | LOG_F(LS_ERROR) << "VE_RUNTIME_REC_ERROR"; |
| 55 | } else if (error == AudioDeviceObserver::kPlayoutError) { |
| 56 | errCode = VE_RUNTIME_PLAY_ERROR; |
| 57 | LOG_F(LS_ERROR) << "VE_RUNTIME_PLAY_ERROR"; |
| 58 | } |
| 59 | if (voiceEngineObserverPtr_) { |
| 60 | // Deliver callback (-1 <=> no channel dependency) |
| 61 | voiceEngineObserverPtr_->CallbackOnError(-1, errCode); |
| 62 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 65 | void VoEBaseImpl::OnWarningIsReported(WarningCode warning) { |
| 66 | CriticalSectionScoped cs(&callbackCritSect_); |
| 67 | int warningCode = 0; |
| 68 | if (warning == AudioDeviceObserver::kRecordingWarning) { |
| 69 | warningCode = VE_RUNTIME_REC_WARNING; |
| 70 | LOG_F(LS_WARNING) << "VE_RUNTIME_REC_WARNING"; |
| 71 | } else if (warning == AudioDeviceObserver::kPlayoutWarning) { |
| 72 | warningCode = VE_RUNTIME_PLAY_WARNING; |
| 73 | LOG_F(LS_WARNING) << "VE_RUNTIME_PLAY_WARNING"; |
| 74 | } |
| 75 | if (voiceEngineObserverPtr_) { |
| 76 | // Deliver callback (-1 <=> no channel dependency) |
| 77 | voiceEngineObserverPtr_->CallbackOnError(-1, warningCode); |
| 78 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 81 | int32_t VoEBaseImpl::RecordedDataIsAvailable( |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 82 | const void* audioSamples, uint32_t nSamples, uint8_t nBytesPerSample, |
| 83 | uint8_t nChannels, uint32_t samplesPerSec, uint32_t totalDelayMS, |
| 84 | int32_t clockDrift, uint32_t micLevel, bool keyPressed, |
| 85 | uint32_t& newMicLevel) { |
| 86 | newMicLevel = static_cast<uint32_t>(ProcessRecordedDataWithAPM( |
| 87 | nullptr, 0, audioSamples, samplesPerSec, nChannels, nSamples, |
| 88 | totalDelayMS, clockDrift, micLevel, keyPressed)); |
| 89 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 92 | int32_t VoEBaseImpl::NeedMorePlayData(uint32_t nSamples, |
| 93 | uint8_t nBytesPerSample, |
| 94 | uint8_t nChannels, uint32_t samplesPerSec, |
| 95 | void* audioSamples, uint32_t& nSamplesOut, |
| 96 | int64_t* elapsed_time_ms, |
| 97 | int64_t* ntp_time_ms) { |
| 98 | GetPlayoutData(static_cast<int>(samplesPerSec), static_cast<int>(nChannels), |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 99 | static_cast<int>(nSamples), true, audioSamples, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 100 | elapsed_time_ms, ntp_time_ms); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 101 | nSamplesOut = audioFrame_.samples_per_channel_; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 102 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 105 | int VoEBaseImpl::OnDataAvailable(const int voe_channels[], |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 106 | int number_of_voe_channels, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 107 | const int16_t* audio_data, int sample_rate, |
| 108 | int number_of_channels, int number_of_frames, |
| 109 | int audio_delay_milliseconds, int volume, |
| 110 | bool key_pressed, bool need_audio_processing) { |
| 111 | if (number_of_voe_channels == 0) return 0; |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 112 | |
| 113 | if (need_audio_processing) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 114 | return ProcessRecordedDataWithAPM( |
| 115 | voe_channels, number_of_voe_channels, audio_data, sample_rate, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 116 | number_of_channels, number_of_frames, audio_delay_milliseconds, 0, |
| 117 | volume, key_pressed); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // No need to go through the APM, demultiplex the data to each VoE channel, |
| 121 | // encode and send to the network. |
| 122 | for (int i = 0; i < number_of_voe_channels; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 123 | // TODO(ajm): In the case where multiple channels are using the same codec |
| 124 | // rate, this path needlessly does extra conversions. We should convert once |
| 125 | // and share between channels. |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 126 | PushCaptureData(voe_channels[i], audio_data, 16, sample_rate, |
| 127 | number_of_channels, number_of_frames); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Return 0 to indicate no need to change the volume. |
| 131 | return 0; |
| 132 | } |
| 133 | |
xians@webrtc.org | c1e2803 | 2014-02-02 15:30:20 +0000 | [diff] [blame] | 134 | void VoEBaseImpl::OnData(int voe_channel, const void* audio_data, |
| 135 | int bits_per_sample, int sample_rate, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 136 | int number_of_channels, int number_of_frames) { |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 137 | PushCaptureData(voe_channel, audio_data, bits_per_sample, sample_rate, |
| 138 | number_of_channels, number_of_frames); |
| 139 | } |
| 140 | |
| 141 | void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data, |
| 142 | int bits_per_sample, int sample_rate, |
| 143 | int number_of_channels, |
| 144 | int number_of_frames) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 145 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel); |
xians@webrtc.org | 07e5196 | 2014-01-29 13:54:02 +0000 | [diff] [blame] | 146 | voe::Channel* channel_ptr = ch.channel(); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 147 | if (!channel_ptr) return; |
xians@webrtc.org | 07e5196 | 2014-01-29 13:54:02 +0000 | [diff] [blame] | 148 | |
henrika@webrtc.org | 6680348 | 2014-04-17 10:45:01 +0000 | [diff] [blame] | 149 | if (channel_ptr->Sending()) { |
xians@webrtc.org | 07e5196 | 2014-01-29 13:54:02 +0000 | [diff] [blame] | 150 | channel_ptr->Demultiplex(static_cast<const int16_t*>(audio_data), |
| 151 | sample_rate, number_of_frames, number_of_channels); |
| 152 | channel_ptr->PrepareEncodeAndSend(sample_rate); |
| 153 | channel_ptr->EncodeAndSend(); |
| 154 | } |
| 155 | } |
| 156 | |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 157 | void VoEBaseImpl::PullRenderData(int bits_per_sample, int sample_rate, |
| 158 | int number_of_channels, int number_of_frames, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 159 | void* audio_data, int64_t* elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 160 | int64_t* ntp_time_ms) { |
Jelena Marusic | 6fc2d2f | 2015-04-13 14:07:04 +0200 | [diff] [blame] | 161 | assert(bits_per_sample == 16); |
| 162 | assert(number_of_frames == static_cast<int>(sample_rate / 100)); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 163 | |
| 164 | GetPlayoutData(sample_rate, number_of_channels, number_of_frames, false, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 165 | audio_data, elapsed_time_ms, ntp_time_ms); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 168 | int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) { |
| 169 | CriticalSectionScoped cs(&callbackCritSect_); |
| 170 | if (voiceEngineObserverPtr_) { |
| 171 | shared_->SetLastError( |
| 172 | VE_INVALID_OPERATION, kTraceError, |
| 173 | "RegisterVoiceEngineObserver() observer already enabled"); |
| 174 | return -1; |
| 175 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 177 | // Register the observer in all active channels |
| 178 | for (voe::ChannelManager::Iterator it(&shared_->channel_manager()); |
| 179 | it.IsValid(); it.Increment()) { |
| 180 | it.GetChannel()->RegisterVoiceEngineObserver(observer); |
| 181 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 182 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 183 | shared_->transmit_mixer()->RegisterVoiceEngineObserver(observer); |
| 184 | voiceEngineObserverPtr_ = &observer; |
| 185 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 188 | int VoEBaseImpl::DeRegisterVoiceEngineObserver() { |
| 189 | CriticalSectionScoped cs(&callbackCritSect_); |
| 190 | if (!voiceEngineObserverPtr_) { |
| 191 | shared_->SetLastError( |
| 192 | VE_INVALID_OPERATION, kTraceError, |
| 193 | "DeRegisterVoiceEngineObserver() observer already disabled"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 195 | } |
| 196 | voiceEngineObserverPtr_ = nullptr; |
| 197 | |
| 198 | // Deregister the observer in all active channels |
| 199 | for (voe::ChannelManager::Iterator it(&shared_->channel_manager()); |
| 200 | it.IsValid(); it.Increment()) { |
| 201 | it.GetChannel()->DeRegisterVoiceEngineObserver(); |
| 202 | } |
| 203 | |
| 204 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 207 | int VoEBaseImpl::Init(AudioDeviceModule* external_adm, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 208 | AudioProcessing* audioproc) { |
| 209 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 210 | WebRtcSpl_Init(); |
| 211 | if (shared_->statistics().Initialized()) { |
| 212 | return 0; |
| 213 | } |
| 214 | if (shared_->process_thread()) { |
| 215 | shared_->process_thread()->Start(); |
| 216 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 218 | // Create an internal ADM if the user has not added an external |
| 219 | // ADM implementation as input to Init(). |
| 220 | if (external_adm == nullptr) { |
| 221 | // Create the internal ADM implementation. |
| 222 | shared_->set_audio_device(AudioDeviceModuleImpl::Create( |
| 223 | VoEId(shared_->instance_id(), -1), shared_->audio_device_layer())); |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 224 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 225 | if (shared_->audio_device() == nullptr) { |
| 226 | shared_->SetLastError(VE_NO_MEMORY, kTraceCritical, |
| 227 | "Init() failed to create the ADM"); |
| 228 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 230 | } else { |
| 231 | // Use the already existing external ADM implementation. |
| 232 | shared_->set_audio_device(external_adm); |
| 233 | LOG_F(LS_INFO) |
| 234 | << "An external ADM implementation will be used in VoiceEngine"; |
| 235 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 237 | // Register the ADM to the process thread, which will drive the error |
| 238 | // callback mechanism |
| 239 | if (shared_->process_thread()) { |
| 240 | shared_->process_thread()->RegisterModule(shared_->audio_device()); |
| 241 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 243 | bool available = false; |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 244 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 245 | // -------------------- |
| 246 | // Reinitialize the ADM |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 247 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 248 | // Register the AudioObserver implementation |
| 249 | if (shared_->audio_device()->RegisterEventObserver(this) != 0) { |
| 250 | shared_->SetLastError( |
| 251 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 252 | "Init() failed to register event observer for the ADM"); |
| 253 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 255 | // Register the AudioTransport implementation |
| 256 | if (shared_->audio_device()->RegisterAudioCallback(this) != 0) { |
| 257 | shared_->SetLastError( |
| 258 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 259 | "Init() failed to register audio callback for the ADM"); |
| 260 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 262 | // ADM initialization |
| 263 | if (shared_->audio_device()->Init() != 0) { |
| 264 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 265 | "Init() failed to initialize the ADM"); |
| 266 | return -1; |
| 267 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 268 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 269 | // Initialize the default speaker |
| 270 | if (shared_->audio_device()->SetPlayoutDevice( |
| 271 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) { |
| 272 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo, |
| 273 | "Init() failed to set the default output device"); |
| 274 | } |
| 275 | if (shared_->audio_device()->InitSpeaker() != 0) { |
| 276 | shared_->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
| 277 | "Init() failed to initialize the speaker"); |
| 278 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 280 | // Initialize the default microphone |
| 281 | if (shared_->audio_device()->SetRecordingDevice( |
| 282 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) { |
| 283 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo, |
| 284 | "Init() failed to set the default input device"); |
| 285 | } |
| 286 | if (shared_->audio_device()->InitMicrophone() != 0) { |
| 287 | shared_->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
| 288 | "Init() failed to initialize the microphone"); |
| 289 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 290 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 291 | // Set number of channels |
| 292 | if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) { |
| 293 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 294 | "Init() failed to query stereo playout mode"); |
| 295 | } |
| 296 | if (shared_->audio_device()->SetStereoPlayout(available) != 0) { |
| 297 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 298 | "Init() failed to set mono/stereo playout mode"); |
| 299 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 301 | // TODO(andrew): These functions don't tell us whether stereo recording |
| 302 | // is truly available. We simply set the AudioProcessing input to stereo |
| 303 | // here, because we have to wait until receiving the first frame to |
| 304 | // determine the actual number of channels anyway. |
| 305 | // |
| 306 | // These functions may be changed; tracked here: |
| 307 | // http://code.google.com/p/webrtc/issues/detail?id=204 |
| 308 | shared_->audio_device()->StereoRecordingIsAvailable(&available); |
| 309 | if (shared_->audio_device()->SetStereoRecording(available) != 0) { |
| 310 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 311 | "Init() failed to set mono/stereo recording mode"); |
| 312 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 314 | if (!audioproc) { |
| 315 | audioproc = AudioProcessing::Create(); |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 316 | if (!audioproc) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 317 | LOG(LS_ERROR) << "Failed to create AudioProcessing."; |
| 318 | shared_->SetLastError(VE_NO_MEMORY); |
| 319 | return -1; |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 320 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 321 | } |
| 322 | shared_->set_audio_processing(audioproc); |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 323 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 324 | // Set the error state for any failures in this block. |
| 325 | shared_->SetLastError(VE_APM_ERROR); |
| 326 | // Configure AudioProcessing components. |
| 327 | if (audioproc->high_pass_filter()->Enable(true) != 0) { |
| 328 | LOG_FERR1(LS_ERROR, high_pass_filter()->Enable, true); |
| 329 | return -1; |
| 330 | } |
| 331 | if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) { |
| 332 | LOG_FERR1(LS_ERROR, enable_drift_compensation, false); |
| 333 | return -1; |
| 334 | } |
| 335 | if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) { |
| 336 | LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode); |
| 337 | return -1; |
| 338 | } |
| 339 | GainControl* agc = audioproc->gain_control(); |
| 340 | if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) { |
| 341 | LOG_FERR2(LS_ERROR, agc->set_analog_level_limits, kMinVolumeLevel, |
| 342 | kMaxVolumeLevel); |
| 343 | return -1; |
| 344 | } |
| 345 | if (agc->set_mode(kDefaultAgcMode) != 0) { |
| 346 | LOG_FERR1(LS_ERROR, agc->set_mode, kDefaultAgcMode); |
| 347 | return -1; |
| 348 | } |
| 349 | if (agc->Enable(kDefaultAgcState) != 0) { |
| 350 | LOG_FERR1(LS_ERROR, agc->Enable, kDefaultAgcState); |
| 351 | return -1; |
| 352 | } |
| 353 | shared_->SetLastError(0); // Clear error state. |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 354 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 355 | #ifdef WEBRTC_VOICE_ENGINE_AGC |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 356 | bool agc_enabled = |
| 357 | agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled(); |
| 358 | if (shared_->audio_device()->SetAGC(agc_enabled) != 0) { |
| 359 | LOG_FERR1(LS_ERROR, audio_device()->SetAGC, agc_enabled); |
| 360 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR); |
| 361 | // TODO(ajm): No error return here due to |
| 362 | // https://code.google.com/p/webrtc/issues/detail?id=1464 |
| 363 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 364 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 366 | return shared_->statistics().SetInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 369 | int VoEBaseImpl::Terminate() { |
| 370 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 371 | return TerminateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 372 | } |
| 373 | |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 374 | int VoEBaseImpl::CreateChannel() { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 375 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 376 | if (!shared_->statistics().Initialized()) { |
| 377 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 378 | return -1; |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 381 | voe::ChannelOwner channel_owner = shared_->channel_manager().CreateChannel(); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 382 | return InitializeChannel(&channel_owner); |
| 383 | } |
| 384 | |
| 385 | int VoEBaseImpl::CreateChannel(const Config& config) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 386 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 387 | if (!shared_->statistics().Initialized()) { |
| 388 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 389 | return -1; |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 390 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 391 | voe::ChannelOwner channel_owner = |
| 392 | shared_->channel_manager().CreateChannel(config); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 393 | return InitializeChannel(&channel_owner); |
| 394 | } |
| 395 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 396 | int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) { |
| 397 | if (channel_owner->channel()->SetEngineInformation( |
| 398 | shared_->statistics(), *shared_->output_mixer(), |
| 399 | *shared_->transmit_mixer(), *shared_->process_thread(), |
| 400 | *shared_->audio_device(), voiceEngineObserverPtr_, |
| 401 | &callbackCritSect_) != 0) { |
| 402 | shared_->SetLastError( |
| 403 | VE_CHANNEL_NOT_CREATED, kTraceError, |
| 404 | "CreateChannel() failed to associate engine and channel." |
| 405 | " Destroying channel."); |
| 406 | shared_->channel_manager().DestroyChannel( |
| 407 | channel_owner->channel()->ChannelId()); |
| 408 | return -1; |
| 409 | } else if (channel_owner->channel()->Init() != 0) { |
| 410 | shared_->SetLastError( |
| 411 | VE_CHANNEL_NOT_CREATED, kTraceError, |
| 412 | "CreateChannel() failed to initialize channel. Destroying" |
| 413 | " channel."); |
| 414 | shared_->channel_manager().DestroyChannel( |
| 415 | channel_owner->channel()->ChannelId()); |
| 416 | return -1; |
| 417 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 418 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 419 | return channel_owner->channel()->ChannelId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 422 | int VoEBaseImpl::DeleteChannel(int channel) { |
| 423 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 424 | if (!shared_->statistics().Initialized()) { |
| 425 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | { |
| 430 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 431 | voe::Channel* channelPtr = ch.channel(); |
| 432 | if (channelPtr == nullptr) { |
| 433 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 434 | "DeleteChannel() failed to locate channel"); |
| 435 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 436 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 437 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 439 | shared_->channel_manager().DestroyChannel(channel); |
| 440 | if (StopSend() != 0) { |
| 441 | return -1; |
| 442 | } |
| 443 | if (StopPlayout() != 0) { |
| 444 | return -1; |
| 445 | } |
| 446 | return 0; |
| 447 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 448 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 449 | int VoEBaseImpl::StartReceive(int channel) { |
| 450 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 451 | if (!shared_->statistics().Initialized()) { |
| 452 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 453 | return -1; |
| 454 | } |
| 455 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 456 | voe::Channel* channelPtr = ch.channel(); |
| 457 | if (channelPtr == nullptr) { |
| 458 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 459 | "StartReceive() failed to locate channel"); |
| 460 | return -1; |
| 461 | } |
| 462 | return channelPtr->StartReceiving(); |
| 463 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 464 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 465 | int VoEBaseImpl::StopReceive(int channel) { |
| 466 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 467 | if (!shared_->statistics().Initialized()) { |
| 468 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 469 | return -1; |
| 470 | } |
| 471 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 472 | voe::Channel* channelPtr = ch.channel(); |
| 473 | if (channelPtr == nullptr) { |
| 474 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 475 | "SetLocalReceiver() failed to locate channel"); |
| 476 | return -1; |
| 477 | } |
| 478 | return channelPtr->StopReceiving(); |
| 479 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 480 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 481 | int VoEBaseImpl::StartPlayout(int channel) { |
| 482 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 483 | if (!shared_->statistics().Initialized()) { |
| 484 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 485 | return -1; |
| 486 | } |
| 487 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 488 | voe::Channel* channelPtr = ch.channel(); |
| 489 | if (channelPtr == nullptr) { |
| 490 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 491 | "StartPlayout() failed to locate channel"); |
| 492 | return -1; |
| 493 | } |
| 494 | if (channelPtr->Playing()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 495 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 496 | } |
| 497 | if (StartPlayout() != 0) { |
| 498 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 499 | "StartPlayout() failed to start playout"); |
| 500 | return -1; |
| 501 | } |
| 502 | return channelPtr->StartPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 505 | int VoEBaseImpl::StopPlayout(int channel) { |
| 506 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 507 | if (!shared_->statistics().Initialized()) { |
| 508 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 509 | return -1; |
| 510 | } |
| 511 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 512 | voe::Channel* channelPtr = ch.channel(); |
| 513 | if (channelPtr == nullptr) { |
| 514 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 515 | "StopPlayout() failed to locate channel"); |
| 516 | return -1; |
| 517 | } |
| 518 | if (channelPtr->StopPlayout() != 0) { |
| 519 | LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel " |
| 520 | << channel; |
| 521 | } |
| 522 | return StopPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 525 | int VoEBaseImpl::StartSend(int channel) { |
| 526 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 527 | if (!shared_->statistics().Initialized()) { |
| 528 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 529 | return -1; |
| 530 | } |
| 531 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 532 | voe::Channel* channelPtr = ch.channel(); |
| 533 | if (channelPtr == nullptr) { |
| 534 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 535 | "StartSend() failed to locate channel"); |
| 536 | return -1; |
| 537 | } |
| 538 | if (channelPtr->Sending()) { |
| 539 | return 0; |
| 540 | } |
| 541 | if (StartSend() != 0) { |
| 542 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 543 | "StartSend() failed to start recording"); |
| 544 | return -1; |
| 545 | } |
| 546 | return channelPtr->StartSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 549 | int VoEBaseImpl::StopSend(int channel) { |
| 550 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 551 | if (!shared_->statistics().Initialized()) { |
| 552 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 553 | return -1; |
| 554 | } |
| 555 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 556 | voe::Channel* channelPtr = ch.channel(); |
| 557 | if (channelPtr == nullptr) { |
| 558 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 559 | "StopSend() failed to locate channel"); |
| 560 | return -1; |
| 561 | } |
| 562 | if (channelPtr->StopSend() != 0) { |
| 563 | LOG_F(LS_WARNING) << "StopSend() failed to stop sending for channel " |
| 564 | << channel; |
| 565 | } |
| 566 | return StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 569 | int VoEBaseImpl::GetVersion(char version[1024]) { |
| 570 | assert(kVoiceEngineVersionMaxMessageSize == 1024); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 571 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 572 | if (version == nullptr) { |
| 573 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError); |
| 574 | return (-1); |
| 575 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 576 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 577 | char versionBuf[kVoiceEngineVersionMaxMessageSize]; |
| 578 | char* versionPtr = versionBuf; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 579 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 580 | int32_t len = 0; |
| 581 | int32_t accLen = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 582 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 583 | len = AddVoEVersion(versionPtr); |
| 584 | if (len == -1) { |
| 585 | return -1; |
| 586 | } |
| 587 | versionPtr += len; |
| 588 | accLen += len; |
| 589 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 591 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 592 | len = AddExternalTransportBuild(versionPtr); |
| 593 | if (len == -1) { |
| 594 | return -1; |
| 595 | } |
| 596 | versionPtr += len; |
| 597 | accLen += len; |
| 598 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 599 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 600 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 601 | memcpy(version, versionBuf, accLen); |
| 602 | version[accLen] = '\0'; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 604 | // to avoid the truncation in the trace, split the string into parts |
| 605 | char partOfVersion[256]; |
| 606 | for (int partStart = 0; partStart < accLen;) { |
| 607 | memset(partOfVersion, 0, sizeof(partOfVersion)); |
| 608 | int partEnd = partStart + 180; |
| 609 | while (version[partEnd] != '\n' && version[partEnd] != '\0') { |
| 610 | partEnd--; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 611 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 612 | if (partEnd < accLen) { |
| 613 | memcpy(partOfVersion, &version[partStart], partEnd - partStart); |
| 614 | } else { |
| 615 | memcpy(partOfVersion, &version[partStart], accLen - partStart); |
| 616 | } |
| 617 | partStart = partEnd; |
| 618 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 619 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 620 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 623 | int32_t VoEBaseImpl::AddVoEVersion(char* str) const { |
| 624 | return sprintf(str, "VoiceEngine 4.1.0\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | } |
| 626 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 627 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 628 | int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const { |
| 629 | return sprintf(str, "External transport build\n"); |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 630 | } |
| 631 | #endif |
| 632 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 633 | int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 634 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 635 | int32_t VoEBaseImpl::StartPlayout() { |
| 636 | if (shared_->audio_device()->Playing()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 637 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 638 | } |
| 639 | if (!shared_->ext_playout()) { |
| 640 | if (shared_->audio_device()->InitPlayout() != 0) { |
| 641 | LOG_F(LS_ERROR) << "Failed to initialize playout"; |
| 642 | return -1; |
| 643 | } |
| 644 | if (shared_->audio_device()->StartPlayout() != 0) { |
| 645 | LOG_F(LS_ERROR) << "Failed to start playout"; |
| 646 | return -1; |
| 647 | } |
| 648 | } |
| 649 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 650 | } |
| 651 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 652 | int32_t VoEBaseImpl::StopPlayout() { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 653 | // Stop audio-device playing if no channel is playing out |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 654 | if (shared_->NumOfPlayingChannels() == 0) { |
| 655 | if (shared_->audio_device()->StopPlayout() != 0) { |
| 656 | shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError, |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 657 | "StopPlayout() failed to stop playout"); |
| 658 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 659 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 660 | } |
| 661 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 664 | int32_t VoEBaseImpl::StartSend() { |
| 665 | if (shared_->audio_device()->Recording()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 666 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 667 | } |
| 668 | if (!shared_->ext_recording()) { |
| 669 | if (shared_->audio_device()->InitRecording() != 0) { |
| 670 | LOG_F(LS_ERROR) << "Failed to initialize recording"; |
| 671 | return -1; |
| 672 | } |
| 673 | if (shared_->audio_device()->StartRecording() != 0) { |
| 674 | LOG_F(LS_ERROR) << "Failed to start recording"; |
| 675 | return -1; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 682 | int32_t VoEBaseImpl::StopSend() { |
| 683 | if (shared_->NumOfSendingChannels() == 0 && |
| 684 | !shared_->transmit_mixer()->IsRecordingMic()) { |
| 685 | // Stop audio-device recording if no channel is recording |
| 686 | if (shared_->audio_device()->StopRecording() != 0) { |
| 687 | shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError, |
| 688 | "StopSend() failed to stop recording"); |
| 689 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 690 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 691 | shared_->transmit_mixer()->StopSend(); |
| 692 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 693 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 694 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 697 | int32_t VoEBaseImpl::TerminateInternal() { |
| 698 | // Delete any remaining channel objects |
| 699 | shared_->channel_manager().DestroyAllChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 700 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 701 | if (shared_->process_thread()) { |
| 702 | if (shared_->audio_device()) { |
| 703 | shared_->process_thread()->DeRegisterModule(shared_->audio_device()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 704 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 705 | shared_->process_thread()->Stop(); |
| 706 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 707 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 708 | if (shared_->audio_device()) { |
| 709 | if (shared_->audio_device()->StopPlayout() != 0) { |
| 710 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 711 | "TerminateInternal() failed to stop playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 712 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 713 | if (shared_->audio_device()->StopRecording() != 0) { |
| 714 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 715 | "TerminateInternal() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 716 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 717 | if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) { |
| 718 | shared_->SetLastError( |
| 719 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 720 | "TerminateInternal() failed to de-register event observer " |
| 721 | "for the ADM"); |
| 722 | } |
| 723 | if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) { |
| 724 | shared_->SetLastError( |
| 725 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 726 | "TerminateInternal() failed to de-register audio callback " |
| 727 | "for the ADM"); |
| 728 | } |
| 729 | if (shared_->audio_device()->Terminate() != 0) { |
| 730 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 731 | "TerminateInternal() failed to terminate the ADM"); |
| 732 | } |
| 733 | shared_->set_audio_device(nullptr); |
| 734 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 735 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 736 | if (shared_->audio_processing()) { |
| 737 | shared_->set_audio_processing(nullptr); |
| 738 | } |
| 739 | |
| 740 | return shared_->statistics().SetUnInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 741 | } |
| 742 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 743 | int VoEBaseImpl::ProcessRecordedDataWithAPM( |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 744 | const int voe_channels[], int number_of_voe_channels, |
| 745 | const void* audio_data, uint32_t sample_rate, uint8_t number_of_channels, |
| 746 | uint32_t number_of_frames, uint32_t audio_delay_milliseconds, |
| 747 | int32_t clock_drift, uint32_t volume, bool key_pressed) { |
| 748 | assert(shared_->transmit_mixer() != nullptr); |
| 749 | assert(shared_->audio_device() != nullptr); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 750 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 751 | uint32_t max_volume = 0; |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 752 | uint16_t voe_mic_level = 0; |
andrew@webrtc.org | 023cc5a | 2014-01-11 01:25:53 +0000 | [diff] [blame] | 753 | // Check for zero to skip this calculation; the consumer may use this to |
| 754 | // indicate no volume is available. |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 755 | if (volume != 0) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 756 | // Scale from ADM to VoE level range |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 757 | if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 758 | if (max_volume) { |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 759 | voe_mic_level = static_cast<uint16_t>( |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 760 | (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) / |
| 761 | max_volume); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 764 | // We learned that on certain systems (e.g Linux) the voe_mic_level |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 765 | // can be greater than the maxVolumeLevel therefore |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 766 | // we are going to cap the voe_mic_level to the maxVolumeLevel |
| 767 | // and change the maxVolume to volume if it turns out that |
| 768 | // the voe_mic_level is indeed greater than the maxVolumeLevel. |
| 769 | if (voe_mic_level > kMaxVolumeLevel) { |
| 770 | voe_mic_level = kMaxVolumeLevel; |
| 771 | max_volume = volume; |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 775 | // Perform channel-independent operations |
| 776 | // (APM, mix with file, record to file, mute, etc.) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 777 | shared_->transmit_mixer()->PrepareDemux( |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 778 | audio_data, number_of_frames, number_of_channels, sample_rate, |
| 779 | static_cast<uint16_t>(audio_delay_milliseconds), clock_drift, |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 780 | voe_mic_level, key_pressed); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 781 | |
| 782 | // Copy the audio frame to each sending channel and perform |
| 783 | // channel-dependent operations (file mixing, mute, etc.), encode and |
| 784 | // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0, |
| 785 | // do the operations on all the existing VoE channels; otherwise the |
| 786 | // operations will be done on specific channels. |
| 787 | if (number_of_voe_channels == 0) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 788 | shared_->transmit_mixer()->DemuxAndMix(); |
| 789 | shared_->transmit_mixer()->EncodeAndSend(); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 790 | } else { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 791 | shared_->transmit_mixer()->DemuxAndMix(voe_channels, |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 792 | number_of_voe_channels); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 793 | shared_->transmit_mixer()->EncodeAndSend(voe_channels, |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 794 | number_of_voe_channels); |
| 795 | } |
| 796 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 797 | // Scale from VoE to ADM level range. |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 798 | uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel(); |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 799 | if (new_voe_mic_level != voe_mic_level) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 800 | // Return the new volume if AGC has changed the volume. |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 801 | return static_cast<int>((new_voe_mic_level * max_volume + |
| 802 | static_cast<int>(kMaxVolumeLevel / 2)) / |
| 803 | kMaxVolumeLevel); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // Return 0 to indicate no change on the volume. |
| 807 | return 0; |
| 808 | } |
| 809 | |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 810 | void VoEBaseImpl::GetPlayoutData(int sample_rate, int number_of_channels, |
| 811 | int number_of_frames, bool feed_data_to_apm, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 812 | void* audio_data, int64_t* elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 813 | int64_t* ntp_time_ms) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 814 | assert(shared_->output_mixer() != nullptr); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 815 | |
| 816 | // TODO(andrew): if the device is running in mono, we should tell the mixer |
| 817 | // here so that it will only request mono from AudioCodingModule. |
| 818 | // Perform mixing of all active participants (channel-based mixing) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 819 | shared_->output_mixer()->MixActiveChannels(); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 820 | |
| 821 | // Additional operations on the combined signal |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 822 | shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 823 | |
| 824 | // Retrieve the final output mix (resampled to match the ADM) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 825 | shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels, |
| 826 | &audioFrame_); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 827 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 828 | assert(number_of_frames == audioFrame_.samples_per_channel_); |
| 829 | assert(sample_rate == audioFrame_.sample_rate_hz_); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 830 | |
| 831 | // Deliver audio (PCM) samples to the ADM |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 832 | memcpy(audio_data, audioFrame_.data_, |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 833 | sizeof(int16_t) * number_of_frames * number_of_channels); |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 834 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 835 | *elapsed_time_ms = audioFrame_.elapsed_time_ms_; |
| 836 | *ntp_time_ms = audioFrame_.ntp_time_ms_; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 837 | } |
| 838 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 839 | } // namespace webrtc |