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