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