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 | |
pbos@webrtc.org | be72fe4 | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/voe_hardware_impl.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 50ff6a5 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | |
pbos@webrtc.org | be72fe4 | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 16 | #include "webrtc/system_wrappers/interface/trace.h" |
| 17 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 18 | #include "webrtc/voice_engine/voice_engine_impl.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc |
| 21 | { |
| 22 | |
| 23 | VoEHardware* VoEHardware::GetInterface(VoiceEngine* voiceEngine) |
| 24 | { |
| 25 | #ifndef WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 26 | return NULL; |
| 27 | #else |
| 28 | if (NULL == voiceEngine) |
| 29 | { |
| 30 | return NULL; |
| 31 | } |
tommi@webrtc.org | a974cea | 2013-02-15 15:07:32 +0000 | [diff] [blame] | 32 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 33 | s->AddRef(); |
| 34 | return s; |
| 35 | #endif |
| 36 | } |
| 37 | |
| 38 | #ifdef WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 39 | |
henrike@webrtc.org | 18881d5 | 2013-03-28 15:58:49 +0000 | [diff] [blame] | 40 | VoEHardwareImpl::VoEHardwareImpl(voe::SharedData* shared) : _shared(shared) |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 41 | { |
| 42 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 43 | "VoEHardwareImpl() - ctor"); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | VoEHardwareImpl::~VoEHardwareImpl() |
| 47 | { |
| 48 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 49 | "~VoEHardwareImpl() - dtor"); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int VoEHardwareImpl::SetAudioDeviceLayer(AudioLayers audioLayer) |
| 53 | { |
| 54 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 55 | "SetAudioDeviceLayer(audioLayer=%d)", audioLayer); |
| 56 | |
| 57 | // Don't allow a change if VoE is initialized |
| 58 | if (_shared->statistics().Initialized()) |
| 59 | { |
| 60 | _shared->SetLastError(VE_ALREADY_INITED, kTraceError); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | // Map to AudioDeviceModule::AudioLayer |
| 65 | AudioDeviceModule::AudioLayer |
| 66 | wantedLayer(AudioDeviceModule::kPlatformDefaultAudio); |
| 67 | switch (audioLayer) |
| 68 | { |
| 69 | case kAudioPlatformDefault: |
| 70 | // already set above |
| 71 | break; |
| 72 | case kAudioWindowsCore: |
| 73 | wantedLayer = AudioDeviceModule::kWindowsCoreAudio; |
| 74 | break; |
| 75 | case kAudioWindowsWave: |
| 76 | wantedLayer = AudioDeviceModule::kWindowsWaveAudio; |
| 77 | break; |
| 78 | case kAudioLinuxAlsa: |
| 79 | wantedLayer = AudioDeviceModule::kLinuxAlsaAudio; |
| 80 | break; |
| 81 | case kAudioLinuxPulse: |
| 82 | wantedLayer = AudioDeviceModule::kLinuxPulseAudio; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | // Save the audio device layer for Init() |
| 87 | _shared->set_audio_device_layer(wantedLayer); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int VoEHardwareImpl::GetAudioDeviceLayer(AudioLayers& audioLayer) |
| 93 | { |
| 94 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 95 | "GetAudioDeviceLayer(devices=?)"); |
| 96 | |
| 97 | // Can always be called regardless of VoE state |
| 98 | |
| 99 | AudioDeviceModule::AudioLayer |
| 100 | activeLayer(AudioDeviceModule::kPlatformDefaultAudio); |
| 101 | |
| 102 | if (_shared->audio_device()) |
| 103 | { |
| 104 | // Get active audio layer from ADM |
| 105 | if (_shared->audio_device()->ActiveAudioLayer(&activeLayer) != 0) |
| 106 | { |
| 107 | _shared->SetLastError(VE_UNDEFINED_SC_ERR, kTraceError, |
| 108 | " Audio Device error"); |
| 109 | return -1; |
| 110 | } |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | // Return VoE's internal layer setting |
| 115 | activeLayer = _shared->audio_device_layer(); |
| 116 | } |
| 117 | |
| 118 | // Map to AudioLayers |
| 119 | switch (activeLayer) |
| 120 | { |
| 121 | case AudioDeviceModule::kPlatformDefaultAudio: |
| 122 | audioLayer = kAudioPlatformDefault; |
| 123 | break; |
| 124 | case AudioDeviceModule::kWindowsCoreAudio: |
| 125 | audioLayer = kAudioWindowsCore; |
| 126 | break; |
| 127 | case AudioDeviceModule::kWindowsWaveAudio: |
| 128 | audioLayer = kAudioWindowsWave; |
| 129 | break; |
| 130 | case AudioDeviceModule::kLinuxAlsaAudio: |
| 131 | audioLayer = kAudioLinuxAlsa; |
| 132 | break; |
| 133 | case AudioDeviceModule::kLinuxPulseAudio: |
| 134 | audioLayer = kAudioLinuxPulse; |
| 135 | break; |
| 136 | default: |
| 137 | _shared->SetLastError(VE_UNDEFINED_SC_ERR, kTraceError, |
| 138 | " unknown audio layer"); |
| 139 | } |
| 140 | |
| 141 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 142 | VoEId(_shared->instance_id(), -1), |
| 143 | " Output: audioLayer=%d", audioLayer); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | int VoEHardwareImpl::GetNumOfRecordingDevices(int& devices) |
| 148 | { |
| 149 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 150 | "GetNumOfRecordingDevices(devices=?)"); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 151 | |
| 152 | if (!_shared->statistics().Initialized()) |
| 153 | { |
| 154 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | devices = static_cast<int> (_shared->audio_device()->RecordingDevices()); |
| 159 | |
| 160 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 161 | VoEId(_shared->instance_id(), -1), " Output: devices=%d", devices); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int VoEHardwareImpl::GetNumOfPlayoutDevices(int& devices) |
| 167 | { |
| 168 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 169 | "GetNumOfPlayoutDevices(devices=?)"); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 170 | |
| 171 | if (!_shared->statistics().Initialized()) |
| 172 | { |
| 173 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | devices = static_cast<int> (_shared->audio_device()->PlayoutDevices()); |
| 178 | |
| 179 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 180 | VoEId(_shared->instance_id(), -1), |
| 181 | " Output: devices=%d", devices); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int VoEHardwareImpl::GetRecordingDeviceName(int index, |
| 187 | char strNameUTF8[128], |
| 188 | char strGuidUTF8[128]) |
| 189 | { |
| 190 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 191 | "GetRecordingDeviceName(index=%d)", index); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 192 | |
| 193 | if (!_shared->statistics().Initialized()) |
| 194 | { |
| 195 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 196 | return -1; |
| 197 | } |
| 198 | if (strNameUTF8 == NULL) |
| 199 | { |
| 200 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 201 | "GetRecordingDeviceName() invalid argument"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | // Note that strGuidUTF8 is allowed to be NULL |
| 206 | |
| 207 | // Init len variable to length of supplied vectors |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 208 | const uint16_t strLen = 128; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 209 | |
| 210 | // Check if length has been changed in module |
| 211 | assert(strLen == kAdmMaxDeviceNameSize); |
| 212 | assert(strLen == kAdmMaxGuidSize); |
| 213 | |
| 214 | char name[strLen]; |
| 215 | char guid[strLen]; |
| 216 | |
| 217 | // Get names from module |
| 218 | if (_shared->audio_device()->RecordingDeviceName(index, name, guid) != 0) |
| 219 | { |
| 220 | _shared->SetLastError(VE_CANNOT_RETRIEVE_DEVICE_NAME, kTraceError, |
| 221 | "GetRecordingDeviceName() failed to get device name"); |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | // Copy to vectors supplied by user |
| 226 | strncpy(strNameUTF8, name, strLen); |
| 227 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 228 | VoEId(_shared->instance_id(), -1), |
| 229 | " Output: strNameUTF8=%s", strNameUTF8); |
| 230 | |
| 231 | if (strGuidUTF8 != NULL) |
| 232 | { |
| 233 | strncpy(strGuidUTF8, guid, strLen); |
| 234 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 235 | VoEId(_shared->instance_id(), -1), |
| 236 | " Output: strGuidUTF8=%s", strGuidUTF8); |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | int VoEHardwareImpl::GetPlayoutDeviceName(int index, |
| 243 | char strNameUTF8[128], |
| 244 | char strGuidUTF8[128]) |
| 245 | { |
| 246 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 247 | "GetPlayoutDeviceName(index=%d)", index); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 248 | |
| 249 | if (!_shared->statistics().Initialized()) |
| 250 | { |
| 251 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 252 | return -1; |
| 253 | } |
| 254 | if (strNameUTF8 == NULL) |
| 255 | { |
| 256 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 257 | "GetPlayoutDeviceName() invalid argument"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | // Note that strGuidUTF8 is allowed to be NULL |
| 262 | |
| 263 | // Init len variable to length of supplied vectors |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 264 | const uint16_t strLen = 128; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 265 | |
| 266 | // Check if length has been changed in module |
| 267 | assert(strLen == kAdmMaxDeviceNameSize); |
| 268 | assert(strLen == kAdmMaxGuidSize); |
| 269 | |
| 270 | char name[strLen]; |
| 271 | char guid[strLen]; |
| 272 | |
| 273 | // Get names from module |
| 274 | if (_shared->audio_device()->PlayoutDeviceName(index, name, guid) != 0) |
| 275 | { |
| 276 | _shared->SetLastError(VE_CANNOT_RETRIEVE_DEVICE_NAME, kTraceError, |
| 277 | "GetPlayoutDeviceName() failed to get device name"); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | // Copy to vectors supplied by user |
| 282 | strncpy(strNameUTF8, name, strLen); |
| 283 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 284 | VoEId(_shared->instance_id(), -1), |
| 285 | " Output: strNameUTF8=%s", strNameUTF8); |
| 286 | |
| 287 | if (strGuidUTF8 != NULL) |
| 288 | { |
| 289 | strncpy(strGuidUTF8, guid, strLen); |
| 290 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 291 | VoEId(_shared->instance_id(), -1), |
| 292 | " Output: strGuidUTF8=%s", strGuidUTF8); |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | int VoEHardwareImpl::SetRecordingDevice(int index, |
| 299 | StereoChannel recordingChannel) |
| 300 | { |
| 301 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 302 | "SetRecordingDevice(index=%d, recordingChannel=%d)", |
| 303 | index, (int) recordingChannel); |
| 304 | CriticalSectionScoped cs(_shared->crit_sec()); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 305 | |
| 306 | if (!_shared->statistics().Initialized()) |
| 307 | { |
| 308 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | bool isRecording(false); |
| 313 | |
| 314 | // Store state about activated recording to be able to restore it after the |
| 315 | // recording device has been modified. |
| 316 | if (_shared->audio_device()->Recording()) |
| 317 | { |
| 318 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 319 | "SetRecordingDevice() device is modified while recording" |
| 320 | " is active..."); |
| 321 | isRecording = true; |
| 322 | if (_shared->audio_device()->StopRecording() == -1) |
| 323 | { |
| 324 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 325 | "SetRecordingDevice() unable to stop recording"); |
| 326 | return -1; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // We let the module do the index sanity |
| 331 | |
| 332 | // Set recording channel |
| 333 | AudioDeviceModule::ChannelType recCh = |
| 334 | AudioDeviceModule::kChannelBoth; |
| 335 | switch (recordingChannel) |
| 336 | { |
| 337 | case kStereoLeft: |
| 338 | recCh = AudioDeviceModule::kChannelLeft; |
| 339 | break; |
| 340 | case kStereoRight: |
| 341 | recCh = AudioDeviceModule::kChannelRight; |
| 342 | break; |
| 343 | case kStereoBoth: |
| 344 | // default setting kChannelBoth (<=> mono) |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | if (_shared->audio_device()->SetRecordingChannel(recCh) != 0) { |
| 349 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 350 | "SetRecordingChannel() unable to set the recording channel"); |
| 351 | } |
| 352 | |
| 353 | // Map indices to unsigned since underlying functions need that |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 354 | uint16_t indexU = static_cast<uint16_t> (index); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 355 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 356 | int32_t res(0); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 357 | |
| 358 | if (index == -1) |
| 359 | { |
| 360 | res = _shared->audio_device()->SetRecordingDevice( |
| 361 | AudioDeviceModule::kDefaultCommunicationDevice); |
| 362 | } |
| 363 | else if (index == -2) |
| 364 | { |
| 365 | res = _shared->audio_device()->SetRecordingDevice( |
| 366 | AudioDeviceModule::kDefaultDevice); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | res = _shared->audio_device()->SetRecordingDevice(indexU); |
| 371 | } |
| 372 | |
| 373 | if (res != 0) |
| 374 | { |
| 375 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 376 | "SetRecordingDevice() unable to set the recording device"); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | // Init microphone, so user can do volume settings etc |
| 381 | if (_shared->audio_device()->InitMicrophone() == -1) |
| 382 | { |
| 383 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceWarning, |
| 384 | "SetRecordingDevice() cannot access microphone"); |
| 385 | } |
| 386 | |
| 387 | // Set number of channels |
| 388 | bool available = false; |
| 389 | if (_shared->audio_device()->StereoRecordingIsAvailable(&available) != 0) { |
| 390 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 391 | "StereoRecordingIsAvailable() failed to query stereo recording"); |
| 392 | } |
| 393 | |
| 394 | if (_shared->audio_device()->SetStereoRecording(available) != 0) |
| 395 | { |
| 396 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 397 | "SetRecordingDevice() failed to set mono recording mode"); |
| 398 | } |
| 399 | |
| 400 | // Restore recording if it was enabled already when calling this function. |
| 401 | if (isRecording) |
| 402 | { |
| 403 | if (!_shared->ext_recording()) |
| 404 | { |
| 405 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 406 | VoEId(_shared->instance_id(), -1), |
| 407 | "SetRecordingDevice() recording is now being restored..."); |
| 408 | if (_shared->audio_device()->InitRecording() != 0) |
| 409 | { |
| 410 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 411 | VoEId(_shared->instance_id(), -1), |
| 412 | "SetRecordingDevice() failed to initialize recording"); |
| 413 | return -1; |
| 414 | } |
| 415 | if (_shared->audio_device()->StartRecording() != 0) |
| 416 | { |
| 417 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 418 | VoEId(_shared->instance_id(), -1), |
| 419 | "SetRecordingDevice() failed to start recording"); |
| 420 | return -1; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int VoEHardwareImpl::SetPlayoutDevice(int index) |
| 429 | { |
| 430 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 431 | "SetPlayoutDevice(index=%d)", index); |
| 432 | CriticalSectionScoped cs(_shared->crit_sec()); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 433 | |
| 434 | if (!_shared->statistics().Initialized()) |
| 435 | { |
| 436 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 437 | return -1; |
| 438 | } |
| 439 | |
| 440 | bool isPlaying(false); |
| 441 | |
| 442 | // Store state about activated playout to be able to restore it after the |
| 443 | // playout device has been modified. |
| 444 | if (_shared->audio_device()->Playing()) |
| 445 | { |
| 446 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 447 | "SetPlayoutDevice() device is modified while playout is " |
| 448 | "active..."); |
| 449 | isPlaying = true; |
| 450 | if (_shared->audio_device()->StopPlayout() == -1) |
| 451 | { |
| 452 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 453 | "SetPlayoutDevice() unable to stop playout"); |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // We let the module do the index sanity |
| 459 | |
| 460 | // Map indices to unsigned since underlying functions need that |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 461 | uint16_t indexU = static_cast<uint16_t> (index); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 462 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 463 | int32_t res(0); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 464 | |
| 465 | if (index == -1) |
| 466 | { |
| 467 | res = _shared->audio_device()->SetPlayoutDevice( |
| 468 | AudioDeviceModule::kDefaultCommunicationDevice); |
| 469 | } |
| 470 | else if (index == -2) |
| 471 | { |
| 472 | res = _shared->audio_device()->SetPlayoutDevice( |
| 473 | AudioDeviceModule::kDefaultDevice); |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | res = _shared->audio_device()->SetPlayoutDevice(indexU); |
| 478 | } |
| 479 | |
| 480 | if (res != 0) |
| 481 | { |
| 482 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceError, |
| 483 | "SetPlayoutDevice() unable to set the playout device"); |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | // Init speaker, so user can do volume settings etc |
| 488 | if (_shared->audio_device()->InitSpeaker() == -1) |
| 489 | { |
| 490 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceWarning, |
| 491 | "SetPlayoutDevice() cannot access speaker"); |
| 492 | } |
| 493 | |
| 494 | // Set number of channels |
| 495 | bool available = false; |
| 496 | _shared->audio_device()->StereoPlayoutIsAvailable(&available); |
| 497 | if (_shared->audio_device()->SetStereoPlayout(available) != 0) |
| 498 | { |
| 499 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
| 500 | "SetPlayoutDevice() failed to set stereo playout mode"); |
| 501 | } |
| 502 | |
| 503 | // Restore playout if it was enabled already when calling this function. |
| 504 | if (isPlaying) |
| 505 | { |
| 506 | if (!_shared->ext_playout()) |
| 507 | { |
| 508 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 509 | VoEId(_shared->instance_id(), -1), |
| 510 | "SetPlayoutDevice() playout is now being restored..."); |
| 511 | if (_shared->audio_device()->InitPlayout() != 0) |
| 512 | { |
| 513 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 514 | VoEId(_shared->instance_id(), -1), |
| 515 | "SetPlayoutDevice() failed to initialize playout"); |
| 516 | return -1; |
| 517 | } |
| 518 | if (_shared->audio_device()->StartPlayout() != 0) |
| 519 | { |
| 520 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 521 | VoEId(_shared->instance_id(), -1), |
| 522 | "SetPlayoutDevice() failed to start playout"); |
| 523 | return -1; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
henrika@webrtc.org | 28e9b66 | 2014-05-09 11:10:50 +0000 | [diff] [blame^] | 531 | int VoEHardwareImpl::GetRecordingDeviceStatus(bool& isAvailable) |
| 532 | { |
| 533 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 534 | "GetRecordingDeviceStatus()"); |
| 535 | |
| 536 | if (!_shared->statistics().Initialized()) |
| 537 | { |
| 538 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | // We let the module do isRecording sanity |
| 543 | |
| 544 | bool available(false); |
| 545 | |
| 546 | // Check availability |
| 547 | if (_shared->audio_device()->RecordingIsAvailable(&available) != 0) |
| 548 | { |
| 549 | _shared->SetLastError(VE_UNDEFINED_SC_REC_ERR, kTraceError, |
| 550 | " Audio Device error"); |
| 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | isAvailable = available; |
| 555 | |
| 556 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 557 | VoEId(_shared->instance_id(), -1), |
| 558 | " Output: isAvailable = %d)", (int) isAvailable); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | int VoEHardwareImpl::GetPlayoutDeviceStatus(bool& isAvailable) |
| 564 | { |
| 565 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 566 | "GetPlayoutDeviceStatus()"); |
| 567 | |
| 568 | if (!_shared->statistics().Initialized()) |
| 569 | { |
| 570 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | // We let the module do isPlaying sanity |
| 575 | |
| 576 | bool available(false); |
| 577 | |
| 578 | // Check availability |
| 579 | if (_shared->audio_device()->PlayoutIsAvailable(&available) != 0) |
| 580 | { |
| 581 | _shared->SetLastError(VE_PLAY_UNDEFINED_SC_ERR, kTraceError, |
| 582 | " Audio Device error"); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | isAvailable = available; |
| 587 | |
| 588 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 589 | VoEId(_shared->instance_id(), -1), |
| 590 | " Output: isAvailable = %d)", (int) isAvailable); |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | int VoEHardwareImpl::ResetAudioDevice() |
| 596 | { |
| 597 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 598 | "ResetAudioDevice()"); |
| 599 | |
| 600 | if (!_shared->statistics().Initialized()) |
| 601 | { |
| 602 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 603 | return -1; |
| 604 | } |
| 605 | |
| 606 | #if defined(WEBRTC_IOS) |
| 607 | if (_shared->audio_device()->ResetAudioDevice() < 0) |
| 608 | { |
| 609 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceError, |
| 610 | " Failed to reset sound device"); |
| 611 | return -1; |
| 612 | } |
| 613 | return 0; |
| 614 | #else |
| 615 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
| 616 | " no support for resetting sound device"); |
| 617 | return -1; |
| 618 | #endif |
| 619 | } |
| 620 | |
| 621 | int VoEHardwareImpl::AudioDeviceControl(unsigned int par1, unsigned int par2, |
| 622 | unsigned int par3) |
| 623 | { |
| 624 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 625 | "AudioDeviceControl(%i, %i, %i)", par1, par2, par3); |
| 626 | if (!_shared->statistics().Initialized()) |
| 627 | { |
| 628 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 629 | return -1; |
| 630 | } |
| 631 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
| 632 | " no support for resetting sound device"); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | int VoEHardwareImpl::SetLoudspeakerStatus(bool enable) |
| 637 | { |
| 638 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 639 | "SetLoudspeakerStatus(enable=%i)", (int) enable); |
| 640 | |
| 641 | if (!_shared->statistics().Initialized()) |
| 642 | { |
| 643 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 644 | return -1; |
| 645 | } |
| 646 | #if defined(WEBRTC_ANDROID) |
| 647 | if (_shared->audio_device()->SetLoudspeakerStatus(enable) < 0) |
| 648 | { |
| 649 | _shared->SetLastError(VE_IGNORED_FUNCTION, kTraceError, |
| 650 | " Failed to set loudspeaker status"); |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | #else |
| 656 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
| 657 | " no support for setting loudspeaker status"); |
| 658 | return -1; |
| 659 | #endif |
| 660 | } |
| 661 | |
| 662 | int VoEHardwareImpl::GetLoudspeakerStatus(bool& enabled) |
| 663 | { |
| 664 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 665 | "GetLoudspeakerStatus()"); |
| 666 | |
| 667 | #if defined(WEBRTC_ANDROID) |
| 668 | if (!_shared->statistics().Initialized()) |
| 669 | { |
| 670 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 671 | return -1; |
| 672 | } |
| 673 | |
| 674 | if (_shared->audio_device()->GetLoudspeakerStatus(&enabled) < 0) |
| 675 | { |
| 676 | _shared->SetLastError(VE_IGNORED_FUNCTION, kTraceError, |
| 677 | " Failed to get loudspeaker status"); |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | #else |
| 683 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
| 684 | " no support for setting loudspeaker status"); |
| 685 | return -1; |
| 686 | #endif |
| 687 | } |
| 688 | |
| 689 | int VoEHardwareImpl::GetCPULoad(int& loadPercent) |
| 690 | { |
| 691 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 692 | "GetCPULoad()"); |
| 693 | |
| 694 | if (!_shared->statistics().Initialized()) |
| 695 | { |
| 696 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 697 | return -1; |
| 698 | } |
| 699 | |
| 700 | // Get CPU load from ADM |
| 701 | uint16_t load(0); |
| 702 | if (_shared->audio_device()->CPULoad(&load) != 0) |
| 703 | { |
| 704 | _shared->SetLastError(VE_CPU_INFO_ERROR, kTraceError, |
| 705 | " error getting system CPU load"); |
| 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | loadPercent = static_cast<int> (load); |
| 710 | |
| 711 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 712 | VoEId(_shared->instance_id(), -1), |
| 713 | " Output: loadPercent = %d", loadPercent); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | int VoEHardwareImpl::EnableBuiltInAEC(bool enable) |
| 719 | { |
| 720 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 721 | "%s", __FUNCTION__); |
| 722 | if (!_shared->statistics().Initialized()) |
| 723 | { |
| 724 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | return _shared->audio_device()->EnableBuiltInAEC(enable); |
| 729 | } |
| 730 | |
| 731 | bool VoEHardwareImpl::BuiltInAECIsEnabled() const |
| 732 | { |
| 733 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 734 | "%s", __FUNCTION__); |
| 735 | if (!_shared->statistics().Initialized()) |
| 736 | { |
| 737 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | return _shared->audio_device()->BuiltInAECIsEnabled(); |
| 742 | } |
| 743 | |
leozwang@webrtc.org | d34a831 | 2012-12-04 19:11:55 +0000 | [diff] [blame] | 744 | int VoEHardwareImpl::SetRecordingSampleRate(unsigned int samples_per_sec) { |
| 745 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 746 | "%s", __FUNCTION__); |
| 747 | if (!_shared->statistics().Initialized()) { |
| 748 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 749 | return false; |
| 750 | } |
| 751 | return _shared->audio_device()->SetRecordingSampleRate(samples_per_sec); |
| 752 | } |
| 753 | |
| 754 | int VoEHardwareImpl::RecordingSampleRate(unsigned int* samples_per_sec) const { |
| 755 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 756 | "%s", __FUNCTION__); |
| 757 | if (!_shared->statistics().Initialized()) { |
| 758 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 759 | return false; |
| 760 | } |
| 761 | return _shared->audio_device()->RecordingSampleRate(samples_per_sec); |
| 762 | } |
| 763 | |
| 764 | int VoEHardwareImpl::SetPlayoutSampleRate(unsigned int samples_per_sec) { |
| 765 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 766 | "%s", __FUNCTION__); |
| 767 | if (!_shared->statistics().Initialized()) { |
| 768 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 769 | return false; |
| 770 | } |
| 771 | return _shared->audio_device()->SetPlayoutSampleRate(samples_per_sec); |
| 772 | } |
| 773 | |
| 774 | int VoEHardwareImpl::PlayoutSampleRate(unsigned int* samples_per_sec) const { |
| 775 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 776 | "%s", __FUNCTION__); |
| 777 | if (!_shared->statistics().Initialized()) { |
| 778 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 779 | return false; |
| 780 | } |
| 781 | return _shared->audio_device()->PlayoutSampleRate(samples_per_sec); |
| 782 | } |
| 783 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 784 | #endif // WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 785 | |
pbos@webrtc.org | 5ab7b93 | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 786 | } // namespace webrtc |