Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qandroidimageregistry_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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 QANDROIDIMAGEREGISTRY_P_H
5#define QANDROIDIMAGEREGISTRY_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 <QtMultimedia/private/qcaptureslotregistry_p.h>
19#include <QtMultimedia/private/qhwvideobuffer_p.h>
20#include <QtMultimedia/qvideoframe.h>
21#include <QtMultimedia/qvideoframeformat.h>
22
23#include <QtCore/qjniobject.h>
24#include <QtCore/qsize.h>
25#include <QtCore/qtconfigmacros.h>
26
27#include <media/NdkImageReader.h>
28
29#include <atomic>
30#include <chrono>
31#include <cstdint>
32#include <functional>
33#include <memory>
34#include <optional>
35#include <utility>
36
37struct AImage;
38struct AImageReader;
39
40QT_BEGIN_NAMESPACE
41
42namespace QFFmpeg {
43
44inline constexpr std::size_t kAndroidImageCount = 12;
45inline constexpr std::size_t kAndroidMinFreeImages = 2;
46
48{
49 void operator()(AImage *image) const;
50};
51
52using AImagePtr = std::unique_ptr<AImage, AImageDeleter>;
53
55{
56 void operator()(AImageReader *reader) const;
57};
58
59using AImageReaderPtr = std::unique_ptr<AImageReader, AImageReaderDeleter>;
60
61class QAndroidImageReader final : public std::enable_shared_from_this<QAndroidImageReader>
62{
63public:
64 using ImageSink = std::function<void(AImagePtr image, std::shared_ptr<QAndroidImageReader>)>;
65
66 static std::shared_ptr<QAndroidImageReader> create(QSize size, std::uint64_t usage);
68
69 QAndroidImageReader(const QAndroidImageReader &) = delete;
70 QAndroidImageReader &operator=(const QAndroidImageReader &) = delete;
71
72 QJniObject surface() const { return m_surface; }
73
74 void setSink(ImageSink sink);
75
76 QSize size() const { return m_size; }
77
78private:
79 QAndroidImageReader(AImageReaderPtr reader, QJniObject surface, QSize size, uintptr_t id);
80
81 static void onImageAvailable(void *context, AImageReader *reader);
82 void deliverImages();
83
84 const AImageReaderPtr m_reader;
85 const QJniObject m_surface;
86 const QSize m_size;
87 const uintptr_t m_id;
88 AImageReader_ImageListener m_listener;
89
90#ifdef __cpp_lib_atomic_shared_ptr
91 std::atomic<std::shared_ptr<const ImageSink>> m_sink;
92#else
93 std::shared_ptr<const ImageSink> m_sink;
94#endif
95};
96
98{
99public:
100 QAndroidImageSlot() = default;
101 QAndroidImageSlot(std::shared_ptr<QAndroidImageReader> reader, AImagePtr image)
103 {
104 }
105
108 QAndroidImageSlot(QAndroidImageSlot &&) noexcept = default;
109 QAndroidImageSlot &operator=(QAndroidImageSlot &&other) noexcept;
110 ~QAndroidImageSlot() = default;
111
112 AImage *image() const { return m_image.get(); }
113
114private:
115 void reset();
116
117 std::shared_ptr<QAndroidImageReader> m_reader;
118 AImagePtr m_image;
119};
120
122{
123 AImage *image = nullptr; // owned by QAndroidImageSlot
125
126 // MaterializableCaptureSource
128 void unmap(QAbstractVideoBuffer::MapData &) const { }
129};
130
131class QAndroidImageRegistry final
132 : public QtMultimediaPrivate::CaptureSlotRegistry<QAndroidImageRegistry, QAndroidImageSlot,
133 kAndroidImageCount>
134{
135public:
136 static std::shared_ptr<QAndroidImageRegistry> create();
137
139
140 // Sets the start time of the frame to the end of the previous one, and its end time to the
141 // given timestamp.
142 void setFrameTimes(QVideoFrame &frame, std::chrono::microseconds timestamp);
143
144private:
145 QAndroidImageRegistry();
146
147 std::optional<std::chrono::microseconds> m_lastTimestamp;
148};
149
150QVideoFrame createVideoFrame(QAndroidImageRegistry &registry, QAndroidImageSlot slot,
151 QVideoFrameFormat format);
152
153} // namespace QFFmpeg
154
155QT_END_NAMESPACE
156
157#endif // QANDROIDIMAGEREGISTRY_P_H
QAndroidImageReader(const QAndroidImageReader &)=delete
std::function< void(AImagePtr image, std::shared_ptr< QAndroidImageReader >)> ImageSink
QAndroidImageReader & operator=(const QAndroidImageReader &)=delete
static std::shared_ptr< QAndroidImageReader > create(QSize size, std::uint64_t usage)
void setFrameTimes(QVideoFrame &frame, std::chrono::microseconds timestamp)
void recycleSlot(QAndroidImageSlot slot) const
static std::shared_ptr< QAndroidImageRegistry > create()
QAndroidImageSlot(const QAndroidImageSlot &)=delete
QAndroidImageSlot & operator=(const QAndroidImageSlot &)=delete
QAndroidImageSlot(std::shared_ptr< QAndroidImageReader > reader, AImagePtr image)
QAndroidImageSlot(QAndroidImageSlot &&) noexcept=default
QAndroidImageSlot & operator=(QAndroidImageSlot &&other) noexcept
std::unique_ptr< AImageReader, AImageReaderDeleter > AImageReaderPtr
QVideoFrame createVideoFrame(QAndroidImageRegistry &registry, QAndroidImageSlot slot, QVideoFrameFormat format)
constexpr std::size_t kAndroidMinFreeImages
constexpr std::size_t kAndroidImageCount
std::unique_ptr< AImage, AImageDeleter > AImagePtr
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
void operator()(AImage *image) const
void operator()(AImageReader *reader) const
QVideoFrameFormat::PixelFormat pixelFormat
void unmap(QAbstractVideoBuffer::MapData &) const
std::optional< QAbstractVideoBuffer::MapData > mapReadOnly() const