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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "voice_engine/voe_base_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 14 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 15 | #include "modules/audio_device/audio_device_impl.h" |
| 16 | #include "modules/audio_processing/include/audio_processing.h" |
| 17 | #include "rtc_base/format_macros.h" |
| 18 | #include "rtc_base/location.h" |
| 19 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "voice_engine/channel.h" |
| 21 | #include "voice_engine/include/voe_errors.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "voice_engine/transmit_mixer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "voice_engine/voice_engine_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 25 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 27 | VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine) { |
| 28 | if (nullptr == voiceEngine) { |
| 29 | return nullptr; |
| 30 | } |
| 31 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 32 | s->AddRef(); |
| 33 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 36 | VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared) |
solenberg | fc3a2e3 | 2017-09-26 09:35:01 -0700 | [diff] [blame] | 37 | : shared_(shared) {} |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 38 | |
| 39 | VoEBaseImpl::~VoEBaseImpl() { |
| 40 | TerminateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 43 | int32_t VoEBaseImpl::RecordedDataIsAvailable( |
| 44 | const void* audio_data, |
| 45 | const size_t number_of_frames, |
| 46 | const size_t bytes_per_sample, |
| 47 | const size_t number_of_channels, |
| 48 | const uint32_t sample_rate, |
| 49 | const uint32_t audio_delay_milliseconds, |
| 50 | const int32_t clock_drift, |
| 51 | const uint32_t volume, |
| 52 | const bool key_pressed, |
| 53 | uint32_t& new_mic_volume) { |
| 54 | RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample); |
| 55 | RTC_DCHECK(shared_->transmit_mixer() != nullptr); |
| 56 | RTC_DCHECK(shared_->audio_device() != nullptr); |
| 57 | |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 58 | constexpr uint32_t kMaxVolumeLevel = 255; |
| 59 | |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 60 | uint32_t max_volume = 0; |
| 61 | uint16_t voe_mic_level = 0; |
| 62 | // Check for zero to skip this calculation; the consumer may use this to |
| 63 | // indicate no volume is available. |
| 64 | if (volume != 0) { |
| 65 | // Scale from ADM to VoE level range |
| 66 | if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) { |
| 67 | if (max_volume) { |
| 68 | voe_mic_level = static_cast<uint16_t>( |
| 69 | (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) / |
| 70 | max_volume); |
| 71 | } |
| 72 | } |
| 73 | // We learned that on certain systems (e.g Linux) the voe_mic_level |
| 74 | // can be greater than the maxVolumeLevel therefore |
| 75 | // we are going to cap the voe_mic_level to the maxVolumeLevel |
| 76 | // and change the maxVolume to volume if it turns out that |
| 77 | // the voe_mic_level is indeed greater than the maxVolumeLevel. |
| 78 | if (voe_mic_level > kMaxVolumeLevel) { |
| 79 | voe_mic_level = kMaxVolumeLevel; |
| 80 | max_volume = volume; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Perform channel-independent operations |
| 85 | // (APM, mix with file, record to file, mute, etc.) |
| 86 | shared_->transmit_mixer()->PrepareDemux( |
| 87 | audio_data, number_of_frames, number_of_channels, sample_rate, |
| 88 | static_cast<uint16_t>(audio_delay_milliseconds), clock_drift, |
| 89 | voe_mic_level, key_pressed); |
| 90 | |
| 91 | // Copy the audio frame to each sending channel and perform |
| 92 | // channel-dependent operations (file mixing, mute, etc.), encode and |
| 93 | // packetize+transmit the RTP packet. |
| 94 | shared_->transmit_mixer()->ProcessAndEncodeAudio(); |
| 95 | |
| 96 | // Scale from VoE to ADM level range. |
| 97 | uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel(); |
| 98 | if (new_voe_mic_level != voe_mic_level) { |
| 99 | // Return the new volume if AGC has changed the volume. |
| 100 | return static_cast<int>((new_voe_mic_level * max_volume + |
| 101 | static_cast<int>(kMaxVolumeLevel / 2)) / |
| 102 | kMaxVolumeLevel); |
| 103 | } |
| 104 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 105 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 108 | int32_t VoEBaseImpl::NeedMorePlayData(const size_t nSamples, |
| 109 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 110 | const size_t nChannels, |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 111 | const uint32_t samplesPerSec, |
| 112 | void* audioSamples, |
| 113 | size_t& nSamplesOut, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 114 | int64_t* elapsed_time_ms, |
| 115 | int64_t* ntp_time_ms) { |
solenberg | 2397b9a | 2017-09-22 06:48:10 -0700 | [diff] [blame] | 116 | RTC_NOTREACHED(); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 117 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 120 | void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data, |
| 121 | int bits_per_sample, int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 122 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 123 | size_t number_of_frames) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 124 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel); |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 125 | voe::Channel* channel = ch.channel(); |
| 126 | if (!channel) |
| 127 | return; |
| 128 | if (channel->Sending()) { |
| 129 | // Send the audio to each channel directly without using the APM in the |
| 130 | // transmit mixer. |
| 131 | channel->ProcessAndEncodeAudio(static_cast<const int16_t*>(audio_data), |
| 132 | sample_rate, number_of_frames, |
| 133 | number_of_channels); |
xians@webrtc.org | 07e5196 | 2014-01-29 13:54:02 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 137 | void VoEBaseImpl::PullRenderData(int bits_per_sample, |
| 138 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 139 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 140 | size_t number_of_frames, |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 141 | void* audio_data, int64_t* elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 142 | int64_t* ntp_time_ms) { |
solenberg | 2397b9a | 2017-09-22 06:48:10 -0700 | [diff] [blame] | 143 | RTC_NOTREACHED(); |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 144 | } |
| 145 | |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 146 | int VoEBaseImpl::Init( |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 147 | AudioDeviceModule* audio_device, |
peah | e67bedb | 2017-07-07 04:25:11 -0700 | [diff] [blame] | 148 | AudioProcessing* audio_processing, |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 149 | const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) { |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 150 | RTC_DCHECK(audio_device); |
peah | e67bedb | 2017-07-07 04:25:11 -0700 | [diff] [blame] | 151 | RTC_DCHECK(audio_processing); |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 152 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 153 | if (shared_->process_thread()) { |
| 154 | shared_->process_thread()->Start(); |
| 155 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 157 | shared_->set_audio_device(audio_device); |
peah | e67bedb | 2017-07-07 04:25:11 -0700 | [diff] [blame] | 158 | shared_->set_audio_processing(audio_processing); |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 159 | |
Karl Wiberg | f3850f6 | 2017-11-02 13:04:41 +0100 | [diff] [blame] | 160 | RTC_DCHECK(decoder_factory); |
| 161 | decoder_factory_ = decoder_factory; |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 162 | |
solenberg | 1c239d4 | 2017-09-29 06:00:28 -0700 | [diff] [blame] | 163 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 166 | void VoEBaseImpl::Terminate() { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 167 | rtc::CritScope cs(shared_->crit_sec()); |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 168 | TerminateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 171 | int VoEBaseImpl::CreateChannel() { |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 172 | return CreateChannel(ChannelConfig()); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 173 | } |
| 174 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 175 | int VoEBaseImpl::CreateChannel(const ChannelConfig& config) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 176 | rtc::CritScope cs(shared_->crit_sec()); |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 177 | ChannelConfig config_copy(config); |
| 178 | config_copy.acm_config.decoder_factory = decoder_factory_; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 179 | voe::ChannelOwner channel_owner = |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 180 | shared_->channel_manager().CreateChannel(config_copy); |
turaj@webrtc.org | 03f3370 | 2013-11-13 00:02:48 +0000 | [diff] [blame] | 181 | return InitializeChannel(&channel_owner); |
| 182 | } |
| 183 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 184 | int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) { |
| 185 | if (channel_owner->channel()->SetEngineInformation( |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 186 | *shared_->process_thread(), *shared_->audio_device(), |
solenberg | 1c239d4 | 2017-09-29 06:00:28 -0700 | [diff] [blame] | 187 | shared_->encoder_queue()) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 188 | RTC_LOG(LS_ERROR) |
| 189 | << "CreateChannel() failed to associate engine and channel." |
| 190 | " Destroying channel."; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 191 | shared_->channel_manager().DestroyChannel( |
| 192 | channel_owner->channel()->ChannelId()); |
| 193 | return -1; |
| 194 | } else if (channel_owner->channel()->Init() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 195 | RTC_LOG(LS_ERROR) |
| 196 | << "CreateChannel() failed to initialize channel. Destroying" |
| 197 | " channel."; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 198 | shared_->channel_manager().DestroyChannel( |
| 199 | channel_owner->channel()->ChannelId()); |
| 200 | return -1; |
| 201 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 202 | return channel_owner->channel()->ChannelId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 205 | int VoEBaseImpl::DeleteChannel(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 206 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 207 | { |
| 208 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 209 | voe::Channel* channelPtr = ch.channel(); |
| 210 | if (channelPtr == nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 211 | RTC_LOG(LS_ERROR) << "DeleteChannel() failed to locate channel"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 212 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 214 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 216 | shared_->channel_manager().DestroyChannel(channel); |
| 217 | if (StopSend() != 0) { |
| 218 | return -1; |
| 219 | } |
| 220 | if (StopPlayout() != 0) { |
| 221 | return -1; |
| 222 | } |
| 223 | return 0; |
| 224 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 226 | int VoEBaseImpl::StartPlayout(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 227 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 228 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 229 | voe::Channel* channelPtr = ch.channel(); |
| 230 | if (channelPtr == nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 231 | RTC_LOG(LS_ERROR) << "StartPlayout() failed to locate channel"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | if (channelPtr->Playing()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | return 0; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 236 | } |
| 237 | if (StartPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 238 | RTC_LOG(LS_ERROR) << "StartPlayout() failed to start playout"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 239 | return -1; |
| 240 | } |
| 241 | return channelPtr->StartPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 244 | int VoEBaseImpl::StopPlayout(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 245 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 246 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 247 | voe::Channel* channelPtr = ch.channel(); |
| 248 | if (channelPtr == nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 249 | RTC_LOG(LS_ERROR) << "StopPlayout() failed to locate channel"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | if (channelPtr->StopPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 253 | RTC_LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel " |
| 254 | << channel; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 255 | } |
| 256 | return StopPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 259 | int VoEBaseImpl::StartSend(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 260 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 261 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 262 | voe::Channel* channelPtr = ch.channel(); |
| 263 | if (channelPtr == nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 264 | RTC_LOG(LS_ERROR) << "StartSend() failed to locate channel"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | if (channelPtr->Sending()) { |
| 268 | return 0; |
| 269 | } |
| 270 | if (StartSend() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 271 | RTC_LOG(LS_ERROR) << "StartSend() failed to start recording"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | return channelPtr->StartSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 277 | int VoEBaseImpl::StopSend(int channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 278 | rtc::CritScope cs(shared_->crit_sec()); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 279 | voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel); |
| 280 | voe::Channel* channelPtr = ch.channel(); |
| 281 | if (channelPtr == nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 282 | RTC_LOG(LS_ERROR) << "StopSend() failed to locate channel"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 283 | return -1; |
| 284 | } |
henrika | ec6fbd2 | 2017-03-31 05:43:36 -0700 | [diff] [blame] | 285 | channelPtr->StopSend(); |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 286 | return StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 289 | int32_t VoEBaseImpl::StartPlayout() { |
solenberg | e313e02 | 2015-09-08 02:16:04 -0700 | [diff] [blame] | 290 | if (!shared_->audio_device()->Playing()) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 291 | if (shared_->audio_device()->InitPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 292 | RTC_LOG_F(LS_ERROR) << "Failed to initialize playout"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 293 | return -1; |
| 294 | } |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 295 | if (playout_enabled_ && shared_->audio_device()->StartPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 296 | RTC_LOG_F(LS_ERROR) << "Failed to start playout"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 297 | return -1; |
| 298 | } |
| 299 | } |
| 300 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | } |
| 302 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 303 | int32_t VoEBaseImpl::StopPlayout() { |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 304 | if (!playout_enabled_) { |
| 305 | return 0; |
| 306 | } |
| 307 | // Stop audio-device playing if no channel is playing out. |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 308 | if (shared_->NumOfPlayingChannels() == 0) { |
| 309 | if (shared_->audio_device()->StopPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 310 | RTC_LOG(LS_ERROR) << "StopPlayout() failed to stop playout"; |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 311 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 312 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 313 | } |
| 314 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 317 | int32_t VoEBaseImpl::StartSend() { |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 318 | if (!shared_->audio_device()->Recording()) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 319 | if (shared_->audio_device()->InitRecording() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 320 | RTC_LOG_F(LS_ERROR) << "Failed to initialize recording"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 321 | return -1; |
| 322 | } |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 323 | if (recording_enabled_ && shared_->audio_device()->StartRecording() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 324 | RTC_LOG_F(LS_ERROR) << "Failed to start recording"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 328 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 331 | int32_t VoEBaseImpl::StopSend() { |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 332 | if (!recording_enabled_) { |
| 333 | return 0; |
| 334 | } |
| 335 | // Stop audio-device recording if no channel is recording. |
solenberg | b63310a | 2017-09-18 03:04:12 -0700 | [diff] [blame] | 336 | if (shared_->NumOfSendingChannels() == 0) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 337 | if (shared_->audio_device()->StopRecording() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 338 | RTC_LOG(LS_ERROR) << "StopSend() failed to stop recording"; |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 339 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | } |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 341 | shared_->transmit_mixer()->StopSend(); |
| 342 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 343 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 344 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 345 | } |
| 346 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 347 | int32_t VoEBaseImpl::SetPlayout(bool enabled) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 348 | RTC_LOG(INFO) << "SetPlayout(" << enabled << ")"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 349 | if (playout_enabled_ == enabled) { |
| 350 | return 0; |
| 351 | } |
| 352 | playout_enabled_ = enabled; |
| 353 | if (shared_->NumOfPlayingChannels() == 0) { |
| 354 | // If there are no channels attempting to play out yet, there's nothing to |
| 355 | // be done; we should be in a "not playing out" state either way. |
| 356 | return 0; |
| 357 | } |
| 358 | int32_t ret; |
| 359 | if (enabled) { |
| 360 | ret = shared_->audio_device()->StartPlayout(); |
| 361 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 362 | RTC_LOG(LS_ERROR) << "SetPlayout(true) failed to start playout"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 363 | } |
| 364 | } else { |
| 365 | ret = shared_->audio_device()->StopPlayout(); |
| 366 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 367 | RTC_LOG(LS_ERROR) << "SetPlayout(false) failed to stop playout"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | int32_t VoEBaseImpl::SetRecording(bool enabled) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 374 | RTC_LOG(INFO) << "SetRecording(" << enabled << ")"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 375 | if (recording_enabled_ == enabled) { |
| 376 | return 0; |
| 377 | } |
| 378 | recording_enabled_ = enabled; |
| 379 | if (shared_->NumOfSendingChannels() == 0) { |
| 380 | // If there are no channels attempting to record out yet, there's nothing to |
| 381 | // be done; we should be in a "not recording" state either way. |
| 382 | return 0; |
| 383 | } |
| 384 | int32_t ret; |
| 385 | if (enabled) { |
| 386 | ret = shared_->audio_device()->StartRecording(); |
| 387 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 388 | RTC_LOG(LS_ERROR) << "SetRecording(true) failed to start recording"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 389 | } |
| 390 | } else { |
| 391 | ret = shared_->audio_device()->StopRecording(); |
| 392 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 393 | RTC_LOG(LS_ERROR) << "SetRecording(false) failed to stop recording"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | return ret; |
| 397 | } |
| 398 | |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 399 | void VoEBaseImpl::TerminateInternal() { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 400 | // Delete any remaining channel objects |
| 401 | shared_->channel_manager().DestroyAllChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 403 | if (shared_->process_thread()) { |
Jelena Marusic | 2dd6a27 | 2015-04-14 09:47:00 +0200 | [diff] [blame] | 404 | shared_->process_thread()->Stop(); |
| 405 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 406 | |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 407 | shared_->set_audio_device(nullptr); |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 408 | shared_->set_audio_processing(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 409 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 410 | } // namespace webrtc |