2017-02-02 17:24:40 +01:00
|
|
|
//
|
|
|
|
// libtgvoip is free and unencumbered public domain software.
|
|
|
|
// For more information, see http://unlicense.org or the UNLICENSE file
|
|
|
|
// you should have received with this source code distribution.
|
|
|
|
//
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <AudioToolbox/AudioToolbox.h>
|
|
|
|
#include "AudioUnitIO.h"
|
|
|
|
#include "AudioInputAudioUnit.h"
|
|
|
|
#include "AudioOutputAudioUnit.h"
|
|
|
|
#include "../../logging.h"
|
2017-03-30 16:06:59 +02:00
|
|
|
#include "../../VoIPController.h"
|
2017-07-19 23:00:13 +02:00
|
|
|
#include "../../VoIPServerConfig.h"
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
#define CHECK_AU_ERROR(res, msg) if(res!=noErr){ LOGE(msg": OSStatus=%d", (int)res); return; }
|
|
|
|
#define BUFFER_SIZE 960 // 20 ms
|
|
|
|
|
|
|
|
#define kOutputBus 0
|
|
|
|
#define kInputBus 1
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
using namespace tgvoip;
|
2017-04-28 13:17:56 +02:00
|
|
|
using namespace tgvoip::audio;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
int AudioUnitIO::refCount=0;
|
|
|
|
AudioUnitIO* AudioUnitIO::sharedInstance=NULL;
|
|
|
|
bool AudioUnitIO::haveAudioSession=false;
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
AudioUnitIO::AudioUnitIO(){
|
2017-03-30 16:06:59 +02:00
|
|
|
input=NULL;
|
|
|
|
output=NULL;
|
|
|
|
configured=false;
|
|
|
|
inputEnabled=false;
|
|
|
|
outputEnabled=false;
|
|
|
|
inBufferList.mBuffers[0].mData=malloc(10240);
|
|
|
|
inBufferList.mBuffers[0].mDataByteSize=10240;
|
|
|
|
inBufferList.mNumberBuffers=1;
|
2017-07-03 03:42:49 +02:00
|
|
|
#ifdef TGVOIP_USE_AUDIO_SESSION
|
2017-03-30 16:06:59 +02:00
|
|
|
if(haveAudioSession)
|
|
|
|
ProcessAudioSessionAcquired();
|
2017-07-03 03:42:49 +02:00
|
|
|
#else
|
|
|
|
haveAudioSession=true;
|
|
|
|
ProcessAudioSessionAcquired();
|
|
|
|
#endif
|
2017-03-30 16:06:59 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
AudioUnitIO::~AudioUnitIO(){
|
2017-03-30 16:06:59 +02:00
|
|
|
if(runFakeIO){
|
|
|
|
runFakeIO=false;
|
|
|
|
join_thread(fakeIOThread);
|
|
|
|
}
|
|
|
|
AudioOutputUnitStop(unit);
|
|
|
|
AudioUnitUninitialize(unit);
|
|
|
|
AudioComponentInstanceDispose(unit);
|
|
|
|
free(inBufferList.mBuffers[0].mData);
|
|
|
|
haveAudioSession=false;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::ProcessAudioSessionAcquired(){
|
2017-02-02 17:24:40 +01:00
|
|
|
OSStatus status;
|
|
|
|
AudioComponentDescription desc;
|
|
|
|
AudioComponent inputComponent;
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
//desc.componentSubType = kAudioUnitSubType_RemoteIO;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
|
|
|
|
#else
|
|
|
|
desc.componentSubType = kAudioUnitSubType_HALOutput;
|
|
|
|
#endif
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
inputComponent = AudioComponentFindNext(NULL, &desc);
|
|
|
|
status = AudioComponentInstanceNew(inputComponent, &unit);
|
|
|
|
|
2017-03-30 16:06:59 +02:00
|
|
|
if(configured)
|
|
|
|
ActuallyConfigure(cfgSampleRate, cfgBitsPerSample, cfgChannels);
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
AudioUnitIO* AudioUnitIO::Get(){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(refCount==0){
|
2017-04-28 13:17:56 +02:00
|
|
|
sharedInstance=new AudioUnitIO();
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
refCount++;
|
|
|
|
assert(refCount>0);
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::Release(){
|
2017-02-02 17:24:40 +01:00
|
|
|
refCount--;
|
|
|
|
assert(refCount>=0);
|
|
|
|
if(refCount==0){
|
|
|
|
delete sharedInstance;
|
|
|
|
sharedInstance=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::AudioSessionAcquired(){
|
2017-03-30 16:06:59 +02:00
|
|
|
haveAudioSession=true;
|
|
|
|
if(sharedInstance)
|
|
|
|
sharedInstance->ProcessAudioSessionAcquired();
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(configured)
|
|
|
|
return;
|
2017-03-30 16:06:59 +02:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
#ifdef TGVOIP_USE_AUDIO_SESSION
|
2017-03-30 16:06:59 +02:00
|
|
|
runFakeIO=true;
|
2017-04-28 13:17:56 +02:00
|
|
|
start_thread(fakeIOThread, AudioUnitIO::StartFakeIOThread, this);
|
2017-03-30 16:06:59 +02:00
|
|
|
set_thread_priority(fakeIOThread, get_thread_max_priority());
|
2017-07-03 03:42:49 +02:00
|
|
|
#endif
|
2017-03-30 16:06:59 +02:00
|
|
|
|
|
|
|
if(haveAudioSession){
|
|
|
|
ActuallyConfigure(sampleRate, bitsPerSample, channels);
|
|
|
|
}else{
|
|
|
|
cfgSampleRate=sampleRate;
|
|
|
|
cfgBitsPerSample=bitsPerSample;
|
|
|
|
cfgChannels=channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
configured=true;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::ActuallyConfigure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){
|
2017-02-02 17:24:40 +01:00
|
|
|
UInt32 flag=1;
|
|
|
|
OSStatus status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag));
|
|
|
|
CHECK_AU_ERROR(status, "Error enabling AudioUnit output");
|
|
|
|
status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag));
|
|
|
|
CHECK_AU_ERROR(status, "Error enabling AudioUnit input");
|
|
|
|
|
2017-07-19 23:00:13 +02:00
|
|
|
flag=ServerConfig::GetSharedInstance()->GetBoolean("use_ios_vpio_agc", true) ? 1 : 0;
|
2017-02-02 17:24:40 +01:00
|
|
|
status=AudioUnitSetProperty(unit, kAUVoiceIOProperty_VoiceProcessingEnableAGC, kAudioUnitScope_Global, kInputBus, &flag, sizeof(flag));
|
|
|
|
CHECK_AU_ERROR(status, "Error disabling AGC");
|
|
|
|
|
|
|
|
AudioStreamBasicDescription audioFormat;
|
|
|
|
audioFormat.mSampleRate = sampleRate;
|
|
|
|
audioFormat.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
|
|
|
|
audioFormat.mFramesPerPacket = 1;
|
|
|
|
audioFormat.mChannelsPerFrame = channels;
|
|
|
|
audioFormat.mBitsPerChannel = bitsPerSample*channels;
|
|
|
|
audioFormat.mBytesPerPacket = bitsPerSample/8*channels;
|
|
|
|
audioFormat.mBytesPerFrame = bitsPerSample/8*channels;
|
|
|
|
|
|
|
|
status = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));
|
|
|
|
CHECK_AU_ERROR(status, "Error setting output format");
|
|
|
|
status = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &audioFormat, sizeof(audioFormat));
|
|
|
|
CHECK_AU_ERROR(status, "Error setting input format");
|
|
|
|
|
|
|
|
AURenderCallbackStruct callbackStruct;
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
callbackStruct.inputProc = AudioUnitIO::BufferCallback;
|
2017-02-02 17:24:40 +01:00
|
|
|
callbackStruct.inputProcRefCon = this;
|
|
|
|
status = AudioUnitSetProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &callbackStruct, sizeof(callbackStruct));
|
|
|
|
CHECK_AU_ERROR(status, "Error setting output buffer callback");
|
|
|
|
status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, kInputBus, &callbackStruct, sizeof(callbackStruct));
|
|
|
|
CHECK_AU_ERROR(status, "Error setting input buffer callback");
|
|
|
|
|
|
|
|
status = AudioUnitInitialize(unit);
|
|
|
|
CHECK_AU_ERROR(status, "Error initializing AudioUnit");
|
|
|
|
status=AudioOutputUnitStart(unit);
|
|
|
|
CHECK_AU_ERROR(status, "Error starting AudioUnit");
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
OSStatus AudioUnitIO::BufferCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData){
|
|
|
|
((AudioUnitIO*)inRefCon)->BufferCallback(ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
|
2017-02-02 17:24:40 +01:00
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::BufferCallback(AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 bus, UInt32 numFrames, AudioBufferList *ioData){
|
2017-03-30 16:06:59 +02:00
|
|
|
runFakeIO=false;
|
2017-02-02 17:24:40 +01:00
|
|
|
if(bus==kOutputBus){
|
|
|
|
if(output && outputEnabled){
|
|
|
|
output->HandleBufferCallback(ioData);
|
|
|
|
}else{
|
|
|
|
memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize);
|
|
|
|
}
|
|
|
|
}else if(bus==kInputBus){
|
|
|
|
if(input && inputEnabled){
|
|
|
|
inBufferList.mBuffers[0].mDataByteSize=10240;
|
|
|
|
AudioUnitRender(unit, ioActionFlags, inTimeStamp, bus, numFrames, &inBufferList);
|
|
|
|
input->HandleBufferCallback(&inBufferList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::AttachInput(AudioInputAudioUnit *i){
|
2017-02-02 17:24:40 +01:00
|
|
|
assert(input==NULL);
|
|
|
|
input=i;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::AttachOutput(AudioOutputAudioUnit *o){
|
2017-02-02 17:24:40 +01:00
|
|
|
assert(output==NULL);
|
|
|
|
output=o;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::DetachInput(){
|
2017-02-02 17:24:40 +01:00
|
|
|
assert(input!=NULL);
|
|
|
|
input=NULL;
|
|
|
|
inputEnabled=false;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::DetachOutput(){
|
2017-02-02 17:24:40 +01:00
|
|
|
assert(output!=NULL);
|
|
|
|
output=NULL;
|
|
|
|
outputEnabled=false;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::EnableInput(bool enabled){
|
2017-02-02 17:24:40 +01:00
|
|
|
inputEnabled=enabled;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::EnableOutput(bool enabled){
|
2017-02-02 17:24:40 +01:00
|
|
|
outputEnabled=enabled;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void* AudioUnitIO::StartFakeIOThread(void *arg){
|
|
|
|
((AudioUnitIO*)arg)->RunFakeIOThread();
|
2017-03-30 16:06:59 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void AudioUnitIO::RunFakeIOThread(){
|
2017-03-30 16:06:59 +02:00
|
|
|
double neededDataDuration=0;
|
2017-04-17 20:57:07 +02:00
|
|
|
double prevTime=VoIPController::GetCurrentTime();
|
2017-03-30 16:06:59 +02:00
|
|
|
while(runFakeIO){
|
2017-04-17 20:57:07 +02:00
|
|
|
double t=VoIPController::GetCurrentTime();
|
2017-03-30 16:06:59 +02:00
|
|
|
neededDataDuration+=t-prevTime;
|
|
|
|
prevTime=t;
|
|
|
|
while(neededDataDuration>=0.020){
|
|
|
|
if(output && outputEnabled){
|
|
|
|
inBufferList.mBuffers[0].mDataByteSize=960*2;
|
|
|
|
output->HandleBufferCallback(&inBufferList);
|
|
|
|
}
|
|
|
|
if((output && outputEnabled) || (input && inputEnabled)){
|
|
|
|
memset(inBufferList.mBuffers[0].mData, 0, 960*2);
|
|
|
|
}
|
|
|
|
if(input && inputEnabled){
|
|
|
|
inBufferList.mBuffers[0].mDataByteSize=960*2;
|
|
|
|
input->HandleBufferCallback(&inBufferList);
|
|
|
|
}
|
|
|
|
neededDataDuration-=0.020;
|
|
|
|
}
|
|
|
|
usleep(5000);
|
|
|
|
}
|
|
|
|
LOGD("FakeIO thread exiting");
|
|
|
|
}
|
|
|
|
|