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 | |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 13 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 14 | #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h" |
| 15 | #include "webrtc/modules/audio_device/audio_device_impl.h" |
| 16 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 17 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 18 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 19 | #include "webrtc/system_wrappers/interface/trace.h" |
| 20 | #include "webrtc/voice_engine/channel.h" |
| 21 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 22 | #include "webrtc/voice_engine/output_mixer.h" |
| 23 | #include "webrtc/voice_engine/transmit_mixer.h" |
| 24 | #include "webrtc/voice_engine/utility.h" |
| 25 | #include "webrtc/voice_engine/voice_engine_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
| 27 | #if (defined(_WIN32) && defined(_DLL) && (_MSC_VER == 1400)) |
| 28 | // Fix for VS 2005 MD/MDd link problem |
| 29 | #include <stdio.h> |
| 30 | extern "C" |
| 31 | { FILE _iob[3] = { __iob_func()[0], __iob_func()[1], __iob_func()[2]}; } |
| 32 | #endif |
| 33 | |
| 34 | namespace webrtc |
| 35 | { |
| 36 | |
| 37 | VoEBase* VoEBase::GetInterface(VoiceEngine* voiceEngine) |
| 38 | { |
| 39 | if (NULL == voiceEngine) |
| 40 | { |
| 41 | return NULL; |
| 42 | } |
tommi@webrtc.org | 0989fb7 | 2013-02-15 15:07:32 +0000 | [diff] [blame] | 43 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 44 | s->AddRef(); |
| 45 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 48 | VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared) : |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | _voiceEngineObserverPtr(NULL), |
| 50 | _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()), |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 51 | _voiceEngineObserver(false), _oldVoEMicLevel(0), _oldMicLevel(0), |
| 52 | _shared(shared) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 54 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | "VoEBaseImpl() - ctor"); |
| 56 | } |
| 57 | |
| 58 | VoEBaseImpl::~VoEBaseImpl() |
| 59 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 60 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | "~VoEBaseImpl() - dtor"); |
| 62 | |
| 63 | TerminateInternal(); |
| 64 | |
| 65 | delete &_callbackCritSect; |
| 66 | } |
| 67 | |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 68 | void VoEBaseImpl::OnErrorIsReported(ErrorCode error) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 70 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | if (_voiceEngineObserver) |
| 72 | { |
| 73 | if (_voiceEngineObserverPtr) |
| 74 | { |
| 75 | int errCode(0); |
| 76 | if (error == AudioDeviceObserver::kRecordingError) |
| 77 | { |
| 78 | errCode = VE_RUNTIME_REC_ERROR; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 79 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 80 | VoEId(_shared->instance_id(), -1), |
| 81 | "VoEBaseImpl::OnErrorIsReported() => VE_RUNTIME_REC_ERROR"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | else if (error == AudioDeviceObserver::kPlayoutError) |
| 84 | { |
| 85 | errCode = VE_RUNTIME_PLAY_ERROR; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 86 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 87 | VoEId(_shared->instance_id(), -1), |
| 88 | "VoEBaseImpl::OnErrorIsReported() => " |
| 89 | "VE_RUNTIME_PLAY_ERROR"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | // Deliver callback (-1 <=> no channel dependency) |
| 92 | _voiceEngineObserverPtr->CallbackOnError(-1, errCode); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 97 | void VoEBaseImpl::OnWarningIsReported(WarningCode warning) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 99 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | if (_voiceEngineObserver) |
| 101 | { |
| 102 | if (_voiceEngineObserverPtr) |
| 103 | { |
| 104 | int warningCode(0); |
| 105 | if (warning == AudioDeviceObserver::kRecordingWarning) |
| 106 | { |
| 107 | warningCode = VE_RUNTIME_REC_WARNING; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 108 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 109 | VoEId(_shared->instance_id(), -1), |
| 110 | "VoEBaseImpl::OnErrorIsReported() => " |
| 111 | "VE_RUNTIME_REC_WARNING"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
| 113 | else if (warning == AudioDeviceObserver::kPlayoutWarning) |
| 114 | { |
| 115 | warningCode = VE_RUNTIME_PLAY_WARNING; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 116 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 117 | VoEId(_shared->instance_id(), -1), |
| 118 | "VoEBaseImpl::OnErrorIsReported() => " |
| 119 | "VE_RUNTIME_PLAY_WARNING"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | // Deliver callback (-1 <=> no channel dependency) |
| 122 | _voiceEngineObserverPtr->CallbackOnError(-1, warningCode); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 127 | int32_t VoEBaseImpl::RecordedDataIsAvailable( |
henrika@webrtc.org | 907bc55 | 2012-03-09 08:59:19 +0000 | [diff] [blame] | 128 | const void* audioSamples, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 129 | uint32_t nSamples, |
| 130 | uint8_t nBytesPerSample, |
| 131 | uint8_t nChannels, |
| 132 | uint32_t samplesPerSec, |
| 133 | uint32_t totalDelayMS, |
| 134 | int32_t clockDrift, |
| 135 | uint32_t currentMicLevel, |
| 136 | bool keyPressed, |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 137 | uint32_t& newMicLevel) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 139 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | "VoEBaseImpl::RecordedDataIsAvailable(nSamples=%u, " |
| 141 | "nBytesPerSample=%u, nChannels=%u, samplesPerSec=%u, " |
| 142 | "totalDelayMS=%u, clockDrift=%d, currentMicLevel=%u)", |
| 143 | nSamples, nBytesPerSample, nChannels, samplesPerSec, |
| 144 | totalDelayMS, clockDrift, currentMicLevel); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 145 | newMicLevel = static_cast<uint32_t>(ProcessRecordedDataWithAPM( |
| 146 | NULL, 0, audioSamples, samplesPerSec, nChannels, nSamples, |
| 147 | totalDelayMS, clockDrift, currentMicLevel, keyPressed)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 152 | int32_t VoEBaseImpl::NeedMorePlayData( |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 153 | uint32_t nSamples, |
| 154 | uint8_t nBytesPerSample, |
| 155 | uint8_t nChannels, |
| 156 | uint32_t samplesPerSec, |
henrika@webrtc.org | 907bc55 | 2012-03-09 08:59:19 +0000 | [diff] [blame] | 157 | void* audioSamples, |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 158 | uint32_t& nSamplesOut) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 160 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | "VoEBaseImpl::NeedMorePlayData(nSamples=%u, " |
| 162 | "nBytesPerSample=%d, nChannels=%d, samplesPerSec=%u)", |
| 163 | nSamples, nBytesPerSample, nChannels, samplesPerSec); |
| 164 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 165 | assert(_shared->output_mixer() != NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 167 | // TODO(andrew): if the device is running in mono, we should tell the mixer |
| 168 | // here so that it will only request mono from AudioCodingModule. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | // Perform mixing of all active participants (channel-based mixing) |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 170 | _shared->output_mixer()->MixActiveChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
| 172 | // Additional operations on the combined signal |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 173 | _shared->output_mixer()->DoOperationsOnCombinedSignal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | |
| 175 | // Retrieve the final output mix (resampled to match the ADM) |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 176 | _shared->output_mixer()->GetMixedAudio(samplesPerSec, nChannels, |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 177 | &_audioFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
andrew@webrtc.org | e59a0ac | 2012-05-08 17:12:40 +0000 | [diff] [blame] | 179 | assert(static_cast<int>(nSamples) == _audioFrame.samples_per_channel_); |
xians@google.com | 0b0665a | 2011-08-08 08:18:44 +0000 | [diff] [blame] | 180 | assert(samplesPerSec == |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 181 | static_cast<uint32_t>(_audioFrame.sample_rate_hz_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | |
| 183 | // Deliver audio (PCM) samples to the ADM |
| 184 | memcpy( |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 185 | (int16_t*) audioSamples, |
| 186 | (const int16_t*) _audioFrame.data_, |
| 187 | sizeof(int16_t) * (_audioFrame.samples_per_channel_ |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 188 | * _audioFrame.num_channels_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 190 | nSamplesOut = _audioFrame.samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 195 | int VoEBaseImpl::OnDataAvailable(const int voe_channels[], |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 196 | int number_of_voe_channels, |
| 197 | const int16_t* audio_data, |
| 198 | int sample_rate, |
| 199 | int number_of_channels, |
| 200 | int number_of_frames, |
| 201 | int audio_delay_milliseconds, |
| 202 | int current_volume, |
| 203 | bool key_pressed, |
| 204 | bool need_audio_processing) { |
| 205 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 206 | "VoEBaseImpl::OnDataAvailable(number_of_voe_channels=%d, " |
| 207 | "sample_rate=%d, number_of_channels=%d, number_of_frames=%d, " |
| 208 | "audio_delay_milliseconds=%d, current_volume=%d, " |
| 209 | "key_pressed=%d, need_audio_processing=%d)", |
| 210 | number_of_voe_channels, sample_rate, number_of_channels, |
| 211 | number_of_frames, audio_delay_milliseconds, current_volume, |
| 212 | key_pressed, need_audio_processing); |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 213 | if (number_of_voe_channels == 0) |
| 214 | return 0; |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 215 | |
| 216 | if (need_audio_processing) { |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 217 | return ProcessRecordedDataWithAPM( |
| 218 | voe_channels, number_of_voe_channels, audio_data, sample_rate, |
| 219 | number_of_channels, number_of_frames, audio_delay_milliseconds, |
| 220 | 0, current_volume, key_pressed); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // No need to go through the APM, demultiplex the data to each VoE channel, |
| 224 | // encode and send to the network. |
| 225 | for (int i = 0; i < number_of_voe_channels; ++i) { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 226 | voe::ChannelOwner ch = |
| 227 | _shared->channel_manager().GetChannel(voe_channels[i]); |
| 228 | voe::Channel* channel_ptr = ch.channel(); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 229 | if (!channel_ptr) |
| 230 | continue; |
| 231 | |
| 232 | if (channel_ptr->InputIsOnHold()) { |
| 233 | channel_ptr->UpdateLocalTimeStamp(); |
| 234 | } else if (channel_ptr->Sending()) { |
| 235 | channel_ptr->Demultiplex(audio_data, sample_rate, number_of_frames, |
| 236 | number_of_channels); |
| 237 | channel_ptr->PrepareEncodeAndSend(sample_rate); |
| 238 | channel_ptr->EncodeAndSend(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Return 0 to indicate no need to change the volume. |
| 243 | return 0; |
| 244 | } |
| 245 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) |
| 247 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 248 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | "RegisterVoiceEngineObserver(observer=0x%d)", &observer); |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 250 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | if (_voiceEngineObserverPtr) |
| 252 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 253 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 254 | "RegisterVoiceEngineObserver() observer already enabled"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | // Register the observer in all active channels |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 259 | for (voe::ChannelManager::Iterator it(&_shared->channel_manager()); |
| 260 | it.IsValid(); |
| 261 | it.Increment()) { |
| 262 | it.GetChannel()->RegisterVoiceEngineObserver(observer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 264 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 265 | _shared->transmit_mixer()->RegisterVoiceEngineObserver(observer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | |
| 267 | _voiceEngineObserverPtr = &observer; |
| 268 | _voiceEngineObserver = true; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int VoEBaseImpl::DeRegisterVoiceEngineObserver() |
| 274 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 275 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | "DeRegisterVoiceEngineObserver()"); |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 277 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | if (!_voiceEngineObserverPtr) |
| 279 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 280 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 281 | "DeRegisterVoiceEngineObserver() observer already disabled"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | _voiceEngineObserver = false; |
| 286 | _voiceEngineObserverPtr = NULL; |
| 287 | |
| 288 | // Deregister the observer in all active channels |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 289 | for (voe::ChannelManager::Iterator it(&_shared->channel_manager()); |
| 290 | it.IsValid(); |
| 291 | it.Increment()) { |
| 292 | it.GetChannel()->DeRegisterVoiceEngineObserver(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 298 | int VoEBaseImpl::Init(AudioDeviceModule* external_adm, |
| 299 | AudioProcessing* audioproc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 301 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 302 | "Init(external_adm=0x%p)", external_adm); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 303 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 304 | |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 305 | WebRtcSpl_Init(); |
| 306 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 307 | if (_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 308 | { |
| 309 | return 0; |
| 310 | } |
| 311 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 312 | if (_shared->process_thread()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 314 | if (_shared->process_thread()->Start() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 315 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 316 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 317 | "Init() failed to start module process thread"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | } |
| 321 | |
andrew@webrtc.org | 3192d65 | 2011-12-21 18:00:59 +0000 | [diff] [blame] | 322 | // 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] | 323 | // ADM implementation as input to Init(). |
| 324 | if (external_adm == NULL) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 325 | { |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 326 | // Create the internal ADM implementation. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 327 | _shared->set_audio_device(AudioDeviceModuleImpl::Create( |
| 328 | VoEId(_shared->instance_id(), -1), _shared->audio_device_layer())); |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 329 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 330 | if (_shared->audio_device() == NULL) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 331 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 332 | _shared->SetLastError(VE_NO_MEMORY, kTraceCritical, |
| 333 | "Init() failed to create the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | } |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 337 | else |
| 338 | { |
| 339 | // Use the already existing external ADM implementation. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 340 | _shared->set_audio_device(external_adm); |
| 341 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 342 | "An external ADM implementation will be used in VoiceEngine"); |
| 343 | } |
| 344 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 345 | // Register the ADM to the process thread, which will drive the error |
| 346 | // callback mechanism |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 347 | if (_shared->process_thread() && |
| 348 | _shared->process_thread()->RegisterModule(_shared->audio_device()) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 349 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 350 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 351 | "Init() failed to register the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | bool available(false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 356 | |
| 357 | // -------------------- |
| 358 | // Reinitialize the ADM |
| 359 | |
| 360 | // Register the AudioObserver implementation |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 361 | if (_shared->audio_device()->RegisterEventObserver(this) != 0) { |
| 362 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 363 | "Init() failed to register event observer for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 364 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | |
| 366 | // Register the AudioTransport implementation |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 367 | if (_shared->audio_device()->RegisterAudioCallback(this) != 0) { |
| 368 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 369 | "Init() failed to register audio callback for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 370 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | |
| 372 | // ADM initialization |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 373 | if (_shared->audio_device()->Init() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 374 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 375 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 376 | "Init() failed to initialize the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | // Initialize the default speaker |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 381 | if (_shared->audio_device()->SetPlayoutDevice( |
| 382 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 383 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 384 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 385 | "Init() failed to set the default output device"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 386 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 387 | if (_shared->audio_device()->SpeakerIsAvailable(&available) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 388 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 389 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 390 | "Init() failed to check speaker availability, trying to " |
| 391 | "initialize speaker anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | } |
| 393 | else if (!available) |
| 394 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 395 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 396 | "Init() speaker not available, trying to initialize speaker " |
| 397 | "anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 398 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 399 | if (_shared->audio_device()->InitSpeaker() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 400 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 401 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 402 | "Init() failed to initialize the speaker"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // Initialize the default microphone |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 406 | if (_shared->audio_device()->SetRecordingDevice( |
| 407 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 408 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 409 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 410 | "Init() failed to set the default input device"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 412 | if (_shared->audio_device()->MicrophoneIsAvailable(&available) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 414 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 415 | "Init() failed to check microphone availability, trying to " |
| 416 | "initialize microphone anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | } |
| 418 | else if (!available) |
| 419 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 420 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 421 | "Init() microphone not available, trying to initialize " |
| 422 | "microphone anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 424 | if (_shared->audio_device()->InitMicrophone() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 426 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 427 | "Init() failed to initialize the microphone"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | } |
| 429 | |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 430 | // Set number of channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 431 | if (_shared->audio_device()->StereoPlayoutIsAvailable(&available) != 0) { |
| 432 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 433 | "Init() failed to query stereo playout mode"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 434 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 435 | if (_shared->audio_device()->SetStereoPlayout(available) != 0) |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 436 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 437 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 438 | "Init() failed to set mono/stereo playout mode"); |
| 439 | } |
andrew@webrtc.org | 3192d65 | 2011-12-21 18:00:59 +0000 | [diff] [blame] | 440 | |
| 441 | // TODO(andrew): These functions don't tell us whether stereo recording |
| 442 | // is truly available. We simply set the AudioProcessing input to stereo |
| 443 | // here, because we have to wait until receiving the first frame to |
| 444 | // determine the actual number of channels anyway. |
| 445 | // |
| 446 | // These functions may be changed; tracked here: |
| 447 | // http://code.google.com/p/webrtc/issues/detail?id=204 |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 448 | _shared->audio_device()->StereoRecordingIsAvailable(&available); |
| 449 | if (_shared->audio_device()->SetStereoRecording(available) != 0) |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 450 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 451 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 452 | "Init() failed to set mono/stereo recording mode"); |
| 453 | } |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 454 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 455 | if (!audioproc) { |
| 456 | audioproc = AudioProcessing::Create(VoEId(_shared->instance_id(), -1)); |
| 457 | if (!audioproc) { |
| 458 | LOG(LS_ERROR) << "Failed to create AudioProcessing."; |
| 459 | _shared->SetLastError(VE_NO_MEMORY); |
| 460 | return -1; |
| 461 | } |
| 462 | } |
| 463 | _shared->set_audio_processing(audioproc); |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 464 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 465 | // Set the error state for any failures in this block. |
| 466 | _shared->SetLastError(VE_APM_ERROR); |
| 467 | if (audioproc->echo_cancellation()->set_device_sample_rate_hz(48000)) { |
| 468 | LOG_FERR1(LS_ERROR, set_device_sample_rate_hz, 48000); |
| 469 | return -1; |
| 470 | } |
| 471 | // Assume 16 kHz mono until the audio frames are received from the capture |
| 472 | // device, at which point this can be updated. |
| 473 | if (audioproc->set_sample_rate_hz(16000)) { |
| 474 | LOG_FERR1(LS_ERROR, set_sample_rate_hz, 16000); |
| 475 | return -1; |
| 476 | } |
| 477 | if (audioproc->set_num_channels(1, 1) != 0) { |
| 478 | LOG_FERR2(LS_ERROR, set_num_channels, 1, 1); |
| 479 | return -1; |
| 480 | } |
| 481 | if (audioproc->set_num_reverse_channels(1) != 0) { |
| 482 | LOG_FERR1(LS_ERROR, set_num_reverse_channels, 1); |
| 483 | return -1; |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 484 | } |
| 485 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 486 | // Configure AudioProcessing components. All are disabled by default. |
| 487 | if (audioproc->high_pass_filter()->Enable(true) != 0) { |
| 488 | LOG_FERR1(LS_ERROR, high_pass_filter()->Enable, true); |
| 489 | return -1; |
| 490 | } |
| 491 | if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) { |
| 492 | LOG_FERR1(LS_ERROR, enable_drift_compensation, false); |
| 493 | return -1; |
| 494 | } |
| 495 | if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) { |
| 496 | LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode); |
| 497 | return -1; |
| 498 | } |
| 499 | GainControl* agc = audioproc->gain_control(); |
| 500 | if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) { |
| 501 | LOG_FERR2(LS_ERROR, agc->set_analog_level_limits, kMinVolumeLevel, |
| 502 | kMaxVolumeLevel); |
| 503 | return -1; |
| 504 | } |
| 505 | if (agc->set_mode(kDefaultAgcMode) != 0) { |
| 506 | LOG_FERR1(LS_ERROR, agc->set_mode, kDefaultAgcMode); |
| 507 | return -1; |
| 508 | } |
| 509 | if (agc->Enable(kDefaultAgcState) != 0) { |
| 510 | LOG_FERR1(LS_ERROR, agc->Enable, kDefaultAgcState); |
| 511 | return -1; |
| 512 | } |
| 513 | _shared->SetLastError(0); // Clear error state. |
| 514 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 515 | #ifdef WEBRTC_VOICE_ENGINE_AGC |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 516 | bool agc_enabled = agc->mode() == GainControl::kAdaptiveAnalog && |
| 517 | agc->is_enabled(); |
| 518 | if (_shared->audio_device()->SetAGC(agc_enabled) != 0) { |
| 519 | LOG_FERR1(LS_ERROR, audio_device()->SetAGC, agc_enabled); |
| 520 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR); |
andrew@webrtc.org | a9a1df0 | 2013-03-05 23:36:10 +0000 | [diff] [blame] | 521 | // TODO(ajm): No error return here due to |
| 522 | // https://code.google.com/p/webrtc/issues/detail?id=1464 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 523 | } |
| 524 | #endif |
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 | return _shared->statistics().SetInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | int VoEBaseImpl::Terminate() |
| 530 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 531 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 532 | "Terminate()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 533 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 534 | return TerminateInternal(); |
| 535 | } |
| 536 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 537 | int VoEBaseImpl::CreateChannel() |
| 538 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 539 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 540 | "CreateChannel()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 541 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 542 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 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 | } |
| 548 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 549 | voe::ChannelOwner channel_owner = |
| 550 | _shared->channel_manager().CreateChannel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 551 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 552 | if (channel_owner.channel()->SetEngineInformation( |
| 553 | _shared->statistics(), |
| 554 | *_shared->output_mixer(), |
| 555 | *_shared->transmit_mixer(), |
| 556 | *_shared->process_thread(), |
| 557 | *_shared->audio_device(), |
| 558 | _voiceEngineObserverPtr, |
| 559 | &_callbackCritSect) != 0) { |
| 560 | _shared->SetLastError( |
| 561 | VE_CHANNEL_NOT_CREATED, |
| 562 | kTraceError, |
| 563 | "CreateChannel() failed to associate engine and channel." |
| 564 | " Destroying channel."); |
| 565 | _shared->channel_manager() |
| 566 | .DestroyChannel(channel_owner.channel()->ChannelId()); |
| 567 | return -1; |
| 568 | } else if (channel_owner.channel()->Init() != 0) { |
| 569 | _shared->SetLastError( |
| 570 | VE_CHANNEL_NOT_CREATED, |
| 571 | kTraceError, |
| 572 | "CreateChannel() failed to initialize channel. Destroying" |
| 573 | " channel."); |
| 574 | _shared->channel_manager() |
| 575 | .DestroyChannel(channel_owner.channel()->ChannelId()); |
| 576 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | } |
| 578 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 579 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 580 | VoEId(_shared->instance_id(), -1), |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 581 | "CreateChannel() => %d", channel_owner.channel()->ChannelId()); |
| 582 | return channel_owner.channel()->ChannelId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | int VoEBaseImpl::DeleteChannel(int channel) |
| 586 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 587 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 588 | "DeleteChannel(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 589 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 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 | } |
| 596 | |
| 597 | { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 598 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 599 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 600 | if (channelPtr == NULL) |
| 601 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 602 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 603 | "DeleteChannel() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 604 | return -1; |
| 605 | } |
| 606 | } |
| 607 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 608 | _shared->channel_manager().DestroyChannel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 609 | |
| 610 | if (StopSend() != 0) |
| 611 | { |
| 612 | return -1; |
| 613 | } |
| 614 | |
| 615 | if (StopPlayout() != 0) |
| 616 | { |
| 617 | return -1; |
| 618 | } |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 619 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 623 | int VoEBaseImpl::StartReceive(int channel) |
| 624 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 625 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 626 | "StartReceive(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 627 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 628 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 629 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 630 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 631 | return -1; |
| 632 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 633 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 634 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 635 | if (channelPtr == NULL) |
| 636 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 637 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 638 | "StartReceive() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 639 | return -1; |
| 640 | } |
| 641 | return channelPtr->StartReceiving(); |
| 642 | } |
| 643 | |
| 644 | int VoEBaseImpl::StopReceive(int channel) |
| 645 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 646 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 647 | "StopListen(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 648 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 649 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 650 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 651 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 652 | return -1; |
| 653 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 654 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 655 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 656 | if (channelPtr == NULL) |
| 657 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 658 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 659 | "SetLocalReceiver() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | return channelPtr->StopReceiving(); |
| 663 | } |
| 664 | |
| 665 | int VoEBaseImpl::StartPlayout(int channel) |
| 666 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 667 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 668 | "StartPlayout(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 669 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 670 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 671 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 672 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 673 | return -1; |
| 674 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 675 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 676 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 677 | if (channelPtr == NULL) |
| 678 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 679 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 680 | "StartPlayout() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 681 | return -1; |
| 682 | } |
| 683 | if (channelPtr->Playing()) |
| 684 | { |
| 685 | return 0; |
| 686 | } |
| 687 | if (StartPlayout() != 0) |
| 688 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 689 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 690 | "StartPlayout() failed to start playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 691 | return -1; |
| 692 | } |
| 693 | return channelPtr->StartPlayout(); |
| 694 | } |
| 695 | |
| 696 | int VoEBaseImpl::StopPlayout(int channel) |
| 697 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 698 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 699 | "StopPlayout(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 700 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 701 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 702 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 703 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 704 | return -1; |
| 705 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 706 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 707 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 708 | if (channelPtr == NULL) |
| 709 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 710 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 711 | "StopPlayout() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 712 | return -1; |
| 713 | } |
| 714 | if (channelPtr->StopPlayout() != 0) |
| 715 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 716 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, |
| 717 | VoEId(_shared->instance_id(), -1), |
| 718 | "StopPlayout() failed to stop playout for channel %d", channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 719 | } |
| 720 | return StopPlayout(); |
| 721 | } |
| 722 | |
| 723 | int VoEBaseImpl::StartSend(int channel) |
| 724 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 725 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 726 | "StartSend(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 727 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 728 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 729 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 730 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 731 | return -1; |
| 732 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 733 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 734 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 735 | if (channelPtr == NULL) |
| 736 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 737 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 738 | "StartSend() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 739 | return -1; |
| 740 | } |
| 741 | if (channelPtr->Sending()) |
| 742 | { |
| 743 | return 0; |
| 744 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 745 | if (StartSend() != 0) |
| 746 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 747 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 748 | "StartSend() failed to start recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 749 | return -1; |
| 750 | } |
| 751 | return channelPtr->StartSend(); |
| 752 | } |
| 753 | |
| 754 | int VoEBaseImpl::StopSend(int channel) |
| 755 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 756 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 757 | "StopSend(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 758 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 759 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 760 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 761 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 762 | return -1; |
| 763 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 764 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 765 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 766 | if (channelPtr == NULL) |
| 767 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 768 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 769 | "StopSend() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 770 | return -1; |
| 771 | } |
| 772 | if (channelPtr->StopSend() != 0) |
| 773 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 774 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, |
| 775 | VoEId(_shared->instance_id(), -1), |
| 776 | "StopSend() failed to stop sending for channel %d", channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 777 | } |
| 778 | return StopSend(); |
| 779 | } |
| 780 | |
| 781 | int VoEBaseImpl::GetVersion(char version[1024]) |
| 782 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 783 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 784 | "GetVersion(version=?)"); |
| 785 | assert(kVoiceEngineVersionMaxMessageSize == 1024); |
| 786 | |
| 787 | if (version == NULL) |
| 788 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 789 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 790 | return (-1); |
| 791 | } |
| 792 | |
| 793 | char versionBuf[kVoiceEngineVersionMaxMessageSize]; |
| 794 | char* versionPtr = versionBuf; |
| 795 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 796 | int32_t len = 0; |
| 797 | int32_t accLen = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 798 | |
| 799 | len = AddVoEVersion(versionPtr); |
| 800 | if (len == -1) |
| 801 | { |
| 802 | return -1; |
| 803 | } |
| 804 | versionPtr += len; |
| 805 | accLen += len; |
| 806 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 807 | |
| 808 | len = AddBuildInfo(versionPtr); |
| 809 | if (len == -1) |
| 810 | { |
| 811 | return -1; |
| 812 | } |
| 813 | versionPtr += len; |
| 814 | accLen += len; |
| 815 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 816 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 817 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
| 818 | len = AddExternalTransportBuild(versionPtr); |
| 819 | if (len == -1) |
| 820 | { |
| 821 | return -1; |
| 822 | } |
| 823 | versionPtr += len; |
| 824 | accLen += len; |
| 825 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 826 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 827 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
| 828 | len = AddExternalRecAndPlayoutBuild(versionPtr); |
| 829 | if (len == -1) |
| 830 | { |
| 831 | return -1; |
| 832 | } |
| 833 | versionPtr += len; |
| 834 | accLen += len; |
| 835 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
pwestin@webrtc.org | c450a19 | 2012-01-04 15:00:12 +0000 | [diff] [blame] | 836 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 837 | |
| 838 | memcpy(version, versionBuf, accLen); |
| 839 | version[accLen] = '\0'; |
| 840 | |
| 841 | // to avoid the truncation in the trace, split the string into parts |
| 842 | char partOfVersion[256]; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 843 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 844 | VoEId(_shared->instance_id(), -1), "GetVersion() =>"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 845 | for (int partStart = 0; partStart < accLen;) |
| 846 | { |
| 847 | memset(partOfVersion, 0, sizeof(partOfVersion)); |
| 848 | int partEnd = partStart + 180; |
| 849 | while (version[partEnd] != '\n' && version[partEnd] != '\0') |
| 850 | { |
| 851 | partEnd--; |
| 852 | } |
| 853 | if (partEnd < accLen) |
| 854 | { |
| 855 | memcpy(partOfVersion, &version[partStart], partEnd - partStart); |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | memcpy(partOfVersion, &version[partStart], accLen - partStart); |
| 860 | } |
| 861 | partStart = partEnd; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 862 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 863 | VoEId(_shared->instance_id(), -1), "%s", partOfVersion); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 869 | int32_t VoEBaseImpl::AddBuildInfo(char* str) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 870 | { |
leozwang@webrtc.org | 48a5df6 | 2012-04-24 14:50:50 +0000 | [diff] [blame] | 871 | return sprintf(str, "Build: svn:%s %s\n", WEBRTC_SVNREVISION, BUILDINFO); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 872 | } |
| 873 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 874 | int32_t VoEBaseImpl::AddVoEVersion(char* str) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 875 | { |
| 876 | return sprintf(str, "VoiceEngine 4.1.0\n"); |
| 877 | } |
| 878 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 879 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 880 | int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 881 | { |
| 882 | return sprintf(str, "External transport build\n"); |
| 883 | } |
| 884 | #endif |
| 885 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 886 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 887 | int32_t VoEBaseImpl::AddExternalRecAndPlayoutBuild(char* str) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 888 | { |
| 889 | return sprintf(str, "External recording and playout build\n"); |
| 890 | } |
| 891 | #endif |
| 892 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 893 | int VoEBaseImpl::LastError() |
| 894 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 895 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 896 | "LastError()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 897 | return (_shared->statistics().LastError()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | |
| 901 | int VoEBaseImpl::SetNetEQPlayoutMode(int channel, NetEqModes mode) |
| 902 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 903 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 904 | "SetNetEQPlayoutMode(channel=%i, mode=%i)", channel, mode); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 905 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 906 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 907 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 908 | return -1; |
| 909 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 910 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 911 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 912 | if (channelPtr == NULL) |
| 913 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 914 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 915 | "SetNetEQPlayoutMode() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 916 | return -1; |
| 917 | } |
| 918 | return channelPtr->SetNetEQPlayoutMode(mode); |
| 919 | } |
| 920 | |
| 921 | int VoEBaseImpl::GetNetEQPlayoutMode(int channel, NetEqModes& mode) |
| 922 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 923 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 924 | "GetNetEQPlayoutMode(channel=%i, mode=?)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 925 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 926 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 927 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 928 | return -1; |
| 929 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 930 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 931 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 932 | if (channelPtr == NULL) |
| 933 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 934 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 935 | "GetNetEQPlayoutMode() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 936 | return -1; |
| 937 | } |
| 938 | return channelPtr->GetNetEQPlayoutMode(mode); |
| 939 | } |
| 940 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 941 | int VoEBaseImpl::SetOnHoldStatus(int channel, bool enable, OnHoldModes mode) |
| 942 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 943 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 944 | "SetOnHoldStatus(channel=%d, enable=%d, mode=%d)", channel, |
| 945 | enable, mode); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 946 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 947 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 948 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 949 | return -1; |
| 950 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 951 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 952 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 953 | if (channelPtr == NULL) |
| 954 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 955 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 956 | "SetOnHoldStatus() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 957 | return -1; |
| 958 | } |
| 959 | return channelPtr->SetOnHoldStatus(enable, mode); |
| 960 | } |
| 961 | |
| 962 | int VoEBaseImpl::GetOnHoldStatus(int channel, bool& enabled, OnHoldModes& mode) |
| 963 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 964 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 965 | "GetOnHoldStatus(channel=%d, enabled=?, mode=?)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 966 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 967 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 968 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 969 | return -1; |
| 970 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 971 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 972 | voe::Channel* channelPtr = ch.channel(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 973 | if (channelPtr == NULL) |
| 974 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 975 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 976 | "GetOnHoldStatus() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 977 | return -1; |
| 978 | } |
| 979 | return channelPtr->GetOnHoldStatus(enabled, mode); |
| 980 | } |
| 981 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 982 | int32_t VoEBaseImpl::StartPlayout() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 983 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 984 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 985 | "VoEBaseImpl::StartPlayout()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 986 | if (_shared->audio_device()->Playing()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 987 | { |
| 988 | return 0; |
| 989 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 990 | if (!_shared->ext_playout()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 991 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 992 | if (_shared->audio_device()->InitPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 993 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 994 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 995 | VoEId(_shared->instance_id(), -1), |
| 996 | "StartPlayout() failed to initialize playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 997 | return -1; |
| 998 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 999 | if (_shared->audio_device()->StartPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1000 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1001 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1002 | VoEId(_shared->instance_id(), -1), |
| 1003 | "StartPlayout() failed to start playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1004 | return -1; |
| 1005 | } |
| 1006 | } |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 1010 | int32_t VoEBaseImpl::StopPlayout() { |
| 1011 | WEBRTC_TRACE(kTraceInfo, |
| 1012 | kTraceVoice, |
| 1013 | VoEId(_shared->instance_id(), -1), |
| 1014 | "VoEBaseImpl::StopPlayout()"); |
| 1015 | // Stop audio-device playing if no channel is playing out |
| 1016 | if (_shared->NumOfSendingChannels() == 0) { |
| 1017 | if (_shared->audio_device()->StopPlayout() != 0) { |
| 1018 | _shared->SetLastError(VE_CANNOT_STOP_PLAYOUT, |
| 1019 | kTraceError, |
| 1020 | "StopPlayout() failed to stop playout"); |
| 1021 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1022 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 1023 | } |
| 1024 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1027 | int32_t VoEBaseImpl::StartSend() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1028 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1029 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1030 | "VoEBaseImpl::StartSend()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1031 | if (_shared->audio_device()->Recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1032 | { |
| 1033 | return 0; |
| 1034 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1035 | if (!_shared->ext_recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1036 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1037 | if (_shared->audio_device()->InitRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1038 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1039 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1040 | VoEId(_shared->instance_id(), -1), |
| 1041 | "StartSend() failed to initialize recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1042 | return -1; |
| 1043 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1044 | if (_shared->audio_device()->StartRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1045 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1046 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1047 | VoEId(_shared->instance_id(), -1), |
| 1048 | "StartSend() failed to start recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1049 | return -1; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1056 | int32_t VoEBaseImpl::StopSend() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1057 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1058 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1059 | "VoEBaseImpl::StopSend()"); |
| 1060 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1061 | if (_shared->NumOfSendingChannels() == 0 && |
| 1062 | !_shared->transmit_mixer()->IsRecordingMic()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1063 | { |
| 1064 | // Stop audio-device recording if no channel is recording |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1065 | if (_shared->audio_device()->StopRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1066 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1067 | _shared->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError, |
| 1068 | "StopSend() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1069 | return -1; |
| 1070 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1071 | _shared->transmit_mixer()->StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1077 | int32_t VoEBaseImpl::TerminateInternal() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1078 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1079 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1080 | "VoEBaseImpl::TerminateInternal()"); |
| 1081 | |
| 1082 | // Delete any remaining channel objects |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 1083 | _shared->channel_manager().DestroyAllChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1084 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1085 | if (_shared->process_thread()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1086 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1087 | if (_shared->audio_device()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1088 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1089 | if (_shared->process_thread()-> |
| 1090 | DeRegisterModule(_shared->audio_device()) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1091 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1092 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 1093 | "TerminateInternal() failed to deregister ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1096 | if (_shared->process_thread()->Stop() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1097 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1098 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 1099 | "TerminateInternal() failed to stop module process thread"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 1103 | if (_shared->audio_device()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1104 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1105 | if (_shared->audio_device()->StopPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1106 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1107 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 1108 | "TerminateInternal() failed to stop playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1109 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1110 | if (_shared->audio_device()->StopRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1111 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1112 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 1113 | "TerminateInternal() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1114 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1115 | if (_shared->audio_device()->RegisterEventObserver(NULL) != 0) { |
| 1116 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 1117 | "TerminateInternal() failed to de-register event observer " |
| 1118 | "for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 1119 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1120 | if (_shared->audio_device()->RegisterAudioCallback(NULL) != 0) { |
| 1121 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 1122 | "TerminateInternal() failed to de-register audio callback " |
| 1123 | "for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 1124 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1125 | if (_shared->audio_device()->Terminate() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1126 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1127 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 1128 | "TerminateInternal() failed to terminate the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1129 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1130 | _shared->set_audio_device(NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 1133 | if (_shared->audio_processing()) { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1134 | _shared->set_audio_processing(NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1137 | return _shared->statistics().SetUnInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 1140 | int VoEBaseImpl::ProcessRecordedDataWithAPM( |
| 1141 | const int voe_channels[], |
| 1142 | int number_of_voe_channels, |
| 1143 | const void* audio_data, |
| 1144 | uint32_t sample_rate, |
| 1145 | uint8_t number_of_channels, |
| 1146 | uint32_t number_of_frames, |
| 1147 | uint32_t audio_delay_milliseconds, |
| 1148 | int32_t clock_drift, |
| 1149 | uint32_t current_volume, |
| 1150 | bool key_pressed) { |
| 1151 | assert(_shared->transmit_mixer() != NULL); |
| 1152 | assert(_shared->audio_device() != NULL); |
| 1153 | |
| 1154 | bool is_analog_agc(false); |
| 1155 | if (_shared->audio_processing() && |
| 1156 | _shared->audio_processing()->gain_control()->mode() == |
| 1157 | GainControl::kAdaptiveAnalog) { |
| 1158 | is_analog_agc = true; |
| 1159 | } |
| 1160 | |
| 1161 | // Only deal with the volume in adaptive analog mode. |
| 1162 | uint32_t max_volume = 0; |
| 1163 | uint16_t current_voe_mic_level = 0; |
| 1164 | if (is_analog_agc) { |
| 1165 | // Scale from ADM to VoE level range |
| 1166 | if (_shared->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) { |
| 1167 | if (max_volume) { |
| 1168 | current_voe_mic_level = static_cast<uint16_t>( |
| 1169 | (current_volume * kMaxVolumeLevel + |
| 1170 | static_cast<int>(max_volume / 2)) / max_volume); |
| 1171 | } |
| 1172 | } |
| 1173 | // We learned that on certain systems (e.g Linux) the current_voe_mic_level |
| 1174 | // can be greater than the maxVolumeLevel therefore |
| 1175 | // we are going to cap the current_voe_mic_level to the maxVolumeLevel |
| 1176 | // and change the maxVolume to current_volume if it turns out that |
| 1177 | // the current_voe_mic_level is indeed greater than the maxVolumeLevel. |
| 1178 | if (current_voe_mic_level > kMaxVolumeLevel) { |
| 1179 | current_voe_mic_level = kMaxVolumeLevel; |
| 1180 | max_volume = current_volume; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | // Keep track if the MicLevel has been changed by the AGC, if not, |
| 1185 | // use the old value AGC returns to let AGC continue its trend, |
| 1186 | // so eventually the AGC is able to change the mic level. This handles |
| 1187 | // issues with truncation introduced by the scaling. |
| 1188 | if (_oldMicLevel == current_volume) |
| 1189 | current_voe_mic_level = static_cast<uint16_t>(_oldVoEMicLevel); |
| 1190 | |
| 1191 | // Perform channel-independent operations |
| 1192 | // (APM, mix with file, record to file, mute, etc.) |
| 1193 | _shared->transmit_mixer()->PrepareDemux( |
| 1194 | audio_data, number_of_frames, number_of_channels, sample_rate, |
| 1195 | static_cast<uint16_t>(audio_delay_milliseconds), clock_drift, |
| 1196 | current_voe_mic_level, key_pressed); |
| 1197 | |
| 1198 | // Copy the audio frame to each sending channel and perform |
| 1199 | // channel-dependent operations (file mixing, mute, etc.), encode and |
| 1200 | // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0, |
| 1201 | // do the operations on all the existing VoE channels; otherwise the |
| 1202 | // operations will be done on specific channels. |
| 1203 | if (number_of_voe_channels == 0) { |
| 1204 | _shared->transmit_mixer()->DemuxAndMix(); |
| 1205 | _shared->transmit_mixer()->EncodeAndSend(); |
| 1206 | } else { |
| 1207 | _shared->transmit_mixer()->DemuxAndMix(voe_channels, |
| 1208 | number_of_voe_channels); |
| 1209 | _shared->transmit_mixer()->EncodeAndSend(voe_channels, |
| 1210 | number_of_voe_channels); |
| 1211 | } |
| 1212 | |
| 1213 | if (!is_analog_agc) |
| 1214 | return 0; |
| 1215 | |
| 1216 | // Scale from VoE to ADM level range. |
| 1217 | uint32_t new_voe_mic_level = _shared->transmit_mixer()->CaptureLevel(); |
| 1218 | |
| 1219 | // Keep track of the value AGC returns. |
| 1220 | _oldVoEMicLevel = new_voe_mic_level; |
| 1221 | _oldMicLevel = current_volume; |
| 1222 | |
| 1223 | if (new_voe_mic_level != current_voe_mic_level) { |
| 1224 | // Return the new volume if AGC has changed the volume. |
| 1225 | return static_cast<int>( |
| 1226 | (new_voe_mic_level * max_volume + |
| 1227 | static_cast<int>(kMaxVolumeLevel / 2)) / kMaxVolumeLevel); |
| 1228 | } |
| 1229 | |
| 1230 | // Return 0 to indicate no change on the volume. |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 1234 | } // namespace webrtc |