mixer/Control: convert pointers to references

This commit is contained in:
Max Kellermann 2022-08-18 14:14:58 +02:00
parent dd2cab1488
commit 3eedcc55b9
8 changed files with 51 additions and 59 deletions

View File

@ -170,7 +170,7 @@ ReplayGainFilter::Update()
_volume = 100; _volume = 100;
try { try {
mixer_set_volume(mixer, _volume); mixer_set_volume(*mixer, _volume);
/* invoke the mixer's listener manually, just /* invoke the mixer's listener manually, just
in case the mixer implementation didn't do in case the mixer implementation didn't do

View File

@ -44,7 +44,7 @@ output_mixer_get_volume(const AudioOutputControl &ao) noexcept
return -1; return -1;
try { try {
return mixer_get_volume(mixer); return mixer_get_volume(*mixer);
} catch (...) { } catch (...) {
FmtError(mixer_domain, FmtError(mixer_domain,
"Failed to read mixer for '{}': {}", "Failed to read mixer for '{}': {}",
@ -98,7 +98,7 @@ output_mixer_set_volume(AudioOutputControl &ao, unsigned volume)
return SetVolumeResult::DISABLED; return SetVolumeResult::DISABLED;
try { try {
mixer_set_volume(mixer, volume); mixer_set_volume(*mixer, volume);
return SetVolumeResult::OK; return SetVolumeResult::OK;
} catch (...) { } catch (...) {
FmtError(mixer_domain, FmtError(mixer_domain,
@ -156,7 +156,7 @@ output_mixer_get_software_volume(const AudioOutputControl &ao) noexcept
if (mixer == nullptr || !mixer->IsPlugin(software_mixer_plugin)) if (mixer == nullptr || !mixer->IsPlugin(software_mixer_plugin))
return -1; return -1;
return mixer_get_volume(mixer); return mixer_get_volume(*mixer);
} }
int int
@ -190,6 +190,6 @@ MultipleOutputs::SetSoftwareVolume(unsigned volume) noexcept
if (mixer != nullptr && if (mixer != nullptr &&
(mixer->IsPlugin(software_mixer_plugin) || (mixer->IsPlugin(software_mixer_plugin) ||
mixer->IsPlugin(null_mixer_plugin))) mixer->IsPlugin(null_mixer_plugin)))
mixer_set_volume(mixer, volume); mixer_set_volume(*mixer, volume);
} }
} }

View File

@ -42,78 +42,71 @@ mixer_free(Mixer *mixer)
/* mixers with the "global" flag set might still be open at /* mixers with the "global" flag set might still be open at
this point (see mixer_auto_close()) */ this point (see mixer_auto_close()) */
mixer_close(mixer); mixer_close(*mixer);
delete mixer; delete mixer;
} }
void void
mixer_open(Mixer *mixer) mixer_open(Mixer &mixer)
{ {
assert(mixer != nullptr); const std::scoped_lock<Mutex> protect(mixer.mutex);
const std::scoped_lock<Mutex> protect(mixer->mutex); if (mixer.open)
if (mixer->open)
return; return;
try { try {
mixer->Open(); mixer.Open();
mixer->open = true; mixer.open = true;
mixer->failure = {}; mixer.failure = {};
} catch (...) { } catch (...) {
mixer->failure = std::current_exception(); mixer.failure = std::current_exception();
throw; throw;
} }
} }
static void static void
mixer_close_internal(Mixer *mixer) mixer_close_internal(Mixer &mixer)
{ {
assert(mixer != nullptr); assert(mixer.open);
assert(mixer->open);
mixer->Close(); mixer.Close();
mixer->open = false; mixer.open = false;
mixer->failure = {}; mixer.failure = {};
} }
void void
mixer_close(Mixer *mixer) mixer_close(Mixer &mixer)
{ {
assert(mixer != nullptr); const std::scoped_lock<Mutex> protect(mixer.mutex);
const std::scoped_lock<Mutex> protect(mixer->mutex); if (mixer.open)
if (mixer->open)
mixer_close_internal(mixer); mixer_close_internal(mixer);
} }
void void
mixer_auto_close(Mixer *mixer) mixer_auto_close(Mixer &mixer)
{ {
if (!mixer->IsGlobal()) if (!mixer.IsGlobal())
mixer_close(mixer); mixer_close(mixer);
} }
int int
mixer_get_volume(Mixer *mixer) mixer_get_volume(Mixer &mixer)
{ {
int volume; int volume;
assert(mixer != nullptr); if (mixer.IsGlobal() && !mixer.failure)
if (mixer->IsGlobal() && !mixer->failure)
mixer_open(mixer); mixer_open(mixer);
const std::scoped_lock<Mutex> protect(mixer->mutex); const std::scoped_lock<Mutex> protect(mixer.mutex);
if (mixer->open) { if (mixer.open) {
try { try {
volume = mixer->GetVolume(); volume = mixer.GetVolume();
} catch (...) { } catch (...) {
mixer_close_internal(mixer); mixer_close_internal(mixer);
mixer->failure = std::current_exception(); mixer.failure = std::current_exception();
throw; throw;
} }
} else } else
@ -123,18 +116,17 @@ mixer_get_volume(Mixer *mixer)
} }
void void
mixer_set_volume(Mixer *mixer, unsigned volume) mixer_set_volume(Mixer &mixer, unsigned volume)
{ {
assert(mixer != nullptr);
assert(volume <= 100); assert(volume <= 100);
if (mixer->IsGlobal() && !mixer->failure) if (mixer.IsGlobal() && !mixer.failure)
mixer_open(mixer); mixer_open(mixer);
const std::scoped_lock<Mutex> protect(mixer->mutex); const std::scoped_lock<Mutex> protect(mixer.mutex);
if (mixer->open) if (mixer.open)
mixer->SetVolume(volume); mixer.SetVolume(volume);
else if (mixer->failure) else if (mixer.failure)
std::rethrow_exception(mixer->failure); std::rethrow_exception(mixer.failure);
} }

