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 <future>
28#include <memory>
29#include <optional>
30#include <vector>
31
32// Error-handling for on-going stream
33@interface QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamDelegate) : NSObject <SCStreamDelegate>
34@end
35
36// Receives frame callbacks
37@interface QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamOutput) : NSObject <SCStreamOutput>
38@end
39
40QT_BEGIN_NAMESPACE
41
42namespace QFFmpeg {
43
45
48
49// Helper class for managing a ScreenCaptureKit stream.
50//
51// TODO: This class has very few FFmpeg specific components. Move this out of FFmpeg media backend
52// in the future.
53//
54// TODO: Use this backend for QScreenCapture as well.
57
58public:
59 // TODO: May become a runtime parameter in the future.
60 // For now we only support BGRA32.
64 // How many frames can be queued up on the background thread before we start dropping frames
65 // ScreenCaptureKit default is 3.
66 // https://developer.apple.com/documentation/screencapturekit/scstreamconfiguration/queuedepth?language=objc
67 static constexpr int queueDepth = 3;
68 [[nodiscard]] static CFStringRef cgColorSpace() { return kCGColorSpaceSRGB; }
75
80
85 QMacScreenCaptureKit(QMacScreenCaptureKit &&other) noexcept = delete;
87
88 [[nodiscard]] int64_t streamId() const noexcept { return m_streamId; }
89
91 QSize resolutionPx,
92 std::optional<qreal> frameRate);
93
95
102
106 SCWindow *,
108
109 // Called from background thread when stream needs to be configured with new
110 // output resolution. Don't call directly.
111 void updateStream(QSize resolutionPx);
112
113signals:
115 // This is commonly signaled if the user stops the stream by
116 // interacting with the system UI, or the stream has stopped
117 // for unknown reasons.
118 // Message is not suitable for forwarding to UI.
119 void streamStoppedWithError(int64_t streamId, QString);
120
121private:
122 int64_t m_streamId = -1;
123
124 // A copy of the frameRate when this class was created. Does not
125 // change after stream has started.
126 std::optional<qreal> m_frameRate;
127
128 AVFScopedPointer<SCStream> m_stream;
129 AVFScopedPointer<dispatch_queue_t> m_dispatchQueue;
130 AVFScopedPointer<QMacScreenCaptureStreamDelegate> m_streamDelegate;
131 AVFScopedPointer<QMacScreenCaptureStreamOutput> m_streamOutput;
132
133 static void startStreamReconfigure(
134 SCStream *scStream,
135 QSize resolutionPx,
136 std::optional<qreal> frameRate);
137};
138
139}
140
141QT_END_NAMESPACE
142
143#endif // QMACSCREENCAPTUREKIT_P_H
QMacScreenCaptureKit & operator=(QMacScreenCaptureKit &&other)=delete
int64_t streamId() const noexcept
static constexpr QVideoFrameFormat::PixelFormat pixelFormat
QMacScreenCaptureKit(const QMacScreenCaptureKit &)=delete
static constexpr QVideoFrameFormat::ColorTransfer colorTransfer
QMacScreenCaptureKit(QMacScreenCaptureKit &&other) noexcept=delete
QMacScreenCaptureKit & operator=(const QMacScreenCaptureKit &)=delete
void updateStream(QSize resolutionPx)
void streamStoppedWithError(int64_t streamId, QString)
static constexpr QVideoFrameFormat::ColorRange colorRange
static constexpr QVideoFrameFormat::ColorSpace colorSpace
static AVFScopedPointer< SCStreamConfiguration > createStreamConfig(QSize resolutionPx, std::optional< qreal > frameRate)
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)
q23::expected< QSize, QString > ReadContentRect(CMSampleBufferRef sampleBuffer)
static void configureStreamDelegate(QMacScreenCaptureStreamDelegate &streamDelegate, int64_t streamId, const QMacScreenCaptureKit &macScreenCaptureKit)
Q_LOGGING_CATEGORY_IMPL(QT_PREPEND_NAMESPACE(QFFmpeg::qLcMacScreenCapture), "qt.multimedia.screencapture.macscreencapturekit")
std::vector< AVFScopedPointer< SCDisplay > > displays
std::vector< AVFScopedPointer< SCWindow > > windows