| 6 |
| 7 | #include "AudioOutputAudioUnitOSX.h" |
| 8 | #include "../../VoIPController.h" |
| 9 | #include "../../logging.h" |
| 10 | #include <cstdio> |
| 11 | #include <cstdlib> |
| 12 | #include <sys/sysctl.h> |
| 13 |
| 14 | #define BUFFER_SIZE 960 |
| 15 | #define CHECK_AU_ERROR(res, msg) \ |
| 16 | if (res != noErr) \ |
| 17 | { \ |
| 18 | LOGE("output: " msg ": OSStatus=%d", (int)res); \ |
| 19 | return; \ |
| 20 | } |
| 21 |
| 22 | #define kOutputBus 0 |
| 23 | #define kInputBus 1 |
| 24 |
| 25 | using namespace tgvoip; |
| 26 | using namespace tgvoip::audio; |
| 27 |
| 28 | AudioOutputAudioUnitLegacy::AudioOutputAudioUnitLegacy(std::string deviceID) |
| 29 | { |
| 30 | remainingDataSize = 0; |
| 31 | isPlaying = false; |
| 32 | sysDevID = 0; |
| 33 |
| 34 | OSStatus status; |
| 35 | AudioComponentDescription inputDesc = { |
| 36 | .componentType = kAudioUnitType_Output, .componentSubType = kAudioUnitSubType_HALOutput, .componentFlags = 0, .componentFlagsMask = 0, .componentManufacturer = kAudioUnitManufacturer_Apple}; |
| 37 | AudioComponent component = AudioComponentFindNext(nullptr, &inputDesc); |
| 38 | status = AudioComponentInstanceNew(component, &unit); |
| 39 | CHECK_AU_ERROR(status, "Error creating AudioUnit"); |
| 40 |
| 41 | UInt32 flag = 1; |
| 42 | status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag)); |
| 43 | CHECK_AU_ERROR(status, "Error enabling AudioUnit output"); |
| 44 | flag = 0; |
| 45 | status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); |
| 46 | CHECK_AU_ERROR(status, "Error enabling AudioUnit input"); |
| 47 |
| 48 | char model[128]; |
| 49 | std::memset(model, 0, sizeof(model)); |
| 50 | std::size_t msize = sizeof(model); |
| 51 | int mres = sysctlbyname("hw.model", model, &msize, nullptr, 0); |
| 52 | if (mres == 0) |
| 53 | { |
| 54 | LOGV("Mac model: %s", model); |
| 55 | isMacBookPro = (strncmp("MacBookPro", model, 10) == 0); |
| 56 | } |
| 57 |
| 58 | SetCurrentDevice(deviceID); |
| 59 |
| 60 | CFRunLoopRef theRunLoop = nullptr; |
| 61 | AudioObjectPropertyAddress propertyAddress = {kAudioHardwarePropertyRunLoop, |
| 62 | kAudioObjectPropertyScopeGlobal, |
| 63 | kAudioObjectPropertyElementMaster}; |
| 64 | status = AudioObjectSetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, sizeof(CFRunLoopRef), &theRunLoop); |
| 65 |
| 66 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 67 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 68 | propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 69 | AudioObjectAddPropertyListener(kAudioObjectSystemObject, &propertyAddress, AudioOutputAudioUnitLegacy::DefaultDeviceChangedCallback, this); |
| 70 |
| 71 | AudioStreamBasicDescription desiredFormat = { |
| 72 | .mSampleRate = /*hardwareFormat.mSampleRate*/ 48000, .mFormatID = kAudioFormatLinearPCM, .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian, .mFramesPerPacket = 1, .mChannelsPerFrame = 1, .mBitsPerChannel = 16, .mBytesPerPacket = 2, .mBytesPerFrame = 2}; |
| 73 |
| 74 | status = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &desiredFormat, sizeof(desiredFormat)); |
| 75 | CHECK_AU_ERROR(status, "Error setting format"); |
| 76 |
| 77 | AURenderCallbackStruct callbackStruct; |
| 78 | callbackStruct.inputProc = AudioOutputAudioUnitLegacy::BufferCallback; |
| 79 | callbackStruct.inputProcRefCon = this; |
| 80 | status = AudioUnitSetProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &callbackStruct, sizeof(callbackStruct)); |
| 81 | CHECK_AU_ERROR(status, "Error setting input buffer callback"); |
| 82 | status = AudioUnitInitialize(unit); |
| 83 | CHECK_AU_ERROR(status, "Error initializing unit"); |
| 84 | } |
| 85 |
| 86 | AudioOutputAudioUnitLegacy::~AudioOutputAudioUnitLegacy() |
| 87 | { |
| 88 | AudioObjectPropertyAddress propertyAddress; |
| 89 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 90 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 91 | propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 92 | AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &propertyAddress, AudioOutputAudioUnitLegacy::DefaultDeviceChangedCallback, this); |
| 93 |
| 94 | AudioObjectPropertyAddress dataSourceProp = { |
| 95 | kAudioDevicePropertyDataSource, |
| 96 | kAudioDevicePropertyScopeOutput, |
| 97 | kAudioObjectPropertyElementMaster}; |
| 98 | if (isMacBookPro && sysDevID && AudioObjectHasProperty(sysDevID, &dataSourceProp)) |
| 99 | { |
| 100 | AudioObjectRemovePropertyListener(sysDevID, &dataSourceProp, AudioOutputAudioUnitLegacy::DefaultDeviceChangedCallback, this); |
| 101 | } |
| 102 |
| 103 | AudioUnitUninitialize(unit); |
| 104 | AudioComponentInstanceDispose(unit); |
| 105 | } |
| 106 |
| 107 | void AudioOutputAudioUnitLegacy::Start() |
| 108 | { |
| 109 | isPlaying = true; |
| 110 | OSStatus status = AudioOutputUnitStart(unit); |
| 111 | CHECK_AU_ERROR(status, "Error starting AudioUnit"); |
| 112 | } |
| 113 |
| 114 | void AudioOutputAudioUnitLegacy::Stop() |
| 115 | { |
| 116 | isPlaying = false; |
| 117 | OSStatus status = AudioOutputUnitStart(unit); |
| 118 | CHECK_AU_ERROR(status, "Error stopping AudioUnit"); |
| 119 | } |
| 120 |
| 121 | OSStatus AudioOutputAudioUnitLegacy::BufferCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData) |
| 122 | { |
| 123 | AudioOutputAudioUnitLegacy* input = reinterpret_cast<AudioOutputAudioUnitLegacy*>(inRefCon); |
| 124 | input->HandleBufferCallback(ioData); |
| 125 | return noErr; |
| 126 | } |
| 127 |
| 128 | bool AudioOutputAudioUnitLegacy::IsPlaying() |
| 129 | { |
| 130 | return isPlaying; |
| 131 | } |
| 132 |
| 133 | void AudioOutputAudioUnitLegacy::HandleBufferCallback(AudioBufferList* ioData) |
| 134 | { |
| 135 | int i; |
| 136 | for (i = 0; i < ioData->mNumberBuffers; i++) |
| 137 | { |
| 138 | AudioBuffer buf = ioData->mBuffers[i]; |
| 139 | if (!isPlaying) |
| 140 | { |
| 141 | std::memset(buf.mData, 0, buf.mDataByteSize); |
| 142 | return; |
| 143 | } |
| 144 | while (remainingDataSize < buf.mDataByteSize) |
| 145 | { |
| 146 | assert(remainingDataSize + BUFFER_SIZE * 2 < 10240); |
| 147 | InvokeCallback(remainingData + remainingDataSize, BUFFER_SIZE * 2); |
| 148 | remainingDataSize += BUFFER_SIZE * 2; |
| 149 | } |
| 150 | std::memcpy(buf.mData, remainingData, buf.mDataByteSize); |
| 151 | remainingDataSize -= buf.mDataByteSize; |
| 152 | memmove(remainingData, remainingData + buf.mDataByteSize, remainingDataSize); |
| 153 | } |
| 154 | } |
| 155 |
| 156 | void AudioOutputAudioUnitLegacy::EnumerateDevices(std::vector<AudioOutputDevice>& devs) |
| 157 | { |
| 158 | AudioObjectPropertyAddress propertyAddress = { |
| 159 | kAudioHardwarePropertyDevices, |
| 160 | kAudioObjectPropertyScopeGlobal, |
| 161 | kAudioObjectPropertyElementMaster}; |
| 162 |
| 163 | UInt32 dataSize = 0; |
| 164 | OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &dataSize); |
| 165 | if (kAudioHardwareNoError != status) |
| 166 | { |
| 167 | LOGE("AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i", status); |
| 168 | return; |
| 169 | } |
| 170 |
| 171 | UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID)); |
| 172 |
| 173 | AudioDeviceID* audioDevices = (AudioDeviceID*)(std::malloc(dataSize)); |
| 174 |
| 175 | status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &dataSize, audioDevices); |
| 176 | if (kAudioHardwareNoError != status) |
| 177 | { |
| 178 | LOGE("AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %i", status); |
| 179 | std::free(audioDevices); |
| 180 | audioDevices = nullptr; |
| 181 | return; |
| 182 | } |
| 183 |
| 184 | // Iterate through all the devices and determine which are input-capable |
| 185 | propertyAddress.mScope = kAudioDevicePropertyScopeOutput; |
| 186 | for (UInt32 i = 0; i < deviceCount; ++i) |
| 187 | { |
| 188 | // Query device UID |
| 189 | CFStringRef deviceUID = nullptr; |
| 190 | dataSize = sizeof(deviceUID); |
| 191 | propertyAddress.mSelector = kAudioDevicePropertyDeviceUID; |
| 192 | status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, nullptr, &dataSize, &deviceUID); |
| 193 | if (kAudioHardwareNoError != status) |
| 194 | { |
| 195 | LOGE("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID) failed: %i", status); |
| 196 | continue; |
| 197 | } |
| 198 |
| 199 | // Query device name |
| 200 | CFStringRef deviceName = nullptr; |
| 201 | dataSize = sizeof(deviceName); |
| 202 | propertyAddress.mSelector = kAudioDevicePropertyDeviceNameCFString; |
| 203 | status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, nullptr, &dataSize, &deviceName); |
| 204 | if (kAudioHardwareNoError != status) |
| 205 | { |
| 206 | LOGE("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceNameCFString) failed: %i", status); |
| 207 | continue; |
| 208 | } |
| 209 |
| 210 | // Determine if the device is an input device (it is an input device if it has input channels) |
| 211 | dataSize = 0; |
| 212 | propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration; |
| 213 | status = AudioObjectGetPropertyDataSize(audioDevices[i], &propertyAddress, 0, nullptr, &dataSize); |
| 214 | if (kAudioHardwareNoError != status) |
| 215 | { |
| 216 | LOGE("AudioObjectGetPropertyDataSize (kAudioDevicePropertyStreamConfiguration) failed: %i", status); |
| 217 | continue; |
| 218 | } |
| 219 |
| 220 | AudioBufferList* bufferList = (AudioBufferList*)(std::malloc(dataSize)); |
| 221 |
| 222 | status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, nullptr, &dataSize, bufferList); |
| 223 | if (kAudioHardwareNoError != status || 0 == bufferList->mNumberBuffers) |
| 224 | { |
| 225 | if (kAudioHardwareNoError != status) |
| 226 | LOGE("AudioObjectGetPropertyData (kAudioDevicePropertyStreamConfiguration) failed: %i", status); |
| 227 | std::free(bufferList); |
| 228 | bufferList = nullptr; |
| 229 | continue; |
| 230 | } |
| 231 |
| 232 | std::free(bufferList); |
| 233 | bufferList = nullptr; |
| 234 |
| 235 | AudioOutputDevice dev; |
| 236 | char buf[1024]; |
| 237 | CFStringGetCString(deviceName, buf, 1024, kCFStringEncodingUTF8); |
| 238 | dev.displayName = std::string(buf); |
| 239 | CFStringGetCString(deviceUID, buf, 1024, kCFStringEncodingUTF8); |
| 240 | dev.id = std::string(buf); |
| 241 | if (dev.id.rfind("VPAUAggregateAudioDevice-0x") == 0) |
| 242 | continue; |
| 243 | devs.push_back(dev); |
| 244 | } |
| 245 |
| 246 | std::free(audioDevices); |
| 247 | audioDevices = nullptr; |
| 248 | } |
| 249 |
| 250 | void AudioOutputAudioUnitLegacy::SetCurrentDevice(std::string deviceID) |
| 251 | { |
| 252 | UInt32 size = sizeof(AudioDeviceID); |
| 253 | AudioDeviceID outputDevice = 0; |
| 254 | OSStatus status; |
| 255 | AudioObjectPropertyAddress dataSourceProp = { |
| 256 | kAudioDevicePropertyDataSource, |
| 257 | kAudioDevicePropertyScopeOutput, |
| 258 | kAudioObjectPropertyElementMaster}; |
| 259 |
| 260 | if (isMacBookPro && sysDevID && AudioObjectHasProperty(sysDevID, &dataSourceProp)) |
| 261 | { |
| 262 | AudioObjectRemovePropertyListener(sysDevID, &dataSourceProp, AudioOutputAudioUnitLegacy::DefaultDeviceChangedCallback, this); |
| 263 | } |
| 264 |
| 265 | if (deviceID == "default") |
| 266 | { |
| 267 | AudioObjectPropertyAddress propertyAddress; |
| 268 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 269 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 270 | propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 271 | UInt32 propsize = sizeof(AudioDeviceID); |
| 272 | status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &propsize, &outputDevice); |
| 273 | CHECK_AU_ERROR(status, "Error getting default input device"); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | AudioObjectPropertyAddress propertyAddress = { |
| 278 | kAudioHardwarePropertyDevices, |
| 279 | kAudioObjectPropertyScopeGlobal, |
| 280 | kAudioObjectPropertyElementMaster}; |
| 281 | UInt32 dataSize = 0; |
| 282 | status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &dataSize); |
| 283 | CHECK_AU_ERROR(status, "Error getting devices size"); |
| 284 | UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID)); |
| 285 | AudioDeviceID audioDevices[deviceCount]; |
| 286 | status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &dataSize, audioDevices); |
| 287 | CHECK_AU_ERROR(status, "Error getting device list"); |
| 288 | for (UInt32 i = 0; i < deviceCount; ++i) |
| 289 | { |
| 290 | // Query device UID |
| 291 | CFStringRef deviceUID = nullptr; |
| 292 | dataSize = sizeof(deviceUID); |
| 293 | propertyAddress.mSelector = kAudioDevicePropertyDeviceUID; |
| 294 | status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, nullptr, &dataSize, &deviceUID); |
| 295 | CHECK_AU_ERROR(status, "Error getting device uid"); |
| 296 | char buf[1024]; |
| 297 | CFStringGetCString(deviceUID, buf, 1024, kCFStringEncodingUTF8); |
| 298 | if (deviceID == buf) |
| 299 | { |
| 300 | LOGV("Found device for id %s", buf); |
| 301 | outputDevice = audioDevices[i]; |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | if (!outputDevice) |
| 306 | { |
| 307 | LOGW("Requested device not found, using default"); |
| 308 | SetCurrentDevice("default"); |
| 309 | return; |
| 310 | } |
| 311 | } |
| 312 |
| 313 | status = AudioUnitSetProperty(unit, |
| 314 | kAudioOutputUnitProperty_CurrentDevice, |
| 315 | kAudioUnitScope_Global, |
| 316 | kOutputBus, |
| 317 | &outputDevice, |
| 318 | size); |
| 319 | CHECK_AU_ERROR(status, "Error setting output device"); |
| 320 |
| 321 | AudioStreamBasicDescription hardwareFormat; |
| 322 | size = sizeof(hardwareFormat); |
| 323 | status = AudioUnitGetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &hardwareFormat, &size); |
| 324 | CHECK_AU_ERROR(status, "Error getting hardware format"); |
| 325 | hardwareSampleRate = hardwareFormat.mSampleRate; |
| 326 |
| 327 | AudioStreamBasicDescription desiredFormat = { |
| 328 | .mSampleRate = 48000, .mFormatID = kAudioFormatLinearPCM, .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian, .mFramesPerPacket = 1, .mChannelsPerFrame = 1, .mBitsPerChannel = 16, .mBytesPerPacket = 2, .mBytesPerFrame = 2}; |
| 329 |
| 330 | status = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &desiredFormat, sizeof(desiredFormat)); |
| 331 | CHECK_AU_ERROR(status, "Error setting format"); |
| 332 |
| 333 | LOGD("Switched playback device, new sample rate %d", hardwareSampleRate); |
| 334 |
| 335 | this->m_currentDevice = deviceID; |
| 336 | sysDevID = outputDevice; |
| 337 |
| 338 | AudioObjectPropertyAddress propertyAddress = { |
| 339 | kAudioDevicePropertyBufferFrameSize, |
| 340 | kAudioObjectPropertyScopeGlobal, |
| 341 | kAudioObjectPropertyElementMaster}; |
| 342 | size = 4; |
| 343 | UInt32 bufferFrameSize; |
| 344 | status = AudioObjectGetPropertyData(outputDevice, &propertyAddress, 0, nullptr, &size, &bufferFrameSize); |
| 345 | if (status == noErr) |
| 346 | { |
| 347 | m_estimatedDelay = bufferFrameSize / 48; |
| 348 | LOGD("CoreAudio buffer size for output device is %u frames (%u ms)", bufferFrameSize, m_estimatedDelay); |
| 349 | } |
| 350 |
| 351 | if (isMacBookPro) |
| 352 | { |
| 353 | if (AudioObjectHasProperty(outputDevice, &dataSourceProp)) |
| 354 | { |
| 355 | UInt32 dataSource; |
| 356 | size = 4; |
| 357 | AudioObjectGetPropertyData(outputDevice, &dataSourceProp, 0, nullptr, &size, &dataSource); |
| 358 | SetPanRight(dataSource == 'ispk'); |
| 359 | AudioObjectAddPropertyListener(outputDevice, &dataSourceProp, AudioOutputAudioUnitLegacy::DefaultDeviceChangedCallback, this); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | SetPanRight(false); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 |
| 368 | void AudioOutputAudioUnitLegacy::SetPanRight(bool panRight) |
| 369 | { |
| 370 | LOGI("%sabling pan right on macbook pro", panRight ? "En" : "Dis"); |
| 371 | std::int32_t channelMap[] = {panRight ? -1 : 0, 0}; |
| 372 | OSStatus status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Global, kOutputBus, channelMap, sizeof(channelMap)); |
| 373 | CHECK_AU_ERROR(status, "Error setting channel map"); |
| 374 | } |
| 375 |
| 376 | OSStatus AudioOutputAudioUnitLegacy::DefaultDeviceChangedCallback(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress* inAddresses, void* inClientData) |
| 377 | { |
| 378 | AudioOutputAudioUnitLegacy* self = reinterpret_cast<AudioOutputAudioUnitLegacy*>(inClientData); |
| 379 | if (inAddresses[0].mSelector == kAudioHardwarePropertyDefaultOutputDevice) |
| 380 | { |
| 381 | LOGV("System default input device changed"); |
| 382 | if (self->m_currentDevice == "default") |
| 383 | { |
| 384 | self->SetCurrentDevice(self->m_currentDevice); |
| 385 | } |
| 386 | } |
| 387 | else if (inAddresses[0].mSelector == kAudioDevicePropertyDataSource) |
| 388 | { |
| 389 | UInt32 dataSource; |
| 390 | UInt32 size = 4; |
| 391 | AudioObjectGetPropertyData(inObjectID, inAddresses, 0, nullptr, &size, &dataSource); |
| 392 | self->SetPanRight(dataSource == 'ispk'); |
| 393 | } |
| 394 | return noErr; |
| 395 | } |
| 396 |