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
86 QMacScreenCaptureKit(QMacScreenCaptureKit &&other) noexcept = delete;
88
89 [[nodiscard]] StreamId streamId() const noexcept { return m_streamId; }
90
92 QSize resolutionPx,
93 std::optional<qreal> frameRate);
94
96
100 SCDisplay *,
103
110
114 SCWindow *,
117
118 // Called from background thread when stream needs to be configured with new
119 // output resolution. Don't call directly.
120 void updateStream(QSize resolutionPx);
121
122signals:
124 // This is commonly signaled if the user stops the stream by
125 // interacting with the system UI, or the stream has stopped
126 // for unknown reasons.
127 // Message is not suitable for forwarding to UI.
128 void streamStoppedWithError(StreamId streamId, QString);
129
130private:
131 StreamId m_streamId = {};
132
133 // A copy of the frameRate when this class was created. Does not
134 // change after stream has started.
135 std::optional<qreal> m_frameRate;
136
137 AVFScopedPointer<SCStream> m_stream;
138 AVFScopedPointer<dispatch_queue_t> m_dispatchQueue;
139 AVFScopedPointer<QMacScreenCaptureStreamDelegate> m_streamDelegate;
140 AVFScopedPointer<QMacScreenCaptureStreamOutput> m_streamOutput;
141
142 static void startStreamReconfigure(
143 SCStream *scStream,
144 QSize resolutionPx,
145 std::optional<qreal> frameRate);
146};
147
148}
149
150QT_END_NAMESPACE
151
152#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, 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)
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