From 3eedcc55b9bcadbb56519d53bbf89059ec326437 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 18 Aug 2022 14:14:58 +0200 Subject: [PATCH] mixer/Control: convert pointers to references --- src/filter/plugins/ReplayGainFilterPlugin.cxx | 2 +- src/mixer/MixerAll.cxx | 8 +-- src/mixer/MixerControl.cxx | 70 ++++++++----------- src/mixer/MixerControl.hxx | 10 +-- src/output/Control.cxx | 8 +-- src/output/OutputCommand.cxx | 4 +- test/read_mixer.cxx | 6 +- test/run_filter.cxx | 2 +- 8 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/filter/plugins/ReplayGainFilterPlugin.cxx b/src/filter/plugins/ReplayGainFilterPlugin.cxx index 4840943d9..2f01badc3 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.cxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.cxx @@ -170,7 +170,7 @@ ReplayGainFilter::Update() _volume = 100; try { - mixer_set_volume(mixer, _volume); + mixer_set_volume(*mixer, _volume); /* invoke the mixer's listener manually, just in case the mixer implementation didn't do diff --git a/src/mixer/MixerAll.cxx b/src/mixer/MixerAll.cxx index abd0d8462..08775bdb2 100644 --- a/src/mixer/MixerAll.cxx +++ b/src/mixer/MixerAll.cxx @@ -44,7 +44,7 @@ output_mixer_get_volume(const AudioOutputControl &ao) noexcept return -1; try { - return mixer_get_volume(mixer); + return mixer_get_volume(*mixer); } catch (...) { FmtError(mixer_domain, "Failed to read mixer for '{}': {}", @@ -98,7 +98,7 @@ output_mixer_set_volume(AudioOutputControl &ao, unsigned volume) return SetVolumeResult::DISABLED; try { - mixer_set_volume(mixer, volume); + mixer_set_volume(*mixer, volume); return SetVolumeResult::OK; } catch (...) { FmtError(mixer_domain, @@ -156,7 +156,7 @@ output_mixer_get_software_volume(const AudioOutputControl &ao) noexcept if (mixer == nullptr || !mixer->IsPlugin(software_mixer_plugin)) return -1; - return mixer_get_volume(mixer); + return mixer_get_volume(*mixer); } int @@ -190,6 +190,6 @@ MultipleOutputs::SetSoftwareVolume(unsigned volume) noexcept if (mixer != nullptr && (mixer->IsPlugin(software_mixer_plugin) || mixer->IsPlugin(null_mixer_plugin))) - mixer_set_volume(mixer, volume); + mixer_set_volume(*mixer, volume); } } diff --git a/src/mixer/MixerControl.cxx b/src/mixer/MixerControl.cxx index b797d4bbe..998d4aaa6 100644 --- a/src/mixer/MixerControl.cxx +++ b/src/mixer/MixerControl.cxx @@ -42,78 +42,71 @@ mixer_free(Mixer *mixer) /* mixers with the "global" flag set might still be open at this point (see mixer_auto_close()) */ - mixer_close(mixer); + mixer_close(*mixer); delete mixer; } void -mixer_open(Mixer *mixer) +mixer_open(Mixer &mixer) { - assert(mixer != nullptr); + const std::scoped_lock protect(mixer.mutex); - const std::scoped_lock protect(mixer->mutex); - - if (mixer->open) + if (mixer.open) return; try { - mixer->Open(); - mixer->open = true; - mixer->failure = {}; + mixer.Open(); + mixer.open = true; + mixer.failure = {}; } catch (...) { - mixer->failure = std::current_exception(); + mixer.failure = std::current_exception(); throw; } } static void -mixer_close_internal(Mixer *mixer) +mixer_close_internal(Mixer &mixer) { - assert(mixer != nullptr); - assert(mixer->open); + assert(mixer.open); - mixer->Close(); - mixer->open = false; - mixer->failure = {}; + mixer.Close(); + mixer.open = false; + mixer.failure = {}; } void -mixer_close(Mixer *mixer) +mixer_close(Mixer &mixer) { - assert(mixer != nullptr); + const std::scoped_lock protect(mixer.mutex); - const std::scoped_lock protect(mixer->mutex); - - if (mixer->open) + if (mixer.open) mixer_close_internal(mixer); } void -mixer_auto_close(Mixer *mixer) +mixer_auto_close(Mixer &mixer) { - if (!mixer->IsGlobal()) + if (!mixer.IsGlobal()) mixer_close(mixer); } int -mixer_get_volume(Mixer *mixer) +mixer_get_volume(Mixer &mixer) { int volume; - assert(mixer != nullptr); - - if (mixer->IsGlobal() && !mixer->failure) + if (mixer.IsGlobal() && !mixer.failure) mixer_open(mixer); - const std::scoped_lock protect(mixer->mutex); + const std::scoped_lock protect(mixer.mutex); - if (mixer->open) { + if (mixer.open) { try { - volume = mixer->GetVolume(); + volume = mixer.GetVolume(); } catch (...) { mixer_close_internal(mixer); - mixer->failure = std::current_exception(); + mixer.failure = std::current_exception(); throw; } } else @@ -123,18 +116,17 @@ mixer_get_volume(Mixer *mixer) } void -mixer_set_volume(Mixer *mixer, unsigned volume) +mixer_set_volume(Mixer &mixer, unsigned volume) { - assert(mixer != nullptr); assert(volume <= 100); - if (mixer->IsGlobal() && !mixer->failure) + if (mixer.IsGlobal() && !mixer.failure) mixer_open(mixer); - const std::scoped_lock protect(mixer->mutex); + const std::scoped_lock protect(mixer.mutex); - if (mixer->open) - mixer->SetVolume(volume); - else if (mixer->failure) - std::rethrow_exception(mixer->failure); + if (mixer.open) + mixer.SetVolume(volume); + else if (mixer.failure) + std::rethrow_exception(mixer.failure); } diff --git a/src/mixer/MixerControl.hxx b/src/mixer/MixerControl.hxx index fbc17d36c..b2e91cba0 100644 --- a/src/mixer/MixerControl.hxx +++ b/src/mixer/MixerControl.hxx @@ -48,28 +48,28 @@ mixer_free(Mixer *mixer); * Throws std::runtime_error on error. */ void -mixer_open(Mixer *mixer); +mixer_open(Mixer &mixer); void -mixer_close(Mixer *mixer); +mixer_close(Mixer &mixer); /** * Close the mixer unless the plugin's "global" flag is set. This is * called when the #AudioOutput is closed. */ void -mixer_auto_close(Mixer *mixer); +mixer_auto_close(Mixer &mixer); /** * Throws std::runtime_error on error. */ int -mixer_get_volume(Mixer *mixer); +mixer_get_volume(Mixer &mixer); /** * Throws std::runtime_error on error. */ void -mixer_set_volume(Mixer *mixer, unsigned volume); +mixer_set_volume(Mixer &mixer, unsigned volume); #endif diff --git a/src/output/Control.cxx b/src/output/Control.cxx index abd288acc..49aa7a5e9 100644 --- a/src/output/Control.cxx +++ b/src/output/Control.cxx @@ -276,7 +276,7 @@ AudioOutputControl::Open(std::unique_lock &lock, if (open2 && output->mixer != nullptr) { const ScopeUnlock unlock(mutex); try { - mixer_open(output->mixer); + mixer_open(*output->mixer); } catch (...) { FmtError(output_domain, "Failed to open mixer for '{}': {}", @@ -296,7 +296,7 @@ AudioOutputControl::CloseWait(std::unique_lock &lock) noexcept return; if (output->mixer != nullptr) - mixer_auto_close(output->mixer); + mixer_auto_close(*output->mixer); assert(!open || !fail_timer.IsDefined()); @@ -360,7 +360,7 @@ AudioOutputControl::LockPauseAsync() noexcept /* the device has no pause mode: close the mixer, unless its "global" flag is set (checked by mixer_auto_close()) */ - mixer_auto_close(output->mixer); + mixer_auto_close(*output->mixer); if (output) output->Interrupt(); @@ -419,7 +419,7 @@ AudioOutputControl::LockRelease() noexcept /* the device has no pause mode: close the mixer, unless its "global" flag is set (checked by mixer_auto_close()) */ - mixer_auto_close(output->mixer); + mixer_auto_close(*output->mixer); std::unique_lock lock(mutex); diff --git a/src/output/OutputCommand.cxx b/src/output/OutputCommand.cxx index 69d87a18d..67269a45d 100644 --- a/src/output/OutputCommand.cxx +++ b/src/output/OutputCommand.cxx @@ -75,7 +75,7 @@ audio_output_disable_index(MultipleOutputs &outputs, auto *mixer = ao.GetMixer(); if (mixer != nullptr) { - mixer_close(mixer); + mixer_close(*mixer); mixer_memento.InvalidateHardwareVolume(); idle_add(IDLE_MIXER); } @@ -102,7 +102,7 @@ audio_output_toggle_index(MultipleOutputs &outputs, if (!enabled) { auto *mixer = ao.GetMixer(); if (mixer != nullptr) { - mixer_close(mixer); + mixer_close(*mixer); mixer_memento.InvalidateHardwareVolume(); idle_add(IDLE_MIXER); } diff --git a/test/read_mixer.cxx b/test/read_mixer.cxx index fa51bc3cd..5725d30e7 100644 --- a/test/read_mixer.cxx +++ b/test/read_mixer.cxx @@ -57,10 +57,10 @@ try { mixer_listener, ConfigBlock()); - mixer_open(mixer); + mixer_open(*mixer); - volume = mixer_get_volume(mixer); - mixer_close(mixer); + volume = mixer_get_volume(*mixer); + mixer_close(*mixer); mixer_free(mixer); assert(volume >= -1 && volume <= 100); diff --git a/test/run_filter.cxx b/test/run_filter.cxx index 75e641cc4..580493981 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -43,7 +43,7 @@ #include void -mixer_set_volume([[maybe_unused]] Mixer *mixer, +mixer_set_volume([[maybe_unused]] Mixer &mixer, [[maybe_unused]] unsigned volume) { }