| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 3 |  | 
|---|
| 4 | #ifndef QGSTREAMERMEDIAPLAYER_P_H | 
|---|
| 5 | #define QGSTREAMERMEDIAPLAYER_P_H | 
|---|
| 6 |  | 
|---|
| 7 | // | 
|---|
| 8 | //  W A R N I N G | 
|---|
| 9 | //  ------------- | 
|---|
| 10 | // | 
|---|
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 12 | // implementation detail.  This header file may change from version to | 
|---|
| 13 | // version without notice, or even be removed. | 
|---|
| 14 | // | 
|---|
| 15 | // We mean it. | 
|---|
| 16 | // | 
|---|
| 17 |  | 
|---|
| 18 | #include <QtMultimedia/private/qplatformmediaplayer_p.h> | 
|---|
| 19 | #include <QtMultimedia/private/qtmultimediaglobal_p.h> | 
|---|
| 20 | #include <QtMultimedia/private/qmultimediautils_p.h> | 
|---|
| 21 |  | 
|---|
| 22 | #include <QtCore/qelapsedtimer.h> | 
|---|
| 23 | #include <QtCore/qtimer.h> | 
|---|
| 24 | #include <QtCore/qurl.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include <common/qgst_bus_observer_p.h> | 
|---|
| 27 | #include <common/qgst_discoverer_p.h> | 
|---|
| 28 | #include <common/qgst_p.h> | 
|---|
| 29 | #include <common/qgstpipeline_p.h> | 
|---|
| 30 |  | 
|---|
| 31 | #include <gst/play/gstplay.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include <array> | 
|---|
| 34 |  | 
|---|
| 35 | QT_BEGIN_NAMESPACE | 
|---|
| 36 |  | 
|---|
| 37 | class QGstreamerMessage; | 
|---|
| 38 | class QGstreamerAudioOutput; | 
|---|
| 39 | class QGstreamerVideoOutput; | 
|---|
| 40 |  | 
|---|
| 41 | class QGstreamerMediaPlayer : public QObject, | 
|---|
| 42 | public QPlatformMediaPlayer, | 
|---|
| 43 | public QGstreamerBusMessageFilter | 
|---|
| 44 | { | 
|---|
| 45 | using QGstPlayHandle = QGstImpl::QGstHandleHelper<GstPlay>::SharedHandle; | 
|---|
| 46 |  | 
|---|
| 47 | public: | 
|---|
| 48 | static QMaybe<QPlatformMediaPlayer *> create(QMediaPlayer *parent = nullptr); | 
|---|
| 49 | ~QGstreamerMediaPlayer() override; | 
|---|
| 50 |  | 
|---|
| 51 | qint64 duration() const override; | 
|---|
| 52 |  | 
|---|
| 53 | float bufferProgress() const override; | 
|---|
| 54 |  | 
|---|
| 55 | QMediaTimeRange availablePlaybackRanges() const override; | 
|---|
| 56 |  | 
|---|
| 57 | qreal playbackRate() const override; | 
|---|
| 58 | void setPlaybackRate(qreal rate) override; | 
|---|
| 59 |  | 
|---|
| 60 | QUrl media() const override; | 
|---|
| 61 | const QIODevice *mediaStream() const override; | 
|---|
| 62 | void setMedia(const QUrl &, QIODevice *) override; | 
|---|
| 63 |  | 
|---|
| 64 | bool streamPlaybackSupported() const override { return true; } | 
|---|
| 65 |  | 
|---|
| 66 | void setAudioOutput(QPlatformAudioOutput *output) override; | 
|---|
| 67 |  | 
|---|
| 68 | QMediaMetaData metaData() const override; | 
|---|
| 69 |  | 
|---|
| 70 | void setVideoSink(QVideoSink *sink) override; | 
|---|
| 71 |  | 
|---|
| 72 | int trackCount(TrackType) override; | 
|---|
| 73 | QMediaMetaData trackMetaData(TrackType /*type*/, int /*streamNumber*/) override; | 
|---|
| 74 | int activeTrack(TrackType) override; | 
|---|
| 75 | void setActiveTrack(TrackType, int /*streamNumber*/) override; | 
|---|
| 76 |  | 
|---|
| 77 | void setPosition(qint64 pos) override; | 
|---|
| 78 | void setPosition(std::chrono::milliseconds pos); | 
|---|
| 79 |  | 
|---|
| 80 | void play() override; | 
|---|
| 81 | void pause() override; | 
|---|
| 82 | void stop() override; | 
|---|
| 83 |  | 
|---|
| 84 | const QGstPipeline &pipeline() const; | 
|---|
| 85 |  | 
|---|
| 86 | bool canPlayQrc() const override; | 
|---|
| 87 |  | 
|---|
| 88 | private: | 
|---|
| 89 | QGstreamerMediaPlayer(QGstreamerVideoOutput *videoOutput, QMediaPlayer *parent); | 
|---|
| 90 |  | 
|---|
| 91 | static void sourceSetupCallback(GstElement *uridecodebin, GstElement *source, | 
|---|
| 92 | QGstreamerMediaPlayer *); | 
|---|
| 93 | bool hasMedia() const; | 
|---|
| 94 | bool hasValidMedia() const; | 
|---|
| 95 |  | 
|---|
| 96 | void updatePositionFromPipeline(); | 
|---|
| 97 | void updateBufferProgress(float); | 
|---|
| 98 |  | 
|---|
| 99 | QUrl m_url; | 
|---|
| 100 | QIODevice *m_stream = nullptr; | 
|---|
| 101 |  | 
|---|
| 102 | enum class ResourceErrorState : uint8_t { | 
|---|
| 103 | NoError, | 
|---|
| 104 | ErrorOccurred, | 
|---|
| 105 | ErrorReported, | 
|---|
| 106 | }; | 
|---|
| 107 |  | 
|---|
| 108 | ResourceErrorState m_resourceErrorState = ResourceErrorState::NoError; | 
|---|
| 109 | float m_bufferProgress = 0.f; | 
|---|
| 110 | std::chrono::milliseconds m_duration{}; | 
|---|
| 111 |  | 
|---|
| 112 | QGstreamerAudioOutput *gstAudioOutput = nullptr; | 
|---|
| 113 | QGstreamerVideoOutput *gstVideoOutput = nullptr; | 
|---|
| 114 |  | 
|---|
| 115 | // // Message handler | 
|---|
| 116 | bool processBusMessage(const QGstreamerMessage &message) override; | 
|---|
| 117 | bool processBusMessageApplication(const QGstreamerMessage &message); | 
|---|
| 118 |  | 
|---|
| 119 | // decoder connections | 
|---|
| 120 | void disconnectDecoderHandlers(); | 
|---|
| 121 | QGObjectHandlerScopedConnection sourceSetup; | 
|---|
| 122 |  | 
|---|
| 123 | bool discover(const QUrl &); | 
|---|
| 124 |  | 
|---|
| 125 | // custom sources | 
|---|
| 126 | void decoderPadAddedCustomSource(const QGstElement &src, const QGstPad &pad); | 
|---|
| 127 | void decoderPadRemovedCustomSource(const QGstElement &src, const QGstPad &pad); | 
|---|
| 128 | bool isCustomSource() const { return m_url.scheme() == QStringLiteral(u "gstreamer-pipeline"); } | 
|---|
| 129 | void setMediaCustomSource(const QUrl &content); | 
|---|
| 130 | void cleanupCustomPipeline(); | 
|---|
| 131 | QGstElement decoder; | 
|---|
| 132 | QGstPipeline customPipeline; | 
|---|
| 133 | QGObjectHandlerScopedConnection padAdded; | 
|---|
| 134 | QGObjectHandlerScopedConnection padRemoved; | 
|---|
| 135 | std::unique_ptr<QTimer> positionUpdateTimer; | 
|---|
| 136 | std::array<QGstElement, 3> customPipelineSinks; | 
|---|
| 137 | std::array<QGstPad, 3> customPipelinePads; | 
|---|
| 138 |  | 
|---|
| 139 | // play | 
|---|
| 140 | QGstPlayHandle m_gstPlay; | 
|---|
| 141 | QGstPipeline m_playbin; | 
|---|
| 142 | QGstBusObserver m_gstPlayBus; | 
|---|
| 143 |  | 
|---|
| 144 | // metadata | 
|---|
| 145 | QMediaMetaData m_metaData; | 
|---|
| 146 | std::array<std::vector<QMediaMetaData>, 3> m_trackMetaData; | 
|---|
| 147 | std::array<std::vector<QByteArray>, 3> m_trackIDs; | 
|---|
| 148 | std::array<int, 3> m_activeTrack{}; | 
|---|
| 149 | QList<QSize> m_nativeSize; | 
|---|
| 150 |  | 
|---|
| 151 | void resetStateForEmptyOrInvalidMedia(); | 
|---|
| 152 | void updateNativeSizeOnVideoOutput(); | 
|---|
| 153 |  | 
|---|
| 154 | void seekToCurrentPosition(); | 
|---|
| 155 |  | 
|---|
| 156 | std::optional<std::chrono::nanoseconds> m_pendingSeek; | 
|---|
| 157 |  | 
|---|
| 158 | int stateChangeToSkip = 0; | 
|---|
| 159 |  | 
|---|
| 160 | void updateVideoTrackEnabled(); | 
|---|
| 161 | void updateAudioTrackEnabled(); | 
|---|
| 162 | void updateSubtitleTrackEnabled(); | 
|---|
| 163 | }; | 
|---|
| 164 |  | 
|---|
| 165 | QT_END_NAMESPACE | 
|---|
| 166 |  | 
|---|
| 167 | #endif | 
|---|
| 168 |  | 
|---|