Merge branch 'v0.23.x'

This commit is contained in:
Max Kellermann
2022-08-08 23:46:22 +02:00
19 changed files with 234 additions and 149 deletions

View File

@@ -17,14 +17,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "Volume.hxx"
#include "Memento.hxx"
#include "output/MultipleOutputs.hxx"
#include "Idle.hxx"
#include "util/StringCompare.hxx"
#include "util/Domain.hxx"
#include "system/PeriodClock.hxx"
#include "io/BufferedOutputStream.hxx"
#include "Log.hxx"
#include <fmt/format.h>
@@ -34,24 +31,8 @@
#define SW_VOLUME_STATE "sw_volume: "
static constexpr Domain volume_domain("volume");
static unsigned volume_software_set = 100;
/** the cached hardware mixer value; invalid if negative */
static int last_hardware_volume = -1;
/** the age of #last_hardware_volume */
static PeriodClock hardware_volume_clock;
void
InvalidateHardwareVolume() noexcept
{
/* flush the hardware volume cache */
last_hardware_volume = -1;
}
int
volume_level_get(const MultipleOutputs &outputs) noexcept
MixerMemento::GetVolume(const MultipleOutputs &outputs) noexcept
{
if (last_hardware_volume >= 0 &&
!hardware_volume_clock.CheckUpdate(std::chrono::seconds(1)))
@@ -62,8 +43,8 @@ volume_level_get(const MultipleOutputs &outputs) noexcept
return last_hardware_volume;
}
static bool
software_volume_change(MultipleOutputs &outputs, unsigned volume)
inline bool
MixerMemento::SetSoftwareVolume(MultipleOutputs &outputs, unsigned volume)
{
assert(volume <= 100);
@@ -73,8 +54,8 @@ software_volume_change(MultipleOutputs &outputs, unsigned volume)
return true;
}
static void
hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
inline void
MixerMemento::SetHardwareVolume(MultipleOutputs &outputs, unsigned volume)
{
/* reset the cache */
last_hardware_volume = -1;
@@ -83,19 +64,17 @@ hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
}
void
volume_level_change(MultipleOutputs &outputs, unsigned volume)
MixerMemento::SetVolume(MultipleOutputs &outputs, unsigned volume)
{
assert(volume <= 100);
volume_software_set = volume;
idle_add(IDLE_MIXER);
hardware_volume_change(outputs, volume);
SetHardwareVolume(outputs, volume);
}
bool
read_sw_volume_state(const char *line, MultipleOutputs &outputs)
MixerMemento::LoadSoftwareVolumeState(const char *line, MultipleOutputs &outputs)
{
char *end = nullptr;
long int sv;
@@ -106,21 +85,13 @@ read_sw_volume_state(const char *line, MultipleOutputs &outputs)
sv = strtol(line, &end, 10);
if (*end == 0 && sv >= 0 && sv <= 100)
software_volume_change(outputs, sv);
else
FmtWarning(volume_domain,
"Can't parse software volume: {}", line);
SetSoftwareVolume(outputs, sv);
return true;
}
void
save_sw_volume_state(BufferedOutputStream &os)
MixerMemento::SaveSoftwareVolumeState(BufferedOutputStream &os) const
{
os.Fmt(FMT_STRING(SW_VOLUME_STATE "{}\n"), volume_software_set);
}
unsigned
sw_volume_state_get_hash() noexcept
{
return volume_software_set;
}

75
src/mixer/Memento.hxx Normal file
View File

@@ -0,0 +1,75 @@
/*
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "system/PeriodClock.hxx"
class MultipleOutputs;
class BufferedOutputStream;
/**
* Cache for hardware/software volume levels.
*/
class MixerMemento {
unsigned volume_software_set = 100;
/** the cached hardware mixer value; invalid if negative */
int last_hardware_volume = -1;
/** the age of #last_hardware_volume */
PeriodClock hardware_volume_clock;
public:
/**
* Flush the hardware volume cache.
*/
void InvalidateHardwareVolume() noexcept {
last_hardware_volume = -1;
}
[[gnu::pure]]
int GetVolume(const MultipleOutputs &outputs) noexcept;
/**
* Throws on error.
*
* Note: the caller is responsible for emitting #IDLE_MIXER.
*/
void SetVolume(MultipleOutputs &outputs, unsigned volume);
bool LoadSoftwareVolumeState(const char *line, MultipleOutputs &outputs);
void SaveSoftwareVolumeState(BufferedOutputStream &os) const;
/**
* Generates a hash number for the current state of the software
* volume control. This is used by timer_save_state_file() to
* determine whether the state has changed and the state file should
* be saved.
*/
[[gnu::pure]]
unsigned GetSoftwareVolumeStateHash() const noexcept {
return volume_software_set;
}
private:
bool SetSoftwareVolume(MultipleOutputs &outputs, unsigned volume);
void SetHardwareVolume(MultipleOutputs &outputs, unsigned volume);
};

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2003-2022 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_VOLUME_HXX
#define MPD_VOLUME_HXX
class MultipleOutputs;
class BufferedOutputStream;
void
InvalidateHardwareVolume() noexcept;
[[gnu::pure]]
int
volume_level_get(const MultipleOutputs &outputs) noexcept;
/**
* Throws on error.
*/
void
volume_level_change(MultipleOutputs &outputs, unsigned volume);
bool
read_sw_volume_state(const char *line, MultipleOutputs &outputs);
void
save_sw_volume_state(BufferedOutputStream &os);
/**
* Generates a hash number for the current state of the software
* volume control. This is used by timer_save_state_file() to
* determine whether the state has changed and the state file should
* be saved.
*/
[[gnu::pure]]
unsigned
sw_volume_state_get_hash() noexcept;
#endif