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
qavfscreencapture.mm
Go to the documentation of this file.
1// Copyright (C) 2022 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#include <QtFFmpegMediaPluginImpl/private/qavfscreencapture_p.h>
5
6#include <QtGui/qscreen.h>
7
8#include <QtFFmpegMediaPluginImpl/private/qavfsamplebufferdelegate_p.h>
9#include <QtFFmpegMediaPluginImpl/private/qffmpegsurfacecapturegrabber_p.h>
10#define AVMediaType XAVMediaType
11#include <QtFFmpegMediaPluginImpl/private/qffmpeghwaccel_p.h>
12#undef AVMediaType
13
14#include <QtMultimedia/private/qavfhelpers_p.h>
15
16#define AVMediaType XAVMediaType
17extern "C" {
18#include <libavutil/hwcontext_videotoolbox.h>
19#include <libavutil/hwcontext.h>
20}
21#undef AVMediaType
22
23#import <AppKit/NSScreen.h>
24
25#import <dispatch/dispatch.h>
26
27namespace {
28
29const auto DefaultCVPixelFormat = kCVPixelFormatType_32BGRA;
30
31CGDirectDisplayID findDisplayByName(const QString &name)
32{
33 for (NSScreen *screen in NSScreen.screens) {
34 if (name == QString::fromNSString(screen.localizedName))
35 return [screen.deviceDescription[@"NSScreenNumber"] unsignedIntValue];
36 }
37 return kCGNullDirectDisplay;
38}
39}
40
41QT_BEGIN_NAMESPACE
42
43namespace QFFmpeg {
44
46{
47public:
85
87 {
90
91 if (m_dispatchQueue) {
92 // Push a blocking job to the background frame thread,
93 // so we guarantee future frames are discarded. This
94 // causes the frameHandler to be destroyed, and the reference
95 // to this QAVFScreenCapture is cleared.
98 [this]() {
100 });
101
103 }
104
109 }
110
111private:
117};
118
123
128
130{
131 if (active) {
135 QLatin1String("Permissions denied"));
136 return false;
137 }
138
139 auto screen = source<ScreenSource>();
140
142 return false;
143
145 } else {
146 resetCapture();
147 }
148
149 return true;
150}
151
153{
154 // Since writing of the format is supposed to be only from one thread,
155 // the read-only comparison without a mutex is thread-safe
156 if (!m_format || m_format != frame.surfaceFormat()) {
158
160
161 locker.unlock();
162
164 }
165
167}
168
170{
171 if (!m_grabber)
172 return {};
173
175 while (!m_format)
177 return *m_format;
178}
179
184
186{
187 const auto screenID = findDisplayByName(screen->name());
188
192 QLatin1String("Screen exists but couldn't been found by name"));
193 return false;
194 }
195
197
198 if (!hwAccel) {
201 QLatin1String("Couldn't create videotoolbox hw acceleration"));
202 return false;
203 }
204
207
211 QLatin1String("Couldn't create hw frames context"));
212 return false;
213 }
214
216 return true;
217}
218
220{
222 m_format = {};
223}
224
226{
227 return std::make_unique<QAVFScreenCapture>();
228}
229
230} // namespace QFFmpeg
231
232QT_END_NAMESPACE
233
234#include "moc_qavfscreencapture_p.cpp"
QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamDelegate) QMacScreenCaptureStreamDelegate
std::unique_ptr< QPlatformSurfaceCapture > makeQAvfScreenCapture()