| 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 QMEDIAPLAYERCONTROL_H | 
| 16 | #define QMEDIAPLAYERCONTROL_H | 
| 17 | |
| 18 | #include <QtMultimedia/qmediaplayer.h> | 
| 19 | #include <QtMultimedia/qmediatimerange.h> | 
| 20 | #include <QtMultimedia/qaudiodevice.h> | 
| 21 | #include <QtMultimedia/qmediametadata.h> | 
| 22 | |
| 23 | #include <QtCore/qpair.h> | 
| 24 | #include <QtCore/private/qglobal_p.h> | 
| 25 | #include <QtCore/qobject.h> | 
| 26 | |
| 27 | QT_BEGIN_NAMESPACE | 
| 28 | |
| 29 | class QMediaStreamsControl; | 
| 30 | class QPlatformAudioOutput; | 
| 31 | |
| 32 | class Q_MULTIMEDIA_EXPORT QPlatformMediaPlayer | 
| 33 | { | 
| 34 | public: | 
| 35 | virtual ~QPlatformMediaPlayer(); | 
| 36 | virtual QMediaPlayer::PlaybackState state() const { return m_state; } | 
| 37 | virtual QMediaPlayer::MediaStatus mediaStatus() const { return m_status; }; | 
| 38 | |
| 39 | virtual qint64 duration() const = 0; | 
| 40 | |
| 41 | virtual qint64 position() const { return m_position; } | 
| 42 | virtual void setPosition(qint64 position) = 0; | 
| 43 | |
| 44 | virtual float bufferProgress() const = 0; | 
| 45 | |
| 46 | virtual bool isAudioAvailable() const { return m_audioAvailable; } | 
| 47 | virtual bool isVideoAvailable() const { return m_videoAvailable; } | 
| 48 | |
| 49 | virtual bool isSeekable() const { return m_seekable; } | 
| 50 | |
| 51 | virtual QMediaTimeRange availablePlaybackRanges() const = 0; | 
| 52 | |
| 53 | virtual qreal playbackRate() const = 0; | 
| 54 | virtual void setPlaybackRate(qreal rate) = 0; | 
| 55 | |
| 56 | virtual QUrl media() const = 0; | 
| 57 | virtual const QIODevice *mediaStream() const = 0; | 
| 58 | virtual void setMedia(const QUrl &media, QIODevice *stream) = 0; | 
| 59 | |
| 60 | virtual void play() = 0; | 
| 61 | virtual void pause() = 0; | 
| 62 | virtual void stop() = 0; | 
| 63 | |
| 64 | virtual bool streamPlaybackSupported() const { return false; } | 
| 65 | |
| 66 | virtual void setAudioOutput(QPlatformAudioOutput *) {} | 
| 67 | |
| 68 | virtual void setAudioBufferOutput(QAudioBufferOutput *) { } | 
| 69 | |
| 70 | virtual QMediaMetaData metaData() const { return {}; } | 
| 71 | |
| 72 | virtual void setVideoSink(QVideoSink * /*sink*/) = 0; | 
| 73 | |
| 74 | virtual bool canPlayQrc() const { return false; } | 
| 75 | |
| 76 | // media streams | 
| 77 | enum TrackType : uint8_t { VideoStream, AudioStream, SubtitleStream, NTrackTypes }; | 
| 78 | |
| 79 | virtual int trackCount(TrackType) { return 0; }; | 
| 80 | virtual QMediaMetaData trackMetaData(TrackType /*type*/, int /*streamNumber*/) { return QMediaMetaData(); } | 
| 81 | virtual int activeTrack(TrackType) { return -1; } | 
| 82 | virtual void setActiveTrack(TrackType, int /*streamNumber*/) {} | 
| 83 | |
| 84 | void durationChanged(std::chrono::milliseconds ms) { durationChanged(duration: ms.count()); } | 
| 85 | void durationChanged(qint64 duration) { emit player->durationChanged(duration); } | 
| 86 | void positionChanged(std::chrono::milliseconds ms) { positionChanged(position: ms.count()); } | 
| 87 | void positionChanged(qint64 position) { | 
| 88 | if (m_position == position) | 
| 89 | return; | 
| 90 | m_position = position; | 
| 91 | emit player->positionChanged(position); | 
| 92 | } | 
| 93 | void audioAvailableChanged(bool audioAvailable) { | 
| 94 | if (m_audioAvailable == audioAvailable) | 
| 95 | return; | 
| 96 | m_audioAvailable = audioAvailable; | 
| 97 | emit player->hasAudioChanged(available: audioAvailable); | 
| 98 | } | 
| 99 | void videoAvailableChanged(bool videoAvailable) { | 
| 100 | if (m_videoAvailable == videoAvailable) | 
| 101 | return; | 
| 102 | m_videoAvailable = videoAvailable; | 
| 103 | emit player->hasVideoChanged(videoAvailable); | 
| 104 | } | 
| 105 | void seekableChanged(bool seekable) { | 
| 106 | if (m_seekable == seekable) | 
| 107 | return; | 
| 108 | m_seekable = seekable; | 
| 109 | emit player->seekableChanged(seekable); | 
| 110 | } | 
| 111 | void playbackRateChanged(qreal rate) { emit player->playbackRateChanged(rate); } | 
| 112 | void bufferProgressChanged(float progress) { emit player->bufferProgressChanged(progress); } | 
| 113 | void metaDataChanged() { emit player->metaDataChanged(); } | 
| 114 | void tracksChanged() { emit player->tracksChanged(); } | 
| 115 | void activeTracksChanged() { emit player->activeTracksChanged(); } | 
| 116 | |
| 117 | void stateChanged(QMediaPlayer::PlaybackState newState); | 
| 118 | void mediaStatusChanged(QMediaPlayer::MediaStatus status); | 
| 119 | void error(int error, const QString &errorString); | 
| 120 | |
| 121 | void resetCurrentLoop() { m_currentLoop = 0; } | 
| 122 | bool doLoop() { | 
| 123 | return isSeekable() && (m_loops < 0 || ++m_currentLoop < m_loops); | 
| 124 | } | 
| 125 | int loops() const { return m_loops; } | 
| 126 | virtual void setLoops(int loops) | 
| 127 | { | 
| 128 | if (m_loops == loops) | 
| 129 | return; | 
| 130 | m_loops = loops; | 
| 131 | Q_EMIT player->loopsChanged(); | 
| 132 | } | 
| 133 | |
| 134 | protected: | 
| 135 | explicit QPlatformMediaPlayer(QMediaPlayer *parent = nullptr); | 
| 136 | |
| 137 | private: | 
| 138 | QMediaPlayer *player = nullptr; | 
| 139 | QMediaPlayer::MediaStatus m_status = QMediaPlayer::NoMedia; | 
| 140 | QMediaPlayer::PlaybackState m_state = QMediaPlayer::StoppedState; | 
| 141 | bool m_seekable = false; | 
| 142 | bool m_videoAvailable = false; | 
| 143 | bool m_audioAvailable = false; | 
| 144 | int m_loops = 1; | 
| 145 | int m_currentLoop = 0; | 
| 146 | qint64 m_position = 0; | 
| 147 | }; | 
| 148 | |
| 149 | #ifndef QT_NO_DEBUG_STREAM | 
| 150 | inline QDebug operator<<(QDebug dbg, QPlatformMediaPlayer::TrackType type) | 
| 151 | { | 
| 152 | QDebugStateSaver save(dbg); | 
| 153 | dbg.nospace(); | 
| 154 | |
| 155 | switch (type) { | 
| 156 | case QPlatformMediaPlayer::TrackType::AudioStream: | 
| 157 | return dbg << "AudioStream"; | 
| 158 | case QPlatformMediaPlayer::TrackType::VideoStream: | 
| 159 | return dbg << "VideoStream"; | 
| 160 | case QPlatformMediaPlayer::TrackType::SubtitleStream: | 
| 161 | return dbg << "SubtitleStream"; | 
| 162 | default: | 
| 163 | Q_UNREACHABLE_RETURN(dbg); | 
| 164 | } | 
| 165 | } | 
| 166 | #endif | 
| 167 | |
| 168 | QT_END_NAMESPACE | 
| 169 | |
| 170 | |
| 171 | #endif // QMEDIAPLAYERCONTROL_H | 
| 172 | |
| 173 | 
