PlayerComponent.h 6.0 KB

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