Skip to content

Commit

Permalink
Add input lock to AudioDriver.
Browse files Browse the repository at this point in the history
  • Loading branch information
kus04e4ek committed Jun 10, 2024
1 parent 0734ff5 commit 3101b48
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
24 changes: 14 additions & 10 deletions platform/android/audio_driver_opensl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void AudioDriverOpenSL::_buffer_callback(
if (pause) {
mix = false;
} else {
mix = mutex.try_lock();
mix = output_mutex.try_lock();
}

if (mix) {
Expand All @@ -57,7 +57,7 @@ void AudioDriverOpenSL::_buffer_callback(
}

if (mix) {
mutex.unlock();
output_mutex.unlock();
}

const int32_t *src_buff = mixdown_buffer;
Expand Down Expand Up @@ -184,13 +184,13 @@ void AudioDriverOpenSL::start() {
}

void AudioDriverOpenSL::_record_buffer_callback(SLAndroidSimpleBufferQueueItf queueItf) {
lock();
input_lock();
for (int i = 0; i < rec_buffer.size(); i++) {
int32_t sample = rec_buffer[i] << 16;
input_buffer_write(sample);
input_buffer_write(sample); // call twice to convert to Stereo
}
unlock();
input_unlock();

SLresult res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
Expand Down Expand Up @@ -294,6 +294,14 @@ Error AudioDriverOpenSL::input_stop() {
return OK;
}

void AudioDriverOpenSL::input_lock() {
input_mutex.lock();
}

void AudioDriverOpenSL::input_unlock() {
input_mutex.unlock();
}

int AudioDriverOpenSL::get_mix_rate() const {
return 44100; // hardcoded for Android, as selected by SL_SAMPLINGRATE_44_1
}
Expand All @@ -303,15 +311,11 @@ AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const {
}

void AudioDriverOpenSL::lock() {
if (active) {
mutex.lock();
}
output_mutex.lock();
}

void AudioDriverOpenSL::unlock() {
if (active) {
mutex.unlock();
}
output_mutex.unlock();
}

void AudioDriverOpenSL::finish() {
Expand Down
6 changes: 5 additions & 1 deletion platform/android/audio_driver_opensl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@

class AudioDriverOpenSL : public AudioDriver {
bool active = false;
Mutex mutex;
Mutex output_mutex;
Mutex input_mutex;

enum {
BUFFER_COUNT = 2
Expand Down Expand Up @@ -103,6 +104,9 @@ class AudioDriverOpenSL : public AudioDriver {
virtual Error input_start() override;
virtual Error input_stop() override;

virtual void input_lock() override;
virtual void input_unlock() override;

void set_pause(bool p_pause);

AudioDriverOpenSL();
Expand Down
5 changes: 2 additions & 3 deletions servers/audio/audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ AudioStreamMicrophone::AudioStreamMicrophone() {
}

int AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
AudioDriver::get_singleton()->lock();
AudioDriver::get_singleton()->input_lock();

Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
Expand Down Expand Up @@ -369,8 +369,7 @@ int AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fra
}
#endif

AudioDriver::get_singleton()->unlock();

AudioDriver::get_singleton()->input_unlock();
return mixed_frames;
}

Expand Down
3 changes: 3 additions & 0 deletions servers/audio_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class AudioDriver {
virtual Error input_start() { return FAILED; }
virtual Error input_stop() { return FAILED; }

virtual void input_lock() { lock(); }
virtual void input_unlock() { unlock(); }

virtual PackedStringArray get_input_device_list();
virtual String get_input_device() { return "Default"; }
virtual void set_input_device(const String &p_name) {}
Expand Down

0 comments on commit 3101b48

Please sign in to comment.