| 6 | #include "AudioUnitIO.h" |
| 7 | #include "../../VoIPController.h" |
| 8 | #include "../../VoIPServerConfig.h" |
| 9 | #include "../../logging.h" |
| 10 | #include "AudioInputAudioUnit.h" |
| 11 | #include "AudioOutputAudioUnit.h" |
| 12 | #include <cstdio> |
| 13 |
| 14 | #define CHECK_AU_ERROR(res, msg) \ |
| 15 | if (res != noErr) \ |
| 16 | { \ |
| 17 | LOGE(msg ": OSStatus=%d", (int)res); \ |
| 18 | failed = true; \ |
| 19 | return; \ |
| 20 | } |
| 21 | #define BUFFER_SIZE 960 // 20 ms |
| 22 |
| 23 | #if TARGET_OS_OSX |
| 24 | #define INPUT_BUFFER_SIZE 20480 |
| 25 | #else |
| 26 | #define INPUT_BUFFER_SIZE 10240 |
| 27 | #endif |
| 28 |
| 29 | #define kOutputBus 0 |
| 30 | #define kInputBus 1 |
| 31 |
| 32 | #if TARGET_OS_OSX && !defined(TGVOIP_NO_OSX_PRIVATE_API) |
| 33 | extern "C" |
| 34 | { |
| 35 | OSStatus AudioDeviceDuck(AudioDeviceID inDevice, |
| 36 | Float32 inDuckedLevel, |
| 37 | const AudioTimeStamp* __nullable inStartTime, |
| 38 | Float32 inRampDuration) __attribute__((weak_import)); |
| 39 | } |
| 40 | #endif |
| 41 |
| 42 | using namespace tgvoip; |
| 43 | using namespace tgvoip::audio; |
| 44 |
| 45 | AudioUnitIO::AudioUnitIO(std::string inputDeviceID, std::string outputDeviceID) |
| 46 | { |
| 47 | input = nullptr; |
| 48 | output = nullptr; |
| 49 | inputEnabled = false; |
| 50 | outputEnabled = false; |
| 51 | m_failed = false; |
| 52 | started = false; |
| 53 | inBufferList.mBuffers[0].mData = std::malloc(INPUT_BUFFER_SIZE); |
| 54 | inBufferList.mBuffers[0].mDataByteSize = INPUT_BUFFER_SIZE; |
| 55 | inBufferList.mNumberBuffers = 1; |
| 56 |
| 57 | #if TARGET_OS_IPHONE |
| 58 | DarwinSpecific::ConfigureAudioSession(); |
| 59 | #endif |
| 60 |
| 61 | OSStatus status; |
| 62 | AudioComponentDescription desc; |
| 63 | AudioComponent inputComponent; |
| 64 | desc.componentType = kAudioUnitType_Output; |
| 65 | desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO; |
| 66 | desc.componentFlags = 0; |
| 67 | desc.componentFlagsMask = 0; |
| 68 | desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
| 69 | inputComponent = AudioComponentFindNext(nullptr, &desc); |
| 70 | status = AudioComponentInstanceNew(inputComponent, &unit); |
| 71 |
| 72 | UInt32 flag = 1; |
| 73 | #if TARGET_OS_IPHONE |
| 74 | status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag)); |
| 75 | CHECK_AU_ERROR(status, "Error enabling AudioUnit output"); |
| 76 | status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); |
| 77 | CHECK_AU_ERROR(status, "Error enabling AudioUnit input"); |
| 78 | #endif |
| 79 |
| 80 | #if TARGET_OS_IPHONE |
| 81 | flag = ServerConfig::GetSharedInstance()->GetBoolean("use_ios_vpio_agc", true) ? 1 : 0; |
| 82 | #else |
| 83 | flag = ServerConfig::GetSharedInstance()->GetBoolean("use_osx_vpio_agc", true) ? 1 : 0; |
| 84 | #endif |
| 85 | status = AudioUnitSetProperty(unit, kAUVoiceIOProperty_VoiceProcessingEnableAGC, kAudioUnitScope_Global, kInputBus, &flag, sizeof(flag)); |
| 86 | CHECK_AU_ERROR(status, "Error disabling AGC"); |
| 87 |
| 88 | AudioStreamBasicDescription audioFormat; |
| 89 | audioFormat.mSampleRate = 48000; |
| 90 | audioFormat.mFormatID = kAudioFormatLinearPCM; |
| 91 | #if TARGET_OS_IPHONE |
| 92 | audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian; |
| 93 | audioFormat.mBitsPerChannel = 16; |
| 94 | audioFormat.mBytesPerPacket = 2; |
| 95 | audioFormat.mBytesPerFrame = 2; |
| 96 | #else // OS X |
| 97 | audioFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian; |
| 98 | audioFormat.mBitsPerChannel = 32; |
| 99 | audioFormat.mBytesPerPacket = 4; |
| 100 | audioFormat.mBytesPerFrame = 4; |
| 101 | #endif |
| 102 | audioFormat.mFramesPerPacket = 1; |
| 103 | audioFormat.mChannelsPerFrame = 1; |
| 104 |
| 105 | status = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat)); |
| 106 | CHECK_AU_ERROR(status, "Error setting output format"); |
| 107 | status = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &audioFormat, sizeof(audioFormat)); |
| 108 | CHECK_AU_ERROR(status, "Error setting input format"); |
| 109 |
| 110 | AURenderCallbackStruct callbackStruct; |
| 111 |
| 112 | callbackStruct.inputProc = AudioUnitIO::BufferCallback; |
| 113 | callbackStruct.inputProcRefCon = this; |
| 114 | status = AudioUnitSetProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &callbackStruct, sizeof(callbackStruct)); |
| 115 | CHECK_AU_ERROR(status, "Error setting output buffer callback"); |
| 116 | status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, kInputBus, &callbackStruct, sizeof(callbackStruct)); |
| 117 | CHECK_AU_ERROR(status, "Error setting input buffer callback"); |
| 118 |
| 119 | #if TARGET_OS_OSX |
| 120 | CFRunLoopRef theRunLoop = nullptr; |
| 121 | AudioObjectPropertyAddress propertyAddress = {kAudioHardwarePropertyRunLoop, |
| 122 | kAudioObjectPropertyScopeGlobal, |
| 123 | kAudioObjectPropertyElementMaster}; |
| 124 | status = AudioObjectSetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, sizeof(CFRunLoopRef), &theRunLoop); |
| 125 |
| 126 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 127 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 128 | propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 129 | AudioObjectAddPropertyListener(kAudioObjectSystemObject, &propertyAddress, AudioUnitIO::DefaultDeviceChangedCallback, this); |
| 130 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice; |
| 131 | AudioObjectAddPropertyListener(kAudioObjectSystemObject, &propertyAddress, AudioUnitIO::DefaultDeviceChangedCallback, this); |
| 132 |
| 133 | #endif |
| 134 |
| 135 | input = new AudioInputAudioUnit(inputDeviceID, this); |
| 136 | output = new AudioOutputAudioUnit(outputDeviceID, this); |
| 137 | } |
| 138 |
| 139 | AudioUnitIO::~AudioUnitIO() |
| 140 | { |
| 141 | #if TARGET_OS_OSX |
| 142 | AudioObjectPropertyAddress propertyAddress; |
| 143 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 144 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 145 | propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 146 | AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &propertyAddress, AudioUnitIO::DefaultDeviceChangedCallback, this); |
| 147 | propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice; |
| 148 | AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &propertyAddress, AudioUnitIO::DefaultDeviceChangedCallback, this); |
| 149 | #endif |
| 150 | AudioOutputUnitStop(unit); |
| 151 | AudioUnitUninitialize(unit); |
| 152 | AudioComponentInstanceDispose(unit); |
| 153 | std::free(inBufferList.mBuffers[0].mData); |
| 154 | delete input; |
| 155 | delete output; |
| 156 | } |
| 157 |
| 158 | OSStatus AudioUnitIO::BufferCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData) |
| 159 | { |
| 160 | (reinterpret_cast<AudioUnitIO*>(inRefCon)->BufferCallback(ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData); |
| 161 | return noErr; |
| 162 | } |
| 163 |
| 164 | void AudioUnitIO::BufferCallback(AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 bus, UInt32 numFrames, AudioBufferList* ioData) |
| 165 | { |
| 166 | if (bus == kOutputBus) |
| 167 | { |
| 168 | if (output && outputEnabled) |
| 169 | { |
| 170 | output->HandleBufferCallback(ioData); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | std::memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize); |
| 175 | } |
| 176 | } |
| 177 | else if (bus == kInputBus) |
| 178 | { |
| 179 | inBufferList.mBuffers[0].mDataByteSize = INPUT_BUFFER_SIZE; |
| 180 | AudioUnitRender(unit, ioActionFlags, inTimeStamp, bus, numFrames, &inBufferList); |
| 181 | if (input && inputEnabled) |
| 182 | { |
| 183 | input->HandleBufferCallback(&inBufferList); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 |
| 188 | void AudioUnitIO::EnableInput(bool enabled) |
| 189 | { |
| 190 | inputEnabled = enabled; |
| 191 | StartIfNeeded(); |
| 192 | } |
| 193 |
| 194 | void AudioUnitIO::EnableOutput(bool enabled) |
| 195 | { |
| 196 | outputEnabled = enabled; |
| 197 | StartIfNeeded(); |
| 198 | #if TARGET_OS_OSX && !defined(TGVOIP_NO_OSX_PRIVATE_API) |
| 199 | if (actualDuckingEnabled != duckingEnabled) |
| 200 | { |
| 201 | actualDuckingEnabled = duckingEnabled; |
| 202 | AudioDeviceDuck(currentOutputDeviceID, duckingEnabled ? 0.177828f : 1.0f, nullptr, 0.1f); |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 |
| 207 | void AudioUnitIO::StartIfNeeded() |
| 208 | { |
| 209 | if (started) |
| 210 | return; |
| 211 | started = true; |
| 212 | OSStatus status = AudioUnitInitialize(unit); |
| 213 | CHECK_AU_ERROR(status, "Error initializing AudioUnit"); |
| 214 | status = AudioOutputUnitStart(unit); |
| 215 | CHECK_AU_ERROR(status, "Error starting AudioUnit"); |
| 216 | } |
| 217 |
| 218 | AudioInput* AudioUnitIO::GetInput() |
| 219 | { |
| 220 | return input; |
| 221 | } |
| 222 |
| 223 | AudioOutput* AudioUnitIO::GetOutput() |
| 224 | { |
| 225 | return output; |
| 226 | } |
| 227 |
| 228 | #if TARGET_OS_OSX |
| 229 | OSStatus AudioUnitIO::DefaultDeviceChangedCallback(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress* inAddresses, void* inClientData) |
| 230 | { |
| 231 | AudioUnitIO* self = (AudioUnitIO*)inClientData; |
| 232 | if (inAddresses[0].mSelector == kAudioHardwarePropertyDefaultOutputDevice) |
| 233 | { |
| 234 | LOGV("System default output device changed"); |
| 235 | if (self->currentOutputDevice == "default") |
| 236 | { |
| 237 | self->SetCurrentDevice(false, self->currentOutputDevice); |
| 238 | } |
| 239 | } |
| 240 | else if (inAddresses[0].mSelector == kAudioHardwarePropertyDefaultInputDevice) |
| 241 | { |
| 242 | LOGV("System default input device changed"); |
| 243 | if (self->currentInputDevice == "default") |
| 244 | { |
| 245 | self->SetCurrentDevice(true, self->currentInputDevice); |
| 246 | } |
| 247 | } |
| 248 | return noErr; |
| 249 | } |
| 250 |
| 251 | void AudioUnitIO::SetCurrentDevice(bool input, std::string deviceID) |
| 252 | { |
| 253 | LOGV("Setting current %sput device: %s", input ? "in" : "out", deviceID.c_str()); |
| 254 | if (started) |
| 255 | { |
| 256 | AudioOutputUnitStop(unit); |
| 257 | AudioUnitUninitialize(unit); |
| 258 | } |
| 259 | UInt32 size = sizeof(AudioDeviceID); |
| 260 | AudioDeviceID device = 0; |
| 261 | OSStatus status; |
| 262 |
| 263 | if (deviceID == "default") |
| 264 | { |
| 265 | AudioObjectPropertyAddress propertyAddress; |
| 266 | propertyAddress.mSelector = input ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice; |
| 267 | propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 268 | propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 269 | UInt32 propsize = sizeof(AudioDeviceID); |
| 270 | status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &propsize, &device); |
| 271 | CHECK_AU_ERROR(status, "Error getting default device"); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | AudioObjectPropertyAddress propertyAddress = { |
| 276 | kAudioHardwarePropertyDevices, |
| 277 | kAudioObjectPropertyScopeGlobal, |
| 278 | kAudioObjectPropertyElementMaster}; |
| 279 | UInt32 dataSize = 0; |
| 280 | status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &dataSize); |
| 281 | CHECK_AU_ERROR(status, "Error getting devices size"); |
| 282 | UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID)); |
| 283 | AudioDeviceID audioDevices[deviceCount]; |
| 284 | status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &dataSize, audioDevices); |
| 285 | CHECK_AU_ERROR(status, "Error getting device list"); |
| 286 | for (UInt32 i = 0; i < deviceCount; ++i) |
| 287 | { |
| 288 | // Query device UID |
| 289 | CFStringRef deviceUID = nullptr; |
| 290 | dataSize = sizeof(deviceUID); |
| 291 | propertyAddress.mSelector = kAudioDevicePropertyDeviceUID; |
| 292 | status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, nullptr, &dataSize, &deviceUID); |
| 293 | CHECK_AU_ERROR(status, "Error getting device uid"); |
| 294 | char buf[1024]; |
| 295 | CFStringGetCString(deviceUID, buf, 1024, kCFStringEncodingUTF8); |
| 296 | if (deviceID == buf) |
| 297 | { |
| 298 | LOGV("Found device for id %s", buf); |
| 299 | device = audioDevices[i]; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (!device) |
| 304 | { |
| 305 | LOGW("Requested device not found, using default"); |
| 306 | SetCurrentDevice(input, "default"); |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 |
| 311 | status = AudioUnitSetProperty(unit, |
| 312 | kAudioOutputUnitProperty_CurrentDevice, |
| 313 | kAudioUnitScope_Global, |
| 314 | input ? kInputBus : kOutputBus, |
| 315 | &device, |
| 316 | size); |
| 317 | CHECK_AU_ERROR(status, "Error setting input device"); |
| 318 |
| 319 | if (input) |
| 320 | currentInputDevice = deviceID; |
| 321 | else |
| 322 | currentOutputDevice = deviceID; |
| 323 |
| 324 | /*AudioObjectPropertyAddress propertyAddress = { |
| 325 | kAudioDevicePropertyBufferFrameSize, |
| 326 | kAudioObjectPropertyScopeGlobal, |
| 327 | kAudioObjectPropertyElementMaster |
| 328 | }; |
| 329 | size=4; |
| 330 | UInt32 bufferFrameSize; |
| 331 | status=AudioObjectGetPropertyData(device, &propertyAddress, 0, nullptr, &size, &bufferFrameSize); |
| 332 | if(status==noErr){ |
| 333 | estimatedDelay=bufferFrameSize/48; |
| 334 | LOGD("CoreAudio buffer size for device is %u frames (%u ms)", bufferFrameSize, estimatedDelay); |
| 335 | }*/ |
| 336 | if (started) |
| 337 | { |
| 338 | started = false; |
| 339 | StartIfNeeded(); |
| 340 | } |
| 341 | if (!input) |
| 342 | { |
| 343 | currentOutputDeviceID = device; |
| 344 | } |
| 345 | LOGV("Set current %sput device done", input ? "in" : "out"); |
| 346 | } |
| 347 |
| 348 | void AudioUnitIO::SetDuckingEnabled(bool enabled) |
| 349 | { |
| 350 | duckingEnabled = enabled; |
| 351 | #ifndef TGVOIP_NO_OSX_PRIVATE_API |
| 352 | if (outputEnabled && duckingEnabled != actualDuckingEnabled) |
| 353 | { |
| 354 | actualDuckingEnabled = enabled; |
| 355 | AudioDeviceDuck(currentOutputDeviceID, enabled ? 0.177828f : 1.0f, nullptr, 0.1f); |
| 356 | } |
| 357 | #endif |
| 358 | } |
| 359 |
| 360 | #endif |
| 361 |