Skip to content

Commit

Permalink
PulseAudio: Remove get_latency() caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Birdulon committed Aug 5, 2023
1 parent b776cf5 commit d90626f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/classes/AudioServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<method name="get_output_latency" qualifiers="const">
<return type="float" />
<description>
Returns the audio driver's output latency.
Returns the audio driver's output latency. This can be expensive, it is not recommended to call this every frame.
</description>
</method>
<method name="get_speaker_mode" qualifiers="const">
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Performance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
Number of islands in the 3D physics engine.
</constant>
<constant name="AUDIO_OUTPUT_LATENCY" value="30" enum="Monitor">
Output latency of the [AudioServer].
Output latency of the [AudioServer]. Equivalent to calling [method AudioServer.get_output_latency], it is not recommended to call this every frame.
</constant>
<constant name="MONITOR_MAX" value="31" enum="Monitor">
Represents the size of the [enum Monitor] enum.
Expand Down
25 changes: 11 additions & 14 deletions drivers/pulseaudio/audio_driver_pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,27 +353,24 @@ Error AudioDriverPulseAudio::init() {
}

float AudioDriverPulseAudio::get_latency() {
if (latency == 0) { //only do this once since it's approximate anyway
lock();
lock();

pa_usec_t palat = 0;
if (pa_stream_get_state(pa_str) == PA_STREAM_READY) {
int negative = 0;
pa_usec_t pa_lat = 0;
if (pa_stream_get_state(pa_str) == PA_STREAM_READY) {
int negative = 0;

if (pa_stream_get_latency(pa_str, &palat, &negative) >= 0) {
if (negative) {
palat = 0;
}
if (pa_stream_get_latency(pa_str, &pa_lat, &negative) >= 0) {
if (negative) {
pa_lat = 0;
}
}
}

if (palat > 0) {
latency = double(palat) / 1000000.0;
}

unlock();
if (pa_lat > 0) {
latency = double(pa_lat) / 1000000.0;
}

unlock();
return latency;
}

Expand Down

0 comments on commit d90626f

Please sign in to comment.