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
qmacscreencapturekit_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 QMACSCREENCAPTUREKIT_P_H
5#define QMACSCREENCAPTUREKIT_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 <QtCore/qobject.h>
19#include <QtCore/qloggingcategory.h>
20#include <QtCore/private/qexpected_p.h>
21
22#include <QtMultimedia/qvideoframe.h>
23#include <QtMultimedia/private/qavfcamerautility_p.h>
24
25#import <ScreenCaptureKit/ScreenCaptureKit.h>
26
27#include <functional>
28#include <future>
29#include <memory>
30#include <optional>
31#include <vector>
32
33// Error-handling for on-going stream
34@interface QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamDelegate) : NSObject <SCStreamDelegate>
35@end
36
37// Receives frame callbacks
38@interface QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamOutput) : NSObject <SCStreamOutput>
39@end
40
41QT_BEGIN_NAMESPACE
42
43namespace QFFmpeg {
44
46
49
50// Helper class for managing a ScreenCaptureKit stream.
51//
52// TODO: This class has very few FFmpeg specific components. Move this out of FFmpeg media backend
53// in the future.
56
57public:
58 enum class StreamId : int64_t{};
59
60 // TODO: May become a runtime parameter in the future.
61 // For now we only support BGRA32.
65 // How many frames can be queued up on the background thread before we start dropping frames
66 // ScreenCaptureKit default is 3.
67 // https://developer.apple.com/documentation/screencapturekit/scstreamconfiguration/queuedepth?language=objc
68 static constexpr int queueDepth = 3;
69 [[nodiscard]] static CFStringRef cgColorSpace() { return kCGColorSpaceSRGB; }
76
81
87
93
98 QMacScreenCaptureKit(QMacScreenCaptureKit &&other) noexcept = delete;
100
101 [[nodiscard]] StreamId streamId() const noexcept { return m_streamId; }
102
104
107 CreateStreamInfo const &,
108 SCDisplay *);
109
112 CreateStreamInfo const &,
114
117 CreateStreamInfo const &,
118 SCWindow *);
119
120 // Called from background thread when stream needs to be configured with new
121 // output resolution. Don't call directly.
122 void updateStream(QSize resolutionPx);
123
124signals:
126 // This is commonly signaled if the user stops the stream by
127 // interacting with the system UI, or the stream has stopped
128 // for unknown reasons.
129 // Message is not suitable for forwarding to UI.
130 void streamStoppedWithError(StreamId streamId, QString);
131
132private:
133 StreamId m_streamId = {};
134
135 // A copy of the stream settings when this class was created. Does
136 // not change after stream has started. Used for auto-reconfiguring
137 // our stream with new size when needed.
138 StreamSettings m_streamSettings = {};
139
140 AVFScopedPointer<SCStream> m_stream;
141 AVFScopedPointer<dispatch_queue_t> m_dispatchQueue;
142 AVFScopedPointer<QMacScreenCaptureStreamDelegate> m_streamDelegate;
143 AVFScopedPointer<QMacScreenCaptureStreamOutput> m_streamOutput;
144
145 static void startStreamReconfigure(
146 SCStream *scStream,
147 QSize resolutionPx,
148 StreamSettings const &);
149};
150
151}
152
153QT_END_NAMESPACE
154
155#endif // QMACSCREENCAPTUREKIT_P_H
QMacScreenCaptureKit & operator=(QMacScreenCaptureKit &&other)=delete
static constexpr QVideoFrameFormat::PixelFormat pixelFormat
QMacScreenCaptureKit(const QMacScreenCaptureKit &)=delete
void streamStoppedWithError(StreamId streamId, QString)
static constexpr FourCharCode cvPixelFormat
static constexpr QVideoFrameFormat::ColorTransfer colorTransfer
QMacScreenCaptureKit(QMacScreenCaptureKit &&other) noexcept=delete
QMacScreenCaptureKit & operator=(const QMacScreenCaptureKit &)=delete
void updateStream(QSize resolutionPx)
StreamId streamId() const noexcept
static constexpr QVideoFrameFormat::ColorRange colorRange
static constexpr QVideoFrameFormat::ColorSpace colorSpace
static AVFScopedPointer< SCStreamConfiguration > createStreamConfig(QSize resolutionPx, QMacScreenCaptureKit::StreamSettings const &settings)
Q_DECLARE_LOGGING_CATEGORY(qLcMacScreenCapture)
static void handleFrameOutput(QMacScreenCaptureStreamOutput &scStreamOutput, CMSampleBufferRef sampleBufferRef)
QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamDelegate) QMacScreenCaptureStreamDelegate
static q23::expected< QVideoFrame, QString > createQVideoFrame(QMacScreenCaptureStreamOutput &scStreamOutput, CMSampleBufferRef sampleBufferRef)
static q23::expected< AVFScopedPointer< QMacScreenCaptureStreamOutput >, QString > createStreamOutput(QMacScreenCaptureKit &macScreenCaptureKit, uint32_t cvPixelFormat, QSize resolution)
static void configureStreamDelegate(QMacScreenCaptureStreamDelegate &streamDelegate, QMacScreenCaptureKit::StreamId streamId, const QMacScreenCaptureKit &macScreenCaptureKit)
static q23::expected< FrameInfo, QString > readFrameInfo(CMSampleBufferRef sampleBuffer)
Q_LOGGING_CATEGORY_IMPL(QT_PREPEND_NAMESPACE(QFFmpeg::qLcMacScreenCapture), "qt.multimedia.screencapture.macscreencapturekit")
std::vector< AVFScopedPointer< SCDisplay > > displays
std::vector< AVFScopedPointer< SCWindow > > windows
std::function< void(QMacScreenCaptureKit &)> connectionSetupFn