summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2024-01-08 01:23:55 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2024-01-08 01:23:55 +0100
commiteed23cdf1102a8099bd7f47bfe3fb44e19148a08 (patch)
tree1b7f6d43daee97d0a3871aa9062432946335b30e /src
parentSpeedup room switching by forcing a bigger initial item size (diff)
downloadnheko-eed23cdf1102a8099bd7f47bfe3fb44e19148a08.tar.xz
Fix lag when media messages are shown and fix media controls
Diffstat (limited to 'src')
-rw-r--r--src/ui/MxcMediaProxy.cpp18
-rw-r--r--src/ui/MxcMediaProxy.h37
2 files changed, 49 insertions, 6 deletions
diff --git a/src/ui/MxcMediaProxy.cpp b/src/ui/MxcMediaProxy.cpp
index 5fae0654..3d486f6a 100644
--- a/src/ui/MxcMediaProxy.cpp
+++ b/src/ui/MxcMediaProxy.cpp
@@ -23,8 +23,6 @@
 MxcMediaProxy::MxcMediaProxy(QObject *parent)
   : QMediaPlayer(parent)
 {
-    connect(this, &MxcMediaProxy::eventIdChanged, &MxcMediaProxy::startDownload);
-    connect(this, &MxcMediaProxy::roomChanged, &MxcMediaProxy::startDownload);
     connect(
       this, &QMediaPlayer::errorOccurred, this, [](QMediaPlayer::Error error, QString errorString) {
           nhlog::ui()->debug("Media player error {} and errorStr {}",
@@ -36,6 +34,17 @@ MxcMediaProxy::MxcMediaProxy(QObject *parent)
                           static_cast<int>(status),
                           static_cast<int>(this->error()));
     });
+    connect(this, &MxcMediaProxy::playbackStateChanged, [this](QMediaPlayer::PlaybackState status) {
+        // We only set the output when starting the playback because otherwise the audio device
+        // lookup takes about 500ms, which causes a lot of stutter...
+        if (status == QMediaPlayer::PlayingState && !audioOutput()) {
+            nhlog::ui()->debug("Set audio output");
+            auto newOut = new QAudioOutput(this);
+            newOut->setMuted(muted_);
+            newOut->setVolume(volume_);
+            setAudioOutput(newOut);
+        }
+    });
     connect(this, &MxcMediaProxy::metaDataChanged, [this]() { emit orientationChanged(); });
 
     connect(ChatPage::instance()->timelineManager()->rooms(),
@@ -55,7 +64,7 @@ MxcMediaProxy::orientation() const
 }
 
 void
-MxcMediaProxy::startDownload()
+MxcMediaProxy::startDownload(bool onlyCached)
 {
     if (!room_)
         return;
@@ -126,6 +135,9 @@ MxcMediaProxy::startDownload()
         }
     }
 
+    if (onlyCached)
+        return;
+
     http::client()->download(url,
                              [filename, url, processBuffer](const std::string &data,
                                                             const std::string &,
diff --git a/src/ui/MxcMediaProxy.h b/src/ui/MxcMediaProxy.h
index 64c61c4f..2e8b2fa1 100644
--- a/src/ui/MxcMediaProxy.h
+++ b/src/ui/MxcMediaProxy.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <QAudioOutput>
 #include <QBuffer>
 #include <QMediaPlayer>
 #include <QObject>
@@ -26,9 +27,16 @@ class MxcMediaProxy : public QMediaPlayer
     Q_PROPERTY(QString eventId READ eventId WRITE setEventId NOTIFY eventIdChanged)
     Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged)
     Q_PROPERTY(int orientation READ orientation NOTIFY orientationChanged)
+    Q_PROPERTY(float volume READ volume WRITE setVolume NOTIFY volumeChanged)
+    Q_PROPERTY(bool muted READ muted WRITE setMuted NOTIFY mutedChanged)
 
 public:
     MxcMediaProxy(QObject *parent = nullptr);
+    ~MxcMediaProxy()
+    {
+        stop();
+        this->setSourceDevice(nullptr);
+    }
 
     bool loaded() const { return buffer.size() > 0; }
     QString eventId() const { return eventId_; }
@@ -45,6 +53,25 @@ public:
     }
     int orientation() const;
 
+    float volume() const { return volume_; }
+    bool muted() const { return muted_; }
+    void setVolume(float val)
+    {
+        volume_ = val;
+        if (auto output = audioOutput()) {
+            output->setVolume(val);
+        }
+        emit volumeChanged();
+    }
+    void setMuted(bool val)
+    {
+        muted_ = val;
+        if (auto output = audioOutput()) {
+            output->setMuted(val);
+        }
+        emit mutedChanged();
+    }
+
 signals:
     void roomChanged();
     void eventIdChanged();
@@ -52,10 +79,12 @@ signals:
     void newBuffer(QUrl, QIODevice *buf);
 
     void orientationChanged();
-    void videoSurfaceChanged();
 
-private slots:
-    void startDownload();
+    void volumeChanged();
+    void mutedChanged();
+
+public slots:
+    void startDownload(bool onlyCached = false);
 
 private:
     TimelineModel *room_ = nullptr;
@@ -63,4 +92,6 @@ private:
     QString filename_;
     QBuffer buffer;
     QObject *m_surface = nullptr;
+    float volume_      = 1.f;
+    bool muted_        = false;
 };