View File

@ -48,28 +48,28 @@ mixer_free(Mixer *mixer);
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
void void
mixer_open(Mixer *mixer); mixer_open(Mixer &mixer);
void void
mixer_close(Mixer *mixer); mixer_close(Mixer &mixer);
/** /**
* Close the mixer unless the plugin's "global" flag is set. This is * Close the mixer unless the plugin's "global" flag is set. This is
* called when the #AudioOutput is closed. * called when the #AudioOutput is closed.
*/ */
void void
mixer_auto_close(Mixer *mixer); mixer_auto_close(Mixer &mixer);
/** /**
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
int int
mixer_get_volume(Mixer *mixer); mixer_get_volume(Mixer &mixer);
/** /**
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
void void
mixer_set_volume(Mixer *mixer, unsigned volume); mixer_set_volume(Mixer &mixer, unsigned volume);
#endif #endif

View File

@ -276,7 +276,7 @@ AudioOutputControl::Open(std::unique_lock<Mutex> &lock,
if (open2 && output->mixer != nullptr) { if (open2 && output->mixer != nullptr) {
const ScopeUnlock unlock(mutex); const ScopeUnlock unlock(mutex);
try { try {
mixer_open(output->mixer); mixer_open(*output->mixer);
} catch (...) { } catch (...) {
FmtError(output_domain, FmtError(output_domain,
"Failed to open mixer for '{}': {}", "Failed to open mixer for '{}': {}",
@ -296,7 +296,7 @@ AudioOutputControl::CloseWait(std::unique_lock<Mutex> &lock) noexcept
return; return;
if (output->mixer != nullptr) if (output->mixer != nullptr)
mixer_auto_close(output->mixer); mixer_auto_close(*output->mixer);
assert(!open || !fail_timer.IsDefined()); assert(!open || !fail_timer.IsDefined());
@ -360,7 +360,7 @@ AudioOutputControl::LockPauseAsync() noexcept
/* the device has no pause mode: close the mixer, /* the device has no pause mode: close the mixer,
unless its "global" flag is set (checked by unless its "global" flag is set (checked by
mixer_auto_close()) */ mixer_auto_close()) */
mixer_auto_close(output->mixer); mixer_auto_close(*output->mixer);
if (output) if (output)
output->Interrupt(); output->Interrupt();
@ -419,7 +419,7 @@ AudioOutputControl::LockRelease() noexcept
/* the device has no pause mode: close the mixer, /* the device has no pause mode: close the mixer,
unless its "global" flag is set (checked by unless its "global" flag is set (checked by
mixer_auto_close()) */ mixer_auto_close()) */
mixer_auto_close(output->mixer); mixer_auto_close(*output->mixer);
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);

View File

@ -75,7 +75,7 @@ audio_output_disable_index(MultipleOutputs &outputs,
auto *mixer = ao.GetMixer(); auto *mixer = ao.GetMixer();
if (mixer != nullptr) { if (mixer != nullptr) {
mixer_close(mixer); mixer_close(*mixer);
mixer_memento.InvalidateHardwareVolume(); mixer_memento.InvalidateHardwareVolume();
idle_add(IDLE_MIXER); idle_add(IDLE_MIXER);
} }
@ -102,7 +102,7 @@ audio_output_toggle_index(MultipleOutputs &outputs,
if (!enabled) { if (!enabled) {
auto *mixer = ao.GetMixer(); auto *mixer = ao.GetMixer();
if (mixer != nullptr) { if (mixer != nullptr) {
mixer_close(mixer); mixer_close(*mixer);
mixer_memento.InvalidateHardwareVolume(); mixer_memento.InvalidateHardwareVolume();
idle_add(IDLE_MIXER); idle_add(IDLE_MIXER);
} }

View File

@ -57,10 +57,10 @@ try {
mixer_listener, mixer_listener,
ConfigBlock()); ConfigBlock());
mixer_open(mixer); mixer_open(*mixer);
volume = mixer_get_volume(mixer); volume = mixer_get_volume(*mixer);
mixer_close(mixer); mixer_close(*mixer);
mixer_free(mixer); mixer_free(mixer);
assert(volume >= -1 && volume <= 100); assert(volume >= -1 && volume <= 100);

View File

@ -43,7 +43,7 @@
#include <stdio.h> #include <stdio.h>
void void
mixer_set_volume([[maybe_unused]] Mixer *mixer, mixer_set_volume([[maybe_unused]] Mixer &mixer,
[[maybe_unused]] unsigned volume) [[maybe_unused]] unsigned volume)
{ {
} }