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_hardware_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/trace.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 16 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 17 | #include "webrtc/voice_engine/voice_engine_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 19 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 21 | VoEHardware* VoEHardware::GetInterface(VoiceEngine* voiceEngine) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | #ifndef WEBRTC_VOICE_ENGINE_HARDWARE_API |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 23 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | #else |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 25 | if (NULL == voiceEngine) { |
| 26 | return NULL; |
| 27 | } |
| 28 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 29 | s->AddRef(); |
| 30 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | #endif |
| 32 | } |
| 33 | |
| 34 | #ifdef WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 35 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 36 | VoEHardwareImpl::VoEHardwareImpl(voe::SharedData* shared) : _shared(shared) { |
| 37 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 38 | "VoEHardwareImpl() - ctor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 41 | VoEHardwareImpl::~VoEHardwareImpl() { |
| 42 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 43 | "~VoEHardwareImpl() - dtor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 46 | int VoEHardwareImpl::SetAudioDeviceLayer(AudioLayers audioLayer) { |
| 47 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 48 | "SetAudioDeviceLayer(audioLayer=%d)", audioLayer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 50 | // Don't allow a change if VoE is initialized |
| 51 | if (_shared->statistics().Initialized()) { |
| 52 | _shared->SetLastError(VE_ALREADY_INITED, kTraceError); |
| 53 | return -1; |
| 54 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 56 | // Map to AudioDeviceModule::AudioLayer |
| 57 | AudioDeviceModule::AudioLayer wantedLayer( |
| 58 | AudioDeviceModule::kPlatformDefaultAudio); |
| 59 | switch (audioLayer) { |
| 60 | case kAudioPlatformDefault: |
| 61 | // already set above |
| 62 | break; |
| 63 | case kAudioWindowsCore: |
| 64 | wantedLayer = AudioDeviceModule::kWindowsCoreAudio; |
| 65 | break; |
| 66 | case kAudioWindowsWave: |
| 67 | wantedLayer = AudioDeviceModule::kWindowsWaveAudio; |
| 68 | break; |
| 69 | case kAudioLinuxAlsa: |
| 70 | wantedLayer = AudioDeviceModule::kLinuxAlsaAudio; |
| 71 | break; |
| 72 | case kAudioLinuxPulse: |
| 73 | wantedLayer = AudioDeviceModule::kLinuxPulseAudio; |
| 74 | break; |
| 75 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 77 | // Save the audio device layer for Init() |
| 78 | _shared->set_audio_device_layer(wantedLayer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 80 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 83 | int VoEHardwareImpl::GetAudioDeviceLayer(AudioLayers& audioLayer) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 84 | // Can always be called regardless of VoE state |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 86 | AudioDeviceModule::AudioLayer activeLayer( |
| 87 | AudioDeviceModule::kPlatformDefaultAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 89 | if (_shared->audio_device()) { |
| 90 | // Get active audio layer from ADM |
| 91 | if (_shared->audio_device()->ActiveAudioLayer(&activeLayer) != 0) { |
| 92 | _shared->SetLastError(VE_UNDEFINED_SC_ERR, kTraceError, |
| 93 | " Audio Device error"); |
| 94 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 96 | } else { |
| 97 | // Return VoE's internal layer setting |
| 98 | activeLayer = _shared->audio_device_layer(); |
| 99 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 101 | // Map to AudioLayers |
| 102 | switch (activeLayer) { |
| 103 | case AudioDeviceModule::kPlatformDefaultAudio: |
| 104 | audioLayer = kAudioPlatformDefault; |
| 105 | break; |
| 106 | case AudioDeviceModule::kWindowsCoreAudio: |
| 107 | audioLayer = kAudioWindowsCore; |
| 108 | break; |
| 109 | case AudioDeviceModule::kWindowsWaveAudio: |
| 110 | audioLayer = kAudioWindowsWave; |
| 111 | break; |
| 112 | case AudioDeviceModule::kLinuxAlsaAudio: |
| 113 | audioLayer = kAudioLinuxAlsa; |
| 114 | break; |
| 115 | case AudioDeviceModule::kLinuxPulseAudio: |
| 116 | audioLayer = kAudioLinuxPulse; |
| 117 | break; |
| 118 | default: |
| 119 | _shared->SetLastError(VE_UNDEFINED_SC_ERR, kTraceError, |
| 120 | " unknown audio layer"); |
| 121 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 123 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | } |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 125 | int VoEHardwareImpl::GetNumOfRecordingDevices(int& devices) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 126 | if (!_shared->statistics().Initialized()) { |
| 127 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 128 | return -1; |
| 129 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 131 | devices = static_cast<int>(_shared->audio_device()->RecordingDevices()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 133 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 136 | int VoEHardwareImpl::GetNumOfPlayoutDevices(int& devices) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 137 | if (!_shared->statistics().Initialized()) { |
| 138 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 139 | return -1; |
| 140 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 142 | devices = static_cast<int>(_shared->audio_device()->PlayoutDevices()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 144 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | int VoEHardwareImpl::GetRecordingDeviceName(int index, |
| 148 | char strNameUTF8[128], |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 149 | char strGuidUTF8[128]) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 150 | if (!_shared->statistics().Initialized()) { |
| 151 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 152 | return -1; |
| 153 | } |
| 154 | if (strNameUTF8 == NULL) { |
| 155 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 156 | "GetRecordingDeviceName() invalid argument"); |
| 157 | return -1; |
| 158 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 160 | // Note that strGuidUTF8 is allowed to be NULL |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 162 | // Init len variable to length of supplied vectors |
| 163 | const uint16_t strLen = 128; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 165 | // Check if length has been changed in module |
André Susano Pinto | 664cdaf | 2015-05-20 11:11:07 +0200 | [diff] [blame] | 166 | static_assert(strLen == kAdmMaxDeviceNameSize, ""); |
| 167 | static_assert(strLen == kAdmMaxGuidSize, ""); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 169 | char name[strLen]; |
| 170 | char guid[strLen]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 172 | // Get names from module |
| 173 | if (_shared->audio_device()->RecordingDeviceName(index, name, guid) != 0) { |
| 174 | _shared->SetLastError(VE_CANNOT_RETRIEVE_DEVICE_NAME, kTraceError, |
| 175 | "GetRecordingDeviceName() failed to get device name"); |
| 176 | return -1; |
| 177 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 179 | // Copy to vectors supplied by user |
| 180 | strncpy(strNameUTF8, name, strLen); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 181 | |
| 182 | if (strGuidUTF8 != NULL) { |
| 183 | strncpy(strGuidUTF8, guid, strLen); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 184 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 186 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | int VoEHardwareImpl::GetPlayoutDeviceName(int index, |
| 190 | char strNameUTF8[128], |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 191 | char strGuidUTF8[128]) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 192 | if (!_shared->statistics().Initialized()) { |
| 193 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 194 | return -1; |
| 195 | } |
| 196 | if (strNameUTF8 == NULL) { |
| 197 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 198 | "GetPlayoutDeviceName() invalid argument"); |
| 199 | return -1; |
| 200 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 202 | // Note that strGuidUTF8 is allowed to be NULL |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 204 | // Init len variable to length of supplied vectors |
| 205 | const uint16_t strLen = 128; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 207 | // Check if length has been changed in module |
André Susano Pinto | 664cdaf | 2015-05-20 11:11:07 +0200 | [diff] [blame] | 208 | static_assert(strLen == kAdmMaxDeviceNameSize, ""); |
| 209 | static_assert(strLen == kAdmMaxGuidSize, ""); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 211 | char name[strLen]; |
| 212 | char guid[strLen]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 214 | // Get names from module |
| 215 | if (_shared->audio_device()->PlayoutDeviceName(index, name, guid) != 0) { |
| 216 | _shared->SetLastError(VE_CANNOT_RETRIEVE_DEVICE_NAME, kTraceError, |
| 217 | "GetPlayoutDeviceName() failed to get device name"); |
| 218 | return -1; |
| 219 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 221 | // Copy to vectors supplied by user |
| 222 | strncpy(strNameUTF8, name, strLen); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 223 | |
| 224 | if (strGuidUTF8 != NULL) { |
| 225 | strncpy(strGuidUTF8, guid, strLen); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 226 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 228 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int VoEHardwareImpl::SetRecordingDevice(int index, |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 232 | StereoChannel recordingChannel) { |
| 233 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 234 | "SetRecordingDevice(index=%d, recordingChannel=%d)", index, |
| 235 | (int)recordingChannel); |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 236 | rtc::CritScope cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 238 | if (!_shared->statistics().Initialized()) { |
| 239 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | bool isRecording(false); |
| 244 | |
| 245 | // Store state about activated recording to be able to restore it after the |
| 246 | // recording device has been modified. |
| 247 | if (_shared->audio_device()->Recording()) { |
| 248 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 249 | "SetRecordingDevice() device is modified while recording" |
| 250 | " is active..."); |
| 251 | isRecording = true; |
| 252 | if (_shared->audio_device()->StopRecording() == -1) { |
| 253 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 254 | "SetRecordingDevice() unable to stop recording"); |
| 255 | return -1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // We let the module do the index sanity |
| 260 | |
| 261 | // Set recording channel |
| 262 | AudioDeviceModule::ChannelType recCh = AudioDeviceModule::kChannelBoth; |
| 263 | switch (recordingChannel) { |
| 264 | case kStereoLeft: |
| 265 | recCh = AudioDeviceModule::kChannelLeft; |
| 266 | break; |
| 267 | case kStereoRight: |
| 268 | recCh = AudioDeviceModule::kChannelRight; |
| 269 | break; |
| 270 | case kStereoBoth: |
| 271 | // default setting kChannelBoth (<=> mono) |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | if (_shared->audio_device()->SetRecordingChannel(recCh) != 0) { |
| 276 | _shared->SetLastError( |
| 277 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
| 278 | "SetRecordingChannel() unable to set the recording channel"); |
| 279 | } |
| 280 | |
| 281 | // Map indices to unsigned since underlying functions need that |
| 282 | uint16_t indexU = static_cast<uint16_t>(index); |
| 283 | |
| 284 | int32_t res(0); |
| 285 | |
| 286 | if (index == -1) { |
| 287 | res = _shared->audio_device()->SetRecordingDevice( |
| 288 | AudioDeviceModule::kDefaultCommunicationDevice); |
| 289 | } else if (index == -2) { |
| 290 | res = _shared->audio_device()->SetRecordingDevice( |
| 291 | AudioDeviceModule::kDefaultDevice); |
| 292 | } else { |
| 293 | res = _shared->audio_device()->SetRecordingDevice(indexU); |
| 294 | } |
| 295 | |
| 296 | if (res != 0) { |
| 297 | _shared->SetLastError( |
| 298 | VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 299 | "SetRecordingDevice() unable to set the recording device"); |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | // Init microphone, so user can do volume settings etc |
| 304 | if (_shared->audio_device()->InitMicrophone() == -1) { |
| 305 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceWarning, |
| 306 | "SetRecordingDevice() cannot access microphone"); |
| 307 | } |
| 308 | |
| 309 | // Set number of channels |
| 310 | bool available = false; |
| 311 | if (_shared->audio_device()->StereoRecordingIsAvailable(&available) != 0) { |
| 312 | _shared->SetLastError( |
| 313 | VE_SOUNDCARD_ERROR, kTraceWarning, |
| 314 | "StereoRecordingIsAvailable() failed to query stereo recording"); |
| 315 | } |
| 316 | |
| 317 | if (_shared->audio_device()->SetStereoRecording(available) != 0) { |
| 318 | _shared->SetLastError( |
| 319 | VE_SOUNDCARD_ERROR, kTraceWarning, |
| 320 | "SetRecordingDevice() failed to set mono recording mode"); |
| 321 | } |
| 322 | |
| 323 | // Restore recording if it was enabled already when calling this function. |
| 324 | if (isRecording) { |
solenberg | e313e02 | 2015-09-08 02:16:04 -0700 | [diff] [blame] | 325 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 326 | "SetRecordingDevice() recording is now being restored..."); |
| 327 | if (_shared->audio_device()->InitRecording() != 0) { |
| 328 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 329 | VoEId(_shared->instance_id(), -1), |
| 330 | "SetRecordingDevice() failed to initialize recording"); |
| 331 | return -1; |
| 332 | } |
| 333 | if (_shared->audio_device()->StartRecording() != 0) { |
| 334 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 335 | VoEId(_shared->instance_id(), -1), |
| 336 | "SetRecordingDevice() failed to start recording"); |
| 337 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 338 | } |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 339 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 341 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 344 | int VoEHardwareImpl::SetPlayoutDevice(int index) { |
| 345 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 346 | "SetPlayoutDevice(index=%d)", index); |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 347 | rtc::CritScope cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 348 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 349 | if (!_shared->statistics().Initialized()) { |
| 350 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | bool isPlaying(false); |
| 355 | |
| 356 | // Store state about activated playout to be able to restore it after the |
| 357 | // playout device has been modified. |
| 358 | if (_shared->audio_device()->Playing()) { |
| 359 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 360 | "SetPlayoutDevice() device is modified while playout is " |
| 361 | "active..."); |
| 362 | isPlaying = true; |
| 363 | if (_shared->audio_device()->StopPlayout() == -1) { |
| 364 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 365 | "SetPlayoutDevice() unable to stop playout"); |
| 366 | return -1; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // We let the module do the index sanity |
| 371 | |
| 372 | // Map indices to unsigned since underlying functions need that |
| 373 | uint16_t indexU = static_cast<uint16_t>(index); |
| 374 | |
| 375 | int32_t res(0); |
| 376 | |
| 377 | if (index == -1) { |
| 378 | res = _shared->audio_device()->SetPlayoutDevice( |
| 379 | AudioDeviceModule::kDefaultCommunicationDevice); |
| 380 | } else if (index == -2) { |
| 381 | res = _shared->audio_device()->SetPlayoutDevice( |
| 382 | AudioDeviceModule::kDefaultDevice); |
| 383 | } else { |
| 384 | res = _shared->audio_device()->SetPlayoutDevice(indexU); |
| 385 | } |
| 386 | |
| 387 | if (res != 0) { |
| 388 | _shared->SetLastError( |
| 389 | VE_SOUNDCARD_ERROR, kTraceError, |
| 390 | "SetPlayoutDevice() unable to set the playout device"); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | // Init speaker, so user can do volume settings etc |
| 395 | if (_shared->audio_device()->InitSpeaker() == -1) { |
| 396 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceWarning, |
| 397 | "SetPlayoutDevice() cannot access speaker"); |
| 398 | } |
| 399 | |
| 400 | // Set number of channels |
| 401 | bool available = false; |
| 402 | _shared->audio_device()->StereoPlayoutIsAvailable(&available); |
| 403 | if (_shared->audio_device()->SetStereoPlayout(available) != 0) { |
| 404 | _shared->SetLastError( |
| 405 | VE_SOUNDCARD_ERROR, kTraceWarning, |
| 406 | "SetPlayoutDevice() failed to set stereo playout mode"); |
| 407 | } |
| 408 | |
| 409 | // Restore playout if it was enabled already when calling this function. |
| 410 | if (isPlaying) { |
solenberg | e313e02 | 2015-09-08 02:16:04 -0700 | [diff] [blame] | 411 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 412 | "SetPlayoutDevice() playout is now being restored..."); |
| 413 | if (_shared->audio_device()->InitPlayout() != 0) { |
| 414 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 415 | VoEId(_shared->instance_id(), -1), |
| 416 | "SetPlayoutDevice() failed to initialize playout"); |
| 417 | return -1; |
| 418 | } |
| 419 | if (_shared->audio_device()->StartPlayout() != 0) { |
| 420 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
| 421 | VoEId(_shared->instance_id(), -1), |
| 422 | "SetPlayoutDevice() failed to start playout"); |
| 423 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 424 | } |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 425 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 426 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 427 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | } |
| 429 | |
leozwang@webrtc.org | 96bcac8 | 2012-12-04 19:11:55 +0000 | [diff] [blame] | 430 | int VoEHardwareImpl::SetRecordingSampleRate(unsigned int samples_per_sec) { |
| 431 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 432 | "%s", __FUNCTION__); |
| 433 | if (!_shared->statistics().Initialized()) { |
| 434 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 435 | return false; |
| 436 | } |
| 437 | return _shared->audio_device()->SetRecordingSampleRate(samples_per_sec); |
| 438 | } |
| 439 | |
| 440 | int VoEHardwareImpl::RecordingSampleRate(unsigned int* samples_per_sec) const { |
leozwang@webrtc.org | 96bcac8 | 2012-12-04 19:11:55 +0000 | [diff] [blame] | 441 | if (!_shared->statistics().Initialized()) { |
| 442 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 443 | return false; |
| 444 | } |
| 445 | return _shared->audio_device()->RecordingSampleRate(samples_per_sec); |
| 446 | } |
| 447 | |
| 448 | int VoEHardwareImpl::SetPlayoutSampleRate(unsigned int samples_per_sec) { |
| 449 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 450 | "%s", __FUNCTION__); |
| 451 | if (!_shared->statistics().Initialized()) { |
| 452 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 453 | return false; |
| 454 | } |
| 455 | return _shared->audio_device()->SetPlayoutSampleRate(samples_per_sec); |
| 456 | } |
| 457 | |
| 458 | int VoEHardwareImpl::PlayoutSampleRate(unsigned int* samples_per_sec) const { |
leozwang@webrtc.org | 96bcac8 | 2012-12-04 19:11:55 +0000 | [diff] [blame] | 459 | if (!_shared->statistics().Initialized()) { |
| 460 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 461 | return false; |
| 462 | } |
| 463 | return _shared->audio_device()->PlayoutSampleRate(samples_per_sec); |
| 464 | } |
| 465 | |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 466 | bool VoEHardwareImpl::BuiltInAECIsAvailable() const { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 467 | if (!_shared->statistics().Initialized()) { |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 468 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 469 | return false; |
| 470 | } |
| 471 | return _shared->audio_device()->BuiltInAECIsAvailable(); |
| 472 | } |
| 473 | |
| 474 | int VoEHardwareImpl::EnableBuiltInAEC(bool enable) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 475 | if (!_shared->statistics().Initialized()) { |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 476 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
Bjorn Volcker | 1d83f1e | 2015-04-07 15:25:39 +0200 | [diff] [blame] | 477 | return -1; |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 478 | } |
| 479 | return _shared->audio_device()->EnableBuiltInAEC(enable); |
| 480 | } |
| 481 | |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 482 | bool VoEHardwareImpl::BuiltInAGCIsAvailable() const { |
| 483 | if (!_shared->statistics().Initialized()) { |
| 484 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 485 | return false; |
| 486 | } |
| 487 | return _shared->audio_device()->BuiltInAGCIsAvailable(); |
| 488 | } |
| 489 | |
| 490 | int VoEHardwareImpl::EnableBuiltInAGC(bool enable) { |
| 491 | if (!_shared->statistics().Initialized()) { |
| 492 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 493 | return -1; |
| 494 | } |
| 495 | return _shared->audio_device()->EnableBuiltInAGC(enable); |
| 496 | } |
| 497 | |
| 498 | bool VoEHardwareImpl::BuiltInNSIsAvailable() const { |
| 499 | if (!_shared->statistics().Initialized()) { |
| 500 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 501 | return false; |
| 502 | } |
| 503 | return _shared->audio_device()->BuiltInNSIsAvailable(); |
| 504 | } |
| 505 | |
| 506 | int VoEHardwareImpl::EnableBuiltInNS(bool enable) { |
| 507 | if (!_shared->statistics().Initialized()) { |
| 508 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 509 | return -1; |
| 510 | } |
| 511 | return _shared->audio_device()->EnableBuiltInNS(enable); |
| 512 | } |
| 513 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 514 | #endif // WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 515 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 516 | } // namespace webrtc |