From af9bb0ea15dfd3dfe8950fcfcce364485dadd92a Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sat, 27 Apr 2019 12:22:47 -0300 Subject: [PATCH] Clean up latency related functions --- modules/theora/video_stream_theora.cpp | 2 +- modules/webm/video_stream_webm.cpp | 4 +-- platform/android/export/export.cpp | 4 +-- .../audio_effect_spectrum_analyzer.cpp | 2 +- servers/audio_server.cpp | 31 +++++++++---------- servers/audio_server.h | 13 +++++--- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/modules/theora/video_stream_theora.cpp b/modules/theora/video_stream_theora.cpp index 14c5ddd7f2f..8db0799b47c 100644 --- a/modules/theora/video_stream_theora.cpp +++ b/modules/theora/video_stream_theora.cpp @@ -365,7 +365,7 @@ void VideoStreamPlaybackTheora::set_file(const String &p_file) { float VideoStreamPlaybackTheora::get_time() const { - return time - AudioServer::get_singleton()->get_output_delay() - delay_compensation; //-((get_total())/(float)vi.rate); + return time - AudioServer::get_singleton()->get_output_latency() - delay_compensation; //-((get_total())/(float)vi.rate); }; Ref VideoStreamPlaybackTheora::get_texture() { diff --git a/modules/webm/video_stream_webm.cpp b/modules/webm/video_stream_webm.cpp index 06f9e39dc79..f2497eeec1f 100644 --- a/modules/webm/video_stream_webm.cpp +++ b/modules/webm/video_stream_webm.cpp @@ -375,7 +375,7 @@ int VideoStreamPlaybackWebm::get_mix_rate() const { inline bool VideoStreamPlaybackWebm::has_enough_video_frames() const { if (video_frames_pos > 0) { - const double audio_delay = AudioServer::get_singleton()->get_output_delay(); + const double audio_delay = AudioServer::get_singleton()->get_output_latency(); const double video_time = video_frames[video_frames_pos - 1]->time; return video_time >= time + audio_delay + delay_compensation; } @@ -383,7 +383,7 @@ inline bool VideoStreamPlaybackWebm::has_enough_video_frames() const { } bool VideoStreamPlaybackWebm::should_process(WebMFrame &video_frame) { - const double audio_delay = AudioServer::get_singleton()->get_output_delay(); + const double audio_delay = AudioServer::get_singleton()->get_output_latency(); return video_frame.time >= time + audio_delay + delay_compensation; } diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index f70cee2964c..2a3d0843a87 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -1735,8 +1735,8 @@ public: new_file += l + "\n"; } else { String base = l.substr(0, last_tag_pos + last_tag.length()); - if (manifest_sections.has("application_tags")) { - for (List::Element *E = manifest_sections["application_tags"].front(); E; E = E->next()) { + if (manifest_sections.has("application_attribs")) { + for (List::Element *E = manifest_sections["application_attribs"].front(); E; E = E->next()) { String to_add = E->get().strip_edges(); base += " " + to_add + " "; } diff --git a/servers/audio/effects/audio_effect_spectrum_analyzer.cpp b/servers/audio/effects/audio_effect_spectrum_analyzer.cpp index 01a52aa01f3..46d92336f3b 100644 --- a/servers/audio/effects/audio_effect_spectrum_analyzer.cpp +++ b/servers/audio/effects/audio_effect_spectrum_analyzer.cpp @@ -130,7 +130,7 @@ Vector2 AudioEffectSpectrumAnalyzerInstance::get_magnitude_for_frequency_range(f } uint64_t time = OS::get_singleton()->get_ticks_usec(); float diff = double(time - last_fft_time) / 1000000.0 + base->get_tap_back_pos(); - diff -= AudioServer::get_singleton()->get_output_delay(); + diff -= AudioServer::get_singleton()->get_output_latency(); float fft_time_size = float(fft_size) / mix_rate; int fft_index = fft_pos; diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index c83c3029f3d..604d500fb37 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -68,16 +68,16 @@ void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_u void AudioDriver::update_mix_time(int p_frames) { - _mix_amount += p_frames; + _last_mix_frames = p_frames; if (OS::get_singleton()) _last_mix_time = OS::get_singleton()->get_ticks_usec(); } -double AudioDriver::get_mix_time() const { +double AudioDriver::get_time_to_next_mix() const { double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0; - total += _mix_amount / (double)get_mix_rate(); - return total; + double mix_buffer = _last_mix_frames / (double)get_mix_rate(); + return mix_buffer - total; } void AudioDriver::input_buffer_init(int driver_buffer_frames) { @@ -148,7 +148,7 @@ Array AudioDriver::capture_get_device_list() { AudioDriver::AudioDriver() { _last_mix_time = 0; - _mix_amount = 0; + _last_mix_frames = 0; input_position = 0; input_size = 0; @@ -281,13 +281,6 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) { to_mix -= to_copy; } - // Calculate latency for Performance.AUDIO_OUTPUT_LATENCY - if (OS::get_singleton()) { - uint64_t ticks = OS::get_singleton()->get_ticks_usec(); - output_latency = (ticks - output_latency_ticks) / 1000000.f; - output_latency_ticks = ticks; - } - #ifdef DEBUG_ENABLED prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks; #endif @@ -1107,13 +1100,14 @@ AudioServer *AudioServer::get_singleton() { return singleton; } -double AudioServer::get_mix_time() const { +double AudioServer::get_output_latency() const { - return 0; + return AudioDriver::get_singleton()->get_latency(); } -double AudioServer::get_output_delay() const { - return 0; +double AudioServer::get_time_to_next_mix() const { + + return AudioDriver::get_singleton()->get_time_to_next_mix(); } AudioServer *AudioServer::singleton = NULL; @@ -1357,6 +1351,9 @@ void AudioServer::_bind_methods() { ClassDB::bind_method(D_METHOD("get_device"), &AudioServer::get_device); ClassDB::bind_method(D_METHOD("set_device", "device"), &AudioServer::set_device); + ClassDB::bind_method(D_METHOD("get_time_to_next_mix"), &AudioServer::get_time_to_next_mix); + ClassDB::bind_method(D_METHOD("get_output_latency"), &AudioServer::get_output_latency); + ClassDB::bind_method(D_METHOD("capture_get_device_list"), &AudioServer::capture_get_device_list); ClassDB::bind_method(D_METHOD("capture_get_device"), &AudioServer::capture_get_device); ClassDB::bind_method(D_METHOD("capture_set_device", "name"), &AudioServer::capture_set_device); @@ -1386,6 +1383,8 @@ AudioServer::AudioServer() { #ifdef DEBUG_ENABLED prof_time = 0; #endif + mix_time = 0; + mix_size = 0; } AudioServer::~AudioServer() { diff --git a/servers/audio_server.h b/servers/audio_server.h index 3b69cb1b88a..59ad4c1dc87 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -45,7 +45,7 @@ class AudioDriver { static AudioDriver *singleton; uint64_t _last_mix_time; - uint64_t _mix_amount; + uint64_t _last_mix_frames; #ifdef DEBUG_ENABLED uint64_t prof_ticks; @@ -71,7 +71,8 @@ protected: #endif public: - double get_mix_time() const; //useful for video -> audio sync + double get_time_since_last_mix() const; //useful for video -> audio sync + double get_time_to_next_mix() const; enum SpeakerMode { SPEAKER_MODE_STEREO, @@ -163,6 +164,9 @@ public: typedef void (*AudioCallback)(void *p_userdata); private: + uint64_t mix_time; + int mix_size; + uint32_t buffer_size; uint64_t mix_count; uint64_t mix_frames; @@ -351,8 +355,8 @@ public: static AudioServer *get_singleton(); - virtual double get_mix_time() const; //useful for video -> audio sync - virtual double get_output_delay() const; + virtual double get_output_latency() const; + virtual double get_time_to_next_mix() const; void *audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data = NULL); void audio_data_free(void *p_data); @@ -377,7 +381,6 @@ public: String capture_get_device(); void capture_set_device(const String &p_name); - float get_output_latency() { return output_latency; } AudioServer(); virtual ~AudioServer(); };