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