blob: 695b06dd6c6b3f46cf6a6d4606617cf1a0aa7b17 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org79af7342012-01-31 12:22:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "voice_engine/voe_base_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#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 Bonadei92ea95e2017-09-15 06:47:31 +020020#include "voice_engine/channel.h"
21#include "voice_engine/include/voe_errors.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "voice_engine/transmit_mixer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
Jelena Marusic2dd6a272015-04-14 09:47:00 +020025namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000026
Jelena Marusic2dd6a272015-04-14 09:47:00 +020027VoEBase* 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.com470e71d2011-07-07 08:21:25 +000034}
35
Jelena Marusic2dd6a272015-04-14 09:47:00 +020036VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared)
solenbergfc3a2e32017-09-26 09:35:01 -070037 : shared_(shared) {}
Jelena Marusic2dd6a272015-04-14 09:47:00 +020038
39VoEBaseImpl::~VoEBaseImpl() {
40 TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
henrikaec6fbd22017-03-31 05:43:36 -070043int32_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
58 uint32_t max_volume = 0;
59 uint16_t voe_mic_level = 0;
60 // Check for zero to skip this calculation; the consumer may use this to
61 // indicate no volume is available.
62 if (volume != 0) {
63 // Scale from ADM to VoE level range
64 if (shared_->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) {
65 if (max_volume) {
66 voe_mic_level = static_cast<uint16_t>(
67 (volume * kMaxVolumeLevel + static_cast<int>(max_volume / 2)) /
68 max_volume);
69 }
70 }
71 // We learned that on certain systems (e.g Linux) the voe_mic_level
72 // can be greater than the maxVolumeLevel therefore
73 // we are going to cap the voe_mic_level to the maxVolumeLevel
74 // and change the maxVolume to volume if it turns out that
75 // the voe_mic_level is indeed greater than the maxVolumeLevel.
76 if (voe_mic_level > kMaxVolumeLevel) {
77 voe_mic_level = kMaxVolumeLevel;
78 max_volume = volume;
79 }
80 }
81
82 // Perform channel-independent operations
83 // (APM, mix with file, record to file, mute, etc.)
84 shared_->transmit_mixer()->PrepareDemux(
85 audio_data, number_of_frames, number_of_channels, sample_rate,
86 static_cast<uint16_t>(audio_delay_milliseconds), clock_drift,
87 voe_mic_level, key_pressed);
88
89 // Copy the audio frame to each sending channel and perform
90 // channel-dependent operations (file mixing, mute, etc.), encode and
91 // packetize+transmit the RTP packet.
92 shared_->transmit_mixer()->ProcessAndEncodeAudio();
93
94 // Scale from VoE to ADM level range.
95 uint32_t new_voe_mic_level = shared_->transmit_mixer()->CaptureLevel();
96 if (new_voe_mic_level != voe_mic_level) {
97 // Return the new volume if AGC has changed the volume.
98 return static_cast<int>((new_voe_mic_level * max_volume +
99 static_cast<int>(kMaxVolumeLevel / 2)) /
100 kMaxVolumeLevel);
101 }
102
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200103 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
105
solenberg13725082015-11-25 08:16:52 -0800106int32_t VoEBaseImpl::NeedMorePlayData(const size_t nSamples,
107 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -0800108 const size_t nChannels,
solenberg13725082015-11-25 08:16:52 -0800109 const uint32_t samplesPerSec,
110 void* audioSamples,
111 size_t& nSamplesOut,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200112 int64_t* elapsed_time_ms,
113 int64_t* ntp_time_ms) {
solenberg2397b9a2017-09-22 06:48:10 -0700114 RTC_NOTREACHED();
xians@webrtc.org56925312014-04-14 10:50:37 +0000115 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
xians@webrtc.org56925312014-04-14 10:50:37 +0000118void VoEBaseImpl::PushCaptureData(int voe_channel, const void* audio_data,
119 int bits_per_sample, int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800120 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700121 size_t number_of_frames) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200122 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(voe_channel);
henrikaec6fbd22017-03-31 05:43:36 -0700123 voe::Channel* channel = ch.channel();
124 if (!channel)
125 return;
126 if (channel->Sending()) {
127 // Send the audio to each channel directly without using the APM in the
128 // transmit mixer.
129 channel->ProcessAndEncodeAudio(static_cast<const int16_t*>(audio_data),
130 sample_rate, number_of_frames,
131 number_of_channels);
xians@webrtc.org07e51962014-01-29 13:54:02 +0000132 }
133}
134
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135void VoEBaseImpl::PullRenderData(int bits_per_sample,
136 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800137 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 size_t number_of_frames,
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200139 void* audio_data, int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000140 int64_t* ntp_time_ms) {
solenberg2397b9a2017-09-22 06:48:10 -0700141 RTC_NOTREACHED();
xians@webrtc.org56925312014-04-14 10:50:37 +0000142}
143
ossu5f7cfa52016-05-30 08:11:28 -0700144int VoEBaseImpl::Init(
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100145 AudioDeviceModule* audio_device,
peahe67bedb2017-07-07 04:25:11 -0700146 AudioProcessing* audio_processing,
ossu5f7cfa52016-05-30 08:11:28 -0700147 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100148 RTC_DCHECK(audio_device);
peahe67bedb2017-07-07 04:25:11 -0700149 RTC_DCHECK(audio_processing);
tommi31fc21f2016-01-21 10:37:37 -0800150 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200151 if (shared_->process_thread()) {
152 shared_->process_thread()->Start();
153 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100155 shared_->set_audio_device(audio_device);
peahe67bedb2017-07-07 04:25:11 -0700156 shared_->set_audio_processing(audio_processing);
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000157
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200158 // Configure AudioProcessing components.
peaha9cc40b2017-06-29 08:32:09 -0700159 // TODO(peah): Move this initialization to webrtcvoiceengine.cc.
peahe67bedb2017-07-07 04:25:11 -0700160 if (audio_processing->high_pass_filter()->Enable(true) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100161 RTC_LOG_F(LS_ERROR) << "Failed to enable high pass filter.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200162 return -1;
163 }
peahe67bedb2017-07-07 04:25:11 -0700164 if (audio_processing->echo_cancellation()->enable_drift_compensation(false) !=
165 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100166 RTC_LOG_F(LS_ERROR) << "Failed to disable drift compensation.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200167 return -1;
168 }
peahe67bedb2017-07-07 04:25:11 -0700169 if (audio_processing->noise_suppression()->set_level(kDefaultNsMode) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100170 RTC_LOG_F(LS_ERROR) << "Failed to set noise suppression level: "
171 << kDefaultNsMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200172 return -1;
173 }
peahe67bedb2017-07-07 04:25:11 -0700174 GainControl* agc = audio_processing->gain_control();
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200175 if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100176 RTC_LOG_F(LS_ERROR) << "Failed to set analog level limits with minimum: "
177 << kMinVolumeLevel
178 << " and maximum: " << kMaxVolumeLevel;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200179 return -1;
180 }
181 if (agc->set_mode(kDefaultAgcMode) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100182 RTC_LOG_F(LS_ERROR) << "Failed to set mode: " << kDefaultAgcMode;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200183 return -1;
184 }
185 if (agc->Enable(kDefaultAgcState) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100186 RTC_LOG_F(LS_ERROR) << "Failed to set agc state: " << kDefaultAgcState;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200187 return -1;
188 }
andrew@webrtc.orgf0a90c32013-03-05 01:12:49 +0000189
niklase@google.com470e71d2011-07-07 08:21:25 +0000190#ifdef WEBRTC_VOICE_ENGINE_AGC
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200191 bool agc_enabled =
192 agc->mode() == GainControl::kAdaptiveAnalog && agc->is_enabled();
193 if (shared_->audio_device()->SetAGC(agc_enabled) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100194 RTC_LOG_F(LS_ERROR) << "Failed to set agc to enabled: " << agc_enabled;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200195 // TODO(ajm): No error return here due to
196 // https://code.google.com/p/webrtc/issues/detail?id=1464
197 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000198#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
Karl Wibergf3850f62017-11-02 13:04:41 +0100200 RTC_DCHECK(decoder_factory);
201 decoder_factory_ = decoder_factory;
ossu5f7cfa52016-05-30 08:11:28 -0700202
solenberg1c239d42017-09-29 06:00:28 -0700203 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204}
205
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200206int VoEBaseImpl::Terminate() {
tommi31fc21f2016-01-21 10:37:37 -0800207 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200208 return TerminateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000209}
210
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000211int VoEBaseImpl::CreateChannel() {
solenberg88499ec2016-09-07 07:34:41 -0700212 return CreateChannel(ChannelConfig());
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000213}
214
solenberg88499ec2016-09-07 07:34:41 -0700215int VoEBaseImpl::CreateChannel(const ChannelConfig& config) {
tommi31fc21f2016-01-21 10:37:37 -0800216 rtc::CritScope cs(shared_->crit_sec());
solenberg88499ec2016-09-07 07:34:41 -0700217 ChannelConfig config_copy(config);
218 config_copy.acm_config.decoder_factory = decoder_factory_;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200219 voe::ChannelOwner channel_owner =
solenberg88499ec2016-09-07 07:34:41 -0700220 shared_->channel_manager().CreateChannel(config_copy);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000221 return InitializeChannel(&channel_owner);
222}
223
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200224int VoEBaseImpl::InitializeChannel(voe::ChannelOwner* channel_owner) {
225 if (channel_owner->channel()->SetEngineInformation(
solenberg796b8f92017-03-01 17:02:23 -0800226 *shared_->process_thread(), *shared_->audio_device(),
solenberg1c239d42017-09-29 06:00:28 -0700227 shared_->encoder_queue()) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_ERROR)
229 << "CreateChannel() failed to associate engine and channel."
230 " Destroying channel.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200231 shared_->channel_manager().DestroyChannel(
232 channel_owner->channel()->ChannelId());
233 return -1;
234 } else if (channel_owner->channel()->Init() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100235 RTC_LOG(LS_ERROR)
236 << "CreateChannel() failed to initialize channel. Destroying"
237 " channel.";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200238 shared_->channel_manager().DestroyChannel(
239 channel_owner->channel()->ChannelId());
240 return -1;
241 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200242 return channel_owner->channel()->ChannelId();
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200245int VoEBaseImpl::DeleteChannel(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800246 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200247 {
248 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
249 voe::Channel* channelPtr = ch.channel();
250 if (channelPtr == nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_ERROR) << "DeleteChannel() failed to locate channel";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200252 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200254 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200256 shared_->channel_manager().DestroyChannel(channel);
257 if (StopSend() != 0) {
258 return -1;
259 }
260 if (StopPlayout() != 0) {
261 return -1;
262 }
263 return 0;
264}
niklase@google.com470e71d2011-07-07 08:21:25 +0000265
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200266int VoEBaseImpl::StartPlayout(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800267 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200268 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
269 voe::Channel* channelPtr = ch.channel();
270 if (channelPtr == nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100271 RTC_LOG(LS_ERROR) << "StartPlayout() failed to locate channel";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200272 return -1;
273 }
274 if (channelPtr->Playing()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 return 0;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200276 }
277 if (StartPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100278 RTC_LOG(LS_ERROR) << "StartPlayout() failed to start playout";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200279 return -1;
280 }
281 return channelPtr->StartPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000282}
283
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200284int VoEBaseImpl::StopPlayout(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800285 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200286 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
287 voe::Channel* channelPtr = ch.channel();
288 if (channelPtr == nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100289 RTC_LOG(LS_ERROR) << "StopPlayout() failed to locate channel";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200290 return -1;
291 }
292 if (channelPtr->StopPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100293 RTC_LOG_F(LS_WARNING) << "StopPlayout() failed to stop playout for channel "
294 << channel;
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200295 }
296 return StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000297}
298
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200299int VoEBaseImpl::StartSend(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800300 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200301 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
302 voe::Channel* channelPtr = ch.channel();
303 if (channelPtr == nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100304 RTC_LOG(LS_ERROR) << "StartSend() failed to locate channel";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200305 return -1;
306 }
307 if (channelPtr->Sending()) {
308 return 0;
309 }
310 if (StartSend() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100311 RTC_LOG(LS_ERROR) << "StartSend() failed to start recording";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200312 return -1;
313 }
314 return channelPtr->StartSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200317int VoEBaseImpl::StopSend(int channel) {
tommi31fc21f2016-01-21 10:37:37 -0800318 rtc::CritScope cs(shared_->crit_sec());
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200319 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
320 voe::Channel* channelPtr = ch.channel();
321 if (channelPtr == nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100322 RTC_LOG(LS_ERROR) << "StopSend() failed to locate channel";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200323 return -1;
324 }
henrikaec6fbd22017-03-31 05:43:36 -0700325 channelPtr->StopSend();
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200326 return StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000327}
328
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200329int32_t VoEBaseImpl::StartPlayout() {
solenberge313e022015-09-08 02:16:04 -0700330 if (!shared_->audio_device()->Playing()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200331 if (shared_->audio_device()->InitPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100332 RTC_LOG_F(LS_ERROR) << "Failed to initialize playout";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200333 return -1;
334 }
henrika5f6bf242017-11-01 11:06:56 +0100335 if (playout_enabled_ && shared_->audio_device()->StartPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100336 RTC_LOG_F(LS_ERROR) << "Failed to start playout";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200337 return -1;
338 }
339 }
340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000341}
342
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000343int32_t VoEBaseImpl::StopPlayout() {
henrika5f6bf242017-11-01 11:06:56 +0100344 if (!playout_enabled_) {
345 return 0;
346 }
347 // Stop audio-device playing if no channel is playing out.
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200348 if (shared_->NumOfPlayingChannels() == 0) {
349 if (shared_->audio_device()->StopPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100350 RTC_LOG(LS_ERROR) << "StopPlayout() failed to stop playout";
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000351 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000353 }
354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355}
356
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200357int32_t VoEBaseImpl::StartSend() {
henrika5f6bf242017-11-01 11:06:56 +0100358 if (!shared_->audio_device()->Recording()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200359 if (shared_->audio_device()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 RTC_LOG_F(LS_ERROR) << "Failed to initialize recording";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200361 return -1;
362 }
henrika5f6bf242017-11-01 11:06:56 +0100363 if (recording_enabled_ && shared_->audio_device()->StartRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100364 RTC_LOG_F(LS_ERROR) << "Failed to start recording";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200365 return -1;
366 }
367 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000369}
370
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200371int32_t VoEBaseImpl::StopSend() {
henrika5f6bf242017-11-01 11:06:56 +0100372 if (!recording_enabled_) {
373 return 0;
374 }
375 // Stop audio-device recording if no channel is recording.
solenbergb63310a2017-09-18 03:04:12 -0700376 if (shared_->NumOfSendingChannels() == 0) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200377 if (shared_->audio_device()->StopRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100378 RTC_LOG(LS_ERROR) << "StopSend() failed to stop recording";
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200379 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000380 }
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200381 shared_->transmit_mixer()->StopSend();
382 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200384 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
henrika5f6bf242017-11-01 11:06:56 +0100387int32_t VoEBaseImpl::SetPlayout(bool enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100388 RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
henrika5f6bf242017-11-01 11:06:56 +0100389 if (playout_enabled_ == enabled) {
390 return 0;
391 }
392 playout_enabled_ = enabled;
393 if (shared_->NumOfPlayingChannels() == 0) {
394 // If there are no channels attempting to play out yet, there's nothing to
395 // be done; we should be in a "not playing out" state either way.
396 return 0;
397 }
398 int32_t ret;
399 if (enabled) {
400 ret = shared_->audio_device()->StartPlayout();
401 if (ret != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100402 RTC_LOG(LS_ERROR) << "SetPlayout(true) failed to start playout";
henrika5f6bf242017-11-01 11:06:56 +0100403 }
404 } else {
405 ret = shared_->audio_device()->StopPlayout();
406 if (ret != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100407 RTC_LOG(LS_ERROR) << "SetPlayout(false) failed to stop playout";
henrika5f6bf242017-11-01 11:06:56 +0100408 }
409 }
410 return ret;
411}
412
413int32_t VoEBaseImpl::SetRecording(bool enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100414 RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
henrika5f6bf242017-11-01 11:06:56 +0100415 if (recording_enabled_ == enabled) {
416 return 0;
417 }
418 recording_enabled_ = enabled;
419 if (shared_->NumOfSendingChannels() == 0) {
420 // If there are no channels attempting to record out yet, there's nothing to
421 // be done; we should be in a "not recording" state either way.
422 return 0;
423 }
424 int32_t ret;
425 if (enabled) {
426 ret = shared_->audio_device()->StartRecording();
427 if (ret != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100428 RTC_LOG(LS_ERROR) << "SetRecording(true) failed to start recording";
henrika5f6bf242017-11-01 11:06:56 +0100429 }
430 } else {
431 ret = shared_->audio_device()->StopRecording();
432 if (ret != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100433 RTC_LOG(LS_ERROR) << "SetRecording(false) failed to stop recording";
henrika5f6bf242017-11-01 11:06:56 +0100434 }
435 }
436 return ret;
437}
438
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200439int32_t VoEBaseImpl::TerminateInternal() {
440 // Delete any remaining channel objects
441 shared_->channel_manager().DestroyAllChannels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200443 if (shared_->process_thread()) {
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200444 shared_->process_thread()->Stop();
445 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100447 shared_->set_audio_device(nullptr);
peaha9cc40b2017-06-29 08:32:09 -0700448 shared_->set_audio_processing(nullptr);
Jelena Marusic2dd6a272015-04-14 09:47:00 +0200449
solenberg1c239d42017-09-29 06:00:28 -0700450 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000452} // namespace webrtc