Fix mutex initialisation in OS X Audio Driver

This commit is contained in:
Marcelo Fernandez 2017-07-30 16:35:08 -03:00
parent 93c226f7a3
commit 76821bbb47
2 changed files with 28 additions and 26 deletions

View File

@ -103,6 +103,7 @@ Error AudioDriverOSX::finishDevice() {
Error AudioDriverOSX::init() {
OSStatus result;
mutex = Mutex::create();
active = false;
channels = 2;
@ -159,43 +160,30 @@ OSStatus AudioDriverOSX::output_callback(void *inRefCon,
UInt32 inBusNumber, UInt32 inNumberFrames,
AudioBufferList *ioData) {
AudioBuffer *abuf;
AudioDriverOSX *ad = (AudioDriverOSX *)inRefCon;
bool mix = true;
if (!ad->active)
mix = false;
else if (ad->mutex) {
mix = ad->mutex->try_lock() == OK;
};
if (!mix) {
if (!ad->active || !ad->try_lock()) {
for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
abuf = &ioData->mBuffers[i];
AudioBuffer *abuf = &ioData->mBuffers[i];
zeromem(abuf->mData, abuf->mDataByteSize);
};
return 0;
};
int frames_left;
for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
abuf = &ioData->mBuffers[i];
frames_left = inNumberFrames;
AudioBuffer *abuf = &ioData->mBuffers[i];
int frames_left = inNumberFrames;
int16_t *out = (int16_t *)abuf->mData;
while (frames_left) {
int frames = MIN(frames_left, ad->buffer_frames);
//ad->lock();
ad->audio_server_process(frames, ad->samples_in);
//ad->unlock();
for (int i = 0; i < frames * ad->channels; i++) {
for (int j = 0; j < frames * ad->channels; j++) {
out[i] = ad->samples_in[i] >> 16;
out[j] = ad->samples_in[j] >> 16;
}
frames_left -= frames;
@ -203,8 +191,7 @@ OSStatus AudioDriverOSX::output_callback(void *inRefCon,
};
};
if (ad->mutex)
ad->mutex->unlock();
ad->unlock();
return 0;
};
@ -232,11 +219,18 @@ void AudioDriverOSX::lock() {
if (mutex)
mutex->lock();
};
void AudioDriverOSX::unlock() {
if (mutex)
mutex->unlock();
};
bool AudioDriverOSX::try_lock() {
if (mutex)
return mutex->try_lock() == OK;
return true;
}
void AudioDriverOSX::finish() {
OSStatus result;
@ -247,16 +241,23 @@ void AudioDriverOSX::finish() {
ERR_PRINT("AudioObjectRemovePropertyListener failed");
}
if (mutex) {
memdelete(mutex);
mutex = NULL;
}
if (samples_in) {
memdelete_arr(samples_in);
samples_in = NULL;
}
};
AudioDriverOSX::AudioDriverOSX() {
mutex = Mutex::create(); //NULL;
mutex = NULL;
samples_in = NULL;
};
AudioDriverOSX::~AudioDriverOSX() {
};
#endif

View File

@ -70,6 +70,7 @@ public:
virtual void unlock();
virtual void finish();
bool try_lock();
Error reopen();
AudioDriverOSX();