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 | |
pbos@webrtc.org | 956aa7e | 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" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +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 | 0989fb7 | 2013-02-15 15:07:32 +0000 | [diff] [blame] | 32 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 33 | s->AddRef(); |
| 34 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | #endif |
| 36 | } |
| 37 | |
| 38 | #ifdef WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 39 | |
henrike@webrtc.org | 93bea51 | 2013-03-28 15:58:49 +0000 | [diff] [blame] | 40 | VoEHardwareImpl::VoEHardwareImpl(voe::SharedData* shared) : _shared(shared) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 42 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | "VoEHardwareImpl() - ctor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | VoEHardwareImpl::~VoEHardwareImpl() |
| 47 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 48 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | "~VoEHardwareImpl() - dtor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | int VoEHardwareImpl::SetAudioDeviceLayer(AudioLayers audioLayer) |
| 53 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 54 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | "SetAudioDeviceLayer(audioLayer=%d)", audioLayer); |
| 56 | |
| 57 | // Don't allow a change if VoE is initialized |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 58 | if (_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 60 | _shared->SetLastError(VE_ALREADY_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Save the audio device layer for Init() |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 87 | _shared->set_audio_device_layer(wantedLayer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int VoEHardwareImpl::GetAudioDeviceLayer(AudioLayers& audioLayer) |
| 93 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 94 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | "GetAudioDeviceLayer(devices=?)"); |
| 96 | |
| 97 | // Can always be called regardless of VoE state |
| 98 | |
| 99 | AudioDeviceModule::AudioLayer |
| 100 | activeLayer(AudioDeviceModule::kPlatformDefaultAudio); |
| 101 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 102 | if (_shared->audio_device()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | { |
| 104 | // Get active audio layer from ADM |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 105 | if (_shared->audio_device()->ActiveAudioLayer(&activeLayer) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 107 | _shared->SetLastError(VE_UNDEFINED_SC_ERR, kTraceError, |
| 108 | " Audio Device error"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | // Return VoE's internal layer setting |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 115 | activeLayer = _shared->audio_device_layer(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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: |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 137 | _shared->SetLastError(VE_UNDEFINED_SC_ERR, kTraceError, |
| 138 | " unknown audio layer"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 141 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 142 | VoEId(_shared->instance_id(), -1), |
| 143 | " Output: audioLayer=%d", audioLayer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | int VoEHardwareImpl::GetNumOfRecordingDevices(int& devices) |
| 148 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 149 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | "GetNumOfRecordingDevices(devices=?)"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 152 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 154 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 158 | devices = static_cast<int> (_shared->audio_device()->RecordingDevices()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 160 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 161 | VoEId(_shared->instance_id(), -1), " Output: devices=%d", devices); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int VoEHardwareImpl::GetNumOfPlayoutDevices(int& devices) |
| 167 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 168 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | "GetNumOfPlayoutDevices(devices=?)"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 171 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 173 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 177 | devices = static_cast<int> (_shared->audio_device()->PlayoutDevices()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 179 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 180 | VoEId(_shared->instance_id(), -1), |
| 181 | " Output: devices=%d", devices); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int VoEHardwareImpl::GetRecordingDeviceName(int index, |
| 187 | char strNameUTF8[128], |
| 188 | char strGuidUTF8[128]) |
| 189 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 190 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | "GetRecordingDeviceName(index=%d)", index); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 193 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 195 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | if (strNameUTF8 == NULL) |
| 199 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 200 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 208 | const uint16_t strLen = 128; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | |
| 210 | // Check if length has been changed in module |
| 211 | assert(strLen == kAdmMaxDeviceNameSize); |
| 212 | assert(strLen == kAdmMaxGuidSize); |
| 213 | |
leozwang@webrtc.org | 813e4b0 | 2012-03-01 18:34:25 +0000 | [diff] [blame] | 214 | char name[strLen]; |
| 215 | char guid[strLen]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | |
| 217 | // Get names from module |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 218 | if (_shared->audio_device()->RecordingDeviceName(index, name, guid) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 220 | _shared->SetLastError(VE_CANNOT_RETRIEVE_DEVICE_NAME, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | "GetRecordingDeviceName() failed to get device name"); |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | // Copy to vectors supplied by user |
| 226 | strncpy(strNameUTF8, name, strLen); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 227 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 228 | VoEId(_shared->instance_id(), -1), |
| 229 | " Output: strNameUTF8=%s", strNameUTF8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | |
| 231 | if (strGuidUTF8 != NULL) |
| 232 | { |
xians@google.com | 49d025f | 2011-09-27 14:43:06 +0000 | [diff] [blame] | 233 | strncpy(strGuidUTF8, guid, strLen); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 234 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 235 | VoEId(_shared->instance_id(), -1), |
| 236 | " Output: strGuidUTF8=%s", strGuidUTF8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | int VoEHardwareImpl::GetPlayoutDeviceName(int index, |
| 243 | char strNameUTF8[128], |
| 244 | char strGuidUTF8[128]) |
| 245 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 246 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | "GetPlayoutDeviceName(index=%d)", index); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 249 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 251 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | return -1; |
| 253 | } |
| 254 | if (strNameUTF8 == NULL) |
| 255 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 256 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 264 | const uint16_t strLen = 128; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | |
| 266 | // Check if length has been changed in module |
| 267 | assert(strLen == kAdmMaxDeviceNameSize); |
| 268 | assert(strLen == kAdmMaxGuidSize); |
| 269 | |
leozwang@webrtc.org | 813e4b0 | 2012-03-01 18:34:25 +0000 | [diff] [blame] | 270 | char name[strLen]; |
| 271 | char guid[strLen]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 272 | |
| 273 | // Get names from module |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 274 | if (_shared->audio_device()->PlayoutDeviceName(index, name, guid) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 276 | _shared->SetLastError(VE_CANNOT_RETRIEVE_DEVICE_NAME, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 277 | "GetPlayoutDeviceName() failed to get device name"); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | // Copy to vectors supplied by user |
| 282 | strncpy(strNameUTF8, name, strLen); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 283 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 284 | VoEId(_shared->instance_id(), -1), |
| 285 | " Output: strNameUTF8=%s", strNameUTF8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 286 | |
| 287 | if (strGuidUTF8 != NULL) |
| 288 | { |
xians@google.com | b875349 | 2011-09-05 12:17:30 +0000 | [diff] [blame] | 289 | strncpy(strGuidUTF8, guid, strLen); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 290 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 291 | VoEId(_shared->instance_id(), -1), |
| 292 | " Output: strGuidUTF8=%s", strGuidUTF8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | int VoEHardwareImpl::SetRecordingDevice(int index, |
| 299 | StereoChannel recordingChannel) |
| 300 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 301 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 302 | "SetRecordingDevice(index=%d, recordingChannel=%d)", |
| 303 | index, (int) recordingChannel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 304 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 305 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 306 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 308 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 316 | if (_shared->audio_device()->Recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 318 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 319 | "SetRecordingDevice() device is modified while recording" |
| 320 | " is active..."); |
| 321 | isRecording = true; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 322 | if (_shared->audio_device()->StopRecording() == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 323 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 324 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | } |
| 347 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 348 | if (_shared->audio_device()->SetRecordingChannel(recCh) != 0) { |
| 349 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 350 | "SetRecordingChannel() unable to set the recording channel"); |
| 351 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 352 | |
| 353 | // Map indices to unsigned since underlying functions need that |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 354 | uint16_t indexU = static_cast<uint16_t> (index); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 355 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 356 | int32_t res(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | |
| 358 | if (index == -1) |
| 359 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 360 | res = _shared->audio_device()->SetRecordingDevice( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 361 | AudioDeviceModule::kDefaultCommunicationDevice); |
| 362 | } |
| 363 | else if (index == -2) |
| 364 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 365 | res = _shared->audio_device()->SetRecordingDevice( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 366 | AudioDeviceModule::kDefaultDevice); |
| 367 | } |
| 368 | else |
| 369 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 370 | res = _shared->audio_device()->SetRecordingDevice(indexU); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (res != 0) |
| 374 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 375 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 376 | "SetRecordingDevice() unable to set the recording device"); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | // Init microphone, so user can do volume settings etc |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 381 | if (_shared->audio_device()->InitMicrophone() == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 383 | _shared->SetLastError(VE_CANNOT_ACCESS_MIC_VOL, kTraceWarning, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 384 | "SetRecordingDevice() cannot access microphone"); |
| 385 | } |
| 386 | |
| 387 | // Set number of channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 388 | bool available = false; |
| 389 | if (_shared->audio_device()->StereoRecordingIsAvailable(&available) != 0) { |
| 390 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 391 | "StereoRecordingIsAvailable() failed to query stereo recording"); |
| 392 | } |
| 393 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 394 | if (_shared->audio_device()->SetStereoRecording(available) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 395 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 396 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 403 | if (!_shared->ext_recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 404 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 405 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, |
| 406 | VoEId(_shared->instance_id(), -1), |
| 407 | "SetRecordingDevice() recording is now being restored..."); |
| 408 | if (_shared->audio_device()->InitRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 409 | { |
| 410 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 411 | VoEId(_shared->instance_id(), -1), |
| 412 | "SetRecordingDevice() failed to initialize recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | return -1; |
| 414 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 415 | if (_shared->audio_device()->StartRecording() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 416 | { |
| 417 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 418 | VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 430 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 431 | "SetPlayoutDevice(index=%d)", index); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 432 | CriticalSectionScoped cs(_shared->crit_sec()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 433 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 434 | if (!_shared->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 435 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 436 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 444 | if (_shared->audio_device()->Playing()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 445 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 446 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 447 | "SetPlayoutDevice() device is modified while playout is " |
| 448 | "active..."); |
| 449 | isPlaying = true; |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 450 | if (_shared->audio_device()->StopPlayout() == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 452 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 461 | uint16_t indexU = static_cast<uint16_t> (index); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 462 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 463 | int32_t res(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 464 | |
| 465 | if (index == -1) |
| 466 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 467 | res = _shared->audio_device()->SetPlayoutDevice( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | AudioDeviceModule::kDefaultCommunicationDevice); |
| 469 | } |
| 470 | else if (index == -2) |
| 471 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 472 | res = _shared->audio_device()->SetPlayoutDevice( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | AudioDeviceModule::kDefaultDevice); |
| 474 | } |
| 475 | else |
| 476 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 477 | res = _shared->audio_device()->SetPlayoutDevice(indexU); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if (res != 0) |
| 481 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 482 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 483 | "SetPlayoutDevice() unable to set the playout device"); |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | // Init speaker, so user can do volume settings etc |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 488 | if (_shared->audio_device()->InitSpeaker() == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 489 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 490 | _shared->SetLastError(VE_CANNOT_ACCESS_SPEAKER_VOL, kTraceWarning, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 491 | "SetPlayoutDevice() cannot access speaker"); |
| 492 | } |
| 493 | |
| 494 | // Set number of channels |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 495 | bool available = false; |
| 496 | _shared->audio_device()->StereoPlayoutIsAvailable(&available); |
| 497 | if (_shared->audio_device()->SetStereoPlayout(available) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 499 | _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceWarning, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 506 | if (!_shared->ext_playout()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 507 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 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) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 512 | { |
| 513 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 514 | VoEId(_shared->instance_id(), -1), |
| 515 | "SetPlayoutDevice() failed to initialize playout"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 516 | return -1; |
| 517 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 518 | if (_shared->audio_device()->StartPlayout() != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | { |
| 520 | WEBRTC_TRACE(kTraceError, kTraceVoice, |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 521 | VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 522 | "SetPlayoutDevice() failed to start playout"); |
| 523 | return -1; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
leozwang@webrtc.org | 96bcac8 | 2012-12-04 19:11:55 +0000 | [diff] [blame] | 531 | int VoEHardwareImpl::SetRecordingSampleRate(unsigned int samples_per_sec) { |
| 532 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 533 | "%s", __FUNCTION__); |
| 534 | if (!_shared->statistics().Initialized()) { |
| 535 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 536 | return false; |
| 537 | } |
| 538 | return _shared->audio_device()->SetRecordingSampleRate(samples_per_sec); |
| 539 | } |
| 540 | |
| 541 | int VoEHardwareImpl::RecordingSampleRate(unsigned int* samples_per_sec) const { |
| 542 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 543 | "%s", __FUNCTION__); |
| 544 | if (!_shared->statistics().Initialized()) { |
| 545 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 546 | return false; |
| 547 | } |
| 548 | return _shared->audio_device()->RecordingSampleRate(samples_per_sec); |
| 549 | } |
| 550 | |
| 551 | int VoEHardwareImpl::SetPlayoutSampleRate(unsigned int samples_per_sec) { |
| 552 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 553 | "%s", __FUNCTION__); |
| 554 | if (!_shared->statistics().Initialized()) { |
| 555 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 556 | return false; |
| 557 | } |
| 558 | return _shared->audio_device()->SetPlayoutSampleRate(samples_per_sec); |
| 559 | } |
| 560 | |
| 561 | int VoEHardwareImpl::PlayoutSampleRate(unsigned int* samples_per_sec) const { |
| 562 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 563 | "%s", __FUNCTION__); |
| 564 | if (!_shared->statistics().Initialized()) { |
| 565 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 566 | return false; |
| 567 | } |
| 568 | return _shared->audio_device()->PlayoutSampleRate(samples_per_sec); |
| 569 | } |
| 570 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 571 | #endif // WEBRTC_VOICE_ENGINE_HARDWARE_API |
| 572 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 573 | } // namespace webrtc |