diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-12-13 21:11:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-13 21:11:02 +0100 |
commit | 016092181c8b575f342e4abaf40482e953815c11 (patch) | |
tree | 1aecd06d3a54d04a6eb0a189084212aa3b935d72 | |
parent | a98baad18f79c1c41dcc2091ecabc3cbaf33a82d (diff) | |
parent | 7da55fb739e4f2f2ad50611e78f09caafc7ede76 (diff) |
Merge pull request #22910 from marcelofg55/nothreads_rec
Fix AudioEffectRecord not working without thread support
-rw-r--r-- | servers/audio/effects/audio_effect_record.cpp | 63 | ||||
-rw-r--r-- | servers/audio/effects/audio_effect_record.h | 4 | ||||
-rw-r--r-- | servers/audio_server.cpp | 24 | ||||
-rw-r--r-- | servers/audio_server.h | 4 |
4 files changed, 78 insertions, 17 deletions
diff --git a/servers/audio/effects/audio_effect_record.cpp b/servers/audio/effects/audio_effect_record.cpp index a0094f66b8..5a583243ca 100644 --- a/servers/audio/effects/audio_effect_record.cpp +++ b/servers/audio/effects/audio_effect_record.cpp @@ -44,20 +44,25 @@ void AudioEffectRecordInstance::process(const AudioFrame *p_src_frames, AudioFra } } +void AudioEffectRecordInstance::_update_buffer() { + //Case: Frames are remaining in the buffer + while (ring_buffer_read_pos < ring_buffer_pos) { + //Read from the buffer into recording_data + _io_store_buffer(); + } +} + +void AudioEffectRecordInstance::_update(void *userdata) { + AudioEffectRecordInstance *ins = (AudioEffectRecordInstance *)userdata; + ins->_update_buffer(); +} + bool AudioEffectRecordInstance::process_silence() const { return true; } void AudioEffectRecordInstance::_io_thread_process() { - - //Reset recorder status thread_active = true; - ring_buffer_pos = 0; - ring_buffer_read_pos = 0; - - //We start a new recording - recording_data.resize(0); //Clear data completely and reset length - is_recording = true; while (is_recording) { //Check: The current recording has been requested to stop @@ -65,13 +70,9 @@ void AudioEffectRecordInstance::_io_thread_process() { is_recording = false; } - //Case: Frames are remaining in the buffer - if (ring_buffer_read_pos < ring_buffer_pos) { - //Read from the buffer into recording_data - _io_store_buffer(); - } - //Case: The buffer is empty - else if (is_recording) { + _update_buffer(); + + if (is_recording) { //Wait to avoid too much busy-wait OS::get_singleton()->delay_usec(500); } @@ -103,7 +104,35 @@ void AudioEffectRecordInstance::_thread_callback(void *_instance) { } void AudioEffectRecordInstance::init() { + //Reset recorder status + ring_buffer_pos = 0; + ring_buffer_read_pos = 0; + + //We start a new recording + recording_data.resize(0); //Clear data completely and reset length + is_recording = true; + +#ifdef NO_THREADS + AudioServer::get_singleton()->add_update_callback(&AudioEffectRecordInstance::_update, this); +#else io_thread = Thread::create(_thread_callback, this); +#endif +} + +void AudioEffectRecordInstance::finish() { + +#ifdef NO_THREADS + AudioServer::get_singleton()->remove_update_callback(&AudioEffectRecordInstance::_update, this); +#else + if (thread_active) { + Thread::wait_to_finish(io_thread); + } +#endif +} + +AudioEffectRecordInstance::~AudioEffectRecordInstance() { + + finish(); } Ref<AudioEffectInstance> AudioEffectRecord::instance() { @@ -145,8 +174,8 @@ Ref<AudioEffectInstance> AudioEffectRecord::instance() { void AudioEffectRecord::ensure_thread_stopped() { recording_active = false; - if (current_instance != 0 && current_instance->thread_active) { - Thread::wait_to_finish(current_instance->io_thread); + if (current_instance != 0) { + current_instance->finish(); } } diff --git a/servers/audio/effects/audio_effect_record.h b/servers/audio/effects/audio_effect_record.h index 4b8ee2bcdd..c5e4866f17 100644 --- a/servers/audio/effects/audio_effect_record.h +++ b/servers/audio/effects/audio_effect_record.h @@ -62,14 +62,18 @@ class AudioEffectRecordInstance : public AudioEffectInstance { void _io_store_buffer(); static void _thread_callback(void *_instance); void _init_recording(); + void _update_buffer(); + static void _update(void *userdata); public: void init(); + void finish(); virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count); virtual bool process_silence() const; AudioEffectRecordInstance() : thread_active(false) {} + ~AudioEffectRecordInstance(); }; class AudioEffectRecord : public AudioEffect { diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 530976f084..0cead5b2ba 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -1027,6 +1027,11 @@ void AudioServer::update() { AudioDriver::get_singleton()->reset_profiling_time(); prof_time = 0; #endif + + for (Set<CallbackItem>::Element *E = update_callbacks.front(); E; E = E->next()) { + + E->get().callback(E->get().userdata); + } } void AudioServer::load_default_bus_layout() { @@ -1152,6 +1157,25 @@ void AudioServer::remove_callback(AudioCallback p_callback, void *p_userdata) { unlock(); } +void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) { + lock(); + CallbackItem ci; + ci.callback = p_callback; + ci.userdata = p_userdata; + update_callbacks.insert(ci); + unlock(); +} + +void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) { + + lock(); + CallbackItem ci; + ci.callback = p_callback; + ci.userdata = p_userdata; + update_callbacks.erase(ci); + unlock(); +} + void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) { ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.size() == 0); diff --git a/servers/audio_server.h b/servers/audio_server.h index 642af9b252..c389e4010f 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -263,6 +263,7 @@ private: }; Set<CallbackItem> callbacks; + Set<CallbackItem> update_callbacks; friend class AudioDriver; void _driver_process(int p_frames, int32_t *p_buffer); @@ -361,6 +362,9 @@ public: void add_callback(AudioCallback p_callback, void *p_userdata); void remove_callback(AudioCallback p_callback, void *p_userdata); + void add_update_callback(AudioCallback p_callback, void *p_userdata); + void remove_update_callback(AudioCallback p_callback, void *p_userdata); + void set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout); Ref<AudioBusLayout> generate_bus_layout() const; |