Fix resetting hardware mixer volume

This commit is contained in:
badaix 2021-01-30 18:50:51 +01:00
parent fa7340a9bc
commit 622c283d78
4 changed files with 21 additions and 25 deletions

View file

@ -236,10 +236,10 @@ void Controller::getNextMessage()
player_->start(); player_->start();
// Don't change the initial hardware mixer volume on the user's device. // Don't change the initial hardware mixer volume on the user's device.
// The player class will send the device's volume to the server instead // The player class will send the device's volume to the server instead
if (settings_.player.mixer.mode != ClientSettings::Mixer::Mode::hardware) // if (settings_.player.mixer.mode != ClientSettings::Mixer::Mode::hardware)
{ // {
player_->setVolume(serverSettings_->getVolume() / 100., serverSettings_->isMuted()); player_->setVolume(serverSettings_->getVolume() / 100., serverSettings_->isMuted());
} // }
} }
else if (response->type == message_type::kStreamTags) else if (response->type == message_type::kStreamTags)
{ {

View file

@ -209,19 +209,17 @@ void AlsaPlayer::waitForEvent()
waitForEvent(); waitForEvent();
return; return;
} }
// Sometimes the old volume is reported after this event has been raised. // Sometimes the old volume is reported by getHardwareVolume after this event has been raised.
// As workaround we defer getting the volume by 20ms. // As workaround we defer getting the volume by 20ms.
timer_.cancel(); timer_.cancel();
timer_.expires_after(20ms); timer_.expires_after(20ms);
timer_.async_wait([this](const boost::system::error_code& ec) { timer_.async_wait([this](const boost::system::error_code& ec) {
if (!ec) if (!ec)
{ {
double volume; if (getHardwareVolume(volume_, muted_))
bool muted;
if (getHardwareVolume(volume, muted))
{ {
LOG(DEBUG, LOG_TAG) << "Volume: " << volume << ", muted: " << muted << "\n"; LOG(DEBUG, LOG_TAG) << "Volume changed: " << volume_ << ", muted: " << muted_ << "\n";
notifyVolumeChange(volume, muted); notifyVolumeChange(volume_, muted_);
} }
} }
}); });

View file

@ -110,16 +110,14 @@ void Player::start()
// If hardware mixer is used, send the initial volume to the server, because this is // If hardware mixer is used, send the initial volume to the server, because this is
// the volume that is configured by the user on his local device, so we shouldn't change it // the volume that is configured by the user on his local device, so we shouldn't change it
// on client start up // on client start up
if (settings_.mixer.mode == ClientSettings::Mixer::Mode::hardware) // if (settings_.mixer.mode == ClientSettings::Mixer::Mode::hardware)
{ // {
double volume; // if (getHardwareVolume(volume_, muted_))
bool muted; // {
if (getHardwareVolume(volume, muted)) // LOG(DEBUG, LOG_TAG) << "Volume: " << volume_ << ", muted: " << muted_ << "\n";
{ // notifyVolumeChange(volume_, muted_);
LOG(DEBUG, LOG_TAG) << "Volume: " << volume << ", muted: " << muted << "\n"; // }
notifyVolumeChange(volume, muted); // }
}
}
} }
@ -205,7 +203,7 @@ void Player::setVolume(double volume, bool mute)
muted_ = mute; muted_ = mute;
if (settings_.mixer.mode == ClientSettings::Mixer::Mode::hardware) if (settings_.mixer.mode == ClientSettings::Mixer::Mode::hardware)
{ {
setHardwareVolume(volume, muted_); setHardwareVolume(volume, mute);
} }
else if (settings_.mixer.mode == ClientSettings::Mixer::Mode::software) else if (settings_.mixer.mode == ClientSettings::Mixer::Mode::software)
{ {

View file

@ -183,9 +183,9 @@ void PulsePlayer::triggerVolumeUpdate()
if (info) if (info)
{ {
auto self = static_cast<PulsePlayer*>(userdata); auto self = static_cast<PulsePlayer*>(userdata);
auto volume = (double)pa_cvolume_avg(&(info->volume)) / (double)PA_VOLUME_NORM; self->volume_ = (double)pa_cvolume_avg(&(info->volume)) / (double)PA_VOLUME_NORM;
bool muted = (info->mute != 0); self->muted_ = (info->mute != 0);
LOG(DEBUG, LOG_TAG) << "volume changed: " << volume << ", muted: " << muted << "\n"; LOG(DEBUG, LOG_TAG) << "Volume changed: " << self->volume_ << ", muted: " << self->muted_ << "\n";
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
if (now - self->last_change_ < 1s) if (now - self->last_change_ < 1s)
@ -195,7 +195,7 @@ void PulsePlayer::triggerVolumeUpdate()
<< " ms => ignoring volume change\n"; << " ms => ignoring volume change\n";
return; return;
} }
self->notifyVolumeChange(volume, muted); self->notifyVolumeChange(self->volume_, self->muted_);
} }
}, },
this); this);