xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 20aabbb | 2012-02-20 09:17:41 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_device/mac/audio_device_mac.h" |
| 12 | #include "modules/audio_device/audio_device_config.h" |
| 13 | #include "modules/audio_device/mac/portaudio/pa_ringbuffer.h" |
| 14 | #include "rtc_base/arraysize.h" |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/platform_thread.h" |
| 17 | #include "system_wrappers/include/event_wrapper.h" |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 18 | |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 19 | #include <ApplicationServices/ApplicationServices.h> |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 20 | #include <libkern/OSAtomic.h> // OSAtomicCompareAndSwap() |
| 21 | #include <mach/mach.h> // mach_task_self() |
| 22 | #include <sys/sysctl.h> // sysctlbyname() |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 23 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 24 | namespace webrtc { |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 25 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 26 | #define WEBRTC_CA_RETURN_ON_ERR(expr) \ |
| 27 | do { \ |
| 28 | err = expr; \ |
| 29 | if (err != noErr) { \ |
| 30 | logCAMsg(rtc::LS_ERROR, "Error in " #expr, (const char*)&err); \ |
| 31 | return -1; \ |
| 32 | } \ |
| 33 | } while (0) |
| 34 | |
| 35 | #define WEBRTC_CA_LOG_ERR(expr) \ |
| 36 | do { \ |
| 37 | err = expr; \ |
| 38 | if (err != noErr) { \ |
| 39 | logCAMsg(rtc::LS_ERROR, "Error in " #expr, (const char*)&err); \ |
| 40 | } \ |
| 41 | } while (0) |
| 42 | |
| 43 | #define WEBRTC_CA_LOG_WARN(expr) \ |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 44 | do { \ |
| 45 | err = expr; \ |
| 46 | if (err != noErr) { \ |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 47 | logCAMsg(rtc::LS_WARNING, "Error in " #expr, (const char*)&err); \ |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 48 | } \ |
| 49 | } while (0) |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 50 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 51 | enum { MaxNumberDevices = 64 }; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 52 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 53 | void AudioDeviceMac::AtomicSet32(int32_t* theValue, int32_t newValue) { |
| 54 | while (1) { |
| 55 | int32_t oldValue = *theValue; |
| 56 | if (OSAtomicCompareAndSwap32Barrier(oldValue, newValue, theValue) == true) { |
| 57 | return; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 58 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 59 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 60 | } |
| 61 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 62 | int32_t AudioDeviceMac::AtomicGet32(int32_t* theValue) { |
| 63 | while (1) { |
| 64 | int32_t value = *theValue; |
| 65 | if (OSAtomicCompareAndSwap32Barrier(value, value, theValue) == true) { |
| 66 | return value; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 67 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 68 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // CoreAudio errors are best interpreted as four character strings. |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 72 | void AudioDeviceMac::logCAMsg(const rtc::LoggingSeverity sev, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 73 | const char* msg, |
| 74 | const char* err) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 75 | RTC_DCHECK(msg != NULL); |
| 76 | RTC_DCHECK(err != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 77 | |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 78 | #ifdef WEBRTC_ARCH_BIG_ENDIAN |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 79 | switch (sev) { |
| 80 | case rtc::LS_ERROR: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 81 | RTC_LOG(LS_ERROR) << msg << ": " << err[0] << err[1] << err[2] << err[3]; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 82 | break; |
| 83 | case rtc::LS_WARNING: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 84 | RTC_LOG(LS_WARNING) << msg << ": " << err[0] << err[1] << err[2] |
| 85 | << err[3]; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 86 | break; |
| 87 | case rtc::LS_VERBOSE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 88 | RTC_LOG(LS_VERBOSE) << msg << ": " << err[0] << err[1] << err[2] |
| 89 | << err[3]; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 90 | break; |
| 91 | default: |
| 92 | break; |
| 93 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 94 | #else |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 95 | // We need to flip the characters in this case. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 96 | switch (sev) { |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 97 | case rtc::LS_ERROR: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 98 | RTC_LOG(LS_ERROR) << msg << ": " << err[3] << err[2] << err[1] << err[0]; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 99 | break; |
| 100 | case rtc::LS_WARNING: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 101 | RTC_LOG(LS_WARNING) << msg << ": " << err[3] << err[2] << err[1] |
| 102 | << err[0]; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 103 | break; |
| 104 | case rtc::LS_VERBOSE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 105 | RTC_LOG(LS_VERBOSE) << msg << ": " << err[3] << err[2] << err[1] |
| 106 | << err[0]; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | break; |
| 110 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 111 | #endif |
| 112 | } |
| 113 | |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 114 | AudioDeviceMac::AudioDeviceMac() |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 115 | : _ptrAudioBuffer(NULL), |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 116 | _stopEventRec(*EventWrapper::Create()), |
| 117 | _stopEvent(*EventWrapper::Create()), |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 118 | _mixerManager(), |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 119 | _inputDeviceIndex(0), |
| 120 | _outputDeviceIndex(0), |
| 121 | _inputDeviceID(kAudioObjectUnknown), |
| 122 | _outputDeviceID(kAudioObjectUnknown), |
| 123 | _inputDeviceIsSpecified(false), |
| 124 | _outputDeviceIsSpecified(false), |
| 125 | _recChannels(N_REC_CHANNELS), |
| 126 | _playChannels(N_PLAY_CHANNELS), |
| 127 | _captureBufData(NULL), |
| 128 | _renderBufData(NULL), |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 129 | _initialized(false), |
| 130 | _isShutDown(false), |
| 131 | _recording(false), |
| 132 | _playing(false), |
| 133 | _recIsInitialized(false), |
| 134 | _playIsInitialized(false), |
| 135 | _AGC(false), |
| 136 | _renderDeviceIsAlive(1), |
| 137 | _captureDeviceIsAlive(1), |
| 138 | _twoDevices(true), |
| 139 | _doStop(false), |
| 140 | _doStopRec(false), |
| 141 | _macBookPro(false), |
| 142 | _macBookProPanRight(false), |
| 143 | _captureLatencyUs(0), |
| 144 | _renderLatencyUs(0), |
| 145 | _captureDelayUs(0), |
| 146 | _renderDelayUs(0), |
| 147 | _renderDelayOffsetSamples(0), |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 148 | _paCaptureBuffer(NULL), |
| 149 | _paRenderBuffer(NULL), |
| 150 | _captureBufSizeSamples(0), |
| 151 | _renderBufSizeSamples(0), |
| 152 | prev_key_state_(), |
| 153 | get_mic_volume_counter_ms_(0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 154 | RTC_LOG(LS_INFO) << __FUNCTION__ << " created"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 155 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 156 | RTC_DCHECK(&_stopEvent != NULL); |
| 157 | RTC_DCHECK(&_stopEventRec != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 158 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 159 | memset(_renderConvertData, 0, sizeof(_renderConvertData)); |
| 160 | memset(&_outStreamFormat, 0, sizeof(AudioStreamBasicDescription)); |
| 161 | memset(&_outDesiredFormat, 0, sizeof(AudioStreamBasicDescription)); |
| 162 | memset(&_inStreamFormat, 0, sizeof(AudioStreamBasicDescription)); |
| 163 | memset(&_inDesiredFormat, 0, sizeof(AudioStreamBasicDescription)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 164 | } |
| 165 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 166 | AudioDeviceMac::~AudioDeviceMac() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 167 | RTC_LOG(LS_INFO) << __FUNCTION__ << " destroyed"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 168 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 169 | if (!_isShutDown) { |
| 170 | Terminate(); |
| 171 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 172 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 173 | RTC_DCHECK(!capture_worker_thread_.get()); |
| 174 | RTC_DCHECK(!render_worker_thread_.get()); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 175 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 176 | if (_paRenderBuffer) { |
| 177 | delete _paRenderBuffer; |
| 178 | _paRenderBuffer = NULL; |
| 179 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 180 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 181 | if (_paCaptureBuffer) { |
| 182 | delete _paCaptureBuffer; |
| 183 | _paCaptureBuffer = NULL; |
| 184 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 185 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 186 | if (_renderBufData) { |
| 187 | delete[] _renderBufData; |
| 188 | _renderBufData = NULL; |
| 189 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 190 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 191 | if (_captureBufData) { |
| 192 | delete[] _captureBufData; |
| 193 | _captureBufData = NULL; |
| 194 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 195 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 196 | kern_return_t kernErr = KERN_SUCCESS; |
| 197 | kernErr = semaphore_destroy(mach_task_self(), _renderSemaphore); |
| 198 | if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 199 | RTC_LOG(LS_ERROR) << "semaphore_destroy() error: " << kernErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 200 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 201 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 202 | kernErr = semaphore_destroy(mach_task_self(), _captureSemaphore); |
| 203 | if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 204 | RTC_LOG(LS_ERROR) << "semaphore_destroy() error: " << kernErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 205 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 206 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 207 | delete &_stopEvent; |
| 208 | delete &_stopEventRec; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // ============================================================================ |
| 212 | // API |
| 213 | // ============================================================================ |
| 214 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 215 | void AudioDeviceMac::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 216 | rtc::CritScope lock(&_critSect); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 217 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 218 | _ptrAudioBuffer = audioBuffer; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 219 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 220 | // inform the AudioBuffer about default settings for this implementation |
| 221 | _ptrAudioBuffer->SetRecordingSampleRate(N_REC_SAMPLES_PER_SEC); |
| 222 | _ptrAudioBuffer->SetPlayoutSampleRate(N_PLAY_SAMPLES_PER_SEC); |
| 223 | _ptrAudioBuffer->SetRecordingChannels(N_REC_CHANNELS); |
| 224 | _ptrAudioBuffer->SetPlayoutChannels(N_PLAY_CHANNELS); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 225 | } |
| 226 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 227 | int32_t AudioDeviceMac::ActiveAudioLayer( |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 228 | AudioDeviceModule::AudioLayer& audioLayer) const { |
| 229 | audioLayer = AudioDeviceModule::kPlatformDefaultAudio; |
| 230 | return 0; |
| 231 | } |
| 232 | |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 233 | AudioDeviceGeneric::InitStatus AudioDeviceMac::Init() { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 234 | rtc::CritScope lock(&_critSect); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 235 | |
| 236 | if (_initialized) { |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 237 | return InitStatus::OK; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | OSStatus err = noErr; |
| 241 | |
| 242 | _isShutDown = false; |
| 243 | |
| 244 | // PortAudio ring buffers require an elementCount which is a power of two. |
| 245 | if (_renderBufData == NULL) { |
| 246 | UInt32 powerOfTwo = 1; |
| 247 | while (powerOfTwo < PLAY_BUF_SIZE_IN_SAMPLES) { |
| 248 | powerOfTwo <<= 1; |
| 249 | } |
| 250 | _renderBufSizeSamples = powerOfTwo; |
| 251 | _renderBufData = new SInt16[_renderBufSizeSamples]; |
| 252 | } |
| 253 | |
| 254 | if (_paRenderBuffer == NULL) { |
| 255 | _paRenderBuffer = new PaUtilRingBuffer; |
| 256 | PaRingBufferSize bufSize = -1; |
| 257 | bufSize = PaUtil_InitializeRingBuffer( |
| 258 | _paRenderBuffer, sizeof(SInt16), _renderBufSizeSamples, _renderBufData); |
| 259 | if (bufSize == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 260 | RTC_LOG(LS_ERROR) << "PaUtil_InitializeRingBuffer() error"; |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 261 | return InitStatus::PLAYOUT_ERROR; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | if (_captureBufData == NULL) { |
| 266 | UInt32 powerOfTwo = 1; |
| 267 | while (powerOfTwo < REC_BUF_SIZE_IN_SAMPLES) { |
| 268 | powerOfTwo <<= 1; |
| 269 | } |
| 270 | _captureBufSizeSamples = powerOfTwo; |
| 271 | _captureBufData = new Float32[_captureBufSizeSamples]; |
| 272 | } |
| 273 | |
| 274 | if (_paCaptureBuffer == NULL) { |
| 275 | _paCaptureBuffer = new PaUtilRingBuffer; |
| 276 | PaRingBufferSize bufSize = -1; |
| 277 | bufSize = |
| 278 | PaUtil_InitializeRingBuffer(_paCaptureBuffer, sizeof(Float32), |
| 279 | _captureBufSizeSamples, _captureBufData); |
| 280 | if (bufSize == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 281 | RTC_LOG(LS_ERROR) << "PaUtil_InitializeRingBuffer() error"; |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 282 | return InitStatus::RECORDING_ERROR; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | kern_return_t kernErr = KERN_SUCCESS; |
| 287 | kernErr = semaphore_create(mach_task_self(), &_renderSemaphore, |
| 288 | SYNC_POLICY_FIFO, 0); |
| 289 | if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 290 | RTC_LOG(LS_ERROR) << "semaphore_create() error: " << kernErr; |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 291 | return InitStatus::OTHER_ERROR; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | kernErr = semaphore_create(mach_task_self(), &_captureSemaphore, |
| 295 | SYNC_POLICY_FIFO, 0); |
| 296 | if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 297 | RTC_LOG(LS_ERROR) << "semaphore_create() error: " << kernErr; |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 298 | return InitStatus::OTHER_ERROR; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Setting RunLoop to NULL here instructs HAL to manage its own thread for |
| 302 | // notifications. This was the default behaviour on OS X 10.5 and earlier, |
| 303 | // but now must be explicitly specified. HAL would otherwise try to use the |
| 304 | // main thread to issue notifications. |
| 305 | AudioObjectPropertyAddress propertyAddress = { |
| 306 | kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, |
| 307 | kAudioObjectPropertyElementMaster}; |
| 308 | CFRunLoopRef runLoop = NULL; |
| 309 | UInt32 size = sizeof(CFRunLoopRef); |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 310 | int aoerr = AudioObjectSetPropertyData( |
| 311 | kAudioObjectSystemObject, &propertyAddress, 0, NULL, size, &runLoop); |
| 312 | if (aoerr != noErr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 313 | RTC_LOG(LS_ERROR) << "Error in AudioObjectSetPropertyData: " |
| 314 | << (const char*)&aoerr; |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 315 | return InitStatus::OTHER_ERROR; |
| 316 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 317 | |
| 318 | // Listen for any device changes. |
| 319 | propertyAddress.mSelector = kAudioHardwarePropertyDevices; |
| 320 | WEBRTC_CA_LOG_ERR(AudioObjectAddPropertyListener( |
| 321 | kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this)); |
| 322 | |
| 323 | // Determine if this is a MacBook Pro |
| 324 | _macBookPro = false; |
| 325 | _macBookProPanRight = false; |
| 326 | char buf[128]; |
| 327 | size_t length = sizeof(buf); |
| 328 | memset(buf, 0, length); |
| 329 | |
| 330 | int intErr = sysctlbyname("hw.model", buf, &length, NULL, 0); |
| 331 | if (intErr != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 332 | RTC_LOG(LS_ERROR) << "Error in sysctlbyname(): " << err; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 333 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 334 | RTC_LOG(LS_VERBOSE) << "Hardware model: " << buf; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 335 | if (strncmp(buf, "MacBookPro", 10) == 0) { |
| 336 | _macBookPro = true; |
| 337 | } |
| 338 | } |
| 339 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 340 | get_mic_volume_counter_ms_ = 0; |
| 341 | |
| 342 | _initialized = true; |
| 343 | |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 344 | return InitStatus::OK; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 345 | } |
| 346 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 347 | int32_t AudioDeviceMac::Terminate() { |
| 348 | if (!_initialized) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 349 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (_recording) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 353 | RTC_LOG(LS_ERROR) << "Recording must be stopped"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | if (_playing) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 358 | RTC_LOG(LS_ERROR) << "Playback must be stopped"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | _critSect.Enter(); |
| 363 | |
| 364 | _mixerManager.Close(); |
| 365 | |
| 366 | OSStatus err = noErr; |
| 367 | int retVal = 0; |
| 368 | |
| 369 | AudioObjectPropertyAddress propertyAddress = { |
| 370 | kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, |
| 371 | kAudioObjectPropertyElementMaster}; |
| 372 | WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( |
| 373 | kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this)); |
| 374 | |
| 375 | err = AudioHardwareUnload(); |
| 376 | if (err != noErr) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 377 | logCAMsg(rtc::LS_ERROR, "Error in AudioHardwareUnload()", |
| 378 | (const char*)&err); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 379 | retVal = -1; |
| 380 | } |
| 381 | |
| 382 | _isShutDown = true; |
| 383 | _initialized = false; |
| 384 | _outputDeviceIsSpecified = false; |
| 385 | _inputDeviceIsSpecified = false; |
| 386 | |
| 387 | _critSect.Leave(); |
| 388 | |
| 389 | return retVal; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 390 | } |
| 391 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 392 | bool AudioDeviceMac::Initialized() const { |
| 393 | return (_initialized); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 394 | } |
| 395 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 396 | int32_t AudioDeviceMac::SpeakerIsAvailable(bool& available) { |
| 397 | bool wasInitialized = _mixerManager.SpeakerIsInitialized(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 398 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 399 | // Make an attempt to open up the |
| 400 | // output mixer corresponding to the currently selected output device. |
| 401 | // |
| 402 | if (!wasInitialized && InitSpeaker() == -1) { |
| 403 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 404 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // Given that InitSpeaker was successful, we know that a valid speaker |
| 408 | // exists. |
| 409 | available = true; |
| 410 | |
| 411 | // Close the initialized output mixer |
| 412 | // |
| 413 | if (!wasInitialized) { |
| 414 | _mixerManager.CloseSpeaker(); |
| 415 | } |
| 416 | |
| 417 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 418 | } |
| 419 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 420 | int32_t AudioDeviceMac::InitSpeaker() { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 421 | rtc::CritScope lock(&_critSect); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 422 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 423 | if (_playing) { |
| 424 | return -1; |
| 425 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 426 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 427 | if (InitDevice(_outputDeviceIndex, _outputDeviceID, false) == -1) { |
| 428 | return -1; |
| 429 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 430 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 431 | if (_inputDeviceID == _outputDeviceID) { |
| 432 | _twoDevices = false; |
| 433 | } else { |
| 434 | _twoDevices = true; |
| 435 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 436 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 437 | if (_mixerManager.OpenSpeaker(_outputDeviceID) == -1) { |
| 438 | return -1; |
| 439 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 440 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 441 | return 0; |
| 442 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 443 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 444 | int32_t AudioDeviceMac::MicrophoneIsAvailable(bool& available) { |
| 445 | bool wasInitialized = _mixerManager.MicrophoneIsInitialized(); |
| 446 | |
| 447 | // Make an attempt to open up the |
| 448 | // input mixer corresponding to the currently selected output device. |
| 449 | // |
| 450 | if (!wasInitialized && InitMicrophone() == -1) { |
| 451 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 452 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // Given that InitMicrophone was successful, we know that a valid microphone |
| 456 | // exists. |
| 457 | available = true; |
| 458 | |
| 459 | // Close the initialized input mixer |
| 460 | // |
| 461 | if (!wasInitialized) { |
| 462 | _mixerManager.CloseMicrophone(); |
| 463 | } |
| 464 | |
| 465 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 466 | } |
| 467 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 468 | int32_t AudioDeviceMac::InitMicrophone() { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 469 | rtc::CritScope lock(&_critSect); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 470 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 471 | if (_recording) { |
| 472 | return -1; |
| 473 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 474 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 475 | if (InitDevice(_inputDeviceIndex, _inputDeviceID, true) == -1) { |
| 476 | return -1; |
| 477 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 478 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 479 | if (_inputDeviceID == _outputDeviceID) { |
| 480 | _twoDevices = false; |
| 481 | } else { |
| 482 | _twoDevices = true; |
| 483 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 484 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 485 | if (_mixerManager.OpenMicrophone(_inputDeviceID) == -1) { |
| 486 | return -1; |
| 487 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 488 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | bool AudioDeviceMac::SpeakerIsInitialized() const { |
| 493 | return (_mixerManager.SpeakerIsInitialized()); |
| 494 | } |
| 495 | |
| 496 | bool AudioDeviceMac::MicrophoneIsInitialized() const { |
| 497 | return (_mixerManager.MicrophoneIsInitialized()); |
| 498 | } |
| 499 | |
| 500 | int32_t AudioDeviceMac::SpeakerVolumeIsAvailable(bool& available) { |
| 501 | bool wasInitialized = _mixerManager.SpeakerIsInitialized(); |
| 502 | |
| 503 | // Make an attempt to open up the |
| 504 | // output mixer corresponding to the currently selected output device. |
| 505 | // |
| 506 | if (!wasInitialized && InitSpeaker() == -1) { |
| 507 | // If we end up here it means that the selected speaker has no volume |
| 508 | // control. |
| 509 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 510 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // Given that InitSpeaker was successful, we know that a volume control exists |
| 514 | // |
| 515 | available = true; |
| 516 | |
| 517 | // Close the initialized output mixer |
| 518 | // |
| 519 | if (!wasInitialized) { |
| 520 | _mixerManager.CloseSpeaker(); |
| 521 | } |
| 522 | |
| 523 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 524 | } |
| 525 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 526 | int32_t AudioDeviceMac::SetSpeakerVolume(uint32_t volume) { |
| 527 | return (_mixerManager.SetSpeakerVolume(volume)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 528 | } |
| 529 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 530 | int32_t AudioDeviceMac::SpeakerVolume(uint32_t& volume) const { |
| 531 | uint32_t level(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 532 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 533 | if (_mixerManager.SpeakerVolume(level) == -1) { |
| 534 | return -1; |
| 535 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 536 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 537 | volume = level; |
| 538 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 539 | } |
| 540 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 541 | int32_t AudioDeviceMac::MaxSpeakerVolume(uint32_t& maxVolume) const { |
| 542 | uint32_t maxVol(0); |
| 543 | |
| 544 | if (_mixerManager.MaxSpeakerVolume(maxVol) == -1) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 545 | return -1; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | maxVolume = maxVol; |
| 549 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 550 | } |
| 551 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 552 | int32_t AudioDeviceMac::MinSpeakerVolume(uint32_t& minVolume) const { |
| 553 | uint32_t minVol(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 554 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 555 | if (_mixerManager.MinSpeakerVolume(minVol) == -1) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 556 | return -1; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | minVolume = minVol; |
| 560 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 561 | } |
| 562 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 563 | int32_t AudioDeviceMac::SpeakerMuteIsAvailable(bool& available) { |
| 564 | bool isAvailable(false); |
| 565 | bool wasInitialized = _mixerManager.SpeakerIsInitialized(); |
| 566 | |
| 567 | // Make an attempt to open up the |
| 568 | // output mixer corresponding to the currently selected output device. |
| 569 | // |
| 570 | if (!wasInitialized && InitSpeaker() == -1) { |
| 571 | // If we end up here it means that the selected speaker has no volume |
| 572 | // control, hence it is safe to state that there is no mute control |
| 573 | // already at this stage. |
| 574 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 575 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // Check if the selected speaker has a mute control |
| 579 | // |
| 580 | _mixerManager.SpeakerMuteIsAvailable(isAvailable); |
| 581 | |
| 582 | available = isAvailable; |
| 583 | |
| 584 | // Close the initialized output mixer |
| 585 | // |
| 586 | if (!wasInitialized) { |
| 587 | _mixerManager.CloseSpeaker(); |
| 588 | } |
| 589 | |
| 590 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 591 | } |
| 592 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 593 | int32_t AudioDeviceMac::SetSpeakerMute(bool enable) { |
| 594 | return (_mixerManager.SetSpeakerMute(enable)); |
| 595 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 596 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 597 | int32_t AudioDeviceMac::SpeakerMute(bool& enabled) const { |
| 598 | bool muted(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 599 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 600 | if (_mixerManager.SpeakerMute(muted) == -1) { |
| 601 | return -1; |
| 602 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 603 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 604 | enabled = muted; |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | int32_t AudioDeviceMac::MicrophoneMuteIsAvailable(bool& available) { |
| 609 | bool isAvailable(false); |
| 610 | bool wasInitialized = _mixerManager.MicrophoneIsInitialized(); |
| 611 | |
| 612 | // Make an attempt to open up the |
| 613 | // input mixer corresponding to the currently selected input device. |
| 614 | // |
| 615 | if (!wasInitialized && InitMicrophone() == -1) { |
| 616 | // If we end up here it means that the selected microphone has no volume |
| 617 | // control, hence it is safe to state that there is no boost control |
| 618 | // already at this stage. |
| 619 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 620 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Check if the selected microphone has a mute control |
| 624 | // |
| 625 | _mixerManager.MicrophoneMuteIsAvailable(isAvailable); |
| 626 | available = isAvailable; |
| 627 | |
| 628 | // Close the initialized input mixer |
| 629 | // |
| 630 | if (!wasInitialized) { |
| 631 | _mixerManager.CloseMicrophone(); |
| 632 | } |
| 633 | |
| 634 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 635 | } |
| 636 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 637 | int32_t AudioDeviceMac::SetMicrophoneMute(bool enable) { |
| 638 | return (_mixerManager.SetMicrophoneMute(enable)); |
| 639 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 640 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 641 | int32_t AudioDeviceMac::MicrophoneMute(bool& enabled) const { |
| 642 | bool muted(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 643 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 644 | if (_mixerManager.MicrophoneMute(muted) == -1) { |
| 645 | return -1; |
| 646 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 647 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 648 | enabled = muted; |
| 649 | return 0; |
| 650 | } |
| 651 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 652 | int32_t AudioDeviceMac::StereoRecordingIsAvailable(bool& available) { |
| 653 | bool isAvailable(false); |
| 654 | bool wasInitialized = _mixerManager.MicrophoneIsInitialized(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 655 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 656 | if (!wasInitialized && InitMicrophone() == -1) { |
| 657 | // Cannot open the specified device |
| 658 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 659 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // Check if the selected microphone can record stereo |
| 663 | // |
| 664 | _mixerManager.StereoRecordingIsAvailable(isAvailable); |
| 665 | available = isAvailable; |
| 666 | |
| 667 | // Close the initialized input mixer |
| 668 | // |
| 669 | if (!wasInitialized) { |
| 670 | _mixerManager.CloseMicrophone(); |
| 671 | } |
| 672 | |
| 673 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 674 | } |
| 675 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 676 | int32_t AudioDeviceMac::SetStereoRecording(bool enable) { |
| 677 | if (enable) |
| 678 | _recChannels = 2; |
| 679 | else |
| 680 | _recChannels = 1; |
| 681 | |
| 682 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 683 | } |
| 684 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 685 | int32_t AudioDeviceMac::StereoRecording(bool& enabled) const { |
| 686 | if (_recChannels == 2) |
| 687 | enabled = true; |
| 688 | else |
| 689 | enabled = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 690 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 691 | return 0; |
| 692 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 693 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 694 | int32_t AudioDeviceMac::StereoPlayoutIsAvailable(bool& available) { |
| 695 | bool isAvailable(false); |
| 696 | bool wasInitialized = _mixerManager.SpeakerIsInitialized(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 697 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 698 | if (!wasInitialized && InitSpeaker() == -1) { |
| 699 | // Cannot open the specified device |
| 700 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 701 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | // Check if the selected microphone can record stereo |
| 705 | // |
| 706 | _mixerManager.StereoPlayoutIsAvailable(isAvailable); |
| 707 | available = isAvailable; |
| 708 | |
| 709 | // Close the initialized input mixer |
| 710 | // |
| 711 | if (!wasInitialized) { |
| 712 | _mixerManager.CloseSpeaker(); |
| 713 | } |
| 714 | |
| 715 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 716 | } |
| 717 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 718 | int32_t AudioDeviceMac::SetStereoPlayout(bool enable) { |
| 719 | if (enable) |
| 720 | _playChannels = 2; |
| 721 | else |
| 722 | _playChannels = 1; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 723 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 724 | return 0; |
| 725 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 726 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 727 | int32_t AudioDeviceMac::StereoPlayout(bool& enabled) const { |
| 728 | if (_playChannels == 2) |
| 729 | enabled = true; |
| 730 | else |
| 731 | enabled = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 732 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 733 | return 0; |
| 734 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 735 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 736 | int32_t AudioDeviceMac::SetAGC(bool enable) { |
| 737 | _AGC = enable; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 738 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | bool AudioDeviceMac::AGC() const { |
| 743 | return _AGC; |
| 744 | } |
| 745 | |
| 746 | int32_t AudioDeviceMac::MicrophoneVolumeIsAvailable(bool& available) { |
| 747 | bool wasInitialized = _mixerManager.MicrophoneIsInitialized(); |
| 748 | |
| 749 | // Make an attempt to open up the |
| 750 | // input mixer corresponding to the currently selected output device. |
| 751 | // |
| 752 | if (!wasInitialized && InitMicrophone() == -1) { |
| 753 | // If we end up here it means that the selected microphone has no volume |
| 754 | // control. |
| 755 | available = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 756 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // Given that InitMicrophone was successful, we know that a volume control |
| 760 | // exists |
| 761 | // |
| 762 | available = true; |
| 763 | |
| 764 | // Close the initialized input mixer |
| 765 | // |
| 766 | if (!wasInitialized) { |
| 767 | _mixerManager.CloseMicrophone(); |
| 768 | } |
| 769 | |
| 770 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 771 | } |
| 772 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 773 | int32_t AudioDeviceMac::SetMicrophoneVolume(uint32_t volume) { |
| 774 | return (_mixerManager.SetMicrophoneVolume(volume)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 775 | } |
| 776 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 777 | int32_t AudioDeviceMac::MicrophoneVolume(uint32_t& volume) const { |
| 778 | uint32_t level(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 779 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 780 | if (_mixerManager.MicrophoneVolume(level) == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 781 | RTC_LOG(LS_WARNING) << "failed to retrieve current microphone level"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 782 | return -1; |
| 783 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 784 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 785 | volume = level; |
| 786 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 787 | } |
| 788 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 789 | int32_t AudioDeviceMac::MaxMicrophoneVolume(uint32_t& maxVolume) const { |
| 790 | uint32_t maxVol(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 791 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 792 | if (_mixerManager.MaxMicrophoneVolume(maxVol) == -1) { |
| 793 | return -1; |
| 794 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 795 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 796 | maxVolume = maxVol; |
| 797 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 798 | } |
| 799 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 800 | int32_t AudioDeviceMac::MinMicrophoneVolume(uint32_t& minVolume) const { |
| 801 | uint32_t minVol(0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 802 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 803 | if (_mixerManager.MinMicrophoneVolume(minVol) == -1) { |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | minVolume = minVol; |
| 808 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 809 | } |
| 810 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 811 | int16_t AudioDeviceMac::PlayoutDevices() { |
| 812 | AudioDeviceID playDevices[MaxNumberDevices]; |
| 813 | return GetNumberDevices(kAudioDevicePropertyScopeOutput, playDevices, |
| 814 | MaxNumberDevices); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 815 | } |
| 816 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 817 | int32_t AudioDeviceMac::SetPlayoutDevice(uint16_t index) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 818 | rtc::CritScope lock(&_critSect); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 819 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 820 | if (_playIsInitialized) { |
| 821 | return -1; |
| 822 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 823 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 824 | AudioDeviceID playDevices[MaxNumberDevices]; |
| 825 | uint32_t nDevices = GetNumberDevices(kAudioDevicePropertyScopeOutput, |
| 826 | playDevices, MaxNumberDevices); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 827 | RTC_LOG(LS_VERBOSE) << "number of available waveform-audio output devices is " |
| 828 | << nDevices; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 829 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 830 | if (index > (nDevices - 1)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 831 | RTC_LOG(LS_ERROR) << "device index is out of range [0," << (nDevices - 1) |
| 832 | << "]"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 833 | return -1; |
| 834 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 835 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 836 | _outputDeviceIndex = index; |
| 837 | _outputDeviceIsSpecified = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 838 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 839 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 840 | } |
| 841 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 842 | int32_t AudioDeviceMac::SetPlayoutDevice( |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 843 | AudioDeviceModule::WindowsDeviceType /*device*/) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 844 | RTC_LOG(LS_ERROR) << "WindowsDeviceType not supported"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | int32_t AudioDeviceMac::PlayoutDeviceName(uint16_t index, |
| 849 | char name[kAdmMaxDeviceNameSize], |
| 850 | char guid[kAdmMaxGuidSize]) { |
| 851 | const uint16_t nDevices(PlayoutDevices()); |
| 852 | |
| 853 | if ((index > (nDevices - 1)) || (name == NULL)) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 854 | return -1; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | memset(name, 0, kAdmMaxDeviceNameSize); |
| 858 | |
| 859 | if (guid != NULL) { |
| 860 | memset(guid, 0, kAdmMaxGuidSize); |
| 861 | } |
| 862 | |
| 863 | return GetDeviceName(kAudioDevicePropertyScopeOutput, index, name); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 864 | } |
| 865 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 866 | int32_t AudioDeviceMac::RecordingDeviceName(uint16_t index, |
| 867 | char name[kAdmMaxDeviceNameSize], |
| 868 | char guid[kAdmMaxGuidSize]) { |
| 869 | const uint16_t nDevices(RecordingDevices()); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 870 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 871 | if ((index > (nDevices - 1)) || (name == NULL)) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 872 | return -1; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | memset(name, 0, kAdmMaxDeviceNameSize); |
| 876 | |
| 877 | if (guid != NULL) { |
| 878 | memset(guid, 0, kAdmMaxGuidSize); |
| 879 | } |
| 880 | |
| 881 | return GetDeviceName(kAudioDevicePropertyScopeInput, index, name); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 882 | } |
| 883 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 884 | int16_t AudioDeviceMac::RecordingDevices() { |
| 885 | AudioDeviceID recDevices[MaxNumberDevices]; |
| 886 | return GetNumberDevices(kAudioDevicePropertyScopeInput, recDevices, |
| 887 | MaxNumberDevices); |
| 888 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 889 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 890 | int32_t AudioDeviceMac::SetRecordingDevice(uint16_t index) { |
| 891 | if (_recIsInitialized) { |
| 892 | return -1; |
| 893 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 894 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 895 | AudioDeviceID recDevices[MaxNumberDevices]; |
| 896 | uint32_t nDevices = GetNumberDevices(kAudioDevicePropertyScopeInput, |
| 897 | recDevices, MaxNumberDevices); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 898 | RTC_LOG(LS_VERBOSE) << "number of available waveform-audio input devices is " |
| 899 | << nDevices; |
punyabrata@google.com | eba8c32 | 2011-08-30 14:32:22 +0000 | [diff] [blame] | 900 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 901 | if (index > (nDevices - 1)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 902 | RTC_LOG(LS_ERROR) << "device index is out of range [0," << (nDevices - 1) |
| 903 | << "]"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 904 | return -1; |
| 905 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 906 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 907 | _inputDeviceIndex = index; |
| 908 | _inputDeviceIsSpecified = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 909 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | int32_t AudioDeviceMac::SetRecordingDevice( |
| 914 | AudioDeviceModule::WindowsDeviceType /*device*/) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 915 | RTC_LOG(LS_ERROR) << "WindowsDeviceType not supported"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 916 | return -1; |
| 917 | } |
| 918 | |
| 919 | int32_t AudioDeviceMac::PlayoutIsAvailable(bool& available) { |
| 920 | available = true; |
| 921 | |
| 922 | // Try to initialize the playout side |
| 923 | if (InitPlayout() == -1) { |
| 924 | available = false; |
| 925 | } |
| 926 | |
| 927 | // We destroy the IOProc created by InitPlayout() in implDeviceIOProc(). |
| 928 | // We must actually start playout here in order to have the IOProc |
| 929 | // deleted by calling StopPlayout(). |
| 930 | if (StartPlayout() == -1) { |
| 931 | available = false; |
| 932 | } |
| 933 | |
| 934 | // Cancel effect of initialization |
| 935 | if (StopPlayout() == -1) { |
| 936 | available = false; |
| 937 | } |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | int32_t AudioDeviceMac::RecordingIsAvailable(bool& available) { |
| 943 | available = true; |
| 944 | |
| 945 | // Try to initialize the recording side |
| 946 | if (InitRecording() == -1) { |
| 947 | available = false; |
| 948 | } |
| 949 | |
| 950 | // We destroy the IOProc created by InitRecording() in implInDeviceIOProc(). |
| 951 | // We must actually start recording here in order to have the IOProc |
| 952 | // deleted by calling StopRecording(). |
| 953 | if (StartRecording() == -1) { |
| 954 | available = false; |
| 955 | } |
| 956 | |
| 957 | // Cancel effect of initialization |
| 958 | if (StopRecording() == -1) { |
| 959 | available = false; |
| 960 | } |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | int32_t AudioDeviceMac::InitPlayout() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 966 | RTC_LOG(LS_INFO) << "InitPlayout"; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 967 | rtc::CritScope lock(&_critSect); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 968 | |
| 969 | if (_playing) { |
| 970 | return -1; |
| 971 | } |
| 972 | |
| 973 | if (!_outputDeviceIsSpecified) { |
| 974 | return -1; |
| 975 | } |
| 976 | |
| 977 | if (_playIsInitialized) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 978 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 979 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 980 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 981 | // Initialize the speaker (devices might have been added or removed) |
| 982 | if (InitSpeaker() == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 983 | RTC_LOG(LS_WARNING) << "InitSpeaker() failed"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 984 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 985 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 986 | if (!MicrophoneIsInitialized()) { |
| 987 | // Make this call to check if we are using |
| 988 | // one or two devices (_twoDevices) |
| 989 | bool available = false; |
| 990 | if (MicrophoneIsAvailable(available) == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 991 | RTC_LOG(LS_WARNING) << "MicrophoneIsAvailable() failed"; |
punyabrata@google.com | eba8c32 | 2011-08-30 14:32:22 +0000 | [diff] [blame] | 992 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 993 | } |
punyabrata@google.com | eba8c32 | 2011-08-30 14:32:22 +0000 | [diff] [blame] | 994 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 995 | PaUtil_FlushRingBuffer(_paRenderBuffer); |
| 996 | |
| 997 | OSStatus err = noErr; |
| 998 | UInt32 size = 0; |
| 999 | _renderDelayOffsetSamples = 0; |
| 1000 | _renderDelayUs = 0; |
| 1001 | _renderLatencyUs = 0; |
| 1002 | _renderDeviceIsAlive = 1; |
| 1003 | _doStop = false; |
| 1004 | |
| 1005 | // The internal microphone of a MacBook Pro is located under the left speaker |
| 1006 | // grille. When the internal speakers are in use, we want to fully stereo |
| 1007 | // pan to the right. |
| 1008 | AudioObjectPropertyAddress propertyAddress = { |
| 1009 | kAudioDevicePropertyDataSource, kAudioDevicePropertyScopeOutput, 0}; |
| 1010 | if (_macBookPro) { |
| 1011 | _macBookProPanRight = false; |
| 1012 | Boolean hasProperty = |
| 1013 | AudioObjectHasProperty(_outputDeviceID, &propertyAddress); |
| 1014 | if (hasProperty) { |
| 1015 | UInt32 dataSource = 0; |
| 1016 | size = sizeof(dataSource); |
| 1017 | WEBRTC_CA_LOG_WARN(AudioObjectGetPropertyData( |
| 1018 | _outputDeviceID, &propertyAddress, 0, NULL, &size, &dataSource)); |
| 1019 | |
| 1020 | if (dataSource == 'ispk') { |
| 1021 | _macBookProPanRight = true; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1022 | RTC_LOG(LS_VERBOSE) |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 1023 | << "MacBook Pro using internal speakers; stereo panning right"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1024 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1025 | RTC_LOG(LS_VERBOSE) << "MacBook Pro not using internal speakers"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | // Add a listener to determine if the status changes. |
| 1029 | WEBRTC_CA_LOG_WARN(AudioObjectAddPropertyListener( |
| 1030 | _outputDeviceID, &propertyAddress, &objectListenerProc, this)); |
punyabrata@google.com | eba8c32 | 2011-08-30 14:32:22 +0000 | [diff] [blame] | 1031 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1032 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1033 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1034 | // Get current stream description |
| 1035 | propertyAddress.mSelector = kAudioDevicePropertyStreamFormat; |
| 1036 | memset(&_outStreamFormat, 0, sizeof(_outStreamFormat)); |
| 1037 | size = sizeof(_outStreamFormat); |
| 1038 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1039 | _outputDeviceID, &propertyAddress, 0, NULL, &size, &_outStreamFormat)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1040 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1041 | if (_outStreamFormat.mFormatID != kAudioFormatLinearPCM) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1042 | logCAMsg(rtc::LS_ERROR, "Unacceptable output stream format -> mFormatID", |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1043 | (const char*)&_outStreamFormat.mFormatID); |
| 1044 | return -1; |
| 1045 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1046 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1047 | if (_outStreamFormat.mChannelsPerFrame > N_DEVICE_CHANNELS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1048 | RTC_LOG(LS_ERROR) |
| 1049 | << "Too many channels on output device (mChannelsPerFrame = " |
| 1050 | << _outStreamFormat.mChannelsPerFrame << ")"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1051 | return -1; |
| 1052 | } |
| 1053 | |
| 1054 | if (_outStreamFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1055 | RTC_LOG(LS_ERROR) << "Non-interleaved audio data is not supported." |
| 1056 | << "AudioHardware streams should not have this format."; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1057 | return -1; |
| 1058 | } |
| 1059 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1060 | RTC_LOG(LS_VERBOSE) << "Ouput stream format:"; |
| 1061 | RTC_LOG(LS_VERBOSE) << "mSampleRate = " << _outStreamFormat.mSampleRate |
| 1062 | << ", mChannelsPerFrame = " |
| 1063 | << _outStreamFormat.mChannelsPerFrame; |
| 1064 | RTC_LOG(LS_VERBOSE) << "mBytesPerPacket = " |
| 1065 | << _outStreamFormat.mBytesPerPacket |
| 1066 | << ", mFramesPerPacket = " |
| 1067 | << _outStreamFormat.mFramesPerPacket; |
| 1068 | RTC_LOG(LS_VERBOSE) << "mBytesPerFrame = " << _outStreamFormat.mBytesPerFrame |
| 1069 | << ", mBitsPerChannel = " |
| 1070 | << _outStreamFormat.mBitsPerChannel; |
| 1071 | RTC_LOG(LS_VERBOSE) << "mFormatFlags = " << _outStreamFormat.mFormatFlags; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 1072 | logCAMsg(rtc::LS_VERBOSE, "mFormatID", |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1073 | (const char*)&_outStreamFormat.mFormatID); |
| 1074 | |
| 1075 | // Our preferred format to work with. |
| 1076 | if (_outStreamFormat.mChannelsPerFrame < 2) { |
| 1077 | // Disable stereo playout when we only have one channel on the device. |
| 1078 | _playChannels = 1; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1079 | RTC_LOG(LS_VERBOSE) << "Stereo playout unavailable on this device"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1080 | } |
| 1081 | WEBRTC_CA_RETURN_ON_ERR(SetDesiredPlayoutFormat()); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1082 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1083 | // Listen for format changes. |
| 1084 | propertyAddress.mSelector = kAudioDevicePropertyStreamFormat; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 1085 | WEBRTC_CA_LOG_WARN(AudioObjectAddPropertyListener( |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1086 | _outputDeviceID, &propertyAddress, &objectListenerProc, this)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1087 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1088 | // Listen for processor overloads. |
| 1089 | propertyAddress.mSelector = kAudioDeviceProcessorOverload; |
| 1090 | WEBRTC_CA_LOG_WARN(AudioObjectAddPropertyListener( |
| 1091 | _outputDeviceID, &propertyAddress, &objectListenerProc, this)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1092 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1093 | if (_twoDevices || !_recIsInitialized) { |
| 1094 | WEBRTC_CA_RETURN_ON_ERR(AudioDeviceCreateIOProcID( |
| 1095 | _outputDeviceID, deviceIOProc, this, &_deviceIOProcID)); |
| 1096 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1097 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1098 | _playIsInitialized = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1099 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1100 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1103 | int32_t AudioDeviceMac::InitRecording() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1104 | RTC_LOG(LS_INFO) << "InitRecording"; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1105 | rtc::CritScope lock(&_critSect); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1106 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1107 | if (_recording) { |
| 1108 | return -1; |
| 1109 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1110 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1111 | if (!_inputDeviceIsSpecified) { |
| 1112 | return -1; |
| 1113 | } |
| 1114 | |
| 1115 | if (_recIsInitialized) { |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | // Initialize the microphone (devices might have been added or removed) |
| 1120 | if (InitMicrophone() == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1121 | RTC_LOG(LS_WARNING) << "InitMicrophone() failed"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | if (!SpeakerIsInitialized()) { |
| 1125 | // Make this call to check if we are using |
| 1126 | // one or two devices (_twoDevices) |
| 1127 | bool available = false; |
| 1128 | if (SpeakerIsAvailable(available) == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1129 | RTC_LOG(LS_WARNING) << "SpeakerIsAvailable() failed"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1130 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1131 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1132 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1133 | OSStatus err = noErr; |
| 1134 | UInt32 size = 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1135 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1136 | PaUtil_FlushRingBuffer(_paCaptureBuffer); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1137 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1138 | _captureDelayUs = 0; |
| 1139 | _captureLatencyUs = 0; |
| 1140 | _captureDeviceIsAlive = 1; |
| 1141 | _doStopRec = false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1142 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1143 | // Get current stream description |
| 1144 | AudioObjectPropertyAddress propertyAddress = { |
| 1145 | kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeInput, 0}; |
| 1146 | memset(&_inStreamFormat, 0, sizeof(_inStreamFormat)); |
| 1147 | size = sizeof(_inStreamFormat); |
| 1148 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1149 | _inputDeviceID, &propertyAddress, 0, NULL, &size, &_inStreamFormat)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1150 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1151 | if (_inStreamFormat.mFormatID != kAudioFormatLinearPCM) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1152 | logCAMsg(rtc::LS_ERROR, "Unacceptable input stream format -> mFormatID", |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1153 | (const char*)&_inStreamFormat.mFormatID); |
| 1154 | return -1; |
| 1155 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1156 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1157 | if (_inStreamFormat.mChannelsPerFrame > N_DEVICE_CHANNELS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1158 | RTC_LOG(LS_ERROR) |
| 1159 | << "Too many channels on input device (mChannelsPerFrame = " |
| 1160 | << _inStreamFormat.mChannelsPerFrame << ")"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1161 | return -1; |
| 1162 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1163 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1164 | const int io_block_size_samples = _inStreamFormat.mChannelsPerFrame * |
| 1165 | _inStreamFormat.mSampleRate / 100 * |
| 1166 | N_BLOCKS_IO; |
| 1167 | if (io_block_size_samples > _captureBufSizeSamples) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1168 | RTC_LOG(LS_ERROR) << "Input IO block size (" << io_block_size_samples |
| 1169 | << ") is larger than ring buffer (" |
| 1170 | << _captureBufSizeSamples << ")"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1171 | return -1; |
| 1172 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1173 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1174 | RTC_LOG(LS_VERBOSE) << "Input stream format:"; |
| 1175 | RTC_LOG(LS_VERBOSE) << "mSampleRate = " << _inStreamFormat.mSampleRate |
| 1176 | << ", mChannelsPerFrame = " |
| 1177 | << _inStreamFormat.mChannelsPerFrame; |
| 1178 | RTC_LOG(LS_VERBOSE) << "mBytesPerPacket = " << _inStreamFormat.mBytesPerPacket |
| 1179 | << ", mFramesPerPacket = " |
| 1180 | << _inStreamFormat.mFramesPerPacket; |
| 1181 | RTC_LOG(LS_VERBOSE) << "mBytesPerFrame = " << _inStreamFormat.mBytesPerFrame |
| 1182 | << ", mBitsPerChannel = " |
| 1183 | << _inStreamFormat.mBitsPerChannel; |
| 1184 | RTC_LOG(LS_VERBOSE) << "mFormatFlags = " << _inStreamFormat.mFormatFlags; |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 1185 | logCAMsg(rtc::LS_VERBOSE, "mFormatID", |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1186 | (const char*)&_inStreamFormat.mFormatID); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1187 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1188 | // Our preferred format to work with |
| 1189 | if (_inStreamFormat.mChannelsPerFrame >= 2 && (_recChannels == 2)) { |
| 1190 | _inDesiredFormat.mChannelsPerFrame = 2; |
| 1191 | } else { |
| 1192 | // Disable stereo recording when we only have one channel on the device. |
| 1193 | _inDesiredFormat.mChannelsPerFrame = 1; |
| 1194 | _recChannels = 1; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1195 | RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1196 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1197 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1198 | if (_ptrAudioBuffer) { |
| 1199 | // Update audio buffer with the selected parameters |
| 1200 | _ptrAudioBuffer->SetRecordingSampleRate(N_REC_SAMPLES_PER_SEC); |
| 1201 | _ptrAudioBuffer->SetRecordingChannels((uint8_t)_recChannels); |
| 1202 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1203 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1204 | _inDesiredFormat.mSampleRate = N_REC_SAMPLES_PER_SEC; |
| 1205 | _inDesiredFormat.mBytesPerPacket = |
| 1206 | _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); |
| 1207 | _inDesiredFormat.mFramesPerPacket = 1; |
| 1208 | _inDesiredFormat.mBytesPerFrame = |
| 1209 | _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); |
| 1210 | _inDesiredFormat.mBitsPerChannel = sizeof(SInt16) * 8; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1211 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1212 | _inDesiredFormat.mFormatFlags = |
| 1213 | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 1214 | #ifdef WEBRTC_ARCH_BIG_ENDIAN |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1215 | _inDesiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1216 | #endif |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1217 | _inDesiredFormat.mFormatID = kAudioFormatLinearPCM; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1218 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1219 | WEBRTC_CA_RETURN_ON_ERR(AudioConverterNew(&_inStreamFormat, &_inDesiredFormat, |
| 1220 | &_captureConverter)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1221 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1222 | // First try to set buffer size to desired value (10 ms * N_BLOCKS_IO) |
| 1223 | // TODO(xians): investigate this block. |
| 1224 | UInt32 bufByteCount = |
| 1225 | (UInt32)((_inStreamFormat.mSampleRate / 1000.0) * 10.0 * N_BLOCKS_IO * |
| 1226 | _inStreamFormat.mChannelsPerFrame * sizeof(Float32)); |
| 1227 | if (_inStreamFormat.mFramesPerPacket != 0) { |
| 1228 | if (bufByteCount % _inStreamFormat.mFramesPerPacket != 0) { |
| 1229 | bufByteCount = |
| 1230 | ((UInt32)(bufByteCount / _inStreamFormat.mFramesPerPacket) + 1) * |
| 1231 | _inStreamFormat.mFramesPerPacket; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1232 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1233 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1234 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1235 | // Ensure the buffer size is within the acceptable range provided by the |
| 1236 | // device. |
| 1237 | propertyAddress.mSelector = kAudioDevicePropertyBufferSizeRange; |
| 1238 | AudioValueRange range; |
| 1239 | size = sizeof(range); |
| 1240 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1241 | _inputDeviceID, &propertyAddress, 0, NULL, &size, &range)); |
| 1242 | if (range.mMinimum > bufByteCount) { |
| 1243 | bufByteCount = range.mMinimum; |
| 1244 | } else if (range.mMaximum < bufByteCount) { |
| 1245 | bufByteCount = range.mMaximum; |
| 1246 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1247 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1248 | propertyAddress.mSelector = kAudioDevicePropertyBufferSize; |
| 1249 | size = sizeof(bufByteCount); |
| 1250 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectSetPropertyData( |
| 1251 | _inputDeviceID, &propertyAddress, 0, NULL, size, &bufByteCount)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1252 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1253 | // Get capture device latency |
| 1254 | propertyAddress.mSelector = kAudioDevicePropertyLatency; |
| 1255 | UInt32 latency = 0; |
| 1256 | size = sizeof(UInt32); |
| 1257 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1258 | _inputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); |
| 1259 | _captureLatencyUs = (UInt32)((1.0e6 * latency) / _inStreamFormat.mSampleRate); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1260 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1261 | // Get capture stream latency |
| 1262 | propertyAddress.mSelector = kAudioDevicePropertyStreams; |
| 1263 | AudioStreamID stream = 0; |
| 1264 | size = sizeof(AudioStreamID); |
| 1265 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1266 | _inputDeviceID, &propertyAddress, 0, NULL, &size, &stream)); |
| 1267 | propertyAddress.mSelector = kAudioStreamPropertyLatency; |
| 1268 | size = sizeof(UInt32); |
| 1269 | latency = 0; |
| 1270 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1271 | _inputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); |
| 1272 | _captureLatencyUs += |
| 1273 | (UInt32)((1.0e6 * latency) / _inStreamFormat.mSampleRate); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1274 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1275 | // Listen for format changes |
| 1276 | // TODO(xians): should we be using kAudioDevicePropertyDeviceHasChanged? |
| 1277 | propertyAddress.mSelector = kAudioDevicePropertyStreamFormat; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 1278 | WEBRTC_CA_LOG_WARN(AudioObjectAddPropertyListener( |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1279 | _inputDeviceID, &propertyAddress, &objectListenerProc, this)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1280 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1281 | // Listen for processor overloads |
| 1282 | propertyAddress.mSelector = kAudioDeviceProcessorOverload; |
| 1283 | WEBRTC_CA_LOG_WARN(AudioObjectAddPropertyListener( |
| 1284 | _inputDeviceID, &propertyAddress, &objectListenerProc, this)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1285 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1286 | if (_twoDevices) { |
| 1287 | WEBRTC_CA_RETURN_ON_ERR(AudioDeviceCreateIOProcID( |
| 1288 | _inputDeviceID, inDeviceIOProc, this, &_inDeviceIOProcID)); |
| 1289 | } else if (!_playIsInitialized) { |
| 1290 | WEBRTC_CA_RETURN_ON_ERR(AudioDeviceCreateIOProcID( |
| 1291 | _inputDeviceID, deviceIOProc, this, &_deviceIOProcID)); |
| 1292 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1293 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1294 | // Mark recording side as initialized |
| 1295 | _recIsInitialized = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1296 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1297 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1300 | int32_t AudioDeviceMac::StartRecording() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1301 | RTC_LOG(LS_INFO) << "StartRecording"; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1302 | rtc::CritScope lock(&_critSect); |
andrew@webrtc.org | b0be7aa | 2011-12-08 20:15:36 +0000 | [diff] [blame] | 1303 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1304 | if (!_recIsInitialized) { |
| 1305 | return -1; |
| 1306 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1307 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1308 | if (_recording) { |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | if (!_initialized) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1313 | RTC_LOG(LS_ERROR) << "Recording worker thread has not been started"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1314 | return -1; |
| 1315 | } |
| 1316 | |
| 1317 | RTC_DCHECK(!capture_worker_thread_.get()); |
| 1318 | capture_worker_thread_.reset( |
| 1319 | new rtc::PlatformThread(RunCapture, this, "CaptureWorkerThread")); |
| 1320 | RTC_DCHECK(capture_worker_thread_.get()); |
| 1321 | capture_worker_thread_->Start(); |
| 1322 | capture_worker_thread_->SetPriority(rtc::kRealtimePriority); |
| 1323 | |
| 1324 | OSStatus err = noErr; |
| 1325 | if (_twoDevices) { |
| 1326 | WEBRTC_CA_RETURN_ON_ERR( |
| 1327 | AudioDeviceStart(_inputDeviceID, _inDeviceIOProcID)); |
| 1328 | } else if (!_playing) { |
| 1329 | WEBRTC_CA_RETURN_ON_ERR(AudioDeviceStart(_inputDeviceID, _deviceIOProcID)); |
| 1330 | } |
| 1331 | |
| 1332 | _recording = true; |
| 1333 | |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | int32_t AudioDeviceMac::StopRecording() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1338 | RTC_LOG(LS_INFO) << "StopRecording"; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1339 | rtc::CritScope lock(&_critSect); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1340 | |
| 1341 | if (!_recIsInitialized) { |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | OSStatus err = noErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1346 | int32_t captureDeviceIsAlive = AtomicGet32(&_captureDeviceIsAlive); |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 1347 | if (_twoDevices && captureDeviceIsAlive == 1) { |
| 1348 | // Recording side uses its own dedicated device and IOProc. |
| 1349 | if (_recording) { |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1350 | _recording = false; |
| 1351 | _doStopRec = true; // Signal to io proc to stop audio device |
| 1352 | _critSect.Leave(); // Cannot be under lock, risk of deadlock |
| 1353 | if (kEventTimeout == _stopEventRec.Wait(2000)) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1354 | rtc::CritScope critScoped(&_critSect); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1355 | RTC_LOG(LS_WARNING) << "Timed out stopping the capture IOProc." |
| 1356 | << "We may have failed to detect a device removal."; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1357 | WEBRTC_CA_LOG_WARN(AudioDeviceStop(_inputDeviceID, _inDeviceIOProcID)); |
| 1358 | WEBRTC_CA_LOG_WARN( |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1359 | AudioDeviceDestroyIOProcID(_inputDeviceID, _inDeviceIOProcID)); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1360 | } |
| 1361 | _critSect.Enter(); |
| 1362 | _doStopRec = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1363 | RTC_LOG(LS_INFO) << "Recording stopped (input device)"; |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 1364 | } else if (_recIsInitialized) { |
| 1365 | WEBRTC_CA_LOG_WARN( |
| 1366 | AudioDeviceDestroyIOProcID(_inputDeviceID, _inDeviceIOProcID)); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1367 | RTC_LOG(LS_INFO) << "Recording uninitialized (input device)"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1368 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1369 | } else { |
| 1370 | // We signal a stop for a shared device even when rendering has |
| 1371 | // not yet ended. This is to ensure the IOProc will return early as |
| 1372 | // intended (by checking |_recording|) before accessing |
| 1373 | // resources we free below (e.g. the capture converter). |
| 1374 | // |
| 1375 | // In the case of a shared devcie, the IOProc will verify |
| 1376 | // rendering has ended before stopping itself. |
| 1377 | if (_recording && captureDeviceIsAlive == 1) { |
| 1378 | _recording = false; |
| 1379 | _doStop = true; // Signal to io proc to stop audio device |
| 1380 | _critSect.Leave(); // Cannot be under lock, risk of deadlock |
| 1381 | if (kEventTimeout == _stopEvent.Wait(2000)) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1382 | rtc::CritScope critScoped(&_critSect); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1383 | RTC_LOG(LS_WARNING) << "Timed out stopping the shared IOProc." |
| 1384 | << "We may have failed to detect a device removal."; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1385 | // We assume rendering on a shared device has stopped as well if |
| 1386 | // the IOProc times out. |
| 1387 | WEBRTC_CA_LOG_WARN(AudioDeviceStop(_outputDeviceID, _deviceIOProcID)); |
| 1388 | WEBRTC_CA_LOG_WARN( |
| 1389 | AudioDeviceDestroyIOProcID(_outputDeviceID, _deviceIOProcID)); |
| 1390 | } |
| 1391 | _critSect.Enter(); |
| 1392 | _doStop = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1393 | RTC_LOG(LS_INFO) << "Recording stopped (shared device)"; |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 1394 | } else if (_recIsInitialized && !_playing && !_playIsInitialized) { |
| 1395 | WEBRTC_CA_LOG_WARN( |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1396 | AudioDeviceDestroyIOProcID(_outputDeviceID, _deviceIOProcID)); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1397 | RTC_LOG(LS_INFO) << "Recording uninitialized (shared device)"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1398 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1399 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1400 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1401 | // Setting this signal will allow the worker thread to be stopped. |
| 1402 | AtomicSet32(&_captureDeviceIsAlive, 0); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1403 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1404 | if (capture_worker_thread_.get()) { |
| 1405 | _critSect.Leave(); |
| 1406 | capture_worker_thread_->Stop(); |
| 1407 | capture_worker_thread_.reset(); |
| 1408 | _critSect.Enter(); |
| 1409 | } |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 1410 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1411 | WEBRTC_CA_LOG_WARN(AudioConverterDispose(_captureConverter)); |
| 1412 | |
| 1413 | // Remove listeners. |
| 1414 | AudioObjectPropertyAddress propertyAddress = { |
| 1415 | kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeInput, 0}; |
| 1416 | WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( |
| 1417 | _inputDeviceID, &propertyAddress, &objectListenerProc, this)); |
| 1418 | |
| 1419 | propertyAddress.mSelector = kAudioDeviceProcessorOverload; |
| 1420 | WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( |
| 1421 | _inputDeviceID, &propertyAddress, &objectListenerProc, this)); |
| 1422 | |
| 1423 | _recIsInitialized = false; |
| 1424 | _recording = false; |
| 1425 | |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | bool AudioDeviceMac::RecordingIsInitialized() const { |
| 1430 | return (_recIsInitialized); |
| 1431 | } |
| 1432 | |
| 1433 | bool AudioDeviceMac::Recording() const { |
| 1434 | return (_recording); |
| 1435 | } |
| 1436 | |
| 1437 | bool AudioDeviceMac::PlayoutIsInitialized() const { |
| 1438 | return (_playIsInitialized); |
| 1439 | } |
| 1440 | |
| 1441 | int32_t AudioDeviceMac::StartPlayout() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1442 | RTC_LOG(LS_INFO) << "StartPlayout"; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1443 | rtc::CritScope lock(&_critSect); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1444 | |
| 1445 | if (!_playIsInitialized) { |
| 1446 | return -1; |
| 1447 | } |
| 1448 | |
| 1449 | if (_playing) { |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | RTC_DCHECK(!render_worker_thread_.get()); |
| 1454 | render_worker_thread_.reset( |
| 1455 | new rtc::PlatformThread(RunRender, this, "RenderWorkerThread")); |
| 1456 | render_worker_thread_->Start(); |
| 1457 | render_worker_thread_->SetPriority(rtc::kRealtimePriority); |
| 1458 | |
| 1459 | if (_twoDevices || !_recording) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1460 | OSStatus err = noErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1461 | WEBRTC_CA_RETURN_ON_ERR(AudioDeviceStart(_outputDeviceID, _deviceIOProcID)); |
| 1462 | } |
| 1463 | _playing = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1464 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1465 | return 0; |
| 1466 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1467 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1468 | int32_t AudioDeviceMac::StopPlayout() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1469 | RTC_LOG(LS_INFO) << "StopPlayout"; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1470 | rtc::CritScope lock(&_critSect); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1471 | |
| 1472 | if (!_playIsInitialized) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1473 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1474 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1475 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1476 | OSStatus err = noErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1477 | int32_t renderDeviceIsAlive = AtomicGet32(&_renderDeviceIsAlive); |
| 1478 | if (_playing && renderDeviceIsAlive == 1) { |
| 1479 | // We signal a stop for a shared device even when capturing has not |
| 1480 | // yet ended. This is to ensure the IOProc will return early as |
| 1481 | // intended (by checking |_playing|) before accessing resources we |
| 1482 | // free below (e.g. the render converter). |
| 1483 | // |
| 1484 | // In the case of a shared device, the IOProc will verify capturing |
| 1485 | // has ended before stopping itself. |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1486 | _playing = false; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1487 | _doStop = true; // Signal to io proc to stop audio device |
| 1488 | _critSect.Leave(); // Cannot be under lock, risk of deadlock |
| 1489 | if (kEventTimeout == _stopEvent.Wait(2000)) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 1490 | rtc::CritScope critScoped(&_critSect); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1491 | RTC_LOG(LS_WARNING) << "Timed out stopping the render IOProc." |
| 1492 | << "We may have failed to detect a device removal."; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1493 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1494 | // We assume capturing on a shared device has stopped as well if the |
| 1495 | // IOProc times out. |
| 1496 | WEBRTC_CA_LOG_WARN(AudioDeviceStop(_outputDeviceID, _deviceIOProcID)); |
| 1497 | WEBRTC_CA_LOG_WARN( |
| 1498 | AudioDeviceDestroyIOProcID(_outputDeviceID, _deviceIOProcID)); |
| 1499 | } |
| 1500 | _critSect.Enter(); |
| 1501 | _doStop = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1502 | RTC_LOG(LS_INFO) << "Playout stopped"; |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 1503 | } else if (_twoDevices && _playIsInitialized) { |
| 1504 | WEBRTC_CA_LOG_WARN( |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1505 | AudioDeviceDestroyIOProcID(_outputDeviceID, _deviceIOProcID)); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1506 | RTC_LOG(LS_INFO) << "Playout uninitialized (output device)"; |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 1507 | } else if (!_twoDevices && _playIsInitialized && !_recIsInitialized) { |
| 1508 | WEBRTC_CA_LOG_WARN( |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1509 | AudioDeviceDestroyIOProcID(_outputDeviceID, _deviceIOProcID)); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1510 | RTC_LOG(LS_INFO) << "Playout uninitialized (shared device)"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | // Setting this signal will allow the worker thread to be stopped. |
| 1514 | AtomicSet32(&_renderDeviceIsAlive, 0); |
| 1515 | if (render_worker_thread_.get()) { |
| 1516 | _critSect.Leave(); |
| 1517 | render_worker_thread_->Stop(); |
| 1518 | render_worker_thread_.reset(); |
| 1519 | _critSect.Enter(); |
| 1520 | } |
| 1521 | |
| 1522 | WEBRTC_CA_LOG_WARN(AudioConverterDispose(_renderConverter)); |
| 1523 | |
| 1524 | // Remove listeners. |
| 1525 | AudioObjectPropertyAddress propertyAddress = { |
| 1526 | kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeOutput, 0}; |
| 1527 | WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( |
| 1528 | _outputDeviceID, &propertyAddress, &objectListenerProc, this)); |
| 1529 | |
| 1530 | propertyAddress.mSelector = kAudioDeviceProcessorOverload; |
| 1531 | WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( |
| 1532 | _outputDeviceID, &propertyAddress, &objectListenerProc, this)); |
| 1533 | |
| 1534 | if (_macBookPro) { |
| 1535 | Boolean hasProperty = |
| 1536 | AudioObjectHasProperty(_outputDeviceID, &propertyAddress); |
| 1537 | if (hasProperty) { |
| 1538 | propertyAddress.mSelector = kAudioDevicePropertyDataSource; |
| 1539 | WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( |
| 1540 | _outputDeviceID, &propertyAddress, &objectListenerProc, this)); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | _playIsInitialized = false; |
| 1545 | _playing = false; |
| 1546 | |
| 1547 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1550 | int32_t AudioDeviceMac::PlayoutDelay(uint16_t& delayMS) const { |
| 1551 | int32_t renderDelayUs = AtomicGet32(&_renderDelayUs); |
| 1552 | delayMS = |
| 1553 | static_cast<uint16_t>(1e-3 * (renderDelayUs + _renderLatencyUs) + 0.5); |
| 1554 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1557 | bool AudioDeviceMac::Playing() const { |
| 1558 | return (_playing); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1561 | // ============================================================================ |
| 1562 | // Private Methods |
| 1563 | // ============================================================================ |
| 1564 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1565 | int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope, |
| 1566 | AudioDeviceID scopedDeviceIds[], |
| 1567 | const uint32_t deviceListLength) { |
| 1568 | OSStatus err = noErr; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1569 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1570 | AudioObjectPropertyAddress propertyAddress = { |
| 1571 | kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, |
| 1572 | kAudioObjectPropertyElementMaster}; |
| 1573 | UInt32 size = 0; |
| 1574 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyDataSize( |
| 1575 | kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size)); |
| 1576 | if (size == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1577 | RTC_LOG(LS_WARNING) << "No devices"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1578 | return 0; |
| 1579 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1580 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1581 | AudioDeviceID* deviceIds = (AudioDeviceID*)malloc(size); |
| 1582 | UInt32 numberDevices = size / sizeof(AudioDeviceID); |
| 1583 | AudioBufferList* bufferList = NULL; |
| 1584 | UInt32 numberScopedDevices = 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1585 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1586 | // First check if there is a default device and list it |
| 1587 | UInt32 hardwareProperty = 0; |
| 1588 | if (scope == kAudioDevicePropertyScopeOutput) { |
| 1589 | hardwareProperty = kAudioHardwarePropertyDefaultOutputDevice; |
| 1590 | } else { |
| 1591 | hardwareProperty = kAudioHardwarePropertyDefaultInputDevice; |
| 1592 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1593 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1594 | AudioObjectPropertyAddress propertyAddressDefault = { |
| 1595 | hardwareProperty, kAudioObjectPropertyScopeGlobal, |
| 1596 | kAudioObjectPropertyElementMaster}; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1597 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1598 | AudioDeviceID usedID; |
| 1599 | UInt32 uintSize = sizeof(UInt32); |
| 1600 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 1601 | &propertyAddressDefault, 0, |
| 1602 | NULL, &uintSize, &usedID)); |
| 1603 | if (usedID != kAudioDeviceUnknown) { |
| 1604 | scopedDeviceIds[numberScopedDevices] = usedID; |
| 1605 | numberScopedDevices++; |
| 1606 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1607 | RTC_LOG(LS_WARNING) << "GetNumberDevices(): Default device unknown"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1608 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1609 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1610 | // Then list the rest of the devices |
| 1611 | bool listOK = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1612 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1613 | WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyData( |
| 1614 | kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, deviceIds)); |
| 1615 | if (err != noErr) { |
| 1616 | listOK = false; |
| 1617 | } else { |
| 1618 | propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration; |
| 1619 | propertyAddress.mScope = scope; |
| 1620 | propertyAddress.mElement = 0; |
| 1621 | for (UInt32 i = 0; i < numberDevices; i++) { |
| 1622 | // Check for input channels |
| 1623 | WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyDataSize( |
| 1624 | deviceIds[i], &propertyAddress, 0, NULL, &size)); |
| 1625 | if (err == kAudioHardwareBadDeviceError) { |
| 1626 | // This device doesn't actually exist; continue iterating. |
| 1627 | continue; |
| 1628 | } else if (err != noErr) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1629 | listOK = false; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1630 | break; |
| 1631 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1632 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1633 | bufferList = (AudioBufferList*)malloc(size); |
| 1634 | WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyData( |
| 1635 | deviceIds[i], &propertyAddress, 0, NULL, &size, bufferList)); |
| 1636 | if (err != noErr) { |
| 1637 | listOK = false; |
| 1638 | break; |
| 1639 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1640 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1641 | if (bufferList->mNumberBuffers > 0) { |
| 1642 | if (numberScopedDevices >= deviceListLength) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1643 | RTC_LOG(LS_ERROR) << "Device list is not long enough"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1644 | listOK = false; |
| 1645 | break; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1648 | scopedDeviceIds[numberScopedDevices] = deviceIds[i]; |
| 1649 | numberScopedDevices++; |
| 1650 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1651 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1652 | free(bufferList); |
| 1653 | bufferList = NULL; |
| 1654 | } // for |
| 1655 | } |
| 1656 | |
| 1657 | if (!listOK) { |
| 1658 | if (deviceIds) { |
| 1659 | free(deviceIds); |
| 1660 | deviceIds = NULL; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1663 | if (bufferList) { |
| 1664 | free(bufferList); |
| 1665 | bufferList = NULL; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1668 | return -1; |
| 1669 | } |
| 1670 | |
| 1671 | // Happy ending |
| 1672 | if (deviceIds) { |
| 1673 | free(deviceIds); |
| 1674 | deviceIds = NULL; |
| 1675 | } |
| 1676 | |
| 1677 | return numberScopedDevices; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1680 | int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope, |
| 1681 | const uint16_t index, |
| 1682 | char* name) { |
| 1683 | OSStatus err = noErr; |
| 1684 | UInt32 len = kAdmMaxDeviceNameSize; |
| 1685 | AudioDeviceID deviceIds[MaxNumberDevices]; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1686 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1687 | int numberDevices = GetNumberDevices(scope, deviceIds, MaxNumberDevices); |
| 1688 | if (numberDevices < 0) { |
| 1689 | return -1; |
| 1690 | } else if (numberDevices == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1691 | RTC_LOG(LS_ERROR) << "No devices"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1692 | return -1; |
| 1693 | } |
| 1694 | |
| 1695 | // If the number is below the number of devices, assume it's "WEBRTC ID" |
| 1696 | // otherwise assume it's a CoreAudio ID |
| 1697 | AudioDeviceID usedID; |
| 1698 | |
| 1699 | // Check if there is a default device |
| 1700 | bool isDefaultDevice = false; |
| 1701 | if (index == 0) { |
| 1702 | UInt32 hardwareProperty = 0; |
| 1703 | if (scope == kAudioDevicePropertyScopeOutput) { |
| 1704 | hardwareProperty = kAudioHardwarePropertyDefaultOutputDevice; |
| 1705 | } else { |
| 1706 | hardwareProperty = kAudioHardwarePropertyDefaultInputDevice; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1707 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1708 | AudioObjectPropertyAddress propertyAddress = { |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1709 | hardwareProperty, kAudioObjectPropertyScopeGlobal, |
| 1710 | kAudioObjectPropertyElementMaster}; |
| 1711 | UInt32 size = sizeof(UInt32); |
| 1712 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1713 | kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, &usedID)); |
| 1714 | if (usedID == kAudioDeviceUnknown) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1715 | RTC_LOG(LS_WARNING) << "GetDeviceName(): Default device unknown"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1716 | } else { |
| 1717 | isDefaultDevice = true; |
| 1718 | } |
| 1719 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1720 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1721 | AudioObjectPropertyAddress propertyAddress = {kAudioDevicePropertyDeviceName, |
| 1722 | scope, 0}; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1723 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1724 | if (isDefaultDevice) { |
| 1725 | char devName[len]; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1726 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1727 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(usedID, &propertyAddress, |
| 1728 | 0, NULL, &len, devName)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1729 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1730 | sprintf(name, "default (%s)", devName); |
| 1731 | } else { |
| 1732 | if (index < numberDevices) { |
| 1733 | usedID = deviceIds[index]; |
| 1734 | } else { |
| 1735 | usedID = index; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1738 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(usedID, &propertyAddress, |
| 1739 | 0, NULL, &len, name)); |
| 1740 | } |
| 1741 | |
| 1742 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 1745 | int32_t AudioDeviceMac::InitDevice(const uint16_t userDeviceIndex, |
| 1746 | AudioDeviceID& deviceId, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1747 | const bool isInput) { |
| 1748 | OSStatus err = noErr; |
| 1749 | UInt32 size = 0; |
| 1750 | AudioObjectPropertyScope deviceScope; |
| 1751 | AudioObjectPropertySelector defaultDeviceSelector; |
| 1752 | AudioDeviceID deviceIds[MaxNumberDevices]; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1753 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1754 | if (isInput) { |
| 1755 | deviceScope = kAudioDevicePropertyScopeInput; |
| 1756 | defaultDeviceSelector = kAudioHardwarePropertyDefaultInputDevice; |
| 1757 | } else { |
| 1758 | deviceScope = kAudioDevicePropertyScopeOutput; |
| 1759 | defaultDeviceSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 1760 | } |
| 1761 | |
| 1762 | AudioObjectPropertyAddress propertyAddress = { |
| 1763 | defaultDeviceSelector, kAudioObjectPropertyScopeGlobal, |
| 1764 | kAudioObjectPropertyElementMaster}; |
| 1765 | |
| 1766 | // Get the actual device IDs |
| 1767 | int numberDevices = |
| 1768 | GetNumberDevices(deviceScope, deviceIds, MaxNumberDevices); |
| 1769 | if (numberDevices < 0) { |
| 1770 | return -1; |
| 1771 | } else if (numberDevices == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1772 | RTC_LOG(LS_ERROR) << "InitDevice(): No devices"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1773 | return -1; |
| 1774 | } |
| 1775 | |
| 1776 | bool isDefaultDevice = false; |
| 1777 | deviceId = kAudioDeviceUnknown; |
| 1778 | if (userDeviceIndex == 0) { |
| 1779 | // Try to use default system device |
| 1780 | size = sizeof(AudioDeviceID); |
| 1781 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1782 | kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, &deviceId)); |
| 1783 | if (deviceId == kAudioDeviceUnknown) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1784 | RTC_LOG(LS_WARNING) << "No default device exists"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1785 | } else { |
| 1786 | isDefaultDevice = true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1787 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1788 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1789 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1790 | if (!isDefaultDevice) { |
| 1791 | deviceId = deviceIds[userDeviceIndex]; |
| 1792 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1793 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1794 | // Obtain device name and manufacturer for logging. |
| 1795 | // Also use this as a test to ensure a user-set device ID is valid. |
| 1796 | char devName[128]; |
| 1797 | char devManf[128]; |
| 1798 | memset(devName, 0, sizeof(devName)); |
| 1799 | memset(devManf, 0, sizeof(devManf)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1800 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1801 | propertyAddress.mSelector = kAudioDevicePropertyDeviceName; |
| 1802 | propertyAddress.mScope = deviceScope; |
| 1803 | propertyAddress.mElement = 0; |
| 1804 | size = sizeof(devName); |
| 1805 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(deviceId, &propertyAddress, |
| 1806 | 0, NULL, &size, devName)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1807 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1808 | propertyAddress.mSelector = kAudioDevicePropertyDeviceManufacturer; |
| 1809 | size = sizeof(devManf); |
| 1810 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(deviceId, &propertyAddress, |
| 1811 | 0, NULL, &size, devManf)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1812 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1813 | if (isInput) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1814 | RTC_LOG(LS_INFO) << "Input device: " << devManf << " " << devName; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1815 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1816 | RTC_LOG(LS_INFO) << "Output device: " << devManf << " " << devName; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1817 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1818 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1819 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1822 | OSStatus AudioDeviceMac::SetDesiredPlayoutFormat() { |
| 1823 | // Our preferred format to work with. |
| 1824 | _outDesiredFormat.mSampleRate = N_PLAY_SAMPLES_PER_SEC; |
| 1825 | _outDesiredFormat.mChannelsPerFrame = _playChannels; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1826 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1827 | if (_ptrAudioBuffer) { |
| 1828 | // Update audio buffer with the selected parameters. |
| 1829 | _ptrAudioBuffer->SetPlayoutSampleRate(N_PLAY_SAMPLES_PER_SEC); |
| 1830 | _ptrAudioBuffer->SetPlayoutChannels((uint8_t)_playChannels); |
| 1831 | } |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1832 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1833 | _renderDelayOffsetSamples = |
| 1834 | _renderBufSizeSamples - N_BUFFERS_OUT * ENGINE_PLAY_BUF_SIZE_IN_SAMPLES * |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1835 | _outDesiredFormat.mChannelsPerFrame; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1836 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1837 | _outDesiredFormat.mBytesPerPacket = |
| 1838 | _outDesiredFormat.mChannelsPerFrame * sizeof(SInt16); |
| 1839 | // In uncompressed audio, a packet is one frame. |
| 1840 | _outDesiredFormat.mFramesPerPacket = 1; |
| 1841 | _outDesiredFormat.mBytesPerFrame = |
| 1842 | _outDesiredFormat.mChannelsPerFrame * sizeof(SInt16); |
| 1843 | _outDesiredFormat.mBitsPerChannel = sizeof(SInt16) * 8; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1844 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1845 | _outDesiredFormat.mFormatFlags = |
| 1846 | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1847 | #ifdef WEBRTC_ARCH_BIG_ENDIAN |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1848 | _outDesiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1849 | #endif |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1850 | _outDesiredFormat.mFormatID = kAudioFormatLinearPCM; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1851 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1852 | OSStatus err = noErr; |
| 1853 | WEBRTC_CA_RETURN_ON_ERR(AudioConverterNew( |
| 1854 | &_outDesiredFormat, &_outStreamFormat, &_renderConverter)); |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1855 | |
henrika | 9868042 | 2017-08-31 06:47:32 -0700 | [diff] [blame] | 1856 | // Try to set buffer size to desired value set to 20ms. |
| 1857 | const uint16_t kPlayBufDelayFixed = 20; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1858 | UInt32 bufByteCount = static_cast<UInt32>( |
henrika | 9868042 | 2017-08-31 06:47:32 -0700 | [diff] [blame] | 1859 | (_outStreamFormat.mSampleRate / 1000.0) * kPlayBufDelayFixed * |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1860 | _outStreamFormat.mChannelsPerFrame * sizeof(Float32)); |
| 1861 | if (_outStreamFormat.mFramesPerPacket != 0) { |
| 1862 | if (bufByteCount % _outStreamFormat.mFramesPerPacket != 0) { |
| 1863 | bufByteCount = (static_cast<UInt32>(bufByteCount / |
| 1864 | _outStreamFormat.mFramesPerPacket) + |
| 1865 | 1) * |
| 1866 | _outStreamFormat.mFramesPerPacket; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1867 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1868 | } |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1869 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1870 | // Ensure the buffer size is within the range provided by the device. |
| 1871 | AudioObjectPropertyAddress propertyAddress = { |
| 1872 | kAudioDevicePropertyDataSource, kAudioDevicePropertyScopeOutput, 0}; |
| 1873 | propertyAddress.mSelector = kAudioDevicePropertyBufferSizeRange; |
| 1874 | AudioValueRange range; |
| 1875 | UInt32 size = sizeof(range); |
| 1876 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1877 | _outputDeviceID, &propertyAddress, 0, NULL, &size, &range)); |
| 1878 | if (range.mMinimum > bufByteCount) { |
| 1879 | bufByteCount = range.mMinimum; |
| 1880 | } else if (range.mMaximum < bufByteCount) { |
| 1881 | bufByteCount = range.mMaximum; |
| 1882 | } |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1883 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1884 | propertyAddress.mSelector = kAudioDevicePropertyBufferSize; |
| 1885 | size = sizeof(bufByteCount); |
| 1886 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectSetPropertyData( |
| 1887 | _outputDeviceID, &propertyAddress, 0, NULL, size, &bufByteCount)); |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1888 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1889 | // Get render device latency. |
| 1890 | propertyAddress.mSelector = kAudioDevicePropertyLatency; |
| 1891 | UInt32 latency = 0; |
| 1892 | size = sizeof(UInt32); |
| 1893 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1894 | _outputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); |
| 1895 | _renderLatencyUs = |
| 1896 | static_cast<uint32_t>((1.0e6 * latency) / _outStreamFormat.mSampleRate); |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1897 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1898 | // Get render stream latency. |
| 1899 | propertyAddress.mSelector = kAudioDevicePropertyStreams; |
| 1900 | AudioStreamID stream = 0; |
| 1901 | size = sizeof(AudioStreamID); |
| 1902 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1903 | _outputDeviceID, &propertyAddress, 0, NULL, &size, &stream)); |
| 1904 | propertyAddress.mSelector = kAudioStreamPropertyLatency; |
| 1905 | size = sizeof(UInt32); |
| 1906 | latency = 0; |
| 1907 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 1908 | _outputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); |
| 1909 | _renderLatencyUs += |
| 1910 | static_cast<uint32_t>((1.0e6 * latency) / _outStreamFormat.mSampleRate); |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1911 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1912 | RTC_LOG(LS_VERBOSE) << "initial playout status: _renderDelayOffsetSamples=" |
| 1913 | << _renderDelayOffsetSamples |
| 1914 | << ", _renderDelayUs=" << _renderDelayUs |
| 1915 | << ", _renderLatencyUs=" << _renderLatencyUs; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1916 | return 0; |
braveyao@webrtc.org | 346a64b | 2015-03-21 01:05:56 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1919 | OSStatus AudioDeviceMac::objectListenerProc( |
| 1920 | AudioObjectID objectId, |
| 1921 | UInt32 numberAddresses, |
| 1922 | const AudioObjectPropertyAddress addresses[], |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1923 | void* clientData) { |
| 1924 | AudioDeviceMac* ptrThis = (AudioDeviceMac*)clientData; |
| 1925 | RTC_DCHECK(ptrThis != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1926 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1927 | ptrThis->implObjectListenerProc(objectId, numberAddresses, addresses); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1928 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1929 | // AudioObjectPropertyListenerProc functions are supposed to return 0 |
| 1930 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | OSStatus AudioDeviceMac::implObjectListenerProc( |
| 1934 | const AudioObjectID objectId, |
| 1935 | const UInt32 numberAddresses, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1936 | const AudioObjectPropertyAddress addresses[]) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1937 | RTC_LOG(LS_VERBOSE) << "AudioDeviceMac::implObjectListenerProc()"; |
andrew@webrtc.org | 6f69eb7 | 2013-06-07 17:56:50 +0000 | [diff] [blame] | 1938 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1939 | for (UInt32 i = 0; i < numberAddresses; i++) { |
| 1940 | if (addresses[i].mSelector == kAudioHardwarePropertyDevices) { |
| 1941 | HandleDeviceChange(); |
| 1942 | } else if (addresses[i].mSelector == kAudioDevicePropertyStreamFormat) { |
| 1943 | HandleStreamFormatChange(objectId, addresses[i]); |
| 1944 | } else if (addresses[i].mSelector == kAudioDevicePropertyDataSource) { |
| 1945 | HandleDataSourceChange(objectId, addresses[i]); |
| 1946 | } else if (addresses[i].mSelector == kAudioDeviceProcessorOverload) { |
| 1947 | HandleProcessorOverload(addresses[i]); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1948 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1949 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1950 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1951 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1954 | int32_t AudioDeviceMac::HandleDeviceChange() { |
| 1955 | OSStatus err = noErr; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1956 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1957 | RTC_LOG(LS_VERBOSE) << "kAudioHardwarePropertyDevices"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1958 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1959 | // A device has changed. Check if our registered devices have been removed. |
| 1960 | // Ensure the devices have been initialized, meaning the IDs are valid. |
| 1961 | if (MicrophoneIsInitialized()) { |
| 1962 | AudioObjectPropertyAddress propertyAddress = { |
| 1963 | kAudioDevicePropertyDeviceIsAlive, kAudioDevicePropertyScopeInput, 0}; |
| 1964 | UInt32 deviceIsAlive = 1; |
| 1965 | UInt32 size = sizeof(UInt32); |
| 1966 | err = AudioObjectGetPropertyData(_inputDeviceID, &propertyAddress, 0, NULL, |
| 1967 | &size, &deviceIsAlive); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1968 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1969 | if (err == kAudioHardwareBadDeviceError || deviceIsAlive == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1970 | RTC_LOG(LS_WARNING) << "Capture device is not alive (probably removed)"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1971 | AtomicSet32(&_captureDeviceIsAlive, 0); |
| 1972 | _mixerManager.CloseMicrophone(); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1973 | } else if (err != noErr) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1974 | logCAMsg(rtc::LS_ERROR, "Error in AudioDeviceGetProperty()", |
| 1975 | (const char*)&err); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1976 | return -1; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1977 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1978 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1979 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1980 | if (SpeakerIsInitialized()) { |
| 1981 | AudioObjectPropertyAddress propertyAddress = { |
| 1982 | kAudioDevicePropertyDeviceIsAlive, kAudioDevicePropertyScopeOutput, 0}; |
| 1983 | UInt32 deviceIsAlive = 1; |
| 1984 | UInt32 size = sizeof(UInt32); |
| 1985 | err = AudioObjectGetPropertyData(_outputDeviceID, &propertyAddress, 0, NULL, |
| 1986 | &size, &deviceIsAlive); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1987 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1988 | if (err == kAudioHardwareBadDeviceError || deviceIsAlive == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1989 | RTC_LOG(LS_WARNING) << "Render device is not alive (probably removed)"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1990 | AtomicSet32(&_renderDeviceIsAlive, 0); |
| 1991 | _mixerManager.CloseSpeaker(); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1992 | } else if (err != noErr) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 1993 | logCAMsg(rtc::LS_ERROR, "Error in AudioDeviceGetProperty()", |
| 1994 | (const char*)&err); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1995 | return -1; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1996 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1997 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1998 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 1999 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 2002 | int32_t AudioDeviceMac::HandleStreamFormatChange( |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2003 | const AudioObjectID objectId, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2004 | const AudioObjectPropertyAddress propertyAddress) { |
| 2005 | OSStatus err = noErr; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2006 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2007 | RTC_LOG(LS_VERBOSE) << "Stream format changed"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2008 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2009 | if (objectId != _inputDeviceID && objectId != _outputDeviceID) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2010 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | // Get the new device format |
| 2014 | AudioStreamBasicDescription streamFormat; |
| 2015 | UInt32 size = sizeof(streamFormat); |
| 2016 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 2017 | objectId, &propertyAddress, 0, NULL, &size, &streamFormat)); |
| 2018 | |
| 2019 | if (streamFormat.mFormatID != kAudioFormatLinearPCM) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 2020 | logCAMsg(rtc::LS_ERROR, "Unacceptable input stream format -> mFormatID", |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2021 | (const char*)&streamFormat.mFormatID); |
| 2022 | return -1; |
| 2023 | } |
| 2024 | |
| 2025 | if (streamFormat.mChannelsPerFrame > N_DEVICE_CHANNELS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2026 | RTC_LOG(LS_ERROR) << "Too many channels on device (mChannelsPerFrame = " |
| 2027 | << streamFormat.mChannelsPerFrame << ")"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2028 | return -1; |
| 2029 | } |
| 2030 | |
Peter Hanspers | d931705 | 2017-10-06 14:13:51 +0200 | [diff] [blame] | 2031 | if (_ptrAudioBuffer && streamFormat.mChannelsPerFrame != _recChannels) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2032 | RTC_LOG(LS_ERROR) << "Changing channels not supported (mChannelsPerFrame = " |
| 2033 | << streamFormat.mChannelsPerFrame << ")"; |
Peter Hanspers | d931705 | 2017-10-06 14:13:51 +0200 | [diff] [blame] | 2034 | return -1; |
| 2035 | } |
| 2036 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2037 | RTC_LOG(LS_VERBOSE) << "Stream format:"; |
| 2038 | RTC_LOG(LS_VERBOSE) << "mSampleRate = " << streamFormat.mSampleRate |
| 2039 | << ", mChannelsPerFrame = " |
| 2040 | << streamFormat.mChannelsPerFrame; |
| 2041 | RTC_LOG(LS_VERBOSE) << "mBytesPerPacket = " << streamFormat.mBytesPerPacket |
| 2042 | << ", mFramesPerPacket = " |
| 2043 | << streamFormat.mFramesPerPacket; |
| 2044 | RTC_LOG(LS_VERBOSE) << "mBytesPerFrame = " << streamFormat.mBytesPerFrame |
| 2045 | << ", mBitsPerChannel = " << streamFormat.mBitsPerChannel; |
| 2046 | RTC_LOG(LS_VERBOSE) << "mFormatFlags = " << streamFormat.mFormatFlags; |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 2047 | logCAMsg(rtc::LS_VERBOSE, "mFormatID", (const char*)&streamFormat.mFormatID); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2048 | |
| 2049 | if (propertyAddress.mScope == kAudioDevicePropertyScopeInput) { |
| 2050 | const int io_block_size_samples = streamFormat.mChannelsPerFrame * |
| 2051 | streamFormat.mSampleRate / 100 * |
| 2052 | N_BLOCKS_IO; |
| 2053 | if (io_block_size_samples > _captureBufSizeSamples) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2054 | RTC_LOG(LS_ERROR) << "Input IO block size (" << io_block_size_samples |
| 2055 | << ") is larger than ring buffer (" |
| 2056 | << _captureBufSizeSamples << ")"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2057 | return -1; |
| 2058 | } |
| 2059 | |
| 2060 | memcpy(&_inStreamFormat, &streamFormat, sizeof(streamFormat)); |
| 2061 | |
| 2062 | if (_inStreamFormat.mChannelsPerFrame >= 2 && (_recChannels == 2)) { |
| 2063 | _inDesiredFormat.mChannelsPerFrame = 2; |
| 2064 | } else { |
| 2065 | // Disable stereo recording when we only have one channel on the device. |
| 2066 | _inDesiredFormat.mChannelsPerFrame = 1; |
| 2067 | _recChannels = 1; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2068 | RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2071 | // Recreate the converter with the new format |
| 2072 | // TODO(xians): make this thread safe |
| 2073 | WEBRTC_CA_RETURN_ON_ERR(AudioConverterDispose(_captureConverter)); |
| 2074 | |
| 2075 | WEBRTC_CA_RETURN_ON_ERR(AudioConverterNew(&streamFormat, &_inDesiredFormat, |
| 2076 | &_captureConverter)); |
| 2077 | } else { |
| 2078 | memcpy(&_outStreamFormat, &streamFormat, sizeof(streamFormat)); |
| 2079 | |
| 2080 | // Our preferred format to work with |
| 2081 | if (_outStreamFormat.mChannelsPerFrame < 2) { |
| 2082 | _playChannels = 1; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2083 | RTC_LOG(LS_VERBOSE) << "Stereo playout unavailable on this device"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2084 | } |
| 2085 | WEBRTC_CA_RETURN_ON_ERR(SetDesiredPlayoutFormat()); |
| 2086 | } |
| 2087 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 2090 | int32_t AudioDeviceMac::HandleDataSourceChange( |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2091 | const AudioObjectID objectId, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2092 | const AudioObjectPropertyAddress propertyAddress) { |
| 2093 | OSStatus err = noErr; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2094 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2095 | if (_macBookPro && |
| 2096 | propertyAddress.mScope == kAudioDevicePropertyScopeOutput) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2097 | RTC_LOG(LS_VERBOSE) << "Data source changed"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2098 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2099 | _macBookProPanRight = false; |
| 2100 | UInt32 dataSource = 0; |
| 2101 | UInt32 size = sizeof(UInt32); |
| 2102 | WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( |
| 2103 | objectId, &propertyAddress, 0, NULL, &size, &dataSource)); |
| 2104 | if (dataSource == 'ispk') { |
| 2105 | _macBookProPanRight = true; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2106 | RTC_LOG(LS_VERBOSE) |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 2107 | << "MacBook Pro using internal speakers; stereo panning right"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2108 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2109 | RTC_LOG(LS_VERBOSE) << "MacBook Pro not using internal speakers"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2110 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2111 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2112 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2113 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2114 | } |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 2115 | int32_t AudioDeviceMac::HandleProcessorOverload( |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2116 | const AudioObjectPropertyAddress propertyAddress) { |
| 2117 | // TODO(xians): we probably want to notify the user in some way of the |
| 2118 | // overload. However, the Windows interpretations of these errors seem to |
| 2119 | // be more severe than what ProcessorOverload is thrown for. |
| 2120 | // |
| 2121 | // We don't log the notification, as it's sent from the HAL's IO thread. We |
| 2122 | // don't want to slow it down even further. |
| 2123 | if (propertyAddress.mScope == kAudioDevicePropertyScopeInput) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2124 | // RTC_LOG(LS_WARNING) << "Capture processor // overload"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2125 | //_callback->ProblemIsReported( |
| 2126 | // SndCardStreamObserver::ERecordingProblem); |
| 2127 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2128 | // RTC_LOG(LS_WARNING) << "Render processor overload"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2129 | //_callback->ProblemIsReported( |
| 2130 | // SndCardStreamObserver::EPlaybackProblem); |
| 2131 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2132 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2133 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | // ============================================================================ |
| 2137 | // Thread Methods |
| 2138 | // ============================================================================ |
| 2139 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2140 | OSStatus AudioDeviceMac::deviceIOProc(AudioDeviceID, |
| 2141 | const AudioTimeStamp*, |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2142 | const AudioBufferList* inputData, |
| 2143 | const AudioTimeStamp* inputTime, |
| 2144 | AudioBufferList* outputData, |
| 2145 | const AudioTimeStamp* outputTime, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2146 | void* clientData) { |
| 2147 | AudioDeviceMac* ptrThis = (AudioDeviceMac*)clientData; |
| 2148 | RTC_DCHECK(ptrThis != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2149 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2150 | ptrThis->implDeviceIOProc(inputData, inputTime, outputData, outputTime); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2151 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2152 | // AudioDeviceIOProc functions are supposed to return 0 |
| 2153 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | OSStatus AudioDeviceMac::outConverterProc(AudioConverterRef, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2157 | UInt32* numberDataPackets, |
| 2158 | AudioBufferList* data, |
| 2159 | AudioStreamPacketDescription**, |
| 2160 | void* userData) { |
| 2161 | AudioDeviceMac* ptrThis = (AudioDeviceMac*)userData; |
| 2162 | RTC_DCHECK(ptrThis != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2163 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2164 | return ptrThis->implOutConverterProc(numberDataPackets, data); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2167 | OSStatus AudioDeviceMac::inDeviceIOProc(AudioDeviceID, |
| 2168 | const AudioTimeStamp*, |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2169 | const AudioBufferList* inputData, |
| 2170 | const AudioTimeStamp* inputTime, |
| 2171 | AudioBufferList*, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2172 | const AudioTimeStamp*, |
| 2173 | void* clientData) { |
| 2174 | AudioDeviceMac* ptrThis = (AudioDeviceMac*)clientData; |
| 2175 | RTC_DCHECK(ptrThis != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2176 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2177 | ptrThis->implInDeviceIOProc(inputData, inputTime); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2178 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2179 | // AudioDeviceIOProc functions are supposed to return 0 |
| 2180 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | OSStatus AudioDeviceMac::inConverterProc( |
| 2184 | AudioConverterRef, |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2185 | UInt32* numberDataPackets, |
| 2186 | AudioBufferList* data, |
| 2187 | AudioStreamPacketDescription** /*dataPacketDescription*/, |
| 2188 | void* userData) { |
| 2189 | AudioDeviceMac* ptrThis = static_cast<AudioDeviceMac*>(userData); |
| 2190 | RTC_DCHECK(ptrThis != NULL); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2191 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2192 | return ptrThis->implInConverterProc(numberDataPackets, data); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2195 | OSStatus AudioDeviceMac::implDeviceIOProc(const AudioBufferList* inputData, |
| 2196 | const AudioTimeStamp* inputTime, |
| 2197 | AudioBufferList* outputData, |
| 2198 | const AudioTimeStamp* outputTime) { |
| 2199 | OSStatus err = noErr; |
| 2200 | UInt64 outputTimeNs = AudioConvertHostTimeToNanos(outputTime->mHostTime); |
| 2201 | UInt64 nowNs = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2202 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2203 | if (!_twoDevices && _recording) { |
| 2204 | implInDeviceIOProc(inputData, inputTime); |
| 2205 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2206 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2207 | // Check if we should close down audio device |
| 2208 | // Double-checked locking optimization to remove locking overhead |
| 2209 | if (_doStop) { |
| 2210 | _critSect.Enter(); |
| 2211 | if (_doStop) { |
| 2212 | if (_twoDevices || (!_recording && !_playing)) { |
| 2213 | // In the case of a shared device, the single driving ioProc |
| 2214 | // is stopped here |
| 2215 | WEBRTC_CA_LOG_ERR(AudioDeviceStop(_outputDeviceID, _deviceIOProcID)); |
| 2216 | WEBRTC_CA_LOG_WARN( |
| 2217 | AudioDeviceDestroyIOProcID(_outputDeviceID, _deviceIOProcID)); |
| 2218 | if (err == noErr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2219 | RTC_LOG(LS_VERBOSE) << "Playout or shared device stopped"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2220 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | _doStop = false; |
| 2224 | _stopEvent.Set(); |
| 2225 | _critSect.Leave(); |
| 2226 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2227 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2228 | _critSect.Leave(); |
| 2229 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2230 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2231 | if (!_playing) { |
| 2232 | // This can be the case when a shared device is capturing but not |
| 2233 | // rendering. We allow the checks above before returning to avoid a |
| 2234 | // timeout when capturing is stopped. |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2235 | return 0; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | RTC_DCHECK(_outStreamFormat.mBytesPerFrame != 0); |
| 2239 | UInt32 size = |
| 2240 | outputData->mBuffers->mDataByteSize / _outStreamFormat.mBytesPerFrame; |
| 2241 | |
| 2242 | // TODO(xians): signal an error somehow? |
| 2243 | err = AudioConverterFillComplexBuffer(_renderConverter, outConverterProc, |
| 2244 | this, &size, outputData, NULL); |
| 2245 | if (err != noErr) { |
| 2246 | if (err == 1) { |
| 2247 | // This is our own error. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2248 | RTC_LOG(LS_ERROR) << "Error in AudioConverterFillComplexBuffer()"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2249 | return 1; |
| 2250 | } else { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 2251 | logCAMsg(rtc::LS_ERROR, "Error in AudioConverterFillComplexBuffer()", |
| 2252 | (const char*)&err); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2253 | return 1; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | PaRingBufferSize bufSizeSamples = |
| 2258 | PaUtil_GetRingBufferReadAvailable(_paRenderBuffer); |
| 2259 | |
| 2260 | int32_t renderDelayUs = |
| 2261 | static_cast<int32_t>(1e-3 * (outputTimeNs - nowNs) + 0.5); |
| 2262 | renderDelayUs += static_cast<int32_t>( |
| 2263 | (1.0e6 * bufSizeSamples) / _outDesiredFormat.mChannelsPerFrame / |
| 2264 | _outDesiredFormat.mSampleRate + |
| 2265 | 0.5); |
| 2266 | |
| 2267 | AtomicSet32(&_renderDelayUs, renderDelayUs); |
| 2268 | |
| 2269 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2272 | OSStatus AudioDeviceMac::implOutConverterProc(UInt32* numberDataPackets, |
| 2273 | AudioBufferList* data) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 2274 | RTC_DCHECK(data->mNumberBuffers == 1); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2275 | PaRingBufferSize numSamples = |
| 2276 | *numberDataPackets * _outDesiredFormat.mChannelsPerFrame; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2277 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2278 | data->mBuffers->mNumberChannels = _outDesiredFormat.mChannelsPerFrame; |
| 2279 | // Always give the converter as much as it wants, zero padding as required. |
| 2280 | data->mBuffers->mDataByteSize = |
| 2281 | *numberDataPackets * _outDesiredFormat.mBytesPerPacket; |
| 2282 | data->mBuffers->mData = _renderConvertData; |
| 2283 | memset(_renderConvertData, 0, sizeof(_renderConvertData)); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2284 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2285 | PaUtil_ReadRingBuffer(_paRenderBuffer, _renderConvertData, numSamples); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2286 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2287 | kern_return_t kernErr = semaphore_signal_all(_renderSemaphore); |
| 2288 | if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2289 | RTC_LOG(LS_ERROR) << "semaphore_signal_all() error: " << kernErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2290 | return 1; |
| 2291 | } |
| 2292 | |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
| 2296 | OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, |
| 2297 | const AudioTimeStamp* inputTime) { |
| 2298 | OSStatus err = noErr; |
| 2299 | UInt64 inputTimeNs = AudioConvertHostTimeToNanos(inputTime->mHostTime); |
| 2300 | UInt64 nowNs = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); |
| 2301 | |
| 2302 | // Check if we should close down audio device |
| 2303 | // Double-checked locking optimization to remove locking overhead |
| 2304 | if (_doStopRec) { |
| 2305 | _critSect.Enter(); |
| 2306 | if (_doStopRec) { |
| 2307 | // This will be signalled only when a shared device is not in use. |
| 2308 | WEBRTC_CA_LOG_ERR(AudioDeviceStop(_inputDeviceID, _inDeviceIOProcID)); |
| 2309 | WEBRTC_CA_LOG_WARN( |
| 2310 | AudioDeviceDestroyIOProcID(_inputDeviceID, _inDeviceIOProcID)); |
| 2311 | if (err == noErr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2312 | RTC_LOG(LS_VERBOSE) << "Recording device stopped"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | _doStopRec = false; |
| 2316 | _stopEventRec.Set(); |
| 2317 | _critSect.Leave(); |
| 2318 | return 0; |
| 2319 | } |
| 2320 | _critSect.Leave(); |
| 2321 | } |
| 2322 | |
| 2323 | if (!_recording) { |
| 2324 | // Allow above checks to avoid a timeout on stopping capture. |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | PaRingBufferSize bufSizeSamples = |
| 2329 | PaUtil_GetRingBufferReadAvailable(_paCaptureBuffer); |
| 2330 | |
| 2331 | int32_t captureDelayUs = |
| 2332 | static_cast<int32_t>(1e-3 * (nowNs - inputTimeNs) + 0.5); |
| 2333 | captureDelayUs += static_cast<int32_t>((1.0e6 * bufSizeSamples) / |
| 2334 | _inStreamFormat.mChannelsPerFrame / |
| 2335 | _inStreamFormat.mSampleRate + |
| 2336 | 0.5); |
| 2337 | |
| 2338 | AtomicSet32(&_captureDelayUs, captureDelayUs); |
| 2339 | |
| 2340 | RTC_DCHECK(inputData->mNumberBuffers == 1); |
| 2341 | PaRingBufferSize numSamples = inputData->mBuffers->mDataByteSize * |
| 2342 | _inStreamFormat.mChannelsPerFrame / |
| 2343 | _inStreamFormat.mBytesPerPacket; |
| 2344 | PaUtil_WriteRingBuffer(_paCaptureBuffer, inputData->mBuffers->mData, |
| 2345 | numSamples); |
| 2346 | |
| 2347 | kern_return_t kernErr = semaphore_signal_all(_captureSemaphore); |
| 2348 | if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2349 | RTC_LOG(LS_ERROR) << "semaphore_signal_all() error: " << kernErr; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | return err; |
| 2353 | } |
| 2354 | |
| 2355 | OSStatus AudioDeviceMac::implInConverterProc(UInt32* numberDataPackets, |
| 2356 | AudioBufferList* data) { |
| 2357 | RTC_DCHECK(data->mNumberBuffers == 1); |
| 2358 | PaRingBufferSize numSamples = |
| 2359 | *numberDataPackets * _inStreamFormat.mChannelsPerFrame; |
| 2360 | |
| 2361 | while (PaUtil_GetRingBufferReadAvailable(_paCaptureBuffer) < numSamples) { |
| 2362 | mach_timespec_t timeout; |
| 2363 | timeout.tv_sec = 0; |
| 2364 | timeout.tv_nsec = TIMER_PERIOD_MS; |
| 2365 | |
| 2366 | kern_return_t kernErr = semaphore_timedwait(_captureSemaphore, timeout); |
| 2367 | if (kernErr == KERN_OPERATION_TIMED_OUT) { |
| 2368 | int32_t signal = AtomicGet32(&_captureDeviceIsAlive); |
| 2369 | if (signal == 0) { |
| 2370 | // The capture device is no longer alive; stop the worker thread. |
| 2371 | *numberDataPackets = 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2372 | return 1; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2373 | } |
| 2374 | } else if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2375 | RTC_LOG(LS_ERROR) << "semaphore_wait() error: " << kernErr; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2376 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2377 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2378 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2379 | // Pass the read pointer directly to the converter to avoid a memcpy. |
| 2380 | void* dummyPtr; |
| 2381 | PaRingBufferSize dummySize; |
| 2382 | PaUtil_GetRingBufferReadRegions(_paCaptureBuffer, numSamples, |
| 2383 | &data->mBuffers->mData, &numSamples, |
| 2384 | &dummyPtr, &dummySize); |
| 2385 | PaUtil_AdvanceRingBufferReadIndex(_paCaptureBuffer, numSamples); |
| 2386 | |
| 2387 | data->mBuffers->mNumberChannels = _inStreamFormat.mChannelsPerFrame; |
| 2388 | *numberDataPackets = numSamples / _inStreamFormat.mChannelsPerFrame; |
| 2389 | data->mBuffers->mDataByteSize = |
| 2390 | *numberDataPackets * _inStreamFormat.mBytesPerPacket; |
| 2391 | |
| 2392 | return 0; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2395 | bool AudioDeviceMac::RunRender(void* ptrThis) { |
| 2396 | return static_cast<AudioDeviceMac*>(ptrThis)->RenderWorkerThread(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2399 | bool AudioDeviceMac::RenderWorkerThread() { |
| 2400 | PaRingBufferSize numSamples = |
| 2401 | ENGINE_PLAY_BUF_SIZE_IN_SAMPLES * _outDesiredFormat.mChannelsPerFrame; |
| 2402 | while (PaUtil_GetRingBufferWriteAvailable(_paRenderBuffer) - |
| 2403 | _renderDelayOffsetSamples < |
| 2404 | numSamples) { |
| 2405 | mach_timespec_t timeout; |
| 2406 | timeout.tv_sec = 0; |
| 2407 | timeout.tv_nsec = TIMER_PERIOD_MS; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2408 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2409 | kern_return_t kernErr = semaphore_timedwait(_renderSemaphore, timeout); |
| 2410 | if (kernErr == KERN_OPERATION_TIMED_OUT) { |
| 2411 | int32_t signal = AtomicGet32(&_renderDeviceIsAlive); |
| 2412 | if (signal == 0) { |
| 2413 | // The render device is no longer alive; stop the worker thread. |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2414 | return false; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2415 | } |
| 2416 | } else if (kernErr != KERN_SUCCESS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2417 | RTC_LOG(LS_ERROR) << "semaphore_timedwait() error: " << kernErr; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2418 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2419 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2420 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2421 | int8_t playBuffer[4 * ENGINE_PLAY_BUF_SIZE_IN_SAMPLES]; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2422 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2423 | if (!_ptrAudioBuffer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2424 | RTC_LOG(LS_ERROR) << "capture AudioBuffer is invalid"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2425 | return false; |
| 2426 | } |
| 2427 | |
| 2428 | // Ask for new PCM data to be played out using the AudioDeviceBuffer. |
| 2429 | uint32_t nSamples = |
| 2430 | _ptrAudioBuffer->RequestPlayoutData(ENGINE_PLAY_BUF_SIZE_IN_SAMPLES); |
| 2431 | |
| 2432 | nSamples = _ptrAudioBuffer->GetPlayoutData(playBuffer); |
| 2433 | if (nSamples != ENGINE_PLAY_BUF_SIZE_IN_SAMPLES) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2434 | RTC_LOG(LS_ERROR) << "invalid number of output samples(" << nSamples << ")"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | uint32_t nOutSamples = nSamples * _outDesiredFormat.mChannelsPerFrame; |
| 2438 | |
| 2439 | SInt16* pPlayBuffer = (SInt16*)&playBuffer; |
| 2440 | if (_macBookProPanRight && (_playChannels == 2)) { |
| 2441 | // Mix entirely into the right channel and zero the left channel. |
| 2442 | SInt32 sampleInt32 = 0; |
| 2443 | for (uint32_t sampleIdx = 0; sampleIdx < nOutSamples; sampleIdx += 2) { |
| 2444 | sampleInt32 = pPlayBuffer[sampleIdx]; |
| 2445 | sampleInt32 += pPlayBuffer[sampleIdx + 1]; |
| 2446 | sampleInt32 /= 2; |
| 2447 | |
| 2448 | if (sampleInt32 > 32767) { |
| 2449 | sampleInt32 = 32767; |
| 2450 | } else if (sampleInt32 < -32768) { |
| 2451 | sampleInt32 = -32768; |
| 2452 | } |
| 2453 | |
| 2454 | pPlayBuffer[sampleIdx] = 0; |
| 2455 | pPlayBuffer[sampleIdx + 1] = static_cast<SInt16>(sampleInt32); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2456 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2457 | } |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2458 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2459 | PaUtil_WriteRingBuffer(_paRenderBuffer, pPlayBuffer, nOutSamples); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2460 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2461 | return true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2464 | bool AudioDeviceMac::RunCapture(void* ptrThis) { |
| 2465 | return static_cast<AudioDeviceMac*>(ptrThis)->CaptureWorkerThread(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2468 | bool AudioDeviceMac::CaptureWorkerThread() { |
| 2469 | OSStatus err = noErr; |
| 2470 | UInt32 noRecSamples = |
| 2471 | ENGINE_REC_BUF_SIZE_IN_SAMPLES * _inDesiredFormat.mChannelsPerFrame; |
| 2472 | SInt16 recordBuffer[noRecSamples]; |
| 2473 | UInt32 size = ENGINE_REC_BUF_SIZE_IN_SAMPLES; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2474 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2475 | AudioBufferList engineBuffer; |
| 2476 | engineBuffer.mNumberBuffers = 1; // Interleaved channels. |
| 2477 | engineBuffer.mBuffers->mNumberChannels = _inDesiredFormat.mChannelsPerFrame; |
| 2478 | engineBuffer.mBuffers->mDataByteSize = |
| 2479 | _inDesiredFormat.mBytesPerPacket * noRecSamples; |
| 2480 | engineBuffer.mBuffers->mData = recordBuffer; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2481 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2482 | err = AudioConverterFillComplexBuffer(_captureConverter, inConverterProc, |
| 2483 | this, &size, &engineBuffer, NULL); |
| 2484 | if (err != noErr) { |
| 2485 | if (err == 1) { |
| 2486 | // This is our own error. |
| 2487 | return false; |
| 2488 | } else { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 2489 | logCAMsg(rtc::LS_ERROR, "Error in AudioConverterFillComplexBuffer()", |
| 2490 | (const char*)&err); |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2491 | return false; |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | // TODO(xians): what if the returned size is incorrect? |
| 2496 | if (size == ENGINE_REC_BUF_SIZE_IN_SAMPLES) { |
| 2497 | uint32_t currentMicLevel(0); |
| 2498 | uint32_t newMicLevel(0); |
| 2499 | int32_t msecOnPlaySide; |
| 2500 | int32_t msecOnRecordSide; |
| 2501 | |
| 2502 | int32_t captureDelayUs = AtomicGet32(&_captureDelayUs); |
| 2503 | int32_t renderDelayUs = AtomicGet32(&_renderDelayUs); |
| 2504 | |
| 2505 | msecOnPlaySide = |
| 2506 | static_cast<int32_t>(1e-3 * (renderDelayUs + _renderLatencyUs) + 0.5); |
| 2507 | msecOnRecordSide = |
| 2508 | static_cast<int32_t>(1e-3 * (captureDelayUs + _captureLatencyUs) + 0.5); |
| 2509 | |
| 2510 | if (!_ptrAudioBuffer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2511 | RTC_LOG(LS_ERROR) << "capture AudioBuffer is invalid"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2512 | return false; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2515 | // store the recorded buffer (no action will be taken if the |
| 2516 | // #recorded samples is not a full buffer) |
| 2517 | _ptrAudioBuffer->SetRecordedBuffer((int8_t*)&recordBuffer, (uint32_t)size); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2518 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2519 | if (AGC()) { |
| 2520 | // Use mod to ensure we check the volume on the first pass. |
| 2521 | if (get_mic_volume_counter_ms_ % kGetMicVolumeIntervalMs == 0) { |
| 2522 | get_mic_volume_counter_ms_ = 0; |
| 2523 | // store current mic level in the audio buffer if AGC is enabled |
| 2524 | if (MicrophoneVolume(currentMicLevel) == 0) { |
| 2525 | // this call does not affect the actual microphone volume |
| 2526 | _ptrAudioBuffer->SetCurrentMicLevel(currentMicLevel); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2527 | } |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2528 | } |
| 2529 | get_mic_volume_counter_ms_ += kBufferSizeMs; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2532 | _ptrAudioBuffer->SetVQEData(msecOnPlaySide, msecOnRecordSide, 0); |
| 2533 | |
| 2534 | _ptrAudioBuffer->SetTypingStatus(KeyPressed()); |
| 2535 | |
| 2536 | // deliver recorded samples at specified sample rate, mic level etc. |
| 2537 | // to the observer using callback |
| 2538 | _ptrAudioBuffer->DeliverRecordedData(); |
| 2539 | |
| 2540 | if (AGC()) { |
| 2541 | newMicLevel = _ptrAudioBuffer->NewMicLevel(); |
| 2542 | if (newMicLevel != 0) { |
| 2543 | // The VQE will only deliver non-zero microphone levels when |
| 2544 | // a change is needed. |
| 2545 | // Set this new mic level (received from the observer as return |
| 2546 | // value in the callback). |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2547 | RTC_LOG(LS_VERBOSE) << "AGC change of volume: old=" << currentMicLevel |
| 2548 | << " => new=" << newMicLevel; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2549 | if (SetMicrophoneVolume(newMicLevel) == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2550 | RTC_LOG(LS_WARNING) |
saza | b4aa4eb | 2017-07-19 01:12:36 -0700 | [diff] [blame] | 2551 | << "the required modification of the microphone volume failed"; |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | return true; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
niklas.enbom@webrtc.org | cc9238e | 2013-08-15 14:19:12 +0000 | [diff] [blame] | 2560 | bool AudioDeviceMac::KeyPressed() { |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 2561 | bool key_down = false; |
niklas.enbom@webrtc.org | cc9238e | 2013-08-15 14:19:12 +0000 | [diff] [blame] | 2562 | // Loop through all Mac virtual key constant values. |
andrew | 2bc63a1 | 2016-01-11 15:59:17 -0800 | [diff] [blame] | 2563 | for (unsigned int key_index = 0; key_index < arraysize(prev_key_state_); |
| 2564 | ++key_index) { |
| 2565 | bool keyState = |
| 2566 | CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key_index); |
niklas.enbom@webrtc.org | cc9238e | 2013-08-15 14:19:12 +0000 | [diff] [blame] | 2567 | // A false -> true change in keymap means a key is pressed. |
| 2568 | key_down |= (keyState && !prev_key_state_[key_index]); |
| 2569 | // Save current state. |
| 2570 | prev_key_state_[key_index] = keyState; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 2571 | } |
niklas.enbom@webrtc.org | cc9238e | 2013-08-15 14:19:12 +0000 | [diff] [blame] | 2572 | return key_down; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 2573 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 2574 | } // namespace webrtc |