Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qffmpegplaybackengine_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 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#ifndef QFFMPEGPLAYBACKENGINE_P_H
4#define QFFMPEGPLAYBACKENGINE_P_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17/* Playback engine design description.
18 *
19 *
20 * PLAYBACK ENGINE OBJECTS
21 *
22 * - Playback engine manages 7 objects inside, each one works in a separate thread.
23 * Each object inherits PlaybackEngineObject. The objects are:
24 * Demuxer
25 * Stream Decoders: audio, video, subtitles
26 * Renderers: audio, video, subtitles
27 *
28 *
29 * THREADS:
30 *
31 * - By default, each object works in a separate thread. It's easy to reconfigure
32 * to using several objects in thread.
33 * - New thread is allocated if a new object is created and the engine doesn't
34 * have free threads. If it does, the thread is to be reused.
35 * - If all objects for some thread are deleted, the thread becomes free and the engine
36 * postpones its termination.
37 *
38 * OBJECTS WEAK CONNECTIVITY
39 *
40 * - The objects know nothing about others and about PlaybackEngine.
41 * For any interractions the objects use slots/signals.
42 *
43 * - PlaybackEngine knows the objects object and is able to create/delete them and
44 * call their public methods.
45 *
46 */
47
53
54#include <unordered_map>
55
57
58class QAudioSink;
59class QVideoSink;
60class QAudioOutput;
62
63namespace QFFmpeg
64{
65
67{
69public:
71
72 ~PlaybackEngine() override;
73
74 bool setMedia(const QUrl &media, QIODevice *stream);
75
77
79
81
83
84 void play() {
86 }
87 void pause() {
89 }
90 void stop() {
92 }
93
94 void seek(qint64 pos);
95
96 void setLoops(int loopsCount);
97
98 void setPlaybackRate(float rate);
99
100 float playbackRate() const;
101
103
105
106 qint64 currentPosition(bool topPos = true) const;
107
108signals:
110 void errorOccured(int, const QString &);
112
113protected: // objects managing
115 {
116 void operator()(PlaybackEngineObject *) const;
117
119 };
120
121 template<typename T>
122 using ObjectPtr = std::unique_ptr<T, ObjectDeleter>;
123
126
127 template<typename T, typename... Args>
129
131
133
134 void updateActiveVideoOutput(QVideoSink *sink, bool cleanOutput = false);
135
136private:
137 void createStreamAndRenderer(QPlatformMediaPlayer::TrackType trackType);
138
139 void createDemuxer();
140
141 void registerObject(PlaybackEngineObject &object);
142
143 template<typename C, typename Action>
144 void forEachExistingObject(Action &&action);
145
146 template<typename Action>
147 void forEachExistingObject(Action &&action);
148
149 void forceUpdate();
150
151 void recreateObjects();
152
153 void createObjectsIfNeeded();
154
155 void updateObjectsPausedState();
156
157 void deleteFreeThreads();
158
159 void onRendererSynchronized(quint64 id, std::chrono::steady_clock::time_point time,
160 qint64 trackTime);
161
162 void onRendererFinished();
163
164 void onRendererLoopChanged(quint64 id, qint64 offset, int loopIndex);
165
166 void triggerStepIfNeeded();
167
168 static QString objectThreadName(const PlaybackEngineObject &object);
169
170 std::optional<Codec> codecForTrack(QPlatformMediaPlayer::TrackType trackType);
171
172 bool hasMediaStream() const;
173
174 void finilizeTime(qint64 pos);
175
176 void finalizeOutputs();
177
178 bool hasRenderer(quint64 id) const;
179
180private:
181 TimeController m_timeController;
182
183 std::unordered_map<QString, std::unique_ptr<QThread>> m_threads;
184 bool m_threadsDirty = false;
185
186 QPointer<QVideoSink> m_videoSink;
187 QPointer<QAudioOutput> m_audioOutput;
188
190
191 ObjectPtr<Demuxer> m_demuxer;
192 std::array<StreamPtr, QPlatformMediaPlayer::NTrackTypes> m_streams;
193 std::array<RendererPtr, QPlatformMediaPlayer::NTrackTypes> m_renderers;
194
195 std::array<std::optional<Codec>, QPlatformMediaPlayer::NTrackTypes> m_codecs;
196 int m_loops = QMediaPlayer::Once;
197 LoopOffset m_currentLoopOffset;
198};
199
200template<typename T, typename... Args>
202{
203 auto result = ObjectPtr<T>(new T(std::forward<Args>(args)...), { this });
204 registerObject(*result);
205 return result;
206}
207}
208
210
211#endif // QFFMPEGPLAYBACKENGINE_P_H
\qmltype AudioOutput \instantiates QAudioOutput
The QAudioSink class provides an interface for sending audio data to an audio output device.
Definition qaudiosink.h:24
int activeTrack(QPlatformMediaPlayer::TrackType type) const
void setVideoSink(QVideoSink *sink)
void setState(QMediaPlayer::PlaybackState state)
virtual RendererPtr createRenderer(QPlatformMediaPlayer::TrackType trackType)
ObjectPtr< T > createPlaybackEngineObject(Args &&...args)
void setAudioSink(QAudioOutput *output)
qint64 currentPosition(bool topPos=true) const
void errorOccured(int, const QString &)
ObjectPtr< Renderer > RendererPtr
bool setMedia(const QUrl &media, QIODevice *stream)
std::unique_ptr< T, ObjectDeleter > ObjectPtr
ObjectPtr< StreamDecoder > StreamPtr
void setActiveTrack(QPlatformMediaPlayer::TrackType type, int streamNumber)
void updateActiveAudioOutput(QAudioOutput *output)
void updateActiveVideoOutput(QVideoSink *sink, bool cleanOutput=false)
\inmodule QtCore \reentrant
Definition qiodevice.h:34
PlaybackState
Defines the current state of a media player.
\inmodule QtCore
Definition qobject.h:90
\inmodule QtCore
Definition qpointer.h:18
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qurl.h:94
The QVideoSink class represents a generic sink for video data.
Definition qvideosink.h:22
else opt state
[0]
Combined button and popup list for selecting options.
EGLStreamKHR stream
GLenum type
GLenum GLuint GLintptr offset
GLuint GLenum * rate
GLsizei GLenum GLboolean sink
GLuint64EXT * result
[6]
#define Q_OBJECT
#define signals
unsigned long long quint64
Definition qtypes.h:56
long long qint64
Definition qtypes.h:55
QT_BEGIN_NAMESPACE typedef uchar * output
QJSValueList args
void operator()(PlaybackEngineObject *) const