andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 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 | |
| 11 | #include "voe_base_impl.h" |
| 12 | |
| 13 | #include "audio_coding_module.h" |
| 14 | #include "audio_processing.h" |
| 15 | #include "channel.h" |
| 16 | #include "critical_section_wrapper.h" |
| 17 | #include "file_wrapper.h" |
| 18 | #include "modules/audio_device/audio_device_impl.h" |
| 19 | #include "output_mixer.h" |
| 20 | #include "signal_processing_library.h" |
| 21 | #include "trace.h" |
| 22 | #include "transmit_mixer.h" |
| 23 | #include "utility.h" |
| 24 | #include "voe_errors.h" |
| 25 | #include "voice_engine_impl.h" |
| 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 | } |
| 43 | VoiceEngineImpl* s = reinterpret_cast<VoiceEngineImpl*>(voiceEngine); |
| 44 | s->AddRef(); |
| 45 | return s; |
| 46 | } |
| 47 | |
| 48 | VoEBaseImpl::VoEBaseImpl(voe::SharedData* shared) : |
| 49 | _voiceEngineObserverPtr(NULL), |
| 50 | _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()), |
| 51 | _voiceEngineObserver(false), _oldVoEMicLevel(0), _oldMicLevel(0), |
| 52 | _shared(shared) |
| 53 | { |
| 54 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 55 | "VoEBaseImpl() - ctor"); |
| 56 | } |
| 57 | |
| 58 | VoEBaseImpl::~VoEBaseImpl() |
| 59 | { |
| 60 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 61 | "~VoEBaseImpl() - dtor"); |
| 62 | |
| 63 | TerminateInternal(); |
| 64 | |
| 65 | delete &_callbackCritSect; |
| 66 | } |
| 67 | |
| 68 | void VoEBaseImpl::OnErrorIsReported(const ErrorCode error) |
| 69 | { |
| 70 | CriticalSectionScoped cs(&_callbackCritSect); |
| 71 | if (_voiceEngineObserver) |
| 72 | { |
| 73 | if (_voiceEngineObserverPtr) |
| 74 | { |
| 75 | int errCode(0); |
| 76 | if (error == AudioDeviceObserver::kRecordingError) |
| 77 | { |
| 78 | errCode = VE_RUNTIME_REC_ERROR; |
| 79 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 80 | VoEId(_shared->instance_id(), -1), |
| 81 | "VoEBaseImpl::OnErrorIsReported() => VE_RUNTIME_REC_ERROR"); |
| 82 | } |
| 83 | else if (error == AudioDeviceObserver::kPlayoutError) |
| 84 | { |
| 85 | errCode = VE_RUNTIME_PLAY_ERROR; |
| 86 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 87 | VoEId(_shared->instance_id(), -1), |
| 88 | "VoEBaseImpl::OnErrorIsReported() => " |
| 89 | "VE_RUNTIME_PLAY_ERROR"); |
| 90 | } |
| 91 | // Deliver callback (-1 <=> no channel dependency) |
| 92 | _voiceEngineObserverPtr->CallbackOnError(-1, errCode); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void VoEBaseImpl::OnWarningIsReported(const WarningCode warning) |
| 98 | { |
| 99 | CriticalSectionScoped cs(&_callbackCritSect); |
| 100 | if (_voiceEngineObserver) |
| 101 | { |
| 102 | if (_voiceEngineObserverPtr) |
| 103 | { |
| 104 | int warningCode(0); |
| 105 | if (warning == AudioDeviceObserver::kRecordingWarning) |
| 106 | { |
| 107 | warningCode = VE_RUNTIME_REC_WARNING; |
| 108 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 109 | VoEId(_shared->instance_id(), -1), |
| 110 | "VoEBaseImpl::OnErrorIsReported() => " |
| 111 | "VE_RUNTIME_REC_WARNING"); |
| 112 | } |
| 113 | else if (warning == AudioDeviceObserver::kPlayoutWarning) |
| 114 | { |
| 115 | warningCode = VE_RUNTIME_PLAY_WARNING; |
| 116 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 117 | VoEId(_shared->instance_id(), -1), |
| 118 | "VoEBaseImpl::OnErrorIsReported() => " |
| 119 | "VE_RUNTIME_PLAY_WARNING"); |
| 120 | } |
| 121 | // Deliver callback (-1 <=> no channel dependency) |
| 122 | _voiceEngineObserverPtr->CallbackOnError(-1, warningCode); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | WebRtc_Word32 VoEBaseImpl::RecordedDataIsAvailable( |
| 128 | const void* audioSamples, |
| 129 | const WebRtc_UWord32 nSamples, |
| 130 | const WebRtc_UWord8 nBytesPerSample, |
| 131 | const WebRtc_UWord8 nChannels, |
| 132 | const WebRtc_UWord32 samplesPerSec, |
| 133 | const WebRtc_UWord32 totalDelayMS, |
| 134 | const WebRtc_Word32 clockDrift, |
| 135 | const WebRtc_UWord32 currentMicLevel, |
| 136 | WebRtc_UWord32& newMicLevel) |
| 137 | { |
| 138 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 139 | "VoEBaseImpl::RecordedDataIsAvailable(nSamples=%u, " |
| 140 | "nBytesPerSample=%u, nChannels=%u, samplesPerSec=%u, " |
| 141 | "totalDelayMS=%u, clockDrift=%d, currentMicLevel=%u)", |
| 142 | nSamples, nBytesPerSample, nChannels, samplesPerSec, |
| 143 | totalDelayMS, clockDrift, currentMicLevel); |
| 144 | |
| 145 | assert(_shared->transmit_mixer() != NULL); |
| 146 | assert(_shared->audio_device() != NULL); |
| 147 | |
| 148 | bool isAnalogAGC(false); |
| 149 | WebRtc_UWord32 maxVolume(0); |
| 150 | WebRtc_UWord16 currentVoEMicLevel(0); |
| 151 | WebRtc_UWord32 newVoEMicLevel(0); |
| 152 | |
| 153 | if (_shared->audio_processing() && |
| 154 | (_shared->audio_processing()->gain_control()->mode() |
| 155 | == GainControl::kAdaptiveAnalog)) |
| 156 | { |
| 157 | isAnalogAGC = true; |
| 158 | } |
| 159 | |
| 160 | // Will only deal with the volume in adaptive analog mode |
| 161 | if (isAnalogAGC) |
| 162 | { |
| 163 | // Scale from ADM to VoE level range |
| 164 | if (_shared->audio_device()->MaxMicrophoneVolume(&maxVolume) == 0) |
| 165 | { |
| 166 | if (0 != maxVolume) |
| 167 | { |
| 168 | currentVoEMicLevel = (WebRtc_UWord16) ((currentMicLevel |
| 169 | * kMaxVolumeLevel + (int) (maxVolume / 2)) |
| 170 | / (maxVolume)); |
| 171 | } |
| 172 | } |
| 173 | // We learned that on certain systems (e.g Linux) the currentVoEMicLevel |
| 174 | // can be greater than the maxVolumeLevel therefore |
| 175 | // we are going to cap the currentVoEMicLevel to the maxVolumeLevel |
| 176 | // and change the maxVolume to currentMicLevel if it turns out that |
| 177 | // the currentVoEMicLevel is indeed greater than the maxVolumeLevel. |
| 178 | if (currentVoEMicLevel > kMaxVolumeLevel) |
| 179 | { |
| 180 | currentVoEMicLevel = kMaxVolumeLevel; |
| 181 | maxVolume = currentMicLevel; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Keep track if the MicLevel has been changed by the AGC, if not, |
| 186 | // use the old value AGC returns to let AGC continue its trend, |
| 187 | // so eventually the AGC is able to change the mic level. This handles |
| 188 | // issues with truncation introduced by the scaling. |
| 189 | if (_oldMicLevel == currentMicLevel) |
| 190 | { |
| 191 | currentVoEMicLevel = (WebRtc_UWord16) _oldVoEMicLevel; |
| 192 | } |
| 193 | |
| 194 | // Perform channel-independent operations |
| 195 | // (APM, mix with file, record to file, mute, etc.) |
| 196 | _shared->transmit_mixer()->PrepareDemux(audioSamples, nSamples, nChannels, |
| 197 | samplesPerSec, static_cast<WebRtc_UWord16>(totalDelayMS), clockDrift, |
| 198 | currentVoEMicLevel); |
| 199 | |
| 200 | // Copy the audio frame to each sending channel and perform |
| 201 | // channel-dependent operations (file mixing, mute, etc.) to prepare |
| 202 | // for encoding. |
| 203 | _shared->transmit_mixer()->DemuxAndMix(); |
| 204 | // Do the encoding and packetize+transmit the RTP packet when encoding |
| 205 | // is done. |
| 206 | _shared->transmit_mixer()->EncodeAndSend(); |
| 207 | |
| 208 | // Will only deal with the volume in adaptive analog mode |
| 209 | if (isAnalogAGC) |
| 210 | { |
| 211 | // Scale from VoE to ADM level range |
| 212 | newVoEMicLevel = _shared->transmit_mixer()->CaptureLevel(); |
| 213 | if (newVoEMicLevel != currentVoEMicLevel) |
| 214 | { |
| 215 | // Add (kMaxVolumeLevel/2) to round the value |
| 216 | newMicLevel = (WebRtc_UWord32) ((newVoEMicLevel * maxVolume |
| 217 | + (int) (kMaxVolumeLevel / 2)) / (kMaxVolumeLevel)); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | // Pass zero if the level is unchanged |
| 222 | newMicLevel = 0; |
| 223 | } |
| 224 | |
| 225 | // Keep track of the value AGC returns |
| 226 | _oldVoEMicLevel = newVoEMicLevel; |
| 227 | _oldMicLevel = currentMicLevel; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | WebRtc_Word32 VoEBaseImpl::NeedMorePlayData( |
| 234 | const WebRtc_UWord32 nSamples, |
| 235 | const WebRtc_UWord8 nBytesPerSample, |
| 236 | const WebRtc_UWord8 nChannels, |
| 237 | const WebRtc_UWord32 samplesPerSec, |
| 238 | void* audioSamples, |
| 239 | WebRtc_UWord32& nSamplesOut) |
| 240 | { |
| 241 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 242 | "VoEBaseImpl::NeedMorePlayData(nSamples=%u, " |
| 243 | "nBytesPerSample=%d, nChannels=%d, samplesPerSec=%u)", |
| 244 | nSamples, nBytesPerSample, nChannels, samplesPerSec); |
| 245 | |
| 246 | assert(_shared->output_mixer() != NULL); |
| 247 | |
| 248 | // TODO(andrew): if the device is running in mono, we should tell the mixer |
| 249 | // here so that it will only request mono from AudioCodingModule. |
| 250 | // Perform mixing of all active participants (channel-based mixing) |
| 251 | _shared->output_mixer()->MixActiveChannels(); |
| 252 | |
| 253 | // Additional operations on the combined signal |
| 254 | _shared->output_mixer()->DoOperationsOnCombinedSignal(); |
| 255 | |
| 256 | // Retrieve the final output mix (resampled to match the ADM) |
| 257 | _shared->output_mixer()->GetMixedAudio(samplesPerSec, nChannels, |
| 258 | &_audioFrame); |
| 259 | |
| 260 | assert(static_cast<int>(nSamples) == _audioFrame.samples_per_channel_); |
| 261 | assert(samplesPerSec == |
| 262 | static_cast<WebRtc_UWord32>(_audioFrame.sample_rate_hz_)); |
| 263 | |
| 264 | // Deliver audio (PCM) samples to the ADM |
| 265 | memcpy( |
| 266 | (WebRtc_Word16*) audioSamples, |
| 267 | (const WebRtc_Word16*) _audioFrame.data_, |
| 268 | sizeof(WebRtc_Word16) * (_audioFrame.samples_per_channel_ |
| 269 | * _audioFrame.num_channels_)); |
| 270 | |
| 271 | nSamplesOut = _audioFrame.samples_per_channel_; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int VoEBaseImpl::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) |
| 277 | { |
| 278 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 279 | "RegisterVoiceEngineObserver(observer=0x%d)", &observer); |
| 280 | CriticalSectionScoped cs(&_callbackCritSect); |
| 281 | if (_voiceEngineObserverPtr) |
| 282 | { |
| 283 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 284 | "RegisterVoiceEngineObserver() observer already enabled"); |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | // Register the observer in all active channels |
| 289 | voe::ScopedChannel sc(_shared->channel_manager()); |
| 290 | void* iterator(NULL); |
| 291 | voe::Channel* channelPtr = sc.GetFirstChannel(iterator); |
| 292 | while (channelPtr != NULL) |
| 293 | { |
| 294 | channelPtr->RegisterVoiceEngineObserver(observer); |
| 295 | channelPtr = sc.GetNextChannel(iterator); |
| 296 | } |
| 297 | _shared->transmit_mixer()->RegisterVoiceEngineObserver(observer); |
| 298 | |
| 299 | _voiceEngineObserverPtr = &observer; |
| 300 | _voiceEngineObserver = true; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | int VoEBaseImpl::DeRegisterVoiceEngineObserver() |
| 306 | { |
| 307 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 308 | "DeRegisterVoiceEngineObserver()"); |
| 309 | CriticalSectionScoped cs(&_callbackCritSect); |
| 310 | if (!_voiceEngineObserverPtr) |
| 311 | { |
| 312 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 313 | "DeRegisterVoiceEngineObserver() observer already disabled"); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | _voiceEngineObserver = false; |
| 318 | _voiceEngineObserverPtr = NULL; |
| 319 | |
| 320 | // Deregister the observer in all active channels |
| 321 | voe::ScopedChannel sc(_shared->channel_manager()); |
| 322 | void* iterator(NULL); |
| 323 | voe::Channel* channelPtr = sc.GetFirstChannel(iterator); |
| 324 | while (channelPtr != NULL) |
| 325 | { |
| 326 | channelPtr->DeRegisterVoiceEngineObserver(); |
| 327 | channelPtr = sc.GetNextChannel(iterator); |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | int VoEBaseImpl::Init(AudioDeviceModule* external_adm) |
| 334 | { |
| 335 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 336 | "Init(external_adm=0x%p)", external_adm); |
| 337 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 338 | |
| 339 | WebRtcSpl_Init(); |
| 340 | |
| 341 | if (_shared->statistics().Initialized()) |
| 342 | { |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | if (_shared->process_thread()) |
| 347 | { |
| 348 | if (_shared->process_thread()->Start() != 0) |
| 349 | { |
| 350 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 351 | "Init() failed to start module process thread"); |
| 352 | return -1; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Create an internal ADM if the user has not added an external |
| 357 | // ADM implementation as input to Init(). |
| 358 | if (external_adm == NULL) |
| 359 | { |
| 360 | // Create the internal ADM implementation. |
| 361 | _shared->set_audio_device(AudioDeviceModuleImpl::Create( |
| 362 | VoEId(_shared->instance_id(), -1), _shared->audio_device_layer())); |
| 363 | |
| 364 | if (_shared->audio_device() == NULL) |
| 365 | { |
| 366 | _shared->SetLastError(VE_NO_MEMORY, kTraceCritical, |
| 367 | "Init() failed to create the ADM"); |
| 368 | return -1; |
| 369 | } |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | // Use the already existing external ADM implementation. |
| 374 | _shared->set_audio_device(external_adm); |
| 375 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 376 | "An external ADM implementation will be used in VoiceEngine"); |
| 377 | } |
| 378 | |
| 379 | // Register the ADM to the process thread, which will drive the error |
| 380 | // callback mechanism |
| 381 | if (_shared->process_thread() && |
| 382 | _shared->process_thread()->RegisterModule(_shared->audio_device()) != 0) |
| 383 | { |
| 384 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 385 | "Init() failed to register the ADM"); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | bool available(false); |
| 390 | |
| 391 | // -------------------- |
| 392 | // Reinitialize the ADM |
| 393 | |
| 394 | // Register the AudioObserver implementation |
| 395 | if (_shared->audio_device()->RegisterEventObserver(this) != 0) { |
| 396 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 397 | "Init() failed to register event observer for the ADM"); |
| 398 | } |
| 399 | |
| 400 | // Register the AudioTransport implementation |
| 401 | if (_shared->audio_device()->RegisterAudioCallback(this) != 0) { |
| 402 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 403 | "Init() failed to register audio callback for the ADM"); |
| 404 | } |
| 405 | |
| 406 | // ADM initialization |
| 407 | if (_shared->audio_device()->Init() != 0) |
| 408 | { |
| 409 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 410 | "Init() failed to initialize the ADM"); |
| 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | // Initialize the default speaker |
| 415 | if (_shared->audio_device()->SetPlayoutDevice( |
| 416 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) |
| 417 | { |
| 418 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceInfo, |
| 419 | "Init() failed to set the default output device"); |
| 420 | } |
| 421 | if (_shared->audio_device()->SpeakerIsAvailable(&available) != 0) |
| 422 | { |
| 423 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
| 424 | "Init() failed to check speaker availability, trying to " |
| 425 | "initialize speaker anyway"); |
| 426 | } |
| 427 | else if (!available) |
| 428 | { |
| 429 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
| 430 | "Init() speaker not available, trying to initialize speaker " |
| 431 | "anyway"); |
| 432 | } |
| 433 | if (_shared->audio_device()->InitSpeaker() != 0) |
| 434 | { |
| 435 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceInfo, |
| 436 | "Init() failed to initialize the speaker"); |
| 437 | } |
| 438 | |
| 439 | // Initialize the default microphone |
| 440 | if (_shared->audio_device()->SetRecordingDevice( |
| 441 | WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) |
| 442 | { |
| 443 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceInfo, |
| 444 | "Init() failed to set the default input device"); |
| 445 | } |
| 446 | if (_shared->audio_device()->MicrophoneIsAvailable(&available) != 0) |
| 447 | { |
| 448 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
| 449 | "Init() failed to check microphone availability, trying to " |
| 450 | "initialize microphone anyway"); |
| 451 | } |
| 452 | else if (!available) |
| 453 | { |
| 454 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
| 455 | "Init() microphone not available, trying to initialize " |
| 456 | "microphone anyway"); |
| 457 | } |
| 458 | if (_shared->audio_device()->InitMicrophone() != 0) |
| 459 | { |
| 460 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceInfo, |
| 461 | "Init() failed to initialize the microphone"); |
| 462 | } |
| 463 | |
| 464 | // Set number of channels |
| 465 | if (_shared->audio_device()->StereoPlayoutIsAvailable(&available) != 0) { |
| 466 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 467 | "Init() failed to query stereo playout mode"); |
| 468 | } |
| 469 | if (_shared->audio_device()->SetStereoPlayout(available) != 0) |
| 470 | { |
| 471 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 472 | "Init() failed to set mono/stereo playout mode"); |
| 473 | } |
| 474 | |
| 475 | // TODO(andrew): These functions don't tell us whether stereo recording |
| 476 | // is truly available. We simply set the AudioProcessing input to stereo |
| 477 | // here, because we have to wait until receiving the first frame to |
| 478 | // determine the actual number of channels anyway. |
| 479 | // |
| 480 | // These functions may be changed; tracked here: |
| 481 | // http://code.google.com/p/webrtc/issues/detail?id=204 |
| 482 | _shared->audio_device()->StereoRecordingIsAvailable(&available); |
| 483 | if (_shared->audio_device()->SetStereoRecording(available) != 0) |
| 484 | { |
| 485 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 486 | "Init() failed to set mono/stereo recording mode"); |
| 487 | } |
| 488 | |
| 489 | // APM initialization done after sound card since we need |
| 490 | // to know if we support stereo recording or not. |
| 491 | |
| 492 | // Create the AudioProcessing Module if it does not exist. |
| 493 | |
| 494 | if (_shared->audio_processing() == NULL) |
| 495 | { |
| 496 | _shared->set_audio_processing(AudioProcessing::Create( |
| 497 | VoEId(_shared->instance_id(), -1))); |
| 498 | if (_shared->audio_processing() == NULL) |
| 499 | { |
| 500 | _shared->SetLastError(VE_NO_MEMORY, kTraceCritical, |
| 501 | "Init() failed to create the AP module"); |
| 502 | return -1; |
| 503 | } |
| 504 | // Ensure that mixers in both directions has access to the created APM |
| 505 | _shared->transmit_mixer()->SetAudioProcessingModule( |
| 506 | _shared->audio_processing()); |
| 507 | _shared->output_mixer()->SetAudioProcessingModule( |
| 508 | _shared->audio_processing()); |
| 509 | |
| 510 | if (_shared->audio_processing()->echo_cancellation()-> |
| 511 | set_device_sample_rate_hz( |
| 512 | kVoiceEngineAudioProcessingDeviceSampleRateHz)) |
| 513 | { |
| 514 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 515 | "Init() failed to set the device sample rate to 48K for AP " |
| 516 | " module"); |
| 517 | return -1; |
| 518 | } |
| 519 | // Using 8 kHz as inital Fs. Might be changed already at first call. |
| 520 | if (_shared->audio_processing()->set_sample_rate_hz(8000)) |
| 521 | { |
| 522 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 523 | "Init() failed to set the sample rate to 8K for AP module"); |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | // Assume mono until the audio frames are received from the capture |
| 528 | // device, at which point this can be updated. |
| 529 | if (_shared->audio_processing()->set_num_channels(1, 1) != 0) |
| 530 | { |
| 531 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceError, |
| 532 | "Init() failed to set channels for the primary audio stream"); |
| 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | if (_shared->audio_processing()->set_num_reverse_channels(1) != 0) |
| 537 | { |
| 538 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceError, |
| 539 | "Init() failed to set channels for the primary audio stream"); |
| 540 | return -1; |
| 541 | } |
| 542 | // high-pass filter |
| 543 | if (_shared->audio_processing()->high_pass_filter()->Enable( |
| 544 | WEBRTC_VOICE_ENGINE_HP_DEFAULT_STATE) != 0) |
| 545 | { |
| 546 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 547 | "Init() failed to set the high-pass filter for AP module"); |
| 548 | return -1; |
| 549 | } |
| 550 | // Echo Cancellation |
| 551 | if (_shared->audio_processing()->echo_cancellation()-> |
| 552 | enable_drift_compensation(false) != 0) |
| 553 | { |
| 554 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 555 | "Init() failed to set drift compensation for AP module"); |
| 556 | return -1; |
| 557 | } |
| 558 | if (_shared->audio_processing()->echo_cancellation()->Enable( |
| 559 | WEBRTC_VOICE_ENGINE_EC_DEFAULT_STATE)) |
| 560 | { |
| 561 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 562 | "Init() failed to set echo cancellation state for AP module"); |
| 563 | return -1; |
| 564 | } |
| 565 | // Noise Reduction |
| 566 | if (_shared->audio_processing()->noise_suppression()->set_level( |
| 567 | (NoiseSuppression::Level) WEBRTC_VOICE_ENGINE_NS_DEFAULT_MODE) |
| 568 | != 0) |
| 569 | { |
| 570 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 571 | "Init() failed to set noise reduction level for AP module"); |
| 572 | return -1; |
| 573 | } |
| 574 | if (_shared->audio_processing()->noise_suppression()->Enable( |
| 575 | WEBRTC_VOICE_ENGINE_NS_DEFAULT_STATE) != 0) |
| 576 | { |
| 577 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 578 | "Init() failed to set noise reduction state for AP module"); |
| 579 | return -1; |
| 580 | } |
| 581 | // Automatic Gain control |
| 582 | if (_shared->audio_processing()->gain_control()-> |
| 583 | set_analog_level_limits(kMinVolumeLevel,kMaxVolumeLevel) != 0) |
| 584 | { |
| 585 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 586 | "Init() failed to set AGC analog level for AP module"); |
| 587 | return -1; |
| 588 | } |
| 589 | if (_shared->audio_processing()->gain_control()->set_mode( |
| 590 | (GainControl::Mode) WEBRTC_VOICE_ENGINE_AGC_DEFAULT_MODE) |
| 591 | != 0) |
| 592 | { |
| 593 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 594 | "Init() failed to set AGC mode for AP module"); |
| 595 | return -1; |
| 596 | } |
| 597 | if (_shared->audio_processing()->gain_control()->Enable( |
| 598 | WEBRTC_VOICE_ENGINE_AGC_DEFAULT_STATE) |
| 599 | != 0) |
| 600 | { |
| 601 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 602 | "Init() failed to set AGC state for AP module"); |
| 603 | return -1; |
| 604 | } |
| 605 | // VAD |
| 606 | if (_shared->audio_processing()->voice_detection()->Enable( |
| 607 | WEBRTC_VOICE_ENGINE_VAD_DEFAULT_STATE) |
| 608 | != 0) |
| 609 | { |
| 610 | _shared->SetLastError(VE_APM_ERROR, kTraceError, |
| 611 | "Init() failed to set VAD state for AP module"); |
| 612 | return -1; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // Set default AGC mode for the ADM |
| 617 | #ifdef WEBRTC_VOICE_ENGINE_AGC |
| 618 | bool enable(false); |
| 619 | if (_shared->audio_processing()->gain_control()->mode() |
| 620 | != GainControl::kFixedDigital) |
| 621 | { |
| 622 | enable = _shared->audio_processing()->gain_control()->is_enabled(); |
| 623 | // Only set the AGC mode for the ADM when Adaptive AGC mode is selected |
| 624 | if (_shared->audio_device()->SetAGC(enable) != 0) |
| 625 | { |
| 626 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, |
| 627 | kTraceError, "Init() failed to set default AGC mode in ADM 0"); |
| 628 | } |
| 629 | } |
| 630 | #endif |
| 631 | |
| 632 | return _shared->statistics().SetInitialized(); |
| 633 | } |
| 634 | |
| 635 | int VoEBaseImpl::Terminate() |
| 636 | { |
| 637 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 638 | "Terminate()"); |
| 639 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 640 | return TerminateInternal(); |
| 641 | } |
| 642 | |
| 643 | int VoEBaseImpl::MaxNumOfChannels() |
| 644 | { |
| 645 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 646 | "MaxNumOfChannels()"); |
| 647 | WebRtc_Word32 maxNumOfChannels = |
| 648 | _shared->channel_manager().MaxNumOfChannels(); |
| 649 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 650 | VoEId(_shared->instance_id(), -1), |
| 651 | "MaxNumOfChannels() => %d", maxNumOfChannels); |
| 652 | return (maxNumOfChannels); |
| 653 | } |
| 654 | |
| 655 | int VoEBaseImpl::CreateChannel() |
| 656 | { |
| 657 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 658 | "CreateChannel()"); |
| 659 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 660 | |
| 661 | if (!_shared->statistics().Initialized()) |
| 662 | { |
| 663 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | WebRtc_Word32 channelId = -1; |
| 668 | |
| 669 | if (!_shared->channel_manager().CreateChannel(channelId)) |
| 670 | { |
| 671 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 672 | "CreateChannel() failed to allocate memory for channel"); |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | bool destroyChannel(false); |
| 677 | { |
| 678 | voe::ScopedChannel sc(_shared->channel_manager(), channelId); |
| 679 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 680 | if (channelPtr == NULL) |
| 681 | { |
| 682 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 683 | "CreateChannel() failed to allocate memory for channel"); |
| 684 | return -1; |
| 685 | } |
| 686 | else if (channelPtr->SetEngineInformation(_shared->statistics(), |
| 687 | *_shared->output_mixer(), |
| 688 | *_shared->transmit_mixer(), |
| 689 | *_shared->process_thread(), |
| 690 | *_shared->audio_device(), |
| 691 | _voiceEngineObserverPtr, |
| 692 | &_callbackCritSect) != 0) |
| 693 | { |
| 694 | destroyChannel = true; |
| 695 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 696 | "CreateChannel() failed to associate engine and channel." |
| 697 | " Destroying channel."); |
| 698 | } |
| 699 | else if (channelPtr->Init() != 0) |
| 700 | { |
| 701 | destroyChannel = true; |
| 702 | _shared->SetLastError(VE_CHANNEL_NOT_CREATED, kTraceError, |
| 703 | "CreateChannel() failed to initialize channel. Destroying" |
| 704 | " channel."); |
| 705 | } |
| 706 | } |
| 707 | if (destroyChannel) |
| 708 | { |
| 709 | _shared->channel_manager().DestroyChannel(channelId); |
| 710 | return -1; |
| 711 | } |
| 712 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 713 | VoEId(_shared->instance_id(), -1), |
| 714 | "CreateChannel() => %d", channelId); |
| 715 | return channelId; |
| 716 | } |
| 717 | |
| 718 | int VoEBaseImpl::DeleteChannel(int channel) |
| 719 | { |
| 720 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 721 | "DeleteChannel(channel=%d)", channel); |
| 722 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 723 | |
| 724 | if (!_shared->statistics().Initialized()) |
| 725 | { |
| 726 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 727 | return -1; |
| 728 | } |
| 729 | |
| 730 | { |
| 731 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 732 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 733 | if (channelPtr == NULL) |
| 734 | { |
| 735 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 736 | "DeleteChannel() failed to locate channel"); |
| 737 | return -1; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | if (_shared->channel_manager().DestroyChannel(channel) != 0) |
| 742 | { |
| 743 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 744 | "DeleteChannel() failed to destroy channel"); |
| 745 | return -1; |
| 746 | } |
| 747 | |
| 748 | if (StopSend() != 0) |
| 749 | { |
| 750 | return -1; |
| 751 | } |
| 752 | |
| 753 | if (StopPlayout() != 0) |
| 754 | { |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | int VoEBaseImpl::SetLocalReceiver(int channel, int port, int RTCPport, |
| 762 | const char ipAddr[64], |
| 763 | const char multiCastAddr[64]) |
| 764 | { |
| 765 | // Inititialize local receive sockets (RTP and RTCP). |
| 766 | // |
| 767 | // The sockets are always first closed and then created again by this |
| 768 | // function call. The created sockets are by default also used for |
| 769 | // transmission (unless source port is set in SetSendDestination). |
| 770 | // |
| 771 | // Note that, sockets can also be created automatically if a user calls |
| 772 | // SetSendDestination and StartSend without having called SetLocalReceiver |
| 773 | // first. The sockets are then created at the first packet transmission. |
| 774 | |
| 775 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 776 | if (ipAddr == NULL && multiCastAddr == NULL) |
| 777 | { |
| 778 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 779 | VoEId(_shared->instance_id(), -1), |
| 780 | "SetLocalReceiver(channel=%d, port=%d, RTCPport=%d)", |
| 781 | channel, port, RTCPport); |
| 782 | } |
| 783 | else if (ipAddr != NULL && multiCastAddr == NULL) |
| 784 | { |
| 785 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 786 | VoEId(_shared->instance_id(), -1), |
| 787 | "SetLocalReceiver(channel=%d, port=%d, RTCPport=%d, ipAddr=%s)", |
| 788 | channel, port, RTCPport, ipAddr); |
| 789 | } |
| 790 | else if (ipAddr == NULL && multiCastAddr != NULL) |
| 791 | { |
| 792 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 793 | VoEId(_shared->instance_id(), -1), |
| 794 | "SetLocalReceiver(channel=%d, port=%d, RTCPport=%d, " |
| 795 | "multiCastAddr=%s)", channel, port, RTCPport, multiCastAddr); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 800 | VoEId(_shared->instance_id(), -1), |
| 801 | "SetLocalReceiver(channel=%d, port=%d, RTCPport=%d, " |
| 802 | "ipAddr=%s, multiCastAddr=%s)", channel, port, RTCPport, ipAddr, |
| 803 | multiCastAddr); |
| 804 | } |
| 805 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 806 | if (!_shared->statistics().Initialized()) |
| 807 | { |
| 808 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 809 | return -1; |
| 810 | } |
| 811 | if ((port < 0) || (port > 65535)) |
| 812 | { |
| 813 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 814 | "SetLocalReceiver() invalid RTP port"); |
| 815 | return -1; |
| 816 | } |
| 817 | if (((RTCPport != kVoEDefault) && (RTCPport < 0)) || ((RTCPport |
| 818 | != kVoEDefault) && (RTCPport > 65535))) |
| 819 | { |
| 820 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 821 | "SetLocalReceiver() invalid RTCP port"); |
| 822 | return -1; |
| 823 | } |
| 824 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 825 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 826 | if (channelPtr == NULL) |
| 827 | { |
| 828 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 829 | "SetLocalReceiver() failed to locate channel"); |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | // Cast RTCP port. In the RTP module 0 corresponds to RTP port + 1 in |
| 834 | // the module, which is the default. |
| 835 | WebRtc_UWord16 rtcpPortUW16(0); |
| 836 | if (RTCPport != kVoEDefault) |
| 837 | { |
| 838 | rtcpPortUW16 = static_cast<WebRtc_UWord16> (RTCPport); |
| 839 | } |
| 840 | |
| 841 | return channelPtr->SetLocalReceiver(port, rtcpPortUW16, ipAddr, |
| 842 | multiCastAddr); |
| 843 | #else |
| 844 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, |
| 845 | kTraceWarning, "SetLocalReceiver() VoE is built for external " |
| 846 | "transport"); |
| 847 | return -1; |
| 848 | #endif |
| 849 | } |
| 850 | |
| 851 | int VoEBaseImpl::GetLocalReceiver(int channel, int& port, int& RTCPport, |
| 852 | char ipAddr[64]) |
| 853 | { |
| 854 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 855 | "GetLocalReceiver(channel=%d, ipAddr[]=?)", channel); |
| 856 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 857 | if (!_shared->statistics().Initialized()) |
| 858 | { |
| 859 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 860 | return -1; |
| 861 | } |
| 862 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 863 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 864 | if (channelPtr == NULL) |
| 865 | { |
| 866 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 867 | "SetLocalReceiver() failed to locate channel"); |
| 868 | return -1; |
| 869 | } |
| 870 | WebRtc_Word32 ret = channelPtr->GetLocalReceiver(port, RTCPport, ipAddr); |
| 871 | if (ipAddr != NULL) |
| 872 | { |
| 873 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 874 | VoEId(_shared->instance_id(), -1), |
| 875 | "GetLocalReceiver() => port=%d, RTCPport=%d, ipAddr=%s", |
| 876 | port, RTCPport, ipAddr); |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 881 | VoEId(_shared->instance_id(), -1), |
| 882 | "GetLocalReceiver() => port=%d, RTCPport=%d", port, RTCPport); |
| 883 | } |
| 884 | return ret; |
| 885 | #else |
| 886 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 887 | "SetLocalReceiver() VoE is built for external transport"); |
| 888 | return -1; |
| 889 | #endif |
| 890 | } |
| 891 | |
| 892 | int VoEBaseImpl::SetSendDestination(int channel, int port, const char* ipaddr, |
| 893 | int sourcePort, int RTCPport) |
| 894 | { |
| 895 | WEBRTC_TRACE( |
| 896 | kTraceApiCall, |
| 897 | kTraceVoice, |
| 898 | VoEId(_shared->instance_id(), -1), |
| 899 | "SetSendDestination(channel=%d, port=%d, ipaddr=%s," |
| 900 | "sourcePort=%d, RTCPport=%d)", |
| 901 | channel, port, ipaddr, sourcePort, RTCPport); |
| 902 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 903 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 904 | if (!_shared->statistics().Initialized()) |
| 905 | { |
| 906 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 907 | return -1; |
| 908 | } |
| 909 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 910 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 911 | if (channelPtr == NULL) |
| 912 | { |
| 913 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 914 | "SetSendDestination() failed to locate channel"); |
| 915 | return -1; |
| 916 | } |
| 917 | if ((port < 0) || (port > 65535)) |
| 918 | { |
| 919 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 920 | "SetSendDestination() invalid RTP port"); |
| 921 | return -1; |
| 922 | } |
| 923 | if (((RTCPport != kVoEDefault) && (RTCPport < 0)) || ((RTCPport |
| 924 | != kVoEDefault) && (RTCPport > 65535))) |
| 925 | { |
| 926 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 927 | "SetSendDestination() invalid RTCP port"); |
| 928 | return -1; |
| 929 | } |
| 930 | if (((sourcePort != kVoEDefault) && (sourcePort < 0)) || ((sourcePort |
| 931 | != kVoEDefault) && (sourcePort > 65535))) |
| 932 | { |
| 933 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 934 | "SetSendDestination() invalid source port"); |
| 935 | return -1; |
| 936 | } |
| 937 | |
| 938 | // Cast RTCP port. In the RTP module 0 corresponds to RTP port + 1 in the |
| 939 | // module, which is the default. |
| 940 | WebRtc_UWord16 rtcpPortUW16(0); |
| 941 | if (RTCPport != kVoEDefault) |
| 942 | { |
| 943 | rtcpPortUW16 = static_cast<WebRtc_UWord16> (RTCPport); |
| 944 | WEBRTC_TRACE( |
| 945 | kTraceInfo, |
| 946 | kTraceVoice, |
| 947 | VoEId(_shared->instance_id(), channel), |
| 948 | "SetSendDestination() non default RTCP port %u will be " |
| 949 | "utilized", |
| 950 | rtcpPortUW16); |
| 951 | } |
| 952 | |
| 953 | return channelPtr->SetSendDestination(port, ipaddr, sourcePort, |
| 954 | rtcpPortUW16); |
| 955 | #else |
| 956 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 957 | "SetSendDestination() VoE is built for external transport"); |
| 958 | return -1; |
| 959 | #endif |
| 960 | } |
| 961 | |
| 962 | int VoEBaseImpl::GetSendDestination(int channel, int& port, char ipAddr[64], |
| 963 | int& sourcePort, int& RTCPport) |
| 964 | { |
| 965 | WEBRTC_TRACE( |
| 966 | kTraceApiCall, |
| 967 | kTraceVoice, |
| 968 | VoEId(_shared->instance_id(), -1), |
| 969 | "GetSendDestination(channel=%d, ipAddr[]=?, sourcePort=?," |
| 970 | "RTCPport=?)", |
| 971 | channel); |
| 972 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 973 | if (!_shared->statistics().Initialized()) |
| 974 | { |
| 975 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 976 | return -1; |
| 977 | } |
| 978 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 979 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 980 | if (channelPtr == NULL) |
| 981 | { |
| 982 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 983 | "GetSendDestination() failed to locate channel"); |
| 984 | return -1; |
| 985 | } |
| 986 | WebRtc_Word32 ret = channelPtr->GetSendDestination(port, ipAddr, |
| 987 | sourcePort, RTCPport); |
| 988 | if (ipAddr != NULL) |
| 989 | { |
| 990 | WEBRTC_TRACE( |
| 991 | kTraceStateInfo, |
| 992 | kTraceVoice, |
| 993 | VoEId(_shared->instance_id(), -1), |
| 994 | "GetSendDestination() => port=%d, RTCPport=%d, ipAddr=%s, " |
| 995 | "sourcePort=%d, RTCPport=%d", |
| 996 | port, RTCPport, ipAddr, sourcePort, RTCPport); |
| 997 | } |
| 998 | else |
| 999 | { |
| 1000 | WEBRTC_TRACE( |
| 1001 | kTraceStateInfo, |
| 1002 | kTraceVoice, |
| 1003 | VoEId(_shared->instance_id(), -1), |
| 1004 | "GetSendDestination() => port=%d, RTCPport=%d, " |
| 1005 | "sourcePort=%d, RTCPport=%d", |
| 1006 | port, RTCPport, sourcePort, RTCPport); |
| 1007 | } |
| 1008 | return ret; |
| 1009 | #else |
| 1010 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 1011 | "GetSendDestination() VoE is built for external transport"); |
| 1012 | return -1; |
| 1013 | #endif |
| 1014 | } |
| 1015 | |
| 1016 | int VoEBaseImpl::StartReceive(int channel) |
| 1017 | { |
| 1018 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1019 | "StartReceive(channel=%d)", channel); |
| 1020 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 1021 | if (!_shared->statistics().Initialized()) |
| 1022 | { |
| 1023 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1027 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1028 | if (channelPtr == NULL) |
| 1029 | { |
| 1030 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1031 | "StartReceive() failed to locate channel"); |
| 1032 | return -1; |
| 1033 | } |
| 1034 | return channelPtr->StartReceiving(); |
| 1035 | } |
| 1036 | |
| 1037 | int VoEBaseImpl::StopReceive(int channel) |
| 1038 | { |
| 1039 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1040 | "StopListen(channel=%d)", channel); |
| 1041 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 1042 | if (!_shared->statistics().Initialized()) |
| 1043 | { |
| 1044 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1048 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1049 | if (channelPtr == NULL) |
| 1050 | { |
| 1051 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1052 | "SetLocalReceiver() failed to locate channel"); |
| 1053 | return -1; |
| 1054 | } |
| 1055 | return channelPtr->StopReceiving(); |
| 1056 | } |
| 1057 | |
| 1058 | int VoEBaseImpl::StartPlayout(int channel) |
| 1059 | { |
| 1060 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1061 | "StartPlayout(channel=%d)", channel); |
| 1062 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 1063 | if (!_shared->statistics().Initialized()) |
| 1064 | { |
| 1065 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1069 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1070 | if (channelPtr == NULL) |
| 1071 | { |
| 1072 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1073 | "StartPlayout() failed to locate channel"); |
| 1074 | return -1; |
| 1075 | } |
| 1076 | if (channelPtr->Playing()) |
| 1077 | { |
| 1078 | return 0; |
| 1079 | } |
| 1080 | if (StartPlayout() != 0) |
| 1081 | { |
| 1082 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 1083 | "StartPlayout() failed to start playout"); |
| 1084 | return -1; |
| 1085 | } |
| 1086 | return channelPtr->StartPlayout(); |
| 1087 | } |
| 1088 | |
| 1089 | int VoEBaseImpl::StopPlayout(int channel) |
| 1090 | { |
| 1091 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1092 | "StopPlayout(channel=%d)", channel); |
| 1093 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 1094 | if (!_shared->statistics().Initialized()) |
| 1095 | { |
| 1096 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1100 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1101 | if (channelPtr == NULL) |
| 1102 | { |
| 1103 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1104 | "StopPlayout() failed to locate channel"); |
| 1105 | return -1; |
| 1106 | } |
| 1107 | if (channelPtr->StopPlayout() != 0) |
| 1108 | { |
| 1109 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, |
| 1110 | VoEId(_shared->instance_id(), -1), |
| 1111 | "StopPlayout() failed to stop playout for channel %d", channel); |
| 1112 | } |
| 1113 | return StopPlayout(); |
| 1114 | } |
| 1115 | |
| 1116 | int VoEBaseImpl::StartSend(int channel) |
| 1117 | { |
| 1118 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1119 | "StartSend(channel=%d)", channel); |
| 1120 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 1121 | if (!_shared->statistics().Initialized()) |
| 1122 | { |
| 1123 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1124 | return -1; |
| 1125 | } |
| 1126 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1127 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1128 | if (channelPtr == NULL) |
| 1129 | { |
| 1130 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1131 | "StartSend() failed to locate channel"); |
| 1132 | return -1; |
| 1133 | } |
| 1134 | if (channelPtr->Sending()) |
| 1135 | { |
| 1136 | return 0; |
| 1137 | } |
| 1138 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 1139 | if (!channelPtr->ExternalTransport() |
| 1140 | && !channelPtr->SendSocketsInitialized()) |
| 1141 | { |
| 1142 | _shared->SetLastError(VE_DESTINATION_NOT_INITED, kTraceError, |
| 1143 | "StartSend() must set send destination first"); |
| 1144 | return -1; |
| 1145 | } |
| 1146 | #endif |
| 1147 | if (StartSend() != 0) |
| 1148 | { |
| 1149 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 1150 | "StartSend() failed to start recording"); |
| 1151 | return -1; |
| 1152 | } |
| 1153 | return channelPtr->StartSend(); |
| 1154 | } |
| 1155 | |
| 1156 | int VoEBaseImpl::StopSend(int channel) |
| 1157 | { |
| 1158 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1159 | "StopSend(channel=%d)", channel); |
| 1160 | CriticalSectionScoped cs(_shared->crit_sec()); |
| 1161 | if (!_shared->statistics().Initialized()) |
| 1162 | { |
| 1163 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1167 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1168 | if (channelPtr == NULL) |
| 1169 | { |
| 1170 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1171 | "StopSend() failed to locate channel"); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | if (channelPtr->StopSend() != 0) |
| 1175 | { |
| 1176 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, |
| 1177 | VoEId(_shared->instance_id(), -1), |
| 1178 | "StopSend() failed to stop sending for channel %d", channel); |
| 1179 | } |
| 1180 | return StopSend(); |
| 1181 | } |
| 1182 | |
| 1183 | int VoEBaseImpl::GetVersion(char version[1024]) |
| 1184 | { |
| 1185 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1186 | "GetVersion(version=?)"); |
| 1187 | assert(kVoiceEngineVersionMaxMessageSize == 1024); |
| 1188 | |
| 1189 | if (version == NULL) |
| 1190 | { |
| 1191 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError); |
| 1192 | return (-1); |
| 1193 | } |
| 1194 | |
| 1195 | char versionBuf[kVoiceEngineVersionMaxMessageSize]; |
| 1196 | char* versionPtr = versionBuf; |
| 1197 | |
| 1198 | WebRtc_Word32 len = 0; |
| 1199 | WebRtc_Word32 accLen = 0; |
| 1200 | |
| 1201 | len = AddVoEVersion(versionPtr); |
| 1202 | if (len == -1) |
| 1203 | { |
| 1204 | return -1; |
| 1205 | } |
| 1206 | versionPtr += len; |
| 1207 | accLen += len; |
| 1208 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 1209 | |
| 1210 | len = AddBuildInfo(versionPtr); |
| 1211 | if (len == -1) |
| 1212 | { |
| 1213 | return -1; |
| 1214 | } |
| 1215 | versionPtr += len; |
| 1216 | accLen += len; |
| 1217 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 1218 | |
| 1219 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
| 1220 | len = AddExternalTransportBuild(versionPtr); |
| 1221 | if (len == -1) |
| 1222 | { |
| 1223 | return -1; |
| 1224 | } |
| 1225 | versionPtr += len; |
| 1226 | accLen += len; |
| 1227 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 1228 | #endif |
| 1229 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
| 1230 | len = AddExternalRecAndPlayoutBuild(versionPtr); |
| 1231 | if (len == -1) |
| 1232 | { |
| 1233 | return -1; |
| 1234 | } |
| 1235 | versionPtr += len; |
| 1236 | accLen += len; |
| 1237 | assert(accLen < kVoiceEngineVersionMaxMessageSize); |
| 1238 | #endif |
| 1239 | |
| 1240 | memcpy(version, versionBuf, accLen); |
| 1241 | version[accLen] = '\0'; |
| 1242 | |
| 1243 | // to avoid the truncation in the trace, split the string into parts |
| 1244 | char partOfVersion[256]; |
| 1245 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 1246 | VoEId(_shared->instance_id(), -1), "GetVersion() =>"); |
| 1247 | for (int partStart = 0; partStart < accLen;) |
| 1248 | { |
| 1249 | memset(partOfVersion, 0, sizeof(partOfVersion)); |
| 1250 | int partEnd = partStart + 180; |
| 1251 | while (version[partEnd] != '\n' && version[partEnd] != '\0') |
| 1252 | { |
| 1253 | partEnd--; |
| 1254 | } |
| 1255 | if (partEnd < accLen) |
| 1256 | { |
| 1257 | memcpy(partOfVersion, &version[partStart], partEnd - partStart); |
| 1258 | } |
| 1259 | else |
| 1260 | { |
| 1261 | memcpy(partOfVersion, &version[partStart], accLen - partStart); |
| 1262 | } |
| 1263 | partStart = partEnd; |
| 1264 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 1265 | VoEId(_shared->instance_id(), -1), "%s", partOfVersion); |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | WebRtc_Word32 VoEBaseImpl::AddBuildInfo(char* str) const |
| 1272 | { |
| 1273 | return sprintf(str, "Build: svn:%s %s\n", WEBRTC_SVNREVISION, BUILDINFO); |
| 1274 | } |
| 1275 | |
| 1276 | WebRtc_Word32 VoEBaseImpl::AddVoEVersion(char* str) const |
| 1277 | { |
| 1278 | return sprintf(str, "VoiceEngine 4.1.0\n"); |
| 1279 | } |
| 1280 | |
| 1281 | #ifdef WEBRTC_EXTERNAL_TRANSPORT |
| 1282 | WebRtc_Word32 VoEBaseImpl::AddExternalTransportBuild(char* str) const |
| 1283 | { |
| 1284 | return sprintf(str, "External transport build\n"); |
| 1285 | } |
| 1286 | #endif |
| 1287 | |
| 1288 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
| 1289 | WebRtc_Word32 VoEBaseImpl::AddExternalRecAndPlayoutBuild(char* str) const |
| 1290 | { |
| 1291 | return sprintf(str, "External recording and playout build\n"); |
| 1292 | } |
| 1293 | #endif |
| 1294 | |
| 1295 | int VoEBaseImpl::LastError() |
| 1296 | { |
| 1297 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1298 | "LastError()"); |
| 1299 | return (_shared->statistics().LastError()); |
| 1300 | } |
| 1301 | |
| 1302 | |
| 1303 | int VoEBaseImpl::SetNetEQPlayoutMode(int channel, NetEqModes mode) |
| 1304 | { |
| 1305 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1306 | "SetNetEQPlayoutMode(channel=%i, mode=%i)", channel, mode); |
| 1307 | if (!_shared->statistics().Initialized()) |
| 1308 | { |
| 1309 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1310 | return -1; |
| 1311 | } |
| 1312 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1313 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1314 | if (channelPtr == NULL) |
| 1315 | { |
| 1316 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1317 | "SetNetEQPlayoutMode() failed to locate channel"); |
| 1318 | return -1; |
| 1319 | } |
| 1320 | return channelPtr->SetNetEQPlayoutMode(mode); |
| 1321 | } |
| 1322 | |
| 1323 | int VoEBaseImpl::GetNetEQPlayoutMode(int channel, NetEqModes& mode) |
| 1324 | { |
| 1325 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1326 | "GetNetEQPlayoutMode(channel=%i, mode=?)", channel); |
| 1327 | if (!_shared->statistics().Initialized()) |
| 1328 | { |
| 1329 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1330 | return -1; |
| 1331 | } |
| 1332 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1333 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1334 | if (channelPtr == NULL) |
| 1335 | { |
| 1336 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1337 | "GetNetEQPlayoutMode() failed to locate channel"); |
| 1338 | return -1; |
| 1339 | } |
| 1340 | return channelPtr->GetNetEQPlayoutMode(mode); |
| 1341 | } |
| 1342 | |
| 1343 | int VoEBaseImpl::SetNetEQBGNMode(int channel, NetEqBgnModes mode) |
| 1344 | { |
| 1345 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1346 | "SetNetEQBGNMode(channel=%i, mode=%i)", channel, mode); |
| 1347 | if (!_shared->statistics().Initialized()) |
| 1348 | { |
| 1349 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1350 | return -1; |
| 1351 | } |
| 1352 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1353 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1354 | if (channelPtr == NULL) |
| 1355 | { |
| 1356 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1357 | "SetNetEQBGNMode() failed to locate channel"); |
| 1358 | return -1; |
| 1359 | } |
| 1360 | return channelPtr->SetNetEQBGNMode(mode); |
| 1361 | } |
| 1362 | |
| 1363 | int VoEBaseImpl::GetNetEQBGNMode(int channel, NetEqBgnModes& mode) |
| 1364 | { |
| 1365 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1366 | "GetNetEQBGNMode(channel=%i, mode=?)", channel); |
| 1367 | if (!_shared->statistics().Initialized()) |
| 1368 | { |
| 1369 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1370 | return -1; |
| 1371 | } |
| 1372 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1373 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1374 | if (channelPtr == NULL) |
| 1375 | { |
| 1376 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1377 | "GetNetEQBGNMode() failed to locate channel"); |
| 1378 | return -1; |
| 1379 | } |
| 1380 | return channelPtr->GetNetEQBGNMode(mode); |
| 1381 | } |
| 1382 | |
| 1383 | int VoEBaseImpl::SetOnHoldStatus(int channel, bool enable, OnHoldModes mode) |
| 1384 | { |
| 1385 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1386 | "SetOnHoldStatus(channel=%d, enable=%d, mode=%d)", channel, |
| 1387 | enable, mode); |
| 1388 | if (!_shared->statistics().Initialized()) |
| 1389 | { |
| 1390 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1391 | return -1; |
| 1392 | } |
| 1393 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1394 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1395 | if (channelPtr == NULL) |
| 1396 | { |
| 1397 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1398 | "SetOnHoldStatus() failed to locate channel"); |
| 1399 | return -1; |
| 1400 | } |
| 1401 | return channelPtr->SetOnHoldStatus(enable, mode); |
| 1402 | } |
| 1403 | |
| 1404 | int VoEBaseImpl::GetOnHoldStatus(int channel, bool& enabled, OnHoldModes& mode) |
| 1405 | { |
| 1406 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1407 | "GetOnHoldStatus(channel=%d, enabled=?, mode=?)", channel); |
| 1408 | if (!_shared->statistics().Initialized()) |
| 1409 | { |
| 1410 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 1411 | return -1; |
| 1412 | } |
| 1413 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 1414 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 1415 | if (channelPtr == NULL) |
| 1416 | { |
| 1417 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 1418 | "GetOnHoldStatus() failed to locate channel"); |
| 1419 | return -1; |
| 1420 | } |
| 1421 | return channelPtr->GetOnHoldStatus(enabled, mode); |
| 1422 | } |
| 1423 | |
| 1424 | WebRtc_Word32 VoEBaseImpl::StartPlayout() |
| 1425 | { |
| 1426 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1427 | "VoEBaseImpl::StartPlayout()"); |
| 1428 | if (_shared->audio_device()->Playing()) |
| 1429 | { |
| 1430 | return 0; |
| 1431 | } |
| 1432 | if (!_shared->ext_playout()) |
| 1433 | { |
| 1434 | if (_shared->audio_device()->InitPlayout() != 0) |
| 1435 | { |
| 1436 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1437 | VoEId(_shared->instance_id(), -1), |
| 1438 | "StartPlayout() failed to initialize playout"); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | if (_shared->audio_device()->StartPlayout() != 0) |
| 1442 | { |
| 1443 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1444 | VoEId(_shared->instance_id(), -1), |
| 1445 | "StartPlayout() failed to start playout"); |
| 1446 | return -1; |
| 1447 | } |
| 1448 | } |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | WebRtc_Word32 VoEBaseImpl::StopPlayout() |
| 1453 | { |
| 1454 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1455 | "VoEBaseImpl::StopPlayout()"); |
| 1456 | |
| 1457 | WebRtc_Word32 numOfChannels = _shared->channel_manager().NumOfChannels(); |
| 1458 | if (numOfChannels <= 0) |
| 1459 | { |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | WebRtc_UWord16 nChannelsPlaying(0); |
| 1464 | WebRtc_Word32* channelsArray = new WebRtc_Word32[numOfChannels]; |
| 1465 | |
| 1466 | // Get number of playing channels |
| 1467 | _shared->channel_manager().GetChannelIds(channelsArray, numOfChannels); |
| 1468 | for (int i = 0; i < numOfChannels; i++) |
| 1469 | { |
| 1470 | voe::ScopedChannel sc(_shared->channel_manager(), channelsArray[i]); |
| 1471 | voe::Channel* chPtr = sc.ChannelPtr(); |
| 1472 | if (chPtr) |
| 1473 | { |
| 1474 | if (chPtr->Playing()) |
| 1475 | { |
| 1476 | nChannelsPlaying++; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | delete[] channelsArray; |
| 1481 | |
| 1482 | // Stop audio-device playing if no channel is playing out |
| 1483 | if (nChannelsPlaying == 0) |
| 1484 | { |
| 1485 | if (_shared->audio_device()->StopPlayout() != 0) |
| 1486 | { |
| 1487 | _shared->SetLastError(VE_CANNOT_STOP_PLAYOUT, kTraceError, |
| 1488 | "StopPlayout() failed to stop playout"); |
| 1489 | return -1; |
| 1490 | } |
| 1491 | } |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | WebRtc_Word32 VoEBaseImpl::StartSend() |
| 1496 | { |
| 1497 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1498 | "VoEBaseImpl::StartSend()"); |
| 1499 | if (_shared->audio_device()->Recording()) |
| 1500 | { |
| 1501 | return 0; |
| 1502 | } |
| 1503 | if (!_shared->ext_recording()) |
| 1504 | { |
| 1505 | if (_shared->audio_device()->InitRecording() != 0) |
| 1506 | { |
| 1507 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1508 | VoEId(_shared->instance_id(), -1), |
| 1509 | "StartSend() failed to initialize recording"); |
| 1510 | return -1; |
| 1511 | } |
| 1512 | if (_shared->audio_device()->StartRecording() != 0) |
| 1513 | { |
| 1514 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 1515 | VoEId(_shared->instance_id(), -1), |
| 1516 | "StartSend() failed to start recording"); |
| 1517 | return -1; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | WebRtc_Word32 VoEBaseImpl::StopSend() |
| 1525 | { |
| 1526 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1527 | "VoEBaseImpl::StopSend()"); |
| 1528 | |
| 1529 | if (_shared->NumOfSendingChannels() == 0 && |
| 1530 | !_shared->transmit_mixer()->IsRecordingMic()) |
| 1531 | { |
| 1532 | // Stop audio-device recording if no channel is recording |
| 1533 | if (_shared->audio_device()->StopRecording() != 0) |
| 1534 | { |
| 1535 | _shared->SetLastError(VE_CANNOT_STOP_RECORDING, kTraceError, |
| 1536 | "StopSend() failed to stop recording"); |
| 1537 | return -1; |
| 1538 | } |
| 1539 | _shared->transmit_mixer()->StopSend(); |
| 1540 | } |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | WebRtc_Word32 VoEBaseImpl::TerminateInternal() |
| 1546 | { |
| 1547 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 1548 | "VoEBaseImpl::TerminateInternal()"); |
| 1549 | |
| 1550 | // Delete any remaining channel objects |
| 1551 | WebRtc_Word32 numOfChannels = _shared->channel_manager().NumOfChannels(); |
| 1552 | if (numOfChannels > 0) |
| 1553 | { |
| 1554 | WebRtc_Word32* channelsArray = new WebRtc_Word32[numOfChannels]; |
| 1555 | _shared->channel_manager().GetChannelIds(channelsArray, numOfChannels); |
| 1556 | for (int i = 0; i < numOfChannels; i++) |
| 1557 | { |
| 1558 | DeleteChannel(channelsArray[i]); |
| 1559 | } |
| 1560 | delete[] channelsArray; |
| 1561 | } |
| 1562 | |
| 1563 | if (_shared->process_thread()) |
| 1564 | { |
| 1565 | if (_shared->audio_device()) |
| 1566 | { |
| 1567 | if (_shared->process_thread()-> |
| 1568 | DeRegisterModule(_shared->audio_device()) != 0) |
| 1569 | { |
| 1570 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 1571 | "TerminateInternal() failed to deregister ADM"); |
| 1572 | } |
| 1573 | } |
| 1574 | if (_shared->process_thread()->Stop() != 0) |
| 1575 | { |
| 1576 | _shared->SetLastError(VE_THREAD_ERROR, kTraceError, |
| 1577 | "TerminateInternal() failed to stop module process thread"); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | // Audio Device Module |
| 1582 | |
| 1583 | if (_shared->audio_device() != NULL) |
| 1584 | { |
| 1585 | if (_shared->audio_device()->StopPlayout() != 0) |
| 1586 | { |
| 1587 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 1588 | "TerminateInternal() failed to stop playout"); |
| 1589 | } |
| 1590 | if (_shared->audio_device()->StopRecording() != 0) |
| 1591 | { |
| 1592 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 1593 | "TerminateInternal() failed to stop recording"); |
| 1594 | } |
| 1595 | if (_shared->audio_device()->RegisterEventObserver(NULL) != 0) { |
| 1596 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 1597 | "TerminateInternal() failed to de-register event observer " |
| 1598 | "for the ADM"); |
| 1599 | } |
| 1600 | if (_shared->audio_device()->RegisterAudioCallback(NULL) != 0) { |
| 1601 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 1602 | "TerminateInternal() failed to de-register audio callback " |
| 1603 | "for the ADM"); |
| 1604 | } |
| 1605 | if (_shared->audio_device()->Terminate() != 0) |
| 1606 | { |
| 1607 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 1608 | "TerminateInternal() failed to terminate the ADM"); |
| 1609 | } |
| 1610 | |
| 1611 | _shared->set_audio_device(NULL); |
| 1612 | } |
| 1613 | |
| 1614 | // AP module |
| 1615 | |
| 1616 | if (_shared->audio_processing() != NULL) |
| 1617 | { |
| 1618 | _shared->transmit_mixer()->SetAudioProcessingModule(NULL); |
| 1619 | _shared->set_audio_processing(NULL); |
| 1620 | } |
| 1621 | |
| 1622 | return _shared->statistics().SetUnInitialized(); |
| 1623 | } |
| 1624 | |
| 1625 | } // namespace webrtc |