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