File indexing completed on 2024-04-14 12:03:22

0001 // SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
0002 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0003 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0004 //
0005 // SPDX-License-Identifier: GPL-3.0-or-later
0006 #pragma once
0007 
0008 #include <QQuickFramebufferObject>
0009 
0010 #include <mpv/client.h>
0011 #include <mpv/render_gl.h>
0012 
0013 #include "qthelper.hpp"
0014 
0015 class MpvRenderer;
0016 
0017 class MpvPlayer : public QQuickFramebufferObject
0018 {
0019     Q_OBJECT
0020     Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
0021     Q_PROPERTY(qreal position READ position NOTIFY positionChanged)
0022     Q_PROPERTY(qreal duration READ duration NOTIFY durationChanged)
0023     Q_PROPERTY(bool paused READ paused NOTIFY pausedChanged)
0024     Q_PROPERTY(QSize sourceSize READ sourceSize NOTIFY sourceSizeChanged)
0025     Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
0026     Q_PROPERTY(bool looping READ looping WRITE setLooping NOTIFY loopingChanged)
0027     Q_PROPERTY(bool autoPlay READ autoPlay WRITE setAutoPlay NOTIFY autoPlayChanged)
0028     Q_PROPERTY(bool stopped READ stopped NOTIFY stoppedChanged)
0029 
0030     friend class MpvRenderer;
0031 
0032 public:
0033     static void on_update(void *ctx);
0034 
0035     explicit MpvPlayer(QQuickItem *parent = nullptr);
0036     ~MpvPlayer() override;
0037 
0038     Renderer *createRenderer() const override;
0039 
0040     qreal position() const;
0041     qreal duration() const;
0042     bool paused() const;
0043     QSize sourceSize() const;
0044     QString source() const;
0045     bool loading() const;
0046     bool looping() const;
0047     bool autoPlay() const;
0048     bool stopped() const;
0049 
0050     void setSource(const QString &source);
0051     void setLooping(bool loop);
0052     void setAutoPlay(bool autoPlay);
0053 
0054 public Q_SLOTS:
0055     Q_INVOKABLE void play();
0056     Q_INVOKABLE void pause();
0057     Q_INVOKABLE void stop();
0058     Q_INVOKABLE void setPosition(double value);
0059     void seek(qreal offset);
0060     void command(const QVariant &params);
0061     void setOption(const QString &name, const QVariant &value);
0062     void setProperty(const QString &name, const QVariant &value);
0063     QVariant getProperty(const QString &name);
0064 
0065 Q_SIGNALS:
0066     void positionChanged();
0067     void durationChanged();
0068     void pausedChanged();
0069     void onUpdate();
0070     void sourceSizeChanged();
0071     void sourceChanged();
0072     void loadingChanged();
0073     void loopingChanged();
0074     void autoPlayChanged();
0075     void stoppedChanged();
0076 
0077 private Q_SLOTS:
0078     void onMpvEvents();
0079     void doUpdate();
0080 
0081 private:
0082     bool m_paused = true;
0083     qreal m_position = 0;
0084     qreal m_duration = 0;
0085     QSize m_sourceSize;
0086     QString m_source;
0087     QString m_currentSource;
0088     bool m_loading = false;
0089     bool m_looping = false;
0090     bool m_autoPlay = false;
0091 
0092     mpv_handle *mpv = nullptr;
0093     mpv_render_context *mpv_gl = nullptr;
0094 };