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 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 13 | #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.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" |
kjellander | 3e6db23 | 2015-11-26 04:44:54 -0800 | [diff] [blame] | 15 | #include "webrtc/modules/audio_coding/include/audio_coding_module.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_device/audio_device_impl.h" |
| 17 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame^] | 18 | #include "webrtc/rtc_base/format_macros.h" |
| 19 | #include "webrtc/rtc_base/location.h" |
| 20 | #include "webrtc/rtc_base/logging.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/include/file_wrapper.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 22 | #include "webrtc/voice_engine/channel.h" |
| 23 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 24 | #include "webrtc/voice_engine/output_mixer.h" |
| 25 | #include "webrtc/voice_engine/transmit_mixer.h" |
| 26 | #include "webrtc/voice_engine/utility.h" |
| 27 | #include "webrtc/voice_engine/voice_engine_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 29 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 31 | VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine) { |
| 32 | if (nullptr == voiceEngine) { |
| 33 | return nullptr; |
| 34 | } |
| 35 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 36 | s->AddRef(); |
| 37 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 40 | VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared) |
| 41 | : voiceEngineObserverPtr_(nullptr), |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 42 | shared_(shared) {} |
| 43 | |
| 44 | VoEBaseImpl::~VoEBaseImpl() { |
| 45 | TerminateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 48 | void VoEBaseImpl::OnErrorIsReported(const ErrorCode error) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 49 | rtc::CritScope cs(&callbackCritSect_); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 50 | int errCode = 0; |
| 51 | if (error == AudioDeviceObserver::kRecordingError) { |
| 52 | errCode = VE_RUNTIME_REC_ERROR; |
| 53 | LOG_F(LS_ERROR) << "VE_RUNTIME_REC_ERROR"; |
| 54 | } else if (error == AudioDeviceObserver::kPlayoutError) { |
| 55 | errCode = VE_RUNTIME_PLAY_ERROR; |
| 56 | LOG_F(LS_ERROR) << "VE_RUNTIME_PLAY_ERROR"; |
| 57 | } |
| 58 | if (voiceEngineObserverPtr_) { |
| 59 | // Deliver callback (-1 <=> no channel dependency) |
| 60 | voiceEngineObserverPtr_->CallbackOnError(-1, errCode); |
| 61 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 64 | void VoEBaseImpl::OnWarningIsReported(const WarningCode warning) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 65 | rtc::CritScope cs(&callbackCritSect_); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 66 | int warningCode = 0; |
| 67 | if (warning == AudioDeviceObserver::kRecordingWarning) { |
| 68 | warningCode = VE_RUNTIME_REC_WARNING; |
| 69 | LOG_F(LS_WARNING) << "VE_RUNTIME_REC_WARNING"; |
| 70 | } else if (warning == AudioDeviceObserver::kPlayoutWarning) { |
| 71 | warningCode = VE_RUNTIME_PLAY_WARNING; |
| 72 | LOG_F(LS_WARNING) << "VE_RUNTIME_PLAY_WARNING"; |
| 73 | } |
| 74 | if (voiceEngineObserverPtr_) { |
| 75 | // Deliver callback (-1 <=> no channel dependency) |
| 76 | voiceEngineObserverPtr_->CallbackOnError(-1, warningCode); |
| 77 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | } |
| 79 | |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 80 | int32_t VoEBaseImpl::RecordedDataIsAvailable( |
| 81 | const void* audio_data, |
| 82 | const size_t number_of_frames, |
| 83 | const size_t bytes_per_sample, |
| 84 | const size_t number_of_channels, |
| 85 | const uint32_t sample_rate, |
| 86 | const uint32_t audio_delay_milliseconds, |
| 87 | const int32_t clock_drift, |
| 88 | const uint32_t volume, |
| 89 | const bool key_pressed, |
| 90 | uint32_t& new_mic_volume) { |
| 91 | RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample); |
| 92 | RTC_DCHECK(shared_->transmit_mixer() != nullptr); |
| 93 | RTC_DCHECK(shared_->audio_device() != nullptr); |
| 94 | |
| 95 | uint32_t max_volume = 0; |
| 96 | uint16_t voe_mic_level = 0; |
| 97 | // Check for zero to skip this calculation; the consumer may use this to |
| 98 | // indicate no volume is available. |
| 99 | if (volume != 0) { |
| 100 | // Scale from ADM to VoE level range |
| 101 | if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) { |
| 102 | if (max_volume) { |
| 103 | voe_mic_level = static_cast<uint16_t>( |
| 104 | (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) / |
| 105 | max_volume); |
| 106 | } |
| 107 | } |
| 108 | // We learned that on certain systems (e.g Linux) the voe_mic_level |
| 109 | // can be greater than the maxVolumeLevel therefore |
| 110 | // we are going to cap the voe_mic_level to the maxVolumeLevel |
| 111 | // and change the maxVolume to volume if it turns out that |
| 112 | // the voe_mic_level is indeed greater than the maxVolumeLevel. |
| 113 | if (voe_mic_level > kMaxVolumeLevel) { |
| 114 | voe_mic_level = kMaxVolumeLevel; |
| 115 | max_volume = volume; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Perform channel-independent operations |
| 120 | // (APM, mix with file, record to file, mute, etc.) |
| 121 | shared_->transmit_mixer()->PrepareDemux( |
| 122 | audio_data, number_of_frames, number_of_channels, sample_rate, |
| 123 | static_cast<uint16_t>(audio_delay_milliseconds), clock_drift, |
| 124 | voe_mic_level, key_pressed); |
| 125 | |
| 126 | // Copy the audio frame to each sending channel and perform |
| 127 | // channel-dependent operations (file mixing, mute, etc.), encode and |
| 128 | // packetize+transmit the RTP packet. |
| 129 | shared_->transmit_mixer()->ProcessAndEncodeAudio(); |
| 130 | |
| 131 | // Scale from VoE to ADM level range. |
| 132 | uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel(); |
| 133 | if (new_voe_mic_level != voe_mic_level) { |
| 134 | // Return the new volume if AGC has changed the volume. |
| 135 | return static_cast<int>((new_voe_mic_level * max_volume + |
| 136 | static_cast<int>(kMaxVolumeLevel / 2)) / |
| 137 | kMaxVolumeLevel); |
| 138 | } |
| 139 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 140 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } |
| 142 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 143 | int32_t VoEBaseImpl::NeedMorePlayData(const size_t nSamples, |
| 144 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 145 | const size_t nChannels, |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 146 | const uint32_t samplesPerSec, |
| 147 | void* audioSamples, |
| 148 | size_t& nSamplesOut, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 149 | int64_t* elapsed_time_ms, |
| 150 | int64_t* ntp_time_ms) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 151 | GetPlayoutData(static_cast<int>(samplesPerSec), nChannels, nSamples, true, |
| 152 | audioSamples, elapsed_time_ms, ntp_time_ms); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 153 | nSamplesOut = audioFrame_.samples_per_channel_; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 154 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 157 | void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data, |
| 158 | int bits_per_sample, int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 159 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 160 | size_t number_of_frames) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 161 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel); |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 162 | voe::Channel* channel = ch.channel(); |
| 163 | if (!channel) |
| 164 | return; |
| 165 | if (channel->Sending()) { |
| 166 | // Send the audio to each channel directly without using the APM in the |
| 167 | // transmit mixer. |
| 168 | channel->ProcessAndEncodeAudio(static_cast<const int16_t*>(audio_data), |
| 169 | sample_rate, number_of_frames, |
| 170 | number_of_channels); |
xians@webrtc.org | 07e5196 | 2014-01-29 13:54:02 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 174 | void VoEBaseImpl::PullRenderData(int bits_per_sample, |
| 175 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 176 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 177 | size_t number_of_frames, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 178 | void* audio_data, int64_t* elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 179 | int64_t* ntp_time_ms) { |
Jelena Marusic | 6fc2d2f | 2015-04-13 14:07:04 +0200 | [diff] [blame] | 180 | assert(bits_per_sample == 16); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 181 | assert(number_of_frames == static_cast<size_t>(sample_rate / 100)); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 182 | |
| 183 | GetPlayoutData(sample_rate, number_of_channels, number_of_frames, false, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 184 | audio_data, elapsed_time_ms, ntp_time_ms); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 187 | int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 188 | rtc::CritScope cs(&callbackCritSect_); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 189 | if (voiceEngineObserverPtr_) { |
| 190 | shared_->SetLastError( |
| 191 | VE_INVALID_OPERATION, kTraceError, |
| 192 | "RegisterVoiceEngineObserver() observer already enabled"); |
| 193 | return -1; |
| 194 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 196 | // Register the observer in all active channels |
| 197 | for (voe::ChannelManager::Iterator it(&shared_->channel_manager()); |
| 198 | it.IsValid(); it.Increment()) { |
| 199 | it.GetChannel()->RegisterVoiceEngineObserver(observer); |
| 200 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 201 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 202 | shared_->transmit_mixer()->RegisterVoiceEngineObserver(observer); |
| 203 | voiceEngineObserverPtr_ = &observer; |
| 204 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 207 | int VoEBaseImpl::DeRegisterVoiceEngineObserver() { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 208 | rtc::CritScope cs(&callbackCritSect_); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 209 | if (!voiceEngineObserverPtr_) { |
| 210 | shared_->SetLastError( |
| 211 | VE_INVALID_OPERATION, kTraceError, |
| 212 | "DeRegisterVoiceEngineObserver() observer already disabled"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 214 | } |
| 215 | voiceEngineObserverPtr_ = nullptr; |
| 216 | |
| 217 | // Deregister the observer in all active channels |
| 218 | for (voe::ChannelManager::Iterator it(&shared_->channel_manager()); |
| 219 | it.IsValid(); it.Increment()) { |
| 220 | it.GetChannel()->DeRegisterVoiceEngineObserver(); |
| 221 | } |
| 222 | |
| 223 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 226 | int VoEBaseImpl::Init( |
| 227 | AudioDeviceModule* external_adm, |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 228 | AudioProcessing* external_apm, |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 229 | const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 230 | // TODO(peah): Add a DCHECK for external_apm when downstream dependencies |
| 231 | // have properly been resolved. |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 232 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 233 | WebRtcSpl_Init(); |
| 234 | if (shared_->statistics().Initialized()) { |
| 235 | return 0; |
| 236 | } |
| 237 | if (shared_->process_thread()) { |
| 238 | shared_->process_thread()->Start(); |
| 239 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 241 | // Create an internal ADM if the user has not added an external |
| 242 | // ADM implementation as input to Init(). |
| 243 | if (external_adm == nullptr) { |
Tommi | 931e658 | 2015-05-20 09:44:38 +0200 | [diff] [blame] | 244 | #if !defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE) |
| 245 | return -1; |
| 246 | #else |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 247 | // Create the internal ADM implementation. |
Peter Boström | 4adbbcf | 2016-05-03 15:51:26 -0400 | [diff] [blame] | 248 | shared_->set_audio_device(AudioDeviceModule::Create( |
solenberg | 5b3e49a | 2017-03-15 08:08:07 -0700 | [diff] [blame] | 249 | VoEId(shared_->instance_id(), -1), |
| 250 | AudioDeviceModule::kPlatformDefaultAudio)); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 251 | if (shared_->audio_device() == nullptr) { |
| 252 | shared_->SetLastError(VE_NO_MEMORY, kTraceCritical, |
| 253 | "Init() failed to create the ADM"); |
| 254 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | } |
Tommi | 931e658 | 2015-05-20 09:44:38 +0200 | [diff] [blame] | 256 | #endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 257 | } else { |
| 258 | // Use the already existing external ADM implementation. |
| 259 | shared_->set_audio_device(external_adm); |
| 260 | LOG_F(LS_INFO) |
| 261 | << "An external ADM implementation will be used in VoiceEngine"; |
| 262 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 264 | // Register the ADM to the process thread, which will drive the error |
| 265 | // callback mechanism |
| 266 | if (shared_->process_thread()) { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 267 | shared_->process_thread()->RegisterModule(shared_->audio_device(), |
| 268 | RTC_FROM_HERE); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 269 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 271 | bool available = false; |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 272 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 273 | // -------------------- |
| 274 | // Reinitialize the ADM |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 275 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 276 | // Register the AudioObserver implementation |
| 277 | if (shared_->audio_device()->RegisterEventObserver(this) != 0) { |
| 278 | shared_->SetLastError( |
| 279 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 280 | "Init() failed to register event observer for the ADM"); |
| 281 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 283 | // Register the AudioTransport implementation |
| 284 | if (shared_->audio_device()->RegisterAudioCallback(this) != 0) { |
| 285 | shared_->SetLastError( |
| 286 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 287 | "Init() failed to register audio callback for the ADM"); |
| 288 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 290 | // ADM initialization |
| 291 | if (shared_->audio_device()->Init() != 0) { |
| 292 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 293 | "Init() failed to initialize the ADM"); |
| 294 | return -1; |
| 295 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 296 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 297 | // Initialize the default speaker |
| 298 | if (shared_->audio_device()->SetPlayoutDevice( |
| 299 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) { |
| 300 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo, |
| 301 | "Init() failed to set the default output device"); |
| 302 | } |
| 303 | if (shared_->audio_device()->InitSpeaker() != 0) { |
| 304 | shared_->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
| 305 | "Init() failed to initialize the speaker"); |
| 306 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 308 | // Initialize the default microphone |
| 309 | if (shared_->audio_device()->SetRecordingDevice( |
| 310 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) { |
| 311 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo, |
| 312 | "Init() failed to set the default input device"); |
| 313 | } |
| 314 | if (shared_->audio_device()->InitMicrophone() != 0) { |
| 315 | shared_->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
| 316 | "Init() failed to initialize the microphone"); |
| 317 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 318 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 319 | // Set number of channels |
| 320 | if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) { |
| 321 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 322 | "Init() failed to query stereo playout mode"); |
| 323 | } |
| 324 | if (shared_->audio_device()->SetStereoPlayout(available) != 0) { |
| 325 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 326 | "Init() failed to set mono/stereo playout mode"); |
| 327 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 329 | // TODO(andrew): These functions don't tell us whether stereo recording |
| 330 | // is truly available. We simply set the AudioProcessing input to stereo |
| 331 | // here, because we have to wait until receiving the first frame to |
| 332 | // determine the actual number of channels anyway. |
| 333 | // |
| 334 | // These functions may be changed; tracked here: |
| 335 | // http://code.google.com/p/webrtc/issues/detail?id=204 |
| 336 | shared_->audio_device()->StereoRecordingIsAvailable(&available); |
| 337 | if (shared_->audio_device()->SetStereoRecording(available) != 0) { |
| 338 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 339 | "Init() failed to set mono/stereo recording mode"); |
| 340 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 342 | // TODO(peah): Remove this when upstream dependencies have properly been |
| 343 | // resolved. |
| 344 | AudioProcessing* apm = nullptr; |
| 345 | if (!external_apm) { |
| 346 | audio_processing_ = AudioProcessing::Create(); |
| 347 | if (!audio_processing_) { |
| 348 | // This can only happen if there are problems allocating the dynamic |
| 349 | // memory in the Create() call. |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 350 | LOG(LS_ERROR) << "Failed to create AudioProcessing."; |
| 351 | shared_->SetLastError(VE_NO_MEMORY); |
| 352 | return -1; |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 353 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 354 | apm = audio_processing_.get(); |
| 355 | } else { |
| 356 | apm = external_apm; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 357 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 358 | |
| 359 | shared_->set_audio_processing(apm); |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 360 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 361 | // Set the error state for any failures in this block. |
| 362 | shared_->SetLastError(VE_APM_ERROR); |
| 363 | // Configure AudioProcessing components. |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 364 | // TODO(peah): Move this initialization to webrtcvoiceengine.cc. |
| 365 | if (apm->high_pass_filter()->Enable(true) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 366 | LOG_F(LS_ERROR) << "Failed to enable high pass filter."; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 367 | return -1; |
| 368 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 369 | if (apm->echo_cancellation()->enable_drift_compensation(false) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 370 | LOG_F(LS_ERROR) << "Failed to disable drift compensation."; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 371 | return -1; |
| 372 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 373 | if (apm->noise_suppression()->set_level(kDefaultNsMode) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 374 | LOG_F(LS_ERROR) << "Failed to set noise suppression level: " |
| 375 | << kDefaultNsMode; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 376 | return -1; |
| 377 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 378 | GainControl* agc = apm->gain_control(); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 379 | if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 380 | LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: " |
| 381 | << kMinVolumeLevel << " and maximum: " << kMaxVolumeLevel; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 382 | return -1; |
| 383 | } |
| 384 | if (agc->set_mode(kDefaultAgcMode) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 385 | LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 386 | return -1; |
| 387 | } |
| 388 | if (agc->Enable(kDefaultAgcState) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 389 | LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 390 | return -1; |
| 391 | } |
| 392 | shared_->SetLastError(0); // Clear error state. |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 393 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 394 | #ifdef WEBRTC_VOICE_ENGINE_AGC |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 395 | bool agc_enabled = |
| 396 | agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled(); |
| 397 | if (shared_->audio_device()->SetAGC(agc_enabled) != 0) { |
Jelena Marusic | f353dd5 | 2015-05-06 15:04:22 +0200 | [diff] [blame] | 398 | LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 399 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR); |
| 400 | // TODO(ajm): No error return here due to |
| 401 | // https://code.google.com/p/webrtc/issues/detail?id=1464 |
| 402 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 403 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 404 | |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 405 | if (decoder_factory) |
| 406 | decoder_factory_ = decoder_factory; |
| 407 | else |
| 408 | decoder_factory_ = CreateBuiltinAudioDecoderFactory(); |
| 409 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 410 | return shared_->statistics().SetInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 413 | int VoEBaseImpl::Terminate() { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 414 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 415 | return TerminateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 416 | } |
| 417 | |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 418 | int VoEBaseImpl::CreateChannel() { |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 419 | return CreateChannel(ChannelConfig()); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 420 | } |
| 421 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 422 | int VoEBaseImpl::CreateChannel(const ChannelConfig& config) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 423 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 424 | if (!shared_->statistics().Initialized()) { |
| 425 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 426 | return -1; |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 427 | } |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 428 | |
| 429 | ChannelConfig config_copy(config); |
| 430 | config_copy.acm_config.decoder_factory = decoder_factory_; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 431 | voe::ChannelOwner channel_owner = |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 432 | shared_->channel_manager().CreateChannel(config_copy); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 433 | return InitializeChannel(&channel_owner); |
| 434 | } |
| 435 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 436 | int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) { |
| 437 | if (channel_owner->channel()->SetEngineInformation( |
| 438 | shared_->statistics(), *shared_->output_mixer(), |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 439 | *shared_->process_thread(), *shared_->audio_device(), |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 440 | voiceEngineObserverPtr_, &callbackCritSect_, |
| 441 | shared_->encoder_queue()) != 0) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 442 | shared_->SetLastError( |
| 443 | VE_CHANNEL_NOT_CREATED, kTraceError, |
| 444 | "CreateChannel() failed to associate engine and channel." |
| 445 | " Destroying channel."); |
| 446 | shared_->channel_manager().DestroyChannel( |
| 447 | channel_owner->channel()->ChannelId()); |
| 448 | return -1; |
| 449 | } else if (channel_owner->channel()->Init() != 0) { |
| 450 | shared_->SetLastError( |
| 451 | VE_CHANNEL_NOT_CREATED, kTraceError, |
| 452 | "CreateChannel() failed to initialize channel. Destroying" |
| 453 | " channel."); |
| 454 | shared_->channel_manager().DestroyChannel( |
| 455 | channel_owner->channel()->ChannelId()); |
| 456 | return -1; |
| 457 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 458 | return channel_owner->channel()->ChannelId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 461 | int VoEBaseImpl::DeleteChannel(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 462 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 463 | if (!shared_->statistics().Initialized()) { |
| 464 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | { |
| 469 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 470 | voe::Channel* channelPtr = ch.channel(); |
| 471 | if (channelPtr == nullptr) { |
| 472 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 473 | "DeleteChannel() failed to locate channel"); |
| 474 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 475 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 476 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 477 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 478 | shared_->channel_manager().DestroyChannel(channel); |
| 479 | if (StopSend() != 0) { |
| 480 | return -1; |
| 481 | } |
| 482 | if (StopPlayout() != 0) { |
| 483 | return -1; |
| 484 | } |
| 485 | return 0; |
| 486 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 487 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 488 | int VoEBaseImpl::StartReceive(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 489 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 490 | if (!shared_->statistics().Initialized()) { |
| 491 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 492 | return -1; |
| 493 | } |
| 494 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 495 | voe::Channel* channelPtr = ch.channel(); |
| 496 | if (channelPtr == nullptr) { |
| 497 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 498 | "StartReceive() failed to locate channel"); |
| 499 | return -1; |
| 500 | } |
solenberg | e566ac7 | 2016-10-31 12:52:33 -0700 | [diff] [blame] | 501 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 502 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 503 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 504 | int VoEBaseImpl::StartPlayout(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 505 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 506 | if (!shared_->statistics().Initialized()) { |
| 507 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 508 | return -1; |
| 509 | } |
| 510 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 511 | voe::Channel* channelPtr = ch.channel(); |
| 512 | if (channelPtr == nullptr) { |
| 513 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 514 | "StartPlayout() failed to locate channel"); |
| 515 | return -1; |
| 516 | } |
| 517 | if (channelPtr->Playing()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 518 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 519 | } |
| 520 | if (StartPlayout() != 0) { |
| 521 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 522 | "StartPlayout() failed to start playout"); |
| 523 | return -1; |
| 524 | } |
| 525 | return channelPtr->StartPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 528 | int VoEBaseImpl::StopPlayout(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 529 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 530 | if (!shared_->statistics().Initialized()) { |
| 531 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 532 | return -1; |
| 533 | } |
| 534 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 535 | voe::Channel* channelPtr = ch.channel(); |
| 536 | if (channelPtr == nullptr) { |
| 537 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 538 | "StopPlayout() failed to locate channel"); |
| 539 | return -1; |
| 540 | } |
| 541 | if (channelPtr->StopPlayout() != 0) { |
| 542 | LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel " |
| 543 | << channel; |
| 544 | } |
| 545 | return StopPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 548 | int VoEBaseImpl::StartSend(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 549 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 550 | if (!shared_->statistics().Initialized()) { |
| 551 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 552 | return -1; |
| 553 | } |
| 554 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 555 | voe::Channel* channelPtr = ch.channel(); |
| 556 | if (channelPtr == nullptr) { |
| 557 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 558 | "StartSend() failed to locate channel"); |
| 559 | return -1; |
| 560 | } |
| 561 | if (channelPtr->Sending()) { |
| 562 | return 0; |
| 563 | } |
| 564 | if (StartSend() != 0) { |
| 565 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 566 | "StartSend() failed to start recording"); |
| 567 | return -1; |
| 568 | } |
| 569 | return channelPtr->StartSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 572 | int VoEBaseImpl::StopSend(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 573 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 574 | if (!shared_->statistics().Initialized()) { |
| 575 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 576 | return -1; |
| 577 | } |
| 578 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 579 | voe::Channel* channelPtr = ch.channel(); |
| 580 | if (channelPtr == nullptr) { |
| 581 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 582 | "StopSend() failed to locate channel"); |
| 583 | return -1; |
| 584 | } |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 585 | channelPtr->StopSend(); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 586 | return StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 589 | int VoEBaseImpl::GetVersion(char version[1024]) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 590 | if (version == nullptr) { |
| 591 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 592 | return -1; |
| 593 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 594 | |
solenberg | 2515af2 | 2015-12-02 06:19:36 -0800 | [diff] [blame] | 595 | std::string versionString = VoiceEngine::GetVersionString(); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 596 | RTC_DCHECK_GT(1024, versionString.size() + 1); |
solenberg | 2515af2 | 2015-12-02 06:19:36 -0800 | [diff] [blame] | 597 | char* end = std::copy(versionString.cbegin(), versionString.cend(), version); |
| 598 | end[0] = '\n'; |
| 599 | end[1] = '\0'; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 600 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 603 | int VoEBaseImpl::LastError() { return (shared_->statistics().LastError()); } |
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 | int32_t VoEBaseImpl::StartPlayout() { |
solenberg | e313e02 | 2015-09-08 02:16:04 -0700 | [diff] [blame] | 606 | if (!shared_->audio_device()->Playing()) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 607 | if (shared_->audio_device()->InitPlayout() != 0) { |
| 608 | LOG_F(LS_ERROR) << "Failed to initialize playout"; |
| 609 | return -1; |
| 610 | } |
| 611 | if (shared_->audio_device()->StartPlayout() != 0) { |
| 612 | LOG_F(LS_ERROR) << "Failed to start playout"; |
| 613 | return -1; |
| 614 | } |
| 615 | } |
| 616 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 617 | } |
| 618 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 619 | int32_t VoEBaseImpl::StopPlayout() { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 620 | // Stop audio-device playing if no channel is playing out |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 621 | if (shared_->NumOfPlayingChannels() == 0) { |
| 622 | if (shared_->audio_device()->StopPlayout() != 0) { |
| 623 | shared_->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError, |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 624 | "StopPlayout() failed to stop playout"); |
| 625 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 626 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 627 | } |
| 628 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 631 | int32_t VoEBaseImpl::StartSend() { |
solenberg | d53a3f9 | 2016-04-14 13:56:37 -0700 | [diff] [blame] | 632 | if (!shared_->audio_device()->RecordingIsInitialized() && |
| 633 | !shared_->audio_device()->Recording()) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 634 | if (shared_->audio_device()->InitRecording() != 0) { |
| 635 | LOG_F(LS_ERROR) << "Failed to initialize recording"; |
| 636 | return -1; |
| 637 | } |
solenberg | d53a3f9 | 2016-04-14 13:56:37 -0700 | [diff] [blame] | 638 | } |
| 639 | if (!shared_->audio_device()->Recording()) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 640 | if (shared_->audio_device()->StartRecording() != 0) { |
| 641 | LOG_F(LS_ERROR) << "Failed to start recording"; |
| 642 | return -1; |
| 643 | } |
| 644 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 645 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 648 | int32_t VoEBaseImpl::StopSend() { |
| 649 | if (shared_->NumOfSendingChannels() == 0 && |
| 650 | !shared_->transmit_mixer()->IsRecordingMic()) { |
| 651 | // Stop audio-device recording if no channel is recording |
| 652 | if (shared_->audio_device()->StopRecording() != 0) { |
| 653 | shared_->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError, |
| 654 | "StopSend() failed to stop recording"); |
| 655 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 656 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 657 | shared_->transmit_mixer()->StopSend(); |
| 658 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 659 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 660 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 663 | int32_t VoEBaseImpl::TerminateInternal() { |
| 664 | // Delete any remaining channel objects |
| 665 | shared_->channel_manager().DestroyAllChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 666 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 667 | if (shared_->process_thread()) { |
| 668 | if (shared_->audio_device()) { |
| 669 | shared_->process_thread()->DeRegisterModule(shared_->audio_device()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 670 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 671 | shared_->process_thread()->Stop(); |
| 672 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 673 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 674 | if (shared_->audio_device()) { |
| 675 | if (shared_->audio_device()->StopPlayout() != 0) { |
| 676 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 677 | "TerminateInternal() failed to stop playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 678 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 679 | if (shared_->audio_device()->StopRecording() != 0) { |
| 680 | shared_->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 681 | "TerminateInternal() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 682 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 683 | if (shared_->audio_device()->RegisterEventObserver(nullptr) != 0) { |
| 684 | shared_->SetLastError( |
| 685 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 686 | "TerminateInternal() failed to de-register event observer " |
| 687 | "for the ADM"); |
| 688 | } |
| 689 | if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) { |
| 690 | shared_->SetLastError( |
| 691 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 692 | "TerminateInternal() failed to de-register audio callback " |
| 693 | "for the ADM"); |
| 694 | } |
| 695 | if (shared_->audio_device()->Terminate() != 0) { |
| 696 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 697 | "TerminateInternal() failed to terminate the ADM"); |
| 698 | } |
| 699 | shared_->set_audio_device(nullptr); |
| 700 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 702 | shared_->set_audio_processing(nullptr); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 703 | |
| 704 | return shared_->statistics().SetUnInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 707 | void VoEBaseImpl::GetPlayoutData(int sample_rate, size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 708 | size_t number_of_frames, bool feed_data_to_apm, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 709 | void* audio_data, int64_t* elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 710 | int64_t* ntp_time_ms) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 711 | assert(shared_->output_mixer() != nullptr); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 712 | |
| 713 | // TODO(andrew): if the device is running in mono, we should tell the mixer |
| 714 | // here so that it will only request mono from AudioCodingModule. |
| 715 | // Perform mixing of all active participants (channel-based mixing) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 716 | shared_->output_mixer()->MixActiveChannels(); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 717 | |
| 718 | // Additional operations on the combined signal |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 719 | shared_->output_mixer()->DoOperationsOnCombinedSignal(feed_data_to_apm); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 720 | |
| 721 | // Retrieve the final output mix (resampled to match the ADM) |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 722 | shared_->output_mixer()->GetMixedAudio(sample_rate, number_of_channels, |
| 723 | &audioFrame_); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 724 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 725 | assert(number_of_frames == audioFrame_.samples_per_channel_); |
| 726 | assert(sample_rate == audioFrame_.sample_rate_hz_); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 727 | |
| 728 | // Deliver audio (PCM) samples to the ADM |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 729 | memcpy(audio_data, audioFrame_.data(), |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 730 | sizeof(int16_t) * number_of_frames * number_of_channels); |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 731 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 732 | *elapsed_time_ms = audioFrame_.elapsed_time_ms_; |
| 733 | *ntp_time_ms = audioFrame_.ntp_time_ms_; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 736 | int VoEBaseImpl::AssociateSendChannel(int channel, |
| 737 | int accociate_send_channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 738 | rtc::CritScope cs(shared_->crit_sec()); |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 739 | |
| 740 | if (!shared_->statistics().Initialized()) { |
| 741 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 746 | voe::Channel* channel_ptr = ch.channel(); |
| 747 | if (channel_ptr == NULL) { |
| 748 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 749 | "AssociateSendChannel() failed to locate channel"); |
| 750 | return -1; |
| 751 | } |
| 752 | |
| 753 | ch = shared_->channel_manager().GetChannel(accociate_send_channel); |
| 754 | voe::Channel* accociate_send_channel_ptr = ch.channel(); |
| 755 | if (accociate_send_channel_ptr == NULL) { |
| 756 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 757 | "AssociateSendChannel() failed to locate accociate_send_channel"); |
| 758 | return -1; |
| 759 | } |
| 760 | |
| 761 | channel_ptr->set_associate_send_channel(ch); |
| 762 | return 0; |
| 763 | } |
| 764 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 765 | } // namespace webrtc |