From 1dfe07003fa00d6491dca894096b6be946ff70cc Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 3 Jan 2023 21:32:20 +0100 Subject: [PATCH] GstEnginePipeline: Set and notify volume only when changed Another fix for #1089 --- src/engine/gstenginepipeline.cpp | 13 ++++++++----- src/engine/gstenginepipeline.h | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/engine/gstenginepipeline.cpp b/src/engine/gstenginepipeline.cpp index 1e9a1ed59..599334005 100644 --- a/src/engine/gstenginepipeline.cpp +++ b/src/engine/gstenginepipeline.cpp @@ -98,6 +98,7 @@ GstEnginePipeline::GstEnginePipeline(QObject *parent) pending_seek_nanosec_(-1), last_known_position_ns_(0), next_uri_set_(false), + volume_internal_(-1.0), volume_percent_(100), use_fudge_timer_(false), pipeline_(nullptr), @@ -873,10 +874,9 @@ void GstEnginePipeline::NotifyVolumeCallback(GstElement *element, GParamSpec *pa GstEnginePipeline *instance = reinterpret_cast(self); - gdouble volume = 0; - g_object_get(G_OBJECT(instance->volume_), "volume", &volume, nullptr); + g_object_get(G_OBJECT(instance->volume_), "volume", &instance->volume_internal_, nullptr); - const uint volume_percent = static_cast(qBound(0L, lround(qBound(0.0, gst_stream_volume_convert_volume(GST_STREAM_VOLUME_FORMAT_LINEAR, GST_STREAM_VOLUME_FORMAT_CUBIC, volume), 1.0) / 0.01), 100L)); + const uint volume_percent = static_cast(qBound(0L, lround(qBound(0.0, gst_stream_volume_convert_volume(GST_STREAM_VOLUME_FORMAT_LINEAR, GST_STREAM_VOLUME_FORMAT_CUBIC, instance->volume_internal_), 1.0) / 0.01), 100L)); if (volume_percent != instance->volume_percent_) { instance->volume_percent_ = volume_percent; emit instance->VolumeChanged(volume_percent); @@ -1516,8 +1516,11 @@ bool GstEnginePipeline::Seek(const qint64 nanosec) { void GstEnginePipeline::SetVolume(const uint volume_percent) { if (volume_) { - const double volume = gst_stream_volume_convert_volume(GST_STREAM_VOLUME_FORMAT_CUBIC, GST_STREAM_VOLUME_FORMAT_LINEAR, static_cast(volume_percent) * 0.01); - g_object_set(G_OBJECT(volume_), "volume", volume, nullptr); + const double volume_internal = lround(static_cast(gst_stream_volume_convert_volume(GST_STREAM_VOLUME_FORMAT_CUBIC, GST_STREAM_VOLUME_FORMAT_LINEAR, static_cast(volume_percent) * 0.01) * 100.0)) / 100.0; + if (volume_internal != volume_internal_) { + volume_internal_ = volume_internal; + g_object_set(G_OBJECT(volume_), "volume", volume_internal, nullptr); + } } volume_percent_ = volume_percent; diff --git a/src/engine/gstenginepipeline.h b/src/engine/gstenginepipeline.h index f3d6b9455..749b3af5e 100644 --- a/src/engine/gstenginepipeline.h +++ b/src/engine/gstenginepipeline.h @@ -275,6 +275,7 @@ class GstEnginePipeline : public QObject { // Complete the transition to the next song when it starts playing bool next_uri_set_; + gdouble volume_internal_; uint volume_percent_; std::shared_ptr fader_;