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 | |
4 | // |
5 | // W A R N I N G |
6 | // ------------- |
7 | // |
8 | // This file is not part of the Qt API. It exists purely as an |
9 | // implementation detail. This header file may change from version to |
10 | // version without notice, or even be removed. |
11 | // |
12 | // We mean it. |
13 | // |
14 | |
15 | #ifndef QFFMPEGMEDIAPLAYER_H |
16 | #define QFFMPEGMEDIAPLAYER_H |
17 | |
18 | #include <private/qplatformmediaplayer_p.h> |
19 | #include <qmediametadata.h> |
20 | #include <qtimer.h> |
21 | #include <qpointer.h> |
22 | #include <qfuture.h> |
23 | #include "qffmpeg_p.h" |
24 | #include "playbackengine/qffmpegmediadataholder_p.h" |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | namespace QFFmpeg { |
29 | class CancelToken; |
30 | |
31 | class PlaybackEngine; |
32 | } |
33 | |
34 | class QPlatformAudioOutput; |
35 | |
36 | class QFFmpegMediaPlayer : public QObject, public QPlatformMediaPlayer |
37 | { |
38 | Q_OBJECT |
39 | public: |
40 | QFFmpegMediaPlayer(QMediaPlayer *player); |
41 | ~QFFmpegMediaPlayer(); |
42 | |
43 | qint64 duration() const override; |
44 | |
45 | void setPosition(qint64 position) override; |
46 | |
47 | float bufferProgress() const override; |
48 | |
49 | QMediaTimeRange availablePlaybackRanges() const override; |
50 | |
51 | qreal playbackRate() const override; |
52 | void setPlaybackRate(qreal rate) override; |
53 | |
54 | QUrl media() const override; |
55 | const QIODevice *mediaStream() const override; |
56 | void setMedia(const QUrl &media, QIODevice *stream) override; |
57 | |
58 | void play() override; |
59 | void pause() override; |
60 | void stop() override; |
61 | |
62 | void setAudioOutput(QPlatformAudioOutput *) override; |
63 | |
64 | void setAudioBufferOutput(QAudioBufferOutput *) override; |
65 | |
66 | QMediaMetaData metaData() const override; |
67 | |
68 | void setVideoSink(QVideoSink *sink) override; |
69 | QVideoSink *videoSink() const; |
70 | |
71 | int trackCount(TrackType) override; |
72 | QMediaMetaData trackMetaData(TrackType type, int streamNumber) override; |
73 | int activeTrack(TrackType) override; |
74 | void setActiveTrack(TrackType, int streamNumber) override; |
75 | void setLoops(int loops) override; |
76 | |
77 | private: |
78 | void runPlayback(); |
79 | void handleIncorrectMedia(QMediaPlayer::MediaStatus status); |
80 | void setMediaAsync(QFFmpeg::MediaDataHolder::Maybe mediaDataHolder, |
81 | const std::shared_ptr<QFFmpeg::CancelToken> &cancelToken); |
82 | |
83 | void mediaStatusChanged(QMediaPlayer::MediaStatus); |
84 | |
85 | private slots: |
86 | void updatePosition(); |
87 | void endOfStream(); |
88 | void error(int error, const QString &errorString) |
89 | { |
90 | QPlatformMediaPlayer::error(error, errorString); |
91 | } |
92 | void onLoopChanged(); |
93 | void onBuffered(); |
94 | |
95 | private: |
96 | QTimer m_positionUpdateTimer; |
97 | QMediaPlayer::PlaybackState m_requestedStatus = QMediaPlayer::StoppedState; |
98 | |
99 | using PlaybackEngine = QFFmpeg::PlaybackEngine; |
100 | |
101 | std::unique_ptr<PlaybackEngine> m_playbackEngine; |
102 | QPlatformAudioOutput *m_audioOutput = nullptr; |
103 | QPointer<QAudioBufferOutput> m_audioBufferOutput; |
104 | QPointer<QVideoSink> m_videoSink; |
105 | |
106 | QUrl m_url; |
107 | QPointer<QIODevice> m_device; |
108 | float m_playbackRate = 1.; |
109 | float m_bufferProgress = 0.f; |
110 | QFuture<void> m_loadMedia; |
111 | std::shared_ptr<QFFmpeg::CancelToken> m_cancelToken; // For interrupting ongoing |
112 | // network connection attempt |
113 | }; |
114 | |
115 | QT_END_NAMESPACE |
116 | |
117 | |
118 | #endif // QMEDIAPLAYERCONTROL_H |
119 | |
120 |