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