From cdd97db6931fd15deb63b876e251b4c9c9dcf052 Mon Sep 17 00:00:00 2001 From: APTX Date: Wed, 1 Mar 2017 16:06:09 +0100 Subject: [PATCH] Proper volume setting not based on sink volume This allows the backend to play volume in a greater range than 0-1. For MPV this is now 0-1.3. --- backendplugins/backend_mpv/backendmpv.cpp | 19 ++++++++++++++----- core/player.cpp | 18 ++++++++++++++---- core/player.h | 5 +++++ core/playerplugininterface.h | 1 + core/qml/PlayerControls.qml | 1 + 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/backendplugins/backend_mpv/backendmpv.cpp b/backendplugins/backend_mpv/backendmpv.cpp index 49cf3d2..d17a384 100644 --- a/backendplugins/backend_mpv/backendmpv.cpp +++ b/backendplugins/backend_mpv/backendmpv.cpp @@ -12,6 +12,8 @@ Q_LOGGING_CATEGORY(mpvBackend, "MPV") Q_LOGGING_CATEGORY(mpvLog, "MPV Log") +static constexpr const char VOLUME[] = "volume"; + BackendMpv::BackendMpv(QObject *parent) : QObject{parent} { #ifdef Q_OS_UNIX setlocale(LC_NUMERIC, "C"); @@ -52,12 +54,19 @@ MpvInstance::MpvInstance(PlayerPluginInterface *playerInterface, qCDebug(mpvBackend) << "register playback-time" << mpv_observe_property(m_handle, 0, "playback-time", MPV_FORMAT_DOUBLE); - qCDebug(mpvBackend) << "register ao-volume" - << mpv_observe_property(m_handle, 0, "ao-volume", + qCDebug(mpvBackend) << "register" << VOLUME + << mpv_observe_property(m_handle, 0, VOLUME, MPV_FORMAT_DOUBLE); qCDebug(mpvBackend) << "request log messages" << mpv_request_log_messages(m_handle, "info"); + { + double maxVolume = 130.0; + mpv_set_property(m_handle, "max-volume", MPV_FORMAT_DOUBLE, &maxVolume); + mpv_get_property(m_handle, "max-volume", MPV_FORMAT_DOUBLE, &maxVolume); + maxVolume /= 100.0; + m_player->playbackMaxVolumeChanged(maxVolume); + } } MpvInstance::~MpvInstance() { @@ -101,8 +110,8 @@ void MpvInstance::seek(TimeStamp pos) { void MpvInstance::setVolume(Volume volume) { double percantageVolume = volume * 100; - int error = mpv_set_property(m_handle, "ao-volume", MPV_FORMAT_DOUBLE, - &percantageVolume); + int error = + mpv_set_property(m_handle, VOLUME, MPV_FORMAT_DOUBLE, &percantageVolume); if (error) { qCDebug(mpvBackend) << "Audio output not yet ready, setting volume at a later time"; @@ -188,7 +197,7 @@ void MpvInstance::processMpvEvents() { m_player->playbackPositionChanged( static_cast( readProperty(property))); - } else if (strcmp(property->name, "ao-volume") == 0) { + } else if (strcmp(property->name, VOLUME) == 0) { if (m_volumeToSet > 0) { qCDebug(mpvBackend) << "Requested volume still not set, skipping this update"; diff --git a/core/player.cpp b/core/player.cpp index 0be95be..3aa6db6 100644 --- a/core/player.cpp +++ b/core/player.cpp @@ -31,6 +31,8 @@ Player::PlayState Player::state() const { return m_state; } Player::Volume Player::volume() const { return m_volume; } +Player::Volume Player::maxVolume() const { return m_maxVolume; } + bool Player::muted() const { return m_muted; } Player::AudioStreams Player::availableAudioStreams() const { @@ -92,7 +94,7 @@ void Player::togglePlay() { PlayState::Playing == state() ? pause() : play(); } void Player::seek(Player::TimeStamp position) { m_backend->seek(position); } void Player::setVolume(Volume volume) { - volume = qBound(Volume{}, volume, MAX_VOLUME); + volume = qBound(Volume{}, volume, m_maxVolume); m_backend->setVolume(volume); } @@ -183,21 +185,29 @@ void Player::playbackPositionChanged( if (qFuzzyCompare(m_position, position)) return; - qCDebug(playerCategory) << "Duration changed to" << position; + qCDebug(playerCategory) << "Position changed to" << position; m_position = position; emit positionChanged(position); } void Player::playbackVolumeChanged(Player::Volume volume) { - qCDebug(playerCategory) << "Volume changed to" << volume; if (qFuzzyCompare(m_volume, volume)) return; - qCDebug(playerCategory) << "Volume changed to!!" << volume; + qCDebug(playerCategory) << "Volume changed to" << volume; m_volume = volume; emit volumeChanged(volume); } +void Player::playbackMaxVolumeChanged(Player::Volume volume) { + if (qFuzzyCompare(m_maxVolume, volume)) + return; + + qCDebug(playerCategory) << "maxVolume changed to" << volume; + m_maxVolume = volume; + emit maxVolumeChanged(volume); +} + void Player::streamsChanged() {} void Player::reqisterQmlTypes() { diff --git a/core/player.h b/core/player.h index c96d582..7eec155 100644 --- a/core/player.h +++ b/core/player.h @@ -24,6 +24,7 @@ class Player : public QObject, Q_PROPERTY(Player::PlayState state READ state NOTIFY stateChanged) Q_PROPERTY(double volume READ volume WRITE setVolume NOTIFY volumeChanged) + Q_PROPERTY(double maxVolume READ maxVolume NOTIFY maxVolumeChanged) Q_PROPERTY(bool muted READ muted WRITE setMuted NOTIFY mutedChanged) Q_PROPERTY(Player::StreamIndex currentAudioStream READ currentAudioStream @@ -72,6 +73,7 @@ public: PlayState state() const; Volume volume() const; + Volume maxVolume() const; bool muted() const; StreamIndex currentAudioStream() const; @@ -88,6 +90,7 @@ public: signals: void stateChanged(PlayState state); void volumeChanged(Volume volume); + void maxVolumeChanged(Volume maxVolume); void mutedChanged(bool muted); void availableAudioStreamsChanged(AudioStreams availableAudioStreams); @@ -140,6 +143,7 @@ protected: void playbackDurationChanged(TimeStamp) override; void playbackPositionChanged(TimeStamp) override; void playbackVolumeChanged(Volume) override; + void playbackMaxVolumeChanged(Volume) override; void streamsChanged() override; public: @@ -154,6 +158,7 @@ private: QUrl m_nextSource; PlayState m_state = PlayState::Stopped; Volume m_volume = MAX_VOLUME; + Volume m_maxVolume = MAX_VOLUME; AudioStreams m_availableAudioStreams; StreamIndex m_currentAudioStream = StreamIndex{}; StreamIndex m_currentVideoStream = StreamIndex{}; diff --git a/core/playerplugininterface.h b/core/playerplugininterface.h index a4b62c9..c198111 100644 --- a/core/playerplugininterface.h +++ b/core/playerplugininterface.h @@ -41,6 +41,7 @@ public: virtual void playbackDurationChanged(TimeStamp) = 0; virtual void playbackPositionChanged(TimeStamp) = 0; virtual void playbackVolumeChanged(Volume) = 0; + virtual void playbackMaxVolumeChanged(Volume) = 0; virtual void streamsChanged() = 0; }; diff --git a/core/qml/PlayerControls.qml b/core/qml/PlayerControls.qml index d69f51a..a8c14a9 100644 --- a/core/qml/PlayerControls.qml +++ b/core/qml/PlayerControls.qml @@ -130,6 +130,7 @@ Row { VolumeSlider { height: fullscreenButton.height width: 100 + maxVolume: controlledPlayer ? controlledPlayer.maxVolume : 1 volume: controlledPlayer ? controlledPlayer.volume : 1 onVolumeChangeRequested: controlledPlayer.setVolume(volume) } -- 2.52.0