| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/arraysize.h" | 11 #include "webrtc/base/arraysize.h" |
| 12 #include "webrtc/base/checks.h" | 12 #include "webrtc/base/checks.h" |
| 13 #include "webrtc/base/logging.h" |
| 13 #include "webrtc/base/platform_thread.h" | 14 #include "webrtc/base/platform_thread.h" |
| 14 #include "webrtc/modules/audio_device/audio_device_config.h" | 15 #include "webrtc/modules/audio_device/audio_device_config.h" |
| 15 #include "webrtc/modules/audio_device/mac/audio_device_mac.h" | 16 #include "webrtc/modules/audio_device/mac/audio_device_mac.h" |
| 16 #include "webrtc/modules/audio_device/mac/portaudio/pa_ringbuffer.h" | 17 #include "webrtc/modules/audio_device/mac/portaudio/pa_ringbuffer.h" |
| 17 #include "webrtc/system_wrappers/include/event_wrapper.h" | 18 #include "webrtc/system_wrappers/include/event_wrapper.h" |
| 18 #include "webrtc/system_wrappers/include/trace.h" | 19 #include "webrtc/system_wrappers/include/trace.h" |
| 19 | 20 |
| 20 #include <ApplicationServices/ApplicationServices.h> | 21 #include <ApplicationServices/ApplicationServices.h> |
| 21 #include <libkern/OSAtomic.h> // OSAtomicCompareAndSwap() | 22 #include <libkern/OSAtomic.h> // OSAtomicCompareAndSwap() |
| 22 #include <mach/mach.h> // mach_task_self() | 23 #include <mach/mach.h> // mach_task_self() |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 _ptrAudioBuffer->SetRecordingChannels(N_REC_CHANNELS); | 215 _ptrAudioBuffer->SetRecordingChannels(N_REC_CHANNELS); |
| 215 _ptrAudioBuffer->SetPlayoutChannels(N_PLAY_CHANNELS); | 216 _ptrAudioBuffer->SetPlayoutChannels(N_PLAY_CHANNELS); |
| 216 } | 217 } |
| 217 | 218 |
| 218 int32_t AudioDeviceMac::ActiveAudioLayer( | 219 int32_t AudioDeviceMac::ActiveAudioLayer( |
| 219 AudioDeviceModule::AudioLayer& audioLayer) const { | 220 AudioDeviceModule::AudioLayer& audioLayer) const { |
| 220 audioLayer = AudioDeviceModule::kPlatformDefaultAudio; | 221 audioLayer = AudioDeviceModule::kPlatformDefaultAudio; |
| 221 return 0; | 222 return 0; |
| 222 } | 223 } |
| 223 | 224 |
| 224 int32_t AudioDeviceMac::Init() { | 225 AudioDeviceGeneric::InitStatus AudioDeviceMac::Init() { |
| 225 CriticalSectionScoped lock(&_critSect); | 226 CriticalSectionScoped lock(&_critSect); |
| 226 | 227 |
| 227 if (_initialized) { | 228 if (_initialized) { |
| 228 return 0; | 229 return InitStatus::OK; |
| 229 } | 230 } |
| 230 | 231 |
| 231 OSStatus err = noErr; | 232 OSStatus err = noErr; |
| 232 | 233 |
| 233 _isShutDown = false; | 234 _isShutDown = false; |
| 234 | 235 |
| 235 // PortAudio ring buffers require an elementCount which is a power of two. | 236 // PortAudio ring buffers require an elementCount which is a power of two. |
| 236 if (_renderBufData == NULL) { | 237 if (_renderBufData == NULL) { |
| 237 UInt32 powerOfTwo = 1; | 238 UInt32 powerOfTwo = 1; |
| 238 while (powerOfTwo < PLAY_BUF_SIZE_IN_SAMPLES) { | 239 while (powerOfTwo < PLAY_BUF_SIZE_IN_SAMPLES) { |
| 239 powerOfTwo <<= 1; | 240 powerOfTwo <<= 1; |
| 240 } | 241 } |
| 241 _renderBufSizeSamples = powerOfTwo; | 242 _renderBufSizeSamples = powerOfTwo; |
| 242 _renderBufData = new SInt16[_renderBufSizeSamples]; | 243 _renderBufData = new SInt16[_renderBufSizeSamples]; |
| 243 } | 244 } |
| 244 | 245 |
| 245 if (_paRenderBuffer == NULL) { | 246 if (_paRenderBuffer == NULL) { |
| 246 _paRenderBuffer = new PaUtilRingBuffer; | 247 _paRenderBuffer = new PaUtilRingBuffer; |
| 247 PaRingBufferSize bufSize = -1; | 248 PaRingBufferSize bufSize = -1; |
| 248 bufSize = PaUtil_InitializeRingBuffer( | 249 bufSize = PaUtil_InitializeRingBuffer( |
| 249 _paRenderBuffer, sizeof(SInt16), _renderBufSizeSamples, _renderBufData); | 250 _paRenderBuffer, sizeof(SInt16), _renderBufSizeSamples, _renderBufData); |
| 250 if (bufSize == -1) { | 251 if (bufSize == -1) { |
| 251 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, | 252 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, |
| 252 " PaUtil_InitializeRingBuffer() error"); | 253 " PaUtil_InitializeRingBuffer() error"); |
| 253 return -1; | 254 return InitStatus::PLAYOUT_ERROR; |
| 254 } | 255 } |
| 255 } | 256 } |
| 256 | 257 |
| 257 if (_captureBufData == NULL) { | 258 if (_captureBufData == NULL) { |
| 258 UInt32 powerOfTwo = 1; | 259 UInt32 powerOfTwo = 1; |
| 259 while (powerOfTwo < REC_BUF_SIZE_IN_SAMPLES) { | 260 while (powerOfTwo < REC_BUF_SIZE_IN_SAMPLES) { |
| 260 powerOfTwo <<= 1; | 261 powerOfTwo <<= 1; |
| 261 } | 262 } |
| 262 _captureBufSizeSamples = powerOfTwo; | 263 _captureBufSizeSamples = powerOfTwo; |
| 263 _captureBufData = new Float32[_captureBufSizeSamples]; | 264 _captureBufData = new Float32[_captureBufSizeSamples]; |
| 264 } | 265 } |
| 265 | 266 |
| 266 if (_paCaptureBuffer == NULL) { | 267 if (_paCaptureBuffer == NULL) { |
| 267 _paCaptureBuffer = new PaUtilRingBuffer; | 268 _paCaptureBuffer = new PaUtilRingBuffer; |
| 268 PaRingBufferSize bufSize = -1; | 269 PaRingBufferSize bufSize = -1; |
| 269 bufSize = | 270 bufSize = |
| 270 PaUtil_InitializeRingBuffer(_paCaptureBuffer, sizeof(Float32), | 271 PaUtil_InitializeRingBuffer(_paCaptureBuffer, sizeof(Float32), |
| 271 _captureBufSizeSamples, _captureBufData); | 272 _captureBufSizeSamples, _captureBufData); |
| 272 if (bufSize == -1) { | 273 if (bufSize == -1) { |
| 273 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, | 274 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, |
| 274 " PaUtil_InitializeRingBuffer() error"); | 275 " PaUtil_InitializeRingBuffer() error"); |
| 275 return -1; | 276 return InitStatus::RECORDING_ERROR; |
| 276 } | 277 } |
| 277 } | 278 } |
| 278 | 279 |
| 279 kern_return_t kernErr = KERN_SUCCESS; | 280 kern_return_t kernErr = KERN_SUCCESS; |
| 280 kernErr = semaphore_create(mach_task_self(), &_renderSemaphore, | 281 kernErr = semaphore_create(mach_task_self(), &_renderSemaphore, |
| 281 SYNC_POLICY_FIFO, 0); | 282 SYNC_POLICY_FIFO, 0); |
| 282 if (kernErr != KERN_SUCCESS) { | 283 if (kernErr != KERN_SUCCESS) { |
| 283 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, | 284 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, |
| 284 " semaphore_create() error: %d", kernErr); | 285 " semaphore_create() error: %d", kernErr); |
| 285 return -1; | 286 return InitStatus::OTHER_ERROR; |
| 286 } | 287 } |
| 287 | 288 |
| 288 kernErr = semaphore_create(mach_task_self(), &_captureSemaphore, | 289 kernErr = semaphore_create(mach_task_self(), &_captureSemaphore, |
| 289 SYNC_POLICY_FIFO, 0); | 290 SYNC_POLICY_FIFO, 0); |
| 290 if (kernErr != KERN_SUCCESS) { | 291 if (kernErr != KERN_SUCCESS) { |
| 291 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, | 292 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, |
| 292 " semaphore_create() error: %d", kernErr); | 293 " semaphore_create() error: %d", kernErr); |
| 293 return -1; | 294 return InitStatus::OTHER_ERROR; |
| 294 } | 295 } |
| 295 | 296 |
| 296 // Setting RunLoop to NULL here instructs HAL to manage its own thread for | 297 // Setting RunLoop to NULL here instructs HAL to manage its own thread for |
| 297 // notifications. This was the default behaviour on OS X 10.5 and earlier, | 298 // notifications. This was the default behaviour on OS X 10.5 and earlier, |
| 298 // but now must be explicitly specified. HAL would otherwise try to use the | 299 // but now must be explicitly specified. HAL would otherwise try to use the |
| 299 // main thread to issue notifications. | 300 // main thread to issue notifications. |
| 300 AudioObjectPropertyAddress propertyAddress = { | 301 AudioObjectPropertyAddress propertyAddress = { |
| 301 kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, | 302 kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, |
| 302 kAudioObjectPropertyElementMaster}; | 303 kAudioObjectPropertyElementMaster}; |
| 303 CFRunLoopRef runLoop = NULL; | 304 CFRunLoopRef runLoop = NULL; |
| 304 UInt32 size = sizeof(CFRunLoopRef); | 305 UInt32 size = sizeof(CFRunLoopRef); |
| 305 WEBRTC_CA_RETURN_ON_ERR(AudioObjectSetPropertyData( | 306 int aoerr = AudioObjectSetPropertyData( |
| 306 kAudioObjectSystemObject, &propertyAddress, 0, NULL, size, &runLoop)); | 307 kAudioObjectSystemObject, &propertyAddress, 0, NULL, size, &runLoop); |
| 308 if (aoerr != noErr) { |
| 309 LOG(LS_ERROR) << "Error in AudioObjectSetPropertyData: " |
| 310 << (const char*)&aoerr; |
| 311 return InitStatus::OTHER_ERROR; |
| 312 } |
| 307 | 313 |
| 308 // Listen for any device changes. | 314 // Listen for any device changes. |
| 309 propertyAddress.mSelector = kAudioHardwarePropertyDevices; | 315 propertyAddress.mSelector = kAudioHardwarePropertyDevices; |
| 310 WEBRTC_CA_LOG_ERR(AudioObjectAddPropertyListener( | 316 WEBRTC_CA_LOG_ERR(AudioObjectAddPropertyListener( |
| 311 kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this)); | 317 kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this)); |
| 312 | 318 |
| 313 // Determine if this is a MacBook Pro | 319 // Determine if this is a MacBook Pro |
| 314 _macBookPro = false; | 320 _macBookPro = false; |
| 315 _macBookProPanRight = false; | 321 _macBookProPanRight = false; |
| 316 char buf[128]; | 322 char buf[128]; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 331 | 337 |
| 332 _playWarning = 0; | 338 _playWarning = 0; |
| 333 _playError = 0; | 339 _playError = 0; |
| 334 _recWarning = 0; | 340 _recWarning = 0; |
| 335 _recError = 0; | 341 _recError = 0; |
| 336 | 342 |
| 337 get_mic_volume_counter_ms_ = 0; | 343 get_mic_volume_counter_ms_ = 0; |
| 338 | 344 |
| 339 _initialized = true; | 345 _initialized = true; |
| 340 | 346 |
| 341 return 0; | 347 return InitStatus::OK; |
| 342 } | 348 } |
| 343 | 349 |
| 344 int32_t AudioDeviceMac::Terminate() { | 350 int32_t AudioDeviceMac::Terminate() { |
| 345 if (!_initialized) { | 351 if (!_initialized) { |
| 346 return 0; | 352 return 0; |
| 347 } | 353 } |
| 348 | 354 |
| 349 if (_recording) { | 355 if (_recording) { |
| 350 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, | 356 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, |
| 351 " Recording must be stopped"); | 357 " Recording must be stopped"); |
| (...skipping 2411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2763 bool keyState = | 2769 bool keyState = |
| 2764 CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key_index); | 2770 CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key_index); |
| 2765 // A false -> true change in keymap means a key is pressed. | 2771 // A false -> true change in keymap means a key is pressed. |
| 2766 key_down |= (keyState && !prev_key_state_[key_index]); | 2772 key_down |= (keyState && !prev_key_state_[key_index]); |
| 2767 // Save current state. | 2773 // Save current state. |
| 2768 prev_key_state_[key_index] = keyState; | 2774 prev_key_state_[key_index] = keyState; |
| 2769 } | 2775 } |
| 2770 return key_down; | 2776 return key_down; |
| 2771 } | 2777 } |
| 2772 } // namespace webrtc | 2778 } // namespace webrtc |
| OLD | NEW |