File indexing completed on 2024-12-15 04:23:42

0001 /*
0002  * SPDX-FileCopyrightText: 2020 George Florea Bănuș <georgefb899@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QtQuick/QQuickFramebufferObject>
0010 
0011 #include <mpv/client.h>
0012 #include <mpv/render_gl.h>
0013 #include "qthelper.h"
0014 //#include "playlistmodel.h"
0015 #include "tracksmodel.h"
0016 
0017 #include <QMediaPlayer>
0018 
0019 class MpvRenderer;
0020 class Track;
0021 
0022 class MpvObject : public QQuickFramebufferObject
0023 {
0024     Q_OBJECT
0025     Q_PROPERTY(TracksModel* audioTracksModel READ audioTracksModel NOTIFY audioTracksModelChanged)
0026     Q_PROPERTY(TracksModel* subtitleTracksModel READ subtitleTracksModel NOTIFY subtitleTracksModelChanged)
0027 
0028     Q_PROPERTY(QUrl source
0029                READ source
0030                WRITE setSource
0031                NOTIFY sourceChanged)
0032 
0033     Q_PROPERTY(bool autoPlay
0034                READ autoPlay
0035                WRITE setAutoPlay
0036                NOTIFY autoPlayChanged)
0037 
0038     Q_PROPERTY(QMediaPlayer::State playbackState
0039                READ getPlaybackState
0040                NOTIFY playbackStateChanged)
0041 
0042     Q_PROPERTY(QMediaPlayer::MediaStatus status
0043                READ getStatus
0044                NOTIFY statusChanged)
0045 
0046     Q_PROPERTY(bool hardwareDecoding
0047                READ hardwareDecoding
0048                WRITE setHardwareDecoding
0049                NOTIFY hardwareDecodingChanged)
0050 
0051     Q_PROPERTY(QString mediaTitle
0052                READ mediaTitle
0053                NOTIFY mediaTitleChanged)
0054 
0055     Q_PROPERTY(double position
0056                READ position
0057                WRITE setPosition
0058                NOTIFY positionChanged)
0059 
0060     Q_PROPERTY(double duration
0061                READ duration
0062                NOTIFY durationChanged)
0063 
0064     Q_PROPERTY(double remaining
0065                READ remaining
0066                NOTIFY remainingChanged)
0067 
0068     Q_PROPERTY(int volume
0069                READ volume
0070                WRITE setVolume
0071                NOTIFY volumeChanged)
0072 
0073     Q_PROPERTY(int chapter
0074                READ chapter
0075                WRITE setChapter
0076                NOTIFY chapterChanged)
0077 
0078     Q_PROPERTY(int audioId
0079                READ audioId
0080                WRITE setAudioId
0081                NOTIFY audioIdChanged)
0082 
0083     Q_PROPERTY(int subtitleId
0084                READ subtitleId
0085                WRITE setSubtitleId
0086                NOTIFY subtitleIdChanged)
0087 
0088     Q_PROPERTY(int secondarySubtitleId
0089                READ secondarySubtitleId
0090                WRITE setSecondarySubtitleId
0091                NOTIFY secondarySubtitleIdChanged)
0092 
0093     Q_PROPERTY(int contrast
0094                READ contrast
0095                WRITE setContrast
0096                NOTIFY contrastChanged)
0097 
0098     Q_PROPERTY(int brightness
0099                READ brightness
0100                WRITE setBrightness
0101                NOTIFY brightnessChanged)
0102 
0103     Q_PROPERTY(int gamma
0104                READ gamma
0105                WRITE setGamma
0106                NOTIFY gammaChanged)
0107 
0108     Q_PROPERTY(int saturation
0109                READ saturation
0110                WRITE setSaturation
0111                NOTIFY saturationChanged)
0112 
0113     Q_PROPERTY(double watchPercentage
0114                MEMBER m_watchPercentage
0115                READ watchPercentage
0116                WRITE setWatchPercentage
0117                NOTIFY watchPercentageChanged)
0118 
0119     Q_PROPERTY(bool hwDecoding
0120                READ hwDecoding
0121                WRITE setHWDecoding
0122                NOTIFY hwDecodingChanged)
0123 
0124 //    Q_PROPERTY(PlayListModel* playlistModel
0125 //               READ playlistModel
0126 //               WRITE setPlaylistModel
0127 //               NOTIFY playlistModelChanged)
0128 
0129 //    PlayListModel *playlistModel();
0130 //    void setPlaylistModel(PlayListModel *model);
0131 
0132     QString mediaTitle();
0133 
0134     double position();
0135     void setPosition(double value);
0136 
0137     double remaining();
0138     double duration();
0139 
0140     int volume();
0141     void setVolume(int value);
0142 
0143     int chapter();
0144     void setChapter(int value);
0145 
0146     int audioId();
0147     void setAudioId(int value);
0148 
0149     int subtitleId();
0150     void setSubtitleId(int value);
0151 
0152     int secondarySubtitleId();
0153     void setSecondarySubtitleId(int value);
0154 
0155     int contrast();
0156     void setContrast(int value);
0157 
0158     int brightness();
0159     void setBrightness(int value);
0160 
0161     int gamma();
0162     void setGamma(int value);
0163 
0164     int saturation();
0165     void setSaturation(int value);
0166 
0167     double watchPercentage();
0168     void setWatchPercentage(double value);
0169 
0170     bool hwDecoding();
0171     void setHWDecoding(bool value);
0172 
0173     mpv_handle *mpv;
0174     mpv_render_context *mpv_gl;
0175 
0176     friend class MpvRenderer;
0177 
0178 public:
0179     MpvObject(QQuickItem * parent = 0);
0180     virtual ~MpvObject();
0181     virtual Renderer *createRenderer() const override;
0182 
0183     Q_INVOKABLE QVariant getProperty(const QString &name);
0184     Q_INVOKABLE QVariant command(const QVariant &params);
0185 
0186     QUrl source() const;
0187 
0188     bool autoPlay() const;
0189 
0190     QMediaPlayer::State getPlaybackState() const;
0191 
0192     QMediaPlayer::MediaStatus getStatus() const;
0193 
0194     bool hardwareDecoding() const;
0195 
0196 public Q_SLOTS:
0197     static void mpvEvents(void *ctx);
0198     void eventHandler();
0199     int setProperty(const QString &name, const QVariant &value);
0200 
0201     void setSource(QUrl url);
0202 
0203     void setAutoPlay(bool autoPlay);
0204 
0205     void play();
0206     void stop();
0207     void pause();
0208 
0209     void seek(const double &value);
0210 
0211     void setHardwareDecoding(bool hardwareDecoding);
0212 
0213 Q_SIGNALS:
0214     void mediaTitleChanged();
0215     void positionChanged();
0216     void durationChanged();
0217     void remainingChanged();
0218     void volumeChanged();
0219 
0220     void paused();
0221     void playing();
0222     void stopped();
0223     void error(QString error);
0224 
0225     void chapterChanged();
0226     void audioIdChanged();
0227     void subtitleIdChanged();
0228     void secondarySubtitleIdChanged();
0229     void contrastChanged();
0230     void brightnessChanged();
0231     void gammaChanged();
0232     void saturationChanged();
0233     void fileLoaded();
0234     void endOfFile();
0235     void watchPercentageChanged();
0236     void ready();
0237     void audioTracksModelChanged();
0238     void subtitleTracksModelChanged();
0239     void hwDecodingChanged();
0240 //    void playlistModelChanged();
0241 //    void youtubePlaylistLoaded();
0242 
0243     void sourceChanged(QUrl url);
0244 
0245     void autoPlayChanged(bool autoPlay);
0246     
0247     void playbackStateChanged(QMediaPlayer::State playbackState);
0248 
0249     void statusChanged(QMediaPlayer::MediaStatus  status);
0250 
0251     void hardwareDecodingChanged(bool hardwareDecoding);
0252 
0253 private:
0254     TracksModel *audioTracksModel() const;
0255     TracksModel *subtitleTracksModel() const;
0256     TracksModel *m_audioTracksModel;
0257     TracksModel *m_subtitleTracksModel;
0258     QMap<int, Track*> m_subtitleTracks;
0259     QMap<int, Track*> m_audioTracks;
0260     QList<int> m_secondsWatched;
0261     double m_watchPercentage;
0262 //    PlayListModel *m_playlistModel;
0263     QString m_file;
0264 
0265     void loadTracks();
0266     void playUrl();
0267     void setPlaybackState(const QMediaPlayer::State &state);
0268     void setStatus(const QMediaPlayer::MediaStatus  &status);
0269 
0270     QUrl m_source;
0271     bool m_autoPlay;
0272     QMediaPlayer::State m_playbackState;
0273     QMediaPlayer::MediaStatus  m_status = QMediaPlayer::NoMedia;
0274     bool m_hardwareDecoding = true;
0275 };
0276 
0277 class MpvRenderer : public QQuickFramebufferObject::Renderer
0278 {
0279 public:
0280     MpvRenderer(MpvObject *new_obj);
0281     ~MpvRenderer() = default;
0282 
0283     MpvObject *obj;
0284 
0285     // This function is called when a new FBO is needed.
0286     // This happens on the initial frame.
0287     QOpenGLFramebufferObject * createFramebufferObject(const QSize &size) override final;
0288 
0289     void render() override final;
0290 };