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) { |
| 226 | voe::ScopedChannel sc(_shared->channel_manager(), voe_channels[i]); |
| 227 | voe::Channel* channel_ptr = sc.ChannelPtr(); |
| 228 | if (!channel_ptr) |
| 229 | continue; |
| 230 | |
| 231 | if (channel_ptr->InputIsOnHold()) { |
| 232 | channel_ptr->UpdateLocalTimeStamp(); |
| 233 | } else if (channel_ptr->Sending()) { |
| 234 | channel_ptr->Demultiplex(audio_data, sample_rate, number_of_frames, |
| 235 | number_of_channels); |
| 236 | channel_ptr->PrepareEncodeAndSend(sample_rate); |
| 237 | channel_ptr->EncodeAndSend(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Return 0 to indicate no need to change the volume. |
| 242 | return 0; |
| 243 | } |
| 244 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) |
| 246 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 247 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | "RegisterVoiceEngineObserver(observer=0x%d)", &observer); |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 249 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | if (_voiceEngineObserverPtr) |
| 251 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 252 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 253 | "RegisterVoiceEngineObserver() observer already enabled"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | // Register the observer in all active channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 258 | voe::ScopedChannel sc(_shared->channel_manager()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | void* iterator(NULL); |
| 260 | voe::Channel* channelPtr = sc.GetFirstChannel(iterator); |
| 261 | while (channelPtr != NULL) |
| 262 | { |
| 263 | channelPtr->RegisterVoiceEngineObserver(observer); |
| 264 | channelPtr = sc.GetNextChannel(iterator); |
| 265 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 266 | _shared->transmit_mixer()->RegisterVoiceEngineObserver(observer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 267 | |
| 268 | _voiceEngineObserverPtr = &observer; |
| 269 | _voiceEngineObserver = true; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int VoEBaseImpl::DeRegisterVoiceEngineObserver() |
| 275 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 276 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 277 | "DeRegisterVoiceEngineObserver()"); |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 278 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | if (!_voiceEngineObserverPtr) |
| 280 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 281 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 282 | "DeRegisterVoiceEngineObserver() observer already disabled"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | _voiceEngineObserver = false; |
| 287 | _voiceEngineObserverPtr = NULL; |
| 288 | |
| 289 | // Deregister the observer in all active channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 290 | voe::ScopedChannel sc(_shared->channel_manager()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 291 | void* iterator(NULL); |
| 292 | voe::Channel* channelPtr = sc.GetFirstChannel(iterator); |
| 293 | while (channelPtr != NULL) |
| 294 | { |
| 295 | channelPtr->DeRegisterVoiceEngineObserver(); |
| 296 | channelPtr = sc.GetNextChannel(iterator); |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 302 | int VoEBaseImpl::Init(AudioDeviceModule* external_adm, |
| 303 | AudioProcessing* audioproc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 304 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 305 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 306 | "Init(external_adm=0x%p)", external_adm); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 307 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 308 | |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 309 | WebRtcSpl_Init(); |
| 310 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 311 | if (_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 312 | { |
| 313 | return 0; |
| 314 | } |
| 315 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 316 | if (_shared->process_thread()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 318 | if (_shared->process_thread()->Start() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 319 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 320 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 321 | "Init() failed to start module process thread"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | return -1; |
| 323 | } |
| 324 | } |
| 325 | |
andrew@webrtc.org | 3192d65 | 2011-12-21 18:00:59 +0000 | [diff] [blame] | 326 | // 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] | 327 | // ADM implementation as input to Init(). |
| 328 | if (external_adm == NULL) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 329 | { |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 330 | // Create the internal ADM implementation. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 331 | _shared->set_audio_device(AudioDeviceModuleImpl::Create( |
| 332 | VoEId(_shared->instance_id(), -1), _shared->audio_device_layer())); |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 333 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 334 | if (_shared->audio_device() == NULL) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 335 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 336 | _shared->SetLastError(VE_NO_MEMORY, kTraceCritical, |
| 337 | "Init() failed to create the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 338 | return -1; |
| 339 | } |
| 340 | } |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 341 | else |
| 342 | { |
| 343 | // Use the already existing external ADM implementation. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 344 | _shared->set_audio_device(external_adm); |
| 345 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 346 | "An external ADM implementation will be used in VoiceEngine"); |
| 347 | } |
| 348 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 349 | // Register the ADM to the process thread, which will drive the error |
| 350 | // callback mechanism |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 351 | if (_shared->process_thread() && |
| 352 | _shared->process_thread()->RegisterModule(_shared->audio_device()) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 353 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 354 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 355 | "Init() failed to register the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | bool available(false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 360 | |
| 361 | // -------------------- |
| 362 | // Reinitialize the ADM |
| 363 | |
| 364 | // Register the AudioObserver implementation |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 365 | if (_shared->audio_device()->RegisterEventObserver(this) != 0) { |
| 366 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 367 | "Init() failed to register event observer for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 368 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 369 | |
| 370 | // Register the AudioTransport implementation |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 371 | if (_shared->audio_device()->RegisterAudioCallback(this) != 0) { |
| 372 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 373 | "Init() failed to register audio callback for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 374 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 375 | |
| 376 | // ADM initialization |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 377 | if (_shared->audio_device()->Init() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 378 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 379 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 380 | "Init() failed to initialize the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | // Initialize the default speaker |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 385 | if (_shared->audio_device()->SetPlayoutDevice( |
| 386 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 387 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 388 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 389 | "Init() failed to set the default output device"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 391 | if (_shared->audio_device()->SpeakerIsAvailable(&available) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 393 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 394 | "Init() failed to check speaker availability, trying to " |
| 395 | "initialize speaker anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 396 | } |
| 397 | else if (!available) |
| 398 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 399 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 400 | "Init() speaker not available, trying to initialize speaker " |
| 401 | "anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 403 | if (_shared->audio_device()->InitSpeaker() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 404 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 405 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 406 | "Init() failed to initialize the speaker"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Initialize the default microphone |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 410 | if (_shared->audio_device()->SetRecordingDevice( |
| 411 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 412 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 413 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 414 | "Init() failed to set the default input device"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 415 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 416 | if (_shared->audio_device()->MicrophoneIsAvailable(&available) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 418 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 419 | "Init() failed to check microphone availability, trying to " |
| 420 | "initialize microphone anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 421 | } |
| 422 | else if (!available) |
| 423 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 424 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 425 | "Init() microphone not available, trying to initialize " |
| 426 | "microphone anyway"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 427 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 428 | if (_shared->audio_device()->InitMicrophone() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 429 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 430 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
andrew@webrtc.org | f458916 | 2011-10-03 15:22:28 +0000 | [diff] [blame] | 431 | "Init() failed to initialize the microphone"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | } |
| 433 | |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 434 | // Set number of channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 435 | if (_shared->audio_device()->StereoPlayoutIsAvailable(&available) != 0) { |
| 436 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 437 | "Init() failed to query stereo playout mode"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 438 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 439 | if (_shared->audio_device()->SetStereoPlayout(available) != 0) |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 440 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 441 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 442 | "Init() failed to set mono/stereo playout mode"); |
| 443 | } |
andrew@webrtc.org | 3192d65 | 2011-12-21 18:00:59 +0000 | [diff] [blame] | 444 | |
| 445 | // TODO(andrew): These functions don't tell us whether stereo recording |
| 446 | // is truly available. We simply set the AudioProcessing input to stereo |
| 447 | // here, because we have to wait until receiving the first frame to |
| 448 | // determine the actual number of channels anyway. |
| 449 | // |
| 450 | // These functions may be changed; tracked here: |
| 451 | // http://code.google.com/p/webrtc/issues/detail?id=204 |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 452 | _shared->audio_device()->StereoRecordingIsAvailable(&available); |
| 453 | if (_shared->audio_device()->SetStereoRecording(available) != 0) |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 454 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 455 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 456 | "Init() failed to set mono/stereo recording mode"); |
| 457 | } |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 458 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 459 | if (!audioproc) { |
| 460 | audioproc = AudioProcessing::Create(VoEId(_shared->instance_id(), -1)); |
| 461 | if (!audioproc) { |
| 462 | LOG(LS_ERROR) << "Failed to create AudioProcessing."; |
| 463 | _shared->SetLastError(VE_NO_MEMORY); |
| 464 | return -1; |
| 465 | } |
| 466 | } |
| 467 | _shared->set_audio_processing(audioproc); |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 468 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 469 | // Set the error state for any failures in this block. |
| 470 | _shared->SetLastError(VE_APM_ERROR); |
| 471 | if (audioproc->echo_cancellation()->set_device_sample_rate_hz(48000)) { |
| 472 | LOG_FERR1(LS_ERROR, set_device_sample_rate_hz, 48000); |
| 473 | return -1; |
| 474 | } |
| 475 | // Assume 16 kHz mono until the audio frames are received from the capture |
| 476 | // device, at which point this can be updated. |
| 477 | if (audioproc->set_sample_rate_hz(16000)) { |
| 478 | LOG_FERR1(LS_ERROR, set_sample_rate_hz, 16000); |
| 479 | return -1; |
| 480 | } |
| 481 | if (audioproc->set_num_channels(1, 1) != 0) { |
| 482 | LOG_FERR2(LS_ERROR, set_num_channels, 1, 1); |
| 483 | return -1; |
| 484 | } |
| 485 | if (audioproc->set_num_reverse_channels(1) != 0) { |
| 486 | LOG_FERR1(LS_ERROR, set_num_reverse_channels, 1); |
| 487 | return -1; |
niklas.enbom@webrtc.org | e33a102 | 2011-11-16 10:33:53 +0000 | [diff] [blame] | 488 | } |
| 489 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 490 | // Configure AudioProcessing components. All are disabled by default. |
| 491 | if (audioproc->high_pass_filter()->Enable(true) != 0) { |
| 492 | LOG_FERR1(LS_ERROR, high_pass_filter()->Enable, true); |
| 493 | return -1; |
| 494 | } |
| 495 | if (audioproc->echo_cancellation()->enable_drift_compensation(false) != 0) { |
| 496 | LOG_FERR1(LS_ERROR, enable_drift_compensation, false); |
| 497 | return -1; |
| 498 | } |
| 499 | if (audioproc->noise_suppression()->set_level(kDefaultNsMode) != 0) { |
| 500 | LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode); |
| 501 | return -1; |
| 502 | } |
| 503 | GainControl* agc = audioproc->gain_control(); |
| 504 | if (agc->set_analog_level_limits(kMinVolumeLevel, kMaxVolumeLevel) != 0) { |
| 505 | LOG_FERR2(LS_ERROR, agc->set_analog_level_limits, kMinVolumeLevel, |
| 506 | kMaxVolumeLevel); |
| 507 | return -1; |
| 508 | } |
| 509 | if (agc->set_mode(kDefaultAgcMode) != 0) { |
| 510 | LOG_FERR1(LS_ERROR, agc->set_mode, kDefaultAgcMode); |
| 511 | return -1; |
| 512 | } |
| 513 | if (agc->Enable(kDefaultAgcState) != 0) { |
| 514 | LOG_FERR1(LS_ERROR, agc->Enable, kDefaultAgcState); |
| 515 | return -1; |
| 516 | } |
| 517 | _shared->SetLastError(0); // Clear error state. |
| 518 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | #ifdef WEBRTC_VOICE_ENGINE_AGC |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 520 | bool agc_enabled = agc->mode() == GainControl::kAdaptiveAnalog && |
| 521 | agc->is_enabled(); |
| 522 | if (_shared->audio_device()->SetAGC(agc_enabled) != 0) { |
| 523 | LOG_FERR1(LS_ERROR, audio_device()->SetAGC, agc_enabled); |
| 524 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR); |
andrew@webrtc.org | a9a1df0 | 2013-03-05 23:36:10 +0000 | [diff] [blame] | 525 | // TODO(ajm): No error return here due to |
| 526 | // https://code.google.com/p/webrtc/issues/detail?id=1464 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 527 | } |
| 528 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 529 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 530 | return _shared->statistics().SetInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | int VoEBaseImpl::Terminate() |
| 534 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 535 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 536 | "Terminate()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 537 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 538 | return TerminateInternal(); |
| 539 | } |
| 540 | |
| 541 | int VoEBaseImpl::MaxNumOfChannels() |
| 542 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 543 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 544 | "MaxNumOfChannels()"); |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 545 | int32_t maxNumOfChannels = |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 546 | _shared->channel_manager().MaxNumOfChannels(); |
| 547 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 548 | VoEId(_shared->instance_id(), -1), |
| 549 | "MaxNumOfChannels() => %d", maxNumOfChannels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | return (maxNumOfChannels); |
| 551 | } |
| 552 | |
| 553 | int VoEBaseImpl::CreateChannel() |
| 554 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 555 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 556 | "CreateChannel()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 557 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 558 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 559 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 560 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 561 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 562 | return -1; |
| 563 | } |
| 564 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 565 | int32_t channelId = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 566 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 567 | if (!_shared->channel_manager().CreateChannel(channelId)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 568 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 569 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 570 | "CreateChannel() failed to allocate memory for channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | bool destroyChannel(false); |
| 575 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 576 | voe::ScopedChannel sc(_shared->channel_manager(), channelId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 578 | if (channelPtr == NULL) |
| 579 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 580 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 581 | "CreateChannel() failed to allocate memory for channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 582 | return -1; |
| 583 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 584 | else if (channelPtr->SetEngineInformation(_shared->statistics(), |
| 585 | *_shared->output_mixer(), |
| 586 | *_shared->transmit_mixer(), |
| 587 | *_shared->process_thread(), |
| 588 | *_shared->audio_device(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 589 | _voiceEngineObserverPtr, |
| 590 | &_callbackCritSect) != 0) |
| 591 | { |
| 592 | destroyChannel = true; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 593 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 594 | "CreateChannel() failed to associate engine and channel." |
| 595 | " Destroying channel."); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 596 | } |
| 597 | else if (channelPtr->Init() != 0) |
| 598 | { |
| 599 | destroyChannel = true; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 600 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 601 | "CreateChannel() failed to initialize channel. Destroying" |
| 602 | " channel."); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | if (destroyChannel) |
| 606 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 607 | _shared->channel_manager().DestroyChannel(channelId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 608 | return -1; |
| 609 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 610 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 611 | VoEId(_shared->instance_id(), -1), |
| 612 | "CreateChannel() => %d", channelId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 613 | return channelId; |
| 614 | } |
| 615 | |
| 616 | int VoEBaseImpl::DeleteChannel(int channel) |
| 617 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 618 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 619 | "DeleteChannel(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 620 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 622 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 623 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 624 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 629 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 630 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 631 | if (channelPtr == NULL) |
| 632 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 633 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 634 | "DeleteChannel() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | } |
| 638 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 639 | if (_shared->channel_manager().DestroyChannel(channel) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 640 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 641 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 642 | "DeleteChannel() failed to destroy channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 643 | return -1; |
| 644 | } |
| 645 | |
| 646 | if (StopSend() != 0) |
| 647 | { |
| 648 | return -1; |
| 649 | } |
| 650 | |
| 651 | if (StopPlayout() != 0) |
| 652 | { |
| 653 | return -1; |
| 654 | } |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 655 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 659 | int VoEBaseImpl::StartReceive(int channel) |
| 660 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 661 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 662 | "StartReceive(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 663 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 664 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 665 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 666 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 667 | return -1; |
| 668 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 669 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 670 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 671 | if (channelPtr == NULL) |
| 672 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 673 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 674 | "StartReceive() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 675 | return -1; |
| 676 | } |
| 677 | return channelPtr->StartReceiving(); |
| 678 | } |
| 679 | |
| 680 | int VoEBaseImpl::StopReceive(int channel) |
| 681 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 682 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 683 | "StopListen(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 684 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 685 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 686 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 687 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 688 | return -1; |
| 689 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 690 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 691 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 692 | if (channelPtr == NULL) |
| 693 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 694 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 695 | "SetLocalReceiver() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 696 | return -1; |
| 697 | } |
| 698 | return channelPtr->StopReceiving(); |
| 699 | } |
| 700 | |
| 701 | int VoEBaseImpl::StartPlayout(int channel) |
| 702 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 703 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 704 | "StartPlayout(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 705 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 706 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 707 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 708 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 709 | return -1; |
| 710 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 711 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 712 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 713 | if (channelPtr == NULL) |
| 714 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 715 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 716 | "StartPlayout() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 717 | return -1; |
| 718 | } |
| 719 | if (channelPtr->Playing()) |
| 720 | { |
| 721 | return 0; |
| 722 | } |
| 723 | if (StartPlayout() != 0) |
| 724 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 725 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 726 | "StartPlayout() failed to start playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 727 | return -1; |
| 728 | } |
| 729 | return channelPtr->StartPlayout(); |
| 730 | } |
| 731 | |
| 732 | int VoEBaseImpl::StopPlayout(int channel) |
| 733 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 734 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 735 | "StopPlayout(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 736 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 737 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 738 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 739 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 740 | return -1; |
| 741 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 742 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 743 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 744 | if (channelPtr == NULL) |
| 745 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 746 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 747 | "StopPlayout() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 748 | return -1; |
| 749 | } |
| 750 | if (channelPtr->StopPlayout() != 0) |
| 751 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 752 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, |
| 753 | VoEId(_shared->instance_id(), -1), |
| 754 | "StopPlayout() failed to stop playout for channel %d", channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 755 | } |
| 756 | return StopPlayout(); |
| 757 | } |
| 758 | |
| 759 | int VoEBaseImpl::StartSend(int channel) |
| 760 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 761 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 762 | "StartSend(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 763 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 764 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 765 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 766 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 767 | return -1; |
| 768 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 769 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 770 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 771 | if (channelPtr == NULL) |
| 772 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 773 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 774 | "StartSend() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 775 | return -1; |
| 776 | } |
| 777 | if (channelPtr->Sending()) |
| 778 | { |
| 779 | return 0; |
| 780 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 781 | if (StartSend() != 0) |
| 782 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 783 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 784 | "StartSend() failed to start recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 785 | return -1; |
| 786 | } |
| 787 | return channelPtr->StartSend(); |
| 788 | } |
| 789 | |
| 790 | int VoEBaseImpl::StopSend(int channel) |
| 791 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 792 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 793 | "StopSend(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 794 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 795 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 796 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 797 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 798 | return -1; |
| 799 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 800 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 801 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 802 | if (channelPtr == NULL) |
| 803 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 804 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 805 | "StopSend() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 806 | return -1; |
| 807 | } |
| 808 | if (channelPtr->StopSend() != 0) |
| 809 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 810 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, |
| 811 | VoEId(_shared->instance_id(), -1), |
| 812 | "StopSend() failed to stop sending for channel %d", channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 813 | } |
| 814 | return StopSend(); |
| 815 | } |
| 816 | |
| 817 | int VoEBaseImpl::GetVersion(char version[1024]) |
| 818 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 819 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 820 | "GetVersion(version=?)"); |
| 821 | assert(kVoiceEngineVersionMaxMessageSize == 1024); |
| 822 | |
| 823 | if (version == NULL) |
| 824 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 825 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 826 | return (-1); |
| 827 | } |
| 828 | |
| 829 | char versionBuf[kVoiceEngineVersionMaxMessageSize]; |
| 830 | char* versionPtr = versionBuf; |
| 831 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 832 | int32_t len = 0; |
| 833 | int32_t accLen = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 834 | |
| 835 | len = AddVoEVersion(versionPtr); |
| 836 | if (len == -1) |
| 837 | { |
| 838 | return -1; |
| 839 | } |
| 840 | versionPtr += len; |
| 841 | accLen += len; |
| 842 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 843 | |
| 844 | len = AddBuildInfo(versionPtr); |
| 845 | if (len == -1) |
| 846 | { |
| 847 | return -1; |
| 848 | } |
| 849 | versionPtr += len; |
| 850 | accLen += len; |
| 851 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 852 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 853 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
| 854 | len = AddExternalTransportBuild(versionPtr); |
| 855 | if (len == -1) |
| 856 | { |
| 857 | return -1; |
| 858 | } |
| 859 | versionPtr += len; |
| 860 | accLen += len; |
| 861 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 862 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 863 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
| 864 | len = AddExternalRecAndPlayoutBuild(versionPtr); |
| 865 | if (len == -1) |
| 866 | { |
| 867 | return -1; |
| 868 | } |
| 869 | versionPtr += len; |
| 870 | accLen += len; |
| 871 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
pwestin@webrtc.org | c450a19 | 2012-01-04 15:00:12 +0000 | [diff] [blame] | 872 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 873 | |
| 874 | memcpy(version, versionBuf, accLen); |
| 875 | version[accLen] = '\0'; |
| 876 | |
| 877 | // to avoid the truncation in the trace, split the string into parts |
| 878 | char partOfVersion[256]; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 879 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 880 | VoEId(_shared->instance_id(), -1), "GetVersion() =>"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 881 | for (int partStart = 0; partStart < accLen;) |
| 882 | { |
| 883 | memset(partOfVersion, 0, sizeof(partOfVersion)); |
| 884 | int partEnd = partStart + 180; |
| 885 | while (version[partEnd] != '\n' && version[partEnd] != '\0') |
| 886 | { |
| 887 | partEnd--; |
| 888 | } |
| 889 | if (partEnd < accLen) |
| 890 | { |
| 891 | memcpy(partOfVersion, &version[partStart], partEnd - partStart); |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | memcpy(partOfVersion, &version[partStart], accLen - partStart); |
| 896 | } |
| 897 | partStart = partEnd; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 898 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 899 | VoEId(_shared->instance_id(), -1), "%s", partOfVersion); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 905 | int32_t VoEBaseImpl::AddBuildInfo(char* str) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 906 | { |
leozwang@webrtc.org | 48a5df6 | 2012-04-24 14:50:50 +0000 | [diff] [blame] | 907 | return sprintf(str, "Build: svn:%s %s\n", WEBRTC_SVNREVISION, BUILDINFO); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 908 | } |
| 909 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 910 | int32_t VoEBaseImpl::AddVoEVersion(char* str) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 911 | { |
| 912 | return sprintf(str, "VoiceEngine 4.1.0\n"); |
| 913 | } |
| 914 | |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 915 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 916 | int32_t VoEBaseImpl::AddExternalTransportBuild(char* str) const |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 917 | { |
| 918 | return sprintf(str, "External transport build\n"); |
| 919 | } |
| 920 | #endif |
| 921 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 922 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 923 | int32_t VoEBaseImpl::AddExternalRecAndPlayoutBuild(char* str) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 924 | { |
| 925 | return sprintf(str, "External recording and playout build\n"); |
| 926 | } |
| 927 | #endif |
| 928 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 929 | int VoEBaseImpl::LastError() |
| 930 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 931 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 932 | "LastError()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 933 | return (_shared->statistics().LastError()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | |
| 937 | int VoEBaseImpl::SetNetEQPlayoutMode(int channel, NetEqModes mode) |
| 938 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 939 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 940 | "SetNetEQPlayoutMode(channel=%i, mode=%i)", channel, mode); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 941 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 942 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 943 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 944 | return -1; |
| 945 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 946 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 947 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 948 | if (channelPtr == NULL) |
| 949 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 950 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 951 | "SetNetEQPlayoutMode() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 952 | return -1; |
| 953 | } |
| 954 | return channelPtr->SetNetEQPlayoutMode(mode); |
| 955 | } |
| 956 | |
| 957 | int VoEBaseImpl::GetNetEQPlayoutMode(int channel, NetEqModes& mode) |
| 958 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 959 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 960 | "GetNetEQPlayoutMode(channel=%i, mode=?)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 961 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 962 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 963 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 964 | return -1; |
| 965 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 966 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 967 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 968 | if (channelPtr == NULL) |
| 969 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 970 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 971 | "GetNetEQPlayoutMode() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 972 | return -1; |
| 973 | } |
| 974 | return channelPtr->GetNetEQPlayoutMode(mode); |
| 975 | } |
| 976 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 977 | int VoEBaseImpl::SetOnHoldStatus(int channel, bool enable, OnHoldModes mode) |
| 978 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 979 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 980 | "SetOnHoldStatus(channel=%d, enable=%d, mode=%d)", channel, |
| 981 | enable, mode); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 982 | if (!_shared->statistics().Initialized()) |
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 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 985 | return -1; |
| 986 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 987 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 988 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 989 | if (channelPtr == NULL) |
| 990 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 991 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 992 | "SetOnHoldStatus() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 993 | return -1; |
| 994 | } |
| 995 | return channelPtr->SetOnHoldStatus(enable, mode); |
| 996 | } |
| 997 | |
| 998 | int VoEBaseImpl::GetOnHoldStatus(int channel, bool& enabled, OnHoldModes& mode) |
| 999 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1000 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1001 | "GetOnHoldStatus(channel=%d, enabled=?, mode=?)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1002 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1003 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1004 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1005 | return -1; |
| 1006 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1007 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1008 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1009 | if (channelPtr == NULL) |
| 1010 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1011 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1012 | "GetOnHoldStatus() failed to locate channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1013 | return -1; |
| 1014 | } |
| 1015 | return channelPtr->GetOnHoldStatus(enabled, mode); |
| 1016 | } |
| 1017 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1018 | int32_t VoEBaseImpl::StartPlayout() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1019 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1020 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1021 | "VoEBaseImpl::StartPlayout()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1022 | if (_shared->audio_device()->Playing()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1023 | { |
| 1024 | return 0; |
| 1025 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1026 | if (!_shared->ext_playout()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1027 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1028 | if (_shared->audio_device()->InitPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1029 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1030 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1031 | VoEId(_shared->instance_id(), -1), |
| 1032 | "StartPlayout() failed to initialize playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1033 | return -1; |
| 1034 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1035 | if (_shared->audio_device()->StartPlayout() != 0) |
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 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1038 | VoEId(_shared->instance_id(), -1), |
| 1039 | "StartPlayout() failed to start playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1040 | return -1; |
| 1041 | } |
| 1042 | } |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1046 | int32_t VoEBaseImpl::StopPlayout() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1047 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1048 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1049 | "VoEBaseImpl::StopPlayout()"); |
| 1050 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1051 | int32_t numOfChannels = _shared->channel_manager().NumOfChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1052 | if (numOfChannels <= 0) |
| 1053 | { |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1057 | uint16_t nChannelsPlaying(0); |
| 1058 | int32_t* channelsArray = new int32_t[numOfChannels]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Get number of playing channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1061 | _shared->channel_manager().GetChannelIds(channelsArray, numOfChannels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1062 | for (int i = 0; i < numOfChannels; i++) |
| 1063 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1064 | voe::ScopedChannel sc(_shared->channel_manager(), channelsArray[i]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1065 | voe::Channel* chPtr = sc.ChannelPtr(); |
| 1066 | if (chPtr) |
| 1067 | { |
| 1068 | if (chPtr->Playing()) |
| 1069 | { |
| 1070 | nChannelsPlaying++; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | delete[] channelsArray; |
| 1075 | |
| 1076 | // Stop audio-device playing if no channel is playing out |
| 1077 | if (nChannelsPlaying == 0) |
| 1078 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1079 | if (_shared->audio_device()->StopPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1080 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1081 | _shared->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError, |
| 1082 | "StopPlayout() failed to stop playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1083 | return -1; |
| 1084 | } |
| 1085 | } |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1089 | int32_t VoEBaseImpl::StartSend() |
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 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1092 | "VoEBaseImpl::StartSend()"); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1093 | if (_shared->audio_device()->Recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1094 | { |
| 1095 | return 0; |
| 1096 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1097 | if (!_shared->ext_recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1098 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1099 | if (_shared->audio_device()->InitRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1100 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1101 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1102 | VoEId(_shared->instance_id(), -1), |
| 1103 | "StartSend() failed to initialize recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1104 | return -1; |
| 1105 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1106 | if (_shared->audio_device()->StartRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1107 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1108 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1109 | VoEId(_shared->instance_id(), -1), |
| 1110 | "StartSend() failed to start recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1111 | return -1; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1118 | int32_t VoEBaseImpl::StopSend() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1119 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1120 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1121 | "VoEBaseImpl::StopSend()"); |
| 1122 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1123 | if (_shared->NumOfSendingChannels() == 0 && |
| 1124 | !_shared->transmit_mixer()->IsRecordingMic()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1125 | { |
| 1126 | // Stop audio-device recording if no channel is recording |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1127 | if (_shared->audio_device()->StopRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1128 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1129 | _shared->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError, |
| 1130 | "StopSend() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1131 | return -1; |
| 1132 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1133 | _shared->transmit_mixer()->StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1139 | int32_t VoEBaseImpl::TerminateInternal() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1140 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1141 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1142 | "VoEBaseImpl::TerminateInternal()"); |
| 1143 | |
| 1144 | // Delete any remaining channel objects |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1145 | int32_t numOfChannels = _shared->channel_manager().NumOfChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1146 | if (numOfChannels > 0) |
| 1147 | { |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 1148 | int32_t* channelsArray = new int32_t[numOfChannels]; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1149 | _shared->channel_manager().GetChannelIds(channelsArray, numOfChannels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1150 | for (int i = 0; i < numOfChannels; i++) |
| 1151 | { |
| 1152 | DeleteChannel(channelsArray[i]); |
| 1153 | } |
| 1154 | delete[] channelsArray; |
| 1155 | } |
| 1156 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1157 | if (_shared->process_thread()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1158 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1159 | if (_shared->audio_device()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1160 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1161 | if (_shared->process_thread()-> |
| 1162 | DeRegisterModule(_shared->audio_device()) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1163 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1164 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 1165 | "TerminateInternal() failed to deregister ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1168 | if (_shared->process_thread()->Stop() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1169 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1170 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 1171 | "TerminateInternal() failed to stop module process thread"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 1175 | if (_shared->audio_device()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1176 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1177 | if (_shared->audio_device()->StopPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1178 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1179 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 1180 | "TerminateInternal() failed to stop playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1181 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1182 | if (_shared->audio_device()->StopRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1183 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1184 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 1185 | "TerminateInternal() failed to stop recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1186 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1187 | if (_shared->audio_device()->RegisterEventObserver(NULL) != 0) { |
| 1188 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 1189 | "TerminateInternal() failed to de-register event observer " |
| 1190 | "for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 1191 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1192 | if (_shared->audio_device()->RegisterAudioCallback(NULL) != 0) { |
| 1193 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 1194 | "TerminateInternal() failed to de-register audio callback " |
| 1195 | "for the ADM"); |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 1196 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1197 | if (_shared->audio_device()->Terminate() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1198 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1199 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 1200 | "TerminateInternal() failed to terminate the ADM"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1201 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1202 | _shared->set_audio_device(NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
andrew@webrtc.org | f0a90c3 | 2013-03-05 01:12:49 +0000 | [diff] [blame] | 1205 | if (_shared->audio_processing()) { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1206 | _shared->set_audio_processing(NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 1209 | return _shared->statistics().SetUnInitialized(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 1212 | int VoEBaseImpl::ProcessRecordedDataWithAPM( |
| 1213 | const int voe_channels[], |
| 1214 | int number_of_voe_channels, |
| 1215 | const void* audio_data, |
| 1216 | uint32_t sample_rate, |
| 1217 | uint8_t number_of_channels, |
| 1218 | uint32_t number_of_frames, |
| 1219 | uint32_t audio_delay_milliseconds, |
| 1220 | int32_t clock_drift, |
| 1221 | uint32_t current_volume, |
| 1222 | bool key_pressed) { |
| 1223 | assert(_shared->transmit_mixer() != NULL); |
| 1224 | assert(_shared->audio_device() != NULL); |
| 1225 | |
| 1226 | bool is_analog_agc(false); |
| 1227 | if (_shared->audio_processing() && |
| 1228 | _shared->audio_processing()->gain_control()->mode() == |
| 1229 | GainControl::kAdaptiveAnalog) { |
| 1230 | is_analog_agc = true; |
| 1231 | } |
| 1232 | |
| 1233 | // Only deal with the volume in adaptive analog mode. |
| 1234 | uint32_t max_volume = 0; |
| 1235 | uint16_t current_voe_mic_level = 0; |
| 1236 | if (is_analog_agc) { |
| 1237 | // Scale from ADM to VoE level range |
| 1238 | if (_shared->audio_device()->MaxMicrophoneVolume(&max_volume) == 0) { |
| 1239 | if (max_volume) { |
| 1240 | current_voe_mic_level = static_cast<uint16_t>( |
| 1241 | (current_volume * kMaxVolumeLevel + |
| 1242 | static_cast<int>(max_volume / 2)) / max_volume); |
| 1243 | } |
| 1244 | } |
| 1245 | // We learned that on certain systems (e.g Linux) the current_voe_mic_level |
| 1246 | // can be greater than the maxVolumeLevel therefore |
| 1247 | // we are going to cap the current_voe_mic_level to the maxVolumeLevel |
| 1248 | // and change the maxVolume to current_volume if it turns out that |
| 1249 | // the current_voe_mic_level is indeed greater than the maxVolumeLevel. |
| 1250 | if (current_voe_mic_level > kMaxVolumeLevel) { |
| 1251 | current_voe_mic_level = kMaxVolumeLevel; |
| 1252 | max_volume = current_volume; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // Keep track if the MicLevel has been changed by the AGC, if not, |
| 1257 | // use the old value AGC returns to let AGC continue its trend, |
| 1258 | // so eventually the AGC is able to change the mic level. This handles |
| 1259 | // issues with truncation introduced by the scaling. |
| 1260 | if (_oldMicLevel == current_volume) |
| 1261 | current_voe_mic_level = static_cast<uint16_t>(_oldVoEMicLevel); |
| 1262 | |
| 1263 | // Perform channel-independent operations |
| 1264 | // (APM, mix with file, record to file, mute, etc.) |
| 1265 | _shared->transmit_mixer()->PrepareDemux( |
| 1266 | audio_data, number_of_frames, number_of_channels, sample_rate, |
| 1267 | static_cast<uint16_t>(audio_delay_milliseconds), clock_drift, |
| 1268 | current_voe_mic_level, key_pressed); |
| 1269 | |
| 1270 | // Copy the audio frame to each sending channel and perform |
| 1271 | // channel-dependent operations (file mixing, mute, etc.), encode and |
| 1272 | // packetize+transmit the RTP packet. When |number_of_voe_channels| == 0, |
| 1273 | // do the operations on all the existing VoE channels; otherwise the |
| 1274 | // operations will be done on specific channels. |
| 1275 | if (number_of_voe_channels == 0) { |
| 1276 | _shared->transmit_mixer()->DemuxAndMix(); |
| 1277 | _shared->transmit_mixer()->EncodeAndSend(); |
| 1278 | } else { |
| 1279 | _shared->transmit_mixer()->DemuxAndMix(voe_channels, |
| 1280 | number_of_voe_channels); |
| 1281 | _shared->transmit_mixer()->EncodeAndSend(voe_channels, |
| 1282 | number_of_voe_channels); |
| 1283 | } |
| 1284 | |
| 1285 | if (!is_analog_agc) |
| 1286 | return 0; |
| 1287 | |
| 1288 | // Scale from VoE to ADM level range. |
| 1289 | uint32_t new_voe_mic_level = _shared->transmit_mixer()->CaptureLevel(); |
| 1290 | |
| 1291 | // Keep track of the value AGC returns. |
| 1292 | _oldVoEMicLevel = new_voe_mic_level; |
| 1293 | _oldMicLevel = current_volume; |
| 1294 | |
| 1295 | if (new_voe_mic_level != current_voe_mic_level) { |
| 1296 | // Return the new volume if AGC has changed the volume. |
| 1297 | return static_cast<int>( |
| 1298 | (new_voe_mic_level * max_volume + |
| 1299 | static_cast<int>(kMaxVolumeLevel / 2)) / kMaxVolumeLevel); |
| 1300 | } |
| 1301 | |
| 1302 | // Return 0 to indicate no change on the volume. |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 1306 | } // namespace webrtc |