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