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) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 328 | LOG_F(LS_ERROR) << "Failed to enable high pass filter."; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 332 | LOG_F(LS_ERROR) << "Failed to disable drift compensation."; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 333 | return -1; |
| 334 | } |
| 335 | if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 336 | LOG_F(LS_ERROR) << "Failed to set noise suppression level: " |
| 337 | << kDefaultNsMode; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 338 | return -1; |
| 339 | } |
| 340 | GainControl* agc = audioproc->gain_control(); |
| 341 | if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 342 | LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: " |
| 343 | << kMinVolumeLevel << " and maximum: " << kMaxVolumeLevel; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | if (agc->set_mode(kDefaultAgcMode) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 347 | LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | if (agc->Enable(kDefaultAgcState) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 351 | LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | shared_->SetLastError(0); // Clear error state. |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 355 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 356 | #ifdef WEBRTC_VOICE_ENGINE_AGC |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 357 | bool agc_enabled = |
| 358 | agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled(); |
| 359 | if (shared_->audio_device()->SetAGC(agc_enabled) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame^] | 360 | LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 361 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR); |
| 362 | // TODO(ajm): No error return here due to |
| 363 | // https://code.google.com/p/webrtc/issues/detail?id=1464 |
| 364 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 366 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 367 | return shared_->statistics().SetInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 370 | int VoEBaseImpl::Terminate() { |
| 371 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 372 | return TerminateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | } |
| 374 | |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 375 | int VoEBaseImpl::CreateChannel() { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 376 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 377 | if (!shared_->statistics().Initialized()) { |
| 378 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 379 | return -1; |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 382 | voe::ChannelOwner channel_owner = shared_->channel_manager().CreateChannel(); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 383 | return InitializeChannel(&channel_owner); |
| 384 | } |
| 385 | |
| 386 | int VoEBaseImpl::CreateChannel(const Config& config) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 387 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 388 | if (!shared_->statistics().Initialized()) { |
| 389 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 390 | return -1; |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 391 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 392 | voe::ChannelOwner channel_owner = |
| 393 | shared_->channel_manager().CreateChannel(config); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 394 | return InitializeChannel(&channel_owner); |
| 395 | } |
| 396 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 397 | int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) { |
| 398 | if (channel_owner->channel()->SetEngineInformation( |
| 399 | shared_->statistics(), *shared_->output_mixer(), |
| 400 | *shared_->transmit_mixer(), *shared_->process_thread(), |
| 401 | *shared_->audio_device(), voiceEngineObserverPtr_, |
| 402 | &callbackCritSect_) != 0) { |
| 403 | shared_->SetLastError( |
| 404 | VE_CHANNEL_NOT_CREATED, kTraceError, |
| 405 | "CreateChannel() failed to associate engine and channel." |
| 406 | " Destroying channel."); |
| 407 | shared_->channel_manager().DestroyChannel( |
| 408 | channel_owner->channel()->ChannelId()); |
| 409 | return -1; |
| 410 | } else if (channel_owner->channel()->Init() != 0) { |
| 411 | shared_->SetLastError( |
| 412 | VE_CHANNEL_NOT_CREATED, kTraceError, |
| 413 | "CreateChannel() failed to initialize channel. Destroying" |
| 414 | " channel."); |
| 415 | shared_->channel_manager().DestroyChannel( |
| 416 | channel_owner->channel()->ChannelId()); |
| 417 | return -1; |
| 418 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 419 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 420 | return channel_owner->channel()->ChannelId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 423 | int VoEBaseImpl::DeleteChannel(int channel) { |
| 424 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 425 | if (!shared_->statistics().Initialized()) { |
| 426 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | { |
| 431 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 432 | voe::Channel* channelPtr = ch.channel(); |
| 433 | if (channelPtr == nullptr) { |
| 434 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 435 | "DeleteChannel() failed to locate channel"); |
| 436 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 437 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 438 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 439 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 440 | shared_->channel_manager().DestroyChannel(channel); |
| 441 | if (StopSend() != 0) { |
| 442 | return -1; |
| 443 | } |
| 444 | if (StopPlayout() != 0) { |
| 445 | return -1; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 449 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 450 | int VoEBaseImpl::StartReceive(int channel) { |
| 451 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 452 | if (!shared_->statistics().Initialized()) { |
| 453 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 454 | return -1; |
| 455 | } |
| 456 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 457 | voe::Channel* channelPtr = ch.channel(); |
| 458 | if (channelPtr == nullptr) { |
| 459 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 460 | "StartReceive() failed to locate channel"); |
| 461 | return -1; |
| 462 | } |
| 463 | return channelPtr->StartReceiving(); |
| 464 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 465 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 466 | int VoEBaseImpl::StopReceive(int channel) { |
| 467 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 468 | if (!shared_->statistics().Initialized()) { |
| 469 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 470 | return -1; |
| 471 | } |
| 472 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 473 | voe::Channel* channelPtr = ch.channel(); |
| 474 | if (channelPtr == nullptr) { |
| 475 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 476 | "SetLocalReceiver() failed to locate channel"); |
| 477 | return -1; |
| 478 | } |
| 479 | return channelPtr->StopReceiving(); |
| 480 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 481 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 482 | int VoEBaseImpl::StartPlayout(int channel) { |
| 483 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 484 | if (!shared_->statistics().Initialized()) { |
| 485 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 486 | return -1; |
| 487 | } |
| 488 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 489 | voe::Channel* channelPtr = ch.channel(); |
| 490 | if (channelPtr == nullptr) { |
| 491 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 492 | "StartPlayout() failed to locate channel"); |
| 493 | return -1; |
| 494 | } |
| 495 | if (channelPtr->Playing()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 496 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 497 | } |
| 498 | if (StartPlayout() != 0) { |
| 499 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 500 | "StartPlayout() failed to start playout"); |
| 501 | return -1; |
| 502 | } |
| 503 | return channelPtr->StartPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 506 | int VoEBaseImpl::StopPlayout(int channel) { |
| 507 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 508 | if (!shared_->statistics().Initialized()) { |
| 509 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 510 | return -1; |
| 511 | } |
| 512 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 513 | voe::Channel* channelPtr = ch.channel(); |
| 514 | if (channelPtr == nullptr) { |
| 515 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 516 | "StopPlayout() failed to locate channel"); |
| 517 | return -1; |
| 518 | } |
| 519 | if (channelPtr->StopPlayout() != 0) { |
| 520 | LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel " |
| 521 | << channel; |
| 522 | } |
| 523 | return StopPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 526 | int VoEBaseImpl::StartSend(int channel) { |
| 527 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 528 | if (!shared_->statistics().Initialized()) { |
| 529 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 530 | return -1; |
| 531 | } |
| 532 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 533 | voe::Channel* channelPtr = ch.channel(); |
| 534 | if (channelPtr == nullptr) { |
| 535 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 536 | "StartSend() failed to locate channel"); |
| 537 | return -1; |
| 538 | } |
| 539 | if (channelPtr->Sending()) { |
| 540 | return 0; |
| 541 | } |
| 542 | if (StartSend() != 0) { |
| 543 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 544 | "StartSend() failed to start recording"); |
| 545 | return -1; |
| 546 | } |
| 547 | return channelPtr->StartSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 550 | int VoEBaseImpl::StopSend(int channel) { |
| 551 | CriticalSectionScoped cs(shared_->crit_sec()); |
| 552 | if (!shared_->statistics().Initialized()) { |
| 553 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 554 | return -1; |
| 555 | } |
| 556 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 557 | voe::Channel* channelPtr = ch.channel(); |
| 558 | if (channelPtr == nullptr) { |
| 559 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 560 | "StopSend() failed to locate channel"); |
| 561 | return -1; |
| 562 | } |
| 563 | if (channelPtr->StopSend() != 0) { |
| 564 | LOG_F(LS_WARNING) << "StopSend() failed to stop sending for channel " |
| 565 | << channel; |
| 566 | } |
| 567 | return StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 570 | int VoEBaseImpl::GetVersion(char version[1024]) { |
| 571 | assert(kVoiceEngineVersionMaxMessageSize == 1024); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 572 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 573 | if (version == nullptr) { |
| 574 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError); |
| 575 | return (-1); |
| 576 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 578 | char versionBuf[kVoiceEngineVersionMaxMessageSize]; |
| 579 | char* versionPtr = versionBuf; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 580 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 581 | int32_t len = 0; |
| 582 | int32_t accLen = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 584 | len = AddVoEVersion(versionPtr); |
| 585 | if (len == -1) { |
| 586 | return -1; |
| 587 | } |
| 588 | versionPtr += len; |
| 589 | accLen += len; |
| 590 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 591 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 592 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 593 | len = AddExternalTransportBuild(versionPtr); |
| 594 | if (len == -1) { |
| 595 | return -1; |
| 596 | } |
| 597 | versionPtr += len; |
| 598 | accLen += len; |
| 599 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 600 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 602 | memcpy(version, versionBuf, accLen); |
| 603 | version[accLen] = '\0'; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 604 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 605 | // to avoid the truncation in the trace, split the string into parts |
| 606 | char partOfVersion[256]; |
| 607 | for (int partStart = 0; partStart < accLen;) { |
| 608 | memset(partOfVersion, 0, sizeof(partOfVersion)); |
| 609 | int partEnd = partStart + 180; |
| 610 | while (version[partEnd] != '\n' && version[partEnd] != '\0') { |
| 611 | partEnd--; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 612 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 613 | if (partEnd < accLen) { |
| 614 | memcpy(partOfVersion, &version[partStart], partEnd - partStart); |
| 615 | } else { |
| 616 | memcpy(partOfVersion, &version[partStart], accLen - partStart); |
| 617 | } |
| 618 | partStart = partEnd; |
| 619 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 621 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 624 | int32_t VoEBaseImpl::AddVoEVersion(char* str) const { |
| 625 | return sprintf(str, "VoiceEngine 4.1.0\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 626 | } |
| 627 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 628 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 629 | int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const { |
| 630 | return sprintf(str, "External transport build\n"); |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 631 | } |
| 632 | #endif |
| 633 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 634 | int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 635 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 636 | int32_t VoEBaseImpl::StartPlayout() { |
| 637 | if (shared_->audio_device()->Playing()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 638 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 639 | } |
| 640 | if (!shared_->ext_playout()) { |
| 641 | if (shared_->audio_device()->InitPlayout() != 0) { |
| 642 | LOG_F(LS_ERROR) << "Failed to initialize playout"; |
| 643 | return -1; |
| 644 | } |
| 645 | if (shared_->audio_device()->StartPlayout() != 0) { |
| 646 | LOG_F(LS_ERROR) << "Failed to start playout"; |
| 647 | return -1; |
| 648 | } |
| 649 | } |
| 650 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 651 | } |
| 652 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 653 | int32_t VoEBaseImpl::StopPlayout() { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 654 | // Stop audio-device playing if no channel is playing out |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 655 | if (shared_->NumOfPlayingChannels() == 0) { |
| 656 | if (shared_->audio_device()->StopPlayout() != 0) { |
| 657 | shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError, |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 658 | "StopPlayout() failed to stop playout"); |
| 659 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 660 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 661 | } |
| 662 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 665 | int32_t VoEBaseImpl::StartSend() { |
| 666 | if (shared_->audio_device()->Recording()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 667 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 668 | } |
| 669 | if (!shared_->ext_recording()) { |
| 670 | if (shared_->audio_device()->InitRecording() != 0) { |
| 671 | LOG_F(LS_ERROR) << "Failed to initialize recording"; |
| 672 | return -1; |
| 673 | } |
| 674 | if (shared_->audio_device()->StartRecording() != 0) { |
| 675 | LOG_F(LS_ERROR) << "Failed to start recording"; |
| 676 | return -1; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 683 | int32_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 691 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 692 | shared_->transmit_mixer()->StopSend(); |
| 693 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 694 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 695 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 698 | int32_t VoEBaseImpl::TerminateInternal() { |
| 699 | // Delete any remaining channel objects |
| 700 | shared_->channel_manager().DestroyAllChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 702 | if (shared_->process_thread()) { |
| 703 | if (shared_->audio_device()) { |
| 704 | shared_->process_thread()->DeRegisterModule(shared_->audio_device()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 705 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 706 | shared_->process_thread()->Stop(); |
| 707 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 708 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 709 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 713 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 714 | if (shared_->audio_device()->StopRecording() != 0) { |
| 715 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 716 | "TerminateInternal() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 717 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 718 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 736 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 737 | if (shared_->audio_processing()) { |
| 738 | shared_->set_audio_processing(nullptr); |
| 739 | } |
| 740 | |
| 741 | return shared_->statistics().SetUnInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 742 | } |
| 743 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 744 | int VoEBaseImpl::ProcessRecordedDataWithAPM( |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 745 | const int voe_channels[], int number_of_voe_channels, |
| 746 | const void* audio_data, uint32_t sample_rate, uint8_t number_of_channels, |
| 747 | uint32_t number_of_frames, uint32_t audio_delay_milliseconds, |
| 748 | int32_t clock_drift, uint32_t volume, bool key_pressed) { |
| 749 | assert(shared_->transmit_mixer() != nullptr); |
| 750 | assert(shared_->audio_device() != nullptr); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 751 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 752 | uint32_t max_volume = 0; |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 753 | uint16_t voe_mic_level = 0; |
andrew@webrtc.org | 023cc5a | 2014-01-11 01:25:53 +0000 | [diff] [blame] | 754 | // Check for zero to skip this calculation; the consumer may use this to |
| 755 | // indicate no volume is available. |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 756 | if (volume != 0) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 757 | // Scale from ADM to VoE level range |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 758 | if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 759 | if (max_volume) { |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 760 | voe_mic_level = static_cast<uint16_t>( |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 761 | (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) / |
| 762 | max_volume); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 765 | // 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] | 766 | // can be greater than the maxVolumeLevel therefore |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 767 | // 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.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 776 | // Perform channel-independent operations |
| 777 | // (APM, mix with file, record to file, mute, etc.) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 778 | shared_->transmit_mixer()->PrepareDemux( |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 779 | audio_data, number_of_frames, number_of_channels, sample_rate, |
| 780 | static_cast<uint16_t>(audio_delay_milliseconds), clock_drift, |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 781 | voe_mic_level, key_pressed); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 782 | |
| 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 Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 789 | shared_->transmit_mixer()->DemuxAndMix(); |
| 790 | shared_->transmit_mixer()->EncodeAndSend(); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 791 | } else { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 792 | shared_->transmit_mixer()->DemuxAndMix(voe_channels, |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 793 | number_of_voe_channels); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 794 | shared_->transmit_mixer()->EncodeAndSend(voe_channels, |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 795 | number_of_voe_channels); |
| 796 | } |
| 797 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 798 | // Scale from VoE to ADM level range. |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 799 | uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel(); |
andrew@webrtc.org | 27c6980 | 2014-02-18 20:24:56 +0000 | [diff] [blame] | 800 | if (new_voe_mic_level != voe_mic_level) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 801 | // Return the new volume if AGC has changed the volume. |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 802 | return static_cast<int>((new_voe_mic_level * max_volume + |
| 803 | static_cast<int>(kMaxVolumeLevel / 2)) / |
| 804 | kMaxVolumeLevel); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // Return 0 to indicate no change on the volume. |
| 808 | return 0; |
| 809 | } |
| 810 | |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 811 | void VoEBaseImpl::GetPlayoutData(int sample_rate, int number_of_channels, |
| 812 | int number_of_frames, bool feed_data_to_apm, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 813 | void* audio_data, int64_t* elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 814 | int64_t* ntp_time_ms) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 815 | assert(shared_->output_mixer() != nullptr); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 816 | |
| 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 Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 820 | shared_->output_mixer()->MixActiveChannels(); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 821 | |
| 822 | // Additional operations on the combined signal |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 823 | shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 824 | |
| 825 | // Retrieve the final output mix (resampled to match the ADM) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 826 | shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels, |
| 827 | &audioFrame_); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 828 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 829 | assert(number_of_frames == audioFrame_.samples_per_channel_); |
| 830 | assert(sample_rate == audioFrame_.sample_rate_hz_); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 831 | |
| 832 | // Deliver audio (PCM) samples to the ADM |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 833 | memcpy(audio_data, audioFrame_.data_, |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 834 | sizeof(int16_t) * number_of_frames * number_of_channels); |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 835 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 836 | *elapsed_time_ms = audioFrame_.elapsed_time_ms_; |
| 837 | *ntp_time_ms = audioFrame_.ntp_time_ms_; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 838 | } |
| 839 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 840 | } // namespace webrtc |