Fix mutex initialisation in OS X Audio Driver

This commit is contained in:
Marcelo Fernandez 2017-07-28 16:17:46 -03:00
parent ac9c4f44a9
commit a6129f0a21
2 changed files with 28 additions and 26 deletions

View File

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

View File

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