]> Some of my projects - aniplayer.git/commitdiff
Proper volume setting not based on sink volume
authorAPTX <marek321@gmail.com>
Wed, 1 Mar 2017 15:06:09 +0000 (16:06 +0100)
committerAPTX <marek321@gmail.com>
Wed, 1 Mar 2017 15:06:09 +0000 (16:06 +0100)
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
core/player.cpp
core/player.h
core/playerplugininterface.h
core/qml/PlayerControls.qml

index 49cf3d24472a8e296a6f55dd222bebdccc913f97..d17a384c1f5835ac712baa620582c8c528bc9233 100644 (file)
@@ -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<PlayerPluginInterface::TimeStamp>(
                 readProperty<MPV_FORMAT_DOUBLE>(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";
index 0be95be021734f79bb5fd169eb2b3b16a183ded3..3aa6db697370f5ad01cd729fcf9eb8cadb916589 100644 (file)
@@ -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() {
index c96d582900993cde87b322cb1922b2e69c0621c3..7eec1558e319b8ad7123a249a4ea3542b15ac08d 100644 (file)
@@ -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{};
index a4b62c96597518e08ccb739452f53c853ec12c39..c198111449532a4126a99609ff1305de86d65410 100644 (file)
@@ -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;
 };
index d69f51afb6746fe0d1f7100aec3b8ab1f4ed3e69..a8c14a9cfcd220c33ad88d43df955eef1ebcfa24 100644 (file)
@@ -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)
     }