| 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 | #ifndef QFFMPEGMEDIADATAHOLDER_P_H | 
| 5 | #define QFFMPEGMEDIADATAHOLDER_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 "qmediametadata.h" | 
| 19 | #include <QtFFmpegMediaPluginImpl/private/qffmpegtime_p.h> | 
| 20 | #include <QtMultimedia/private/qplatformmediaplayer_p.h> | 
| 21 | #include <QtFFmpegMediaPluginImpl/private/qffmpeg_p.h> | 
| 22 | #include "qvideoframe.h" | 
| 23 | #include <QtMultimedia/private/qmultimediautils_p.h> | 
| 24 |  | 
| 25 | #include <array> | 
| 26 | #include <optional> | 
| 27 |  | 
| 28 | QT_BEGIN_NAMESPACE | 
| 29 |  | 
| 30 | namespace QFFmpeg { | 
| 31 |  | 
| 32 | struct ICancelToken | 
| 33 | { | 
| 34 |     virtual ~ICancelToken() = default; | 
| 35 |     virtual bool isCancelled() const = 0; | 
| 36 | }; | 
| 37 |  | 
| 38 | using AVFormatContextUPtr = std::unique_ptr<AVFormatContext, AVDeleter<decltype(&avformat_close_input), &avformat_close_input>>; | 
| 39 |  | 
| 40 | class MediaDataHolder | 
| 41 | { | 
| 42 | public: | 
| 43 |     struct StreamInfo | 
| 44 |     { | 
| 45 |         int avStreamIndex = -1; | 
| 46 |         bool isDefault = false; | 
| 47 |         QMediaMetaData metaData; | 
| 48 |     }; | 
| 49 |  | 
| 50 |     struct ContextError | 
| 51 |     { | 
| 52 |         int code = 0; | 
| 53 |         QString description; | 
| 54 |     }; | 
| 55 |  | 
| 56 |     using StreamsMap = std::array<QList<StreamInfo>, QPlatformMediaPlayer::NTrackTypes>; | 
| 57 |     using StreamIndexes = std::array<int, QPlatformMediaPlayer::NTrackTypes>; | 
| 58 |  | 
| 59 |     MediaDataHolder() = default; | 
| 60 |     MediaDataHolder(AVFormatContextUPtr context, const std::shared_ptr<ICancelToken> &cancelToken); | 
| 61 |  | 
| 62 |     static QPlatformMediaPlayer::TrackType trackTypeFromMediaType(int mediaType); | 
| 63 |  | 
| 64 |     int activeTrack(QPlatformMediaPlayer::TrackType type) const; | 
| 65 |  | 
| 66 |     const QList<StreamInfo> &streamInfo(QPlatformMediaPlayer::TrackType trackType) const; | 
| 67 |  | 
| 68 |     TrackDuration duration() const { return m_duration; } | 
| 69 |  | 
| 70 |     const QMediaMetaData &metaData() const { return m_metaData; } | 
| 71 |  | 
| 72 |     bool isSeekable() const { return m_isSeekable; } | 
| 73 |  | 
| 74 |     VideoTransformation transformation() const; | 
| 75 |  | 
| 76 |     AVFormatContext *avContext(); | 
| 77 |  | 
| 78 |     int currentStreamIndex(QPlatformMediaPlayer::TrackType trackType) const; | 
| 79 |  | 
| 80 |     using Maybe = QMaybe<QSharedPointer<MediaDataHolder>, ContextError>; | 
| 81 |     static Maybe create(const QUrl &url, QIODevice *stream, | 
| 82 |                         const std::shared_ptr<ICancelToken> &cancelToken); | 
| 83 |  | 
| 84 |     bool setActiveTrack(QPlatformMediaPlayer::TrackType type, int streamNumber); | 
| 85 |  | 
| 86 | private: | 
| 87 |     void updateMetaData(); | 
| 88 |  | 
| 89 |     std::shared_ptr<ICancelToken> m_cancelToken; // NOTE: Cancel token may be accessed by | 
| 90 |                                                  // AVFormatContext during destruction and | 
| 91 |                                                  // must outlive the context object | 
| 92 |     AVFormatContextUPtr m_context; | 
| 93 |  | 
| 94 |     bool m_isSeekable = false; | 
| 95 |  | 
| 96 |     StreamIndexes m_currentAVStreamIndex = { -1, -1, -1 }; | 
| 97 |     StreamsMap m_streamMap; | 
| 98 |     StreamIndexes m_requestedStreams = { -1, -1, -1 }; | 
| 99 |     TrackDuration m_duration = TrackDuration(0); | 
| 100 |     QMediaMetaData m_metaData; | 
| 101 |     std::optional<QImage> m_cachedThumbnail; | 
| 102 | }; | 
| 103 |  | 
| 104 | } // namespace QFFmpeg | 
| 105 |  | 
| 106 | QT_END_NAMESPACE | 
| 107 |  | 
| 108 | #endif // QFFMPEGMEDIADATAHOLDER_P_H | 
| 109 |  |