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