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>::UniqueHandle; |
46 | |
47 | public: |
48 | static QMaybe<QPlatformMediaPlayer *> create(QMediaPlayer *parent = nullptr); |
49 | ~QGstreamerMediaPlayer(); |
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 | // play |
126 | QGstPlayHandle m_gstPlay; |
127 | |
128 | QGstPipeline m_playbin; |
129 | |
130 | QGstBusObserver m_gstPlayBus; |
131 | |
132 | // metadata |
133 | QMediaMetaData m_metaData; |
134 | std::array<std::vector<QMediaMetaData>, 3> m_trackMetaData; |
135 | std::array<std::vector<QByteArray>, 3> m_trackIDs; |
136 | std::array<int, 3> m_activeTrack{}; |
137 | QList<QSize> m_nativeSize; |
138 | |
139 | void resetStateForEmptyOrInvalidMedia(); |
140 | void updateNativeSizeOnVideoOutput(); |
141 | |
142 | void seekToCurrentPosition(); |
143 | |
144 | std::optional<std::chrono::nanoseconds> m_pendingSeek; |
145 | |
146 | int stateChangeToSkip = 0; |
147 | |
148 | void updateVideoTrackEnabled(); |
149 | void updateAudioTrackEnabled(); |
150 | void updateSubtitleTrackEnabled(); |
151 | }; |
152 | |
153 | QT_END_NAMESPACE |
154 | |
155 | #endif |
156 | |