PlayerComponent.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef PLAYERCOMPONENT_H
  2. #define PLAYERCOMPONENT_H
  3. #include <QObject>
  4. #include <QtCore/qglobal.h>
  5. #include <QVariant>
  6. #include <QSet>
  7. #include <QQuickWindow>
  8. #include <QTimer>
  9. #include <QTextStream>
  10. #include "ComponentManager.h"
  11. #include <mpv/client.h>
  12. #include <mpv/qthelper.hpp>
  13. void initD3DDevice(void);
  14. ///////////////////////////////////////////////////////////////////////////////////////////////////
  15. class PlayerComponent : public ComponentBase
  16. {
  17. Q_OBJECT
  18. DEFINE_SINGLETON(PlayerComponent);
  19. public:
  20. virtual const char* componentName() { return "player"; }
  21. virtual bool componentExport() { return true; }
  22. virtual bool componentInitialize();
  23. virtual void componentPostInitialize();
  24. explicit PlayerComponent(QObject* parent = 0);
  25. virtual ~PlayerComponent();
  26. // Deprecated. Corresponds to stop() + queueMedia().
  27. Q_INVOKABLE bool load(const QString& url, const QVariantMap& options, const QVariantMap& metadata, const QString& audioStream = QString(), const QString& subtitleStream = QString());
  28. // Append a media item to the internal playlist. If nothing is played yet, the
  29. // newly appended item will start playing immediately.
  30. // options:
  31. // - startMilliseconds: start playback at this time (in ms)
  32. // - autoplay: if false, start playback paused; if true, start normally
  33. Q_INVOKABLE void queueMedia(const QString& url, const QVariantMap& options, const QVariantMap &metadata, const QString& audioStream, const QString& subtitleStream);
  34. // This clears all items queued with queueMedia().
  35. // It explicitly excludes the currently playing item. The main use of this function
  36. // is updating the next item that should be played (for the purpose of gapless audio).
  37. // If you want to wipe everything, use stop().
  38. Q_INVOKABLE void clearQueue();
  39. Q_INVOKABLE virtual void seekTo(qint64 milliseconds);
  40. // Stop playback and clear all queued items.
  41. Q_INVOKABLE virtual void stop();
  42. // A full reload of the stream is imminent (stop() + load())
  43. // Used ofr not resetting display mode with the next stop() call.
  44. Q_INVOKABLE virtual void streamSwitch();
  45. Q_INVOKABLE virtual void pause();
  46. Q_INVOKABLE virtual void play();
  47. /* 0-100 volume 0=mute and 100=normal */
  48. Q_INVOKABLE virtual void setVolume(quint8 volume);
  49. Q_INVOKABLE virtual quint8 volume();
  50. // Returns a QVariant of the following format:
  51. // QVariantList (list of audio device entries)
  52. // QVariantMap (an audio device entry)
  53. // "name" -> QString (symbolic name/ID of the device)
  54. // "description" -> QString (human readable description intended for display)
  55. //
  56. Q_INVOKABLE virtual QVariant getAudioDeviceList();
  57. // Uses the "name" from the device list.
  58. Q_INVOKABLE virtual void setAudioDevice(const QString& name);
  59. Q_INVOKABLE virtual void setAudioStream(const QString& audioStream);
  60. Q_INVOKABLE virtual void setSubtitleStream(const QString& subtitleStream);
  61. Q_INVOKABLE virtual void setAudioDelay(qint64 milliseconds);
  62. Q_INVOKABLE virtual void setSubtitleDelay(qint64 milliseconds);
  63. // If enabled, hide the web view (whether it's OSD or not), and show video
  64. // only. If no video is running, render a black background only.
  65. Q_INVOKABLE virtual void setVideoOnlyMode(bool enable);
  66. Q_INVOKABLE void userCommand(QString command);
  67. const mpv::qt::Handle getMpvHandle() const { return m_mpv; }
  68. virtual void setWindow(QQuickWindow* window);
  69. QString videoInformation() const;
  70. static QStringList AudioCodecsAll() { return { "ac3", "dts", "eac3", "dts-hd", "truehd" }; };
  71. static QStringList AudioCodecsSPDIF() { return { "ac3", "dts" }; };
  72. public Q_SLOTS:
  73. void setAudioConfiguration();
  74. void updateAudioDeviceList();
  75. void updateSubtitleSettings();
  76. void updateVideoSettings();
  77. private Q_SLOTS:
  78. void handleMpvEvents();
  79. void onRestoreDisplay();
  80. void onRefreshRateChange();
  81. void onReloadAudio();
  82. Q_SIGNALS:
  83. void playing(const QString& url);
  84. void buffering(float);
  85. // playback has stopped due to a stop() or loadMedia() request
  86. void stopped(const QString& url);
  87. // playback has stopped because the current media was fully played
  88. void finished(const QString& url);
  89. // playback has stopped due to any reason - this always happens if the
  90. // playing() signal was emitted
  91. void playbackEnded(const QString& url);
  92. // emitted if playback has ended, and no more items are queued for playback
  93. void playbackAllDone();
  94. // emitted after playing(), and as soon as the the media is fully loaded, and
  95. // playback starts normally
  96. void playbackStarting();
  97. void paused(bool paused);
  98. void windowVisible(bool visible);
  99. // emitted as soon as the duration of the current file is known
  100. void updateDuration(qint64 milliseconds);
  101. // an error happened during playback - this implies abort of playback
  102. // the id is the (negative) error number, and the message parameter is a short
  103. // English description of the error (always the same for the same id, no
  104. // further information)
  105. void error(int id, const QString& message);
  106. // current position in ms should be triggered 2 times a second
  107. // when position updates
  108. void positionUpdate(quint64);
  109. void onMpvEvents();
  110. private:
  111. // this is the function actually implemented in the backends. the variantmap contains
  112. // a few known keys:
  113. // * subtitleStreamIndex
  114. // * subtitleStreamIdentifier
  115. // * audioStreamIndex
  116. // * audioStreamIdentifier
  117. // * viewOffset
  118. //
  119. void loadWithOptions(const QVariantMap& options);
  120. void setRpiWindow(QQuickWindow* window);
  121. void setQtQuickWindow(QQuickWindow* window);
  122. void handleMpvEvent(mpv_event *event);
  123. // Potentially switch the display refresh rate, and return true if the refresh rate
  124. // was actually changed.
  125. bool switchDisplayFrameRate();
  126. void checkCurrentAudioDevice(const QSet<QString>& old_devs, const QSet<QString>& new_devs);
  127. void appendAudioFormat(QTextStream& info, const QString& property) const;
  128. mpv::qt::Handle m_mpv;
  129. double m_lastPositionUpdate;
  130. qint64 m_playbackAudioDelay;
  131. QString m_CurrentUrl;
  132. bool m_playbackStartSent;
  133. QQuickWindow* m_window;
  134. float m_mediaFrameRate;
  135. QTimer m_restoreDisplayTimer;
  136. QTimer m_reloadAudioTimer;
  137. QSet<QString> m_audioDevices;
  138. bool m_streamSwitchImminent;
  139. };
  140. #endif // PLAYERCOMPONENT_H