97 QPlatformSurfaceCapture::Error err;
101 using TryStartResult = q23::expected<std::unique_ptr<ActiveData>, ErrorPair>;
103 auto tryStartStream = [&]() -> TryStartResult {
104 QCapturableWindow capturableWindow = source<WindowSource>();
105 const QCapturableWindowPrivate *handle = QCapturableWindowPrivate::handle(capturableWindow);
107 return q23::unexpected{ ErrorPair{
108 QPlatformSurfaceCapture::Error::NotFound,
109 u"Selected window is null"_s } };
111 CGWindowID cgWindowId =
static_cast<CGWindowID>(handle->id);
115 q23::expected<AVFScopedPointer<SCWindow>, QString> scWindowResult = findScWindow(cgWindowId);
116 if (!scWindowResult) {
117 qCWarning(qLcMacScreenCapture)
118 <<
"Could not find associated SCWindow: "
119 << scWindowResult.error();
120 return q23::unexpected{ ErrorPair {
121 QPlatformSurfaceCapture::Error::NotFound,
122 u"Backend was unable to find selected QCapturableWindow"_s } };
125 int64_t newStreamId = m_streamIdTracker++;
127 AVFScopedPointer<SCWindow> &scWindow = *scWindowResult;
130 using ResultType = q23::expected<std::unique_ptr<QMacScreenCaptureKit>, QString>;
131 std::future<ResultType> streamResultFuture = QMacScreenCaptureKit::createStream(
135 ResultType streamResult = streamResultFuture.get();
137 qCWarning(qLcMacScreenCapture)
138 <<
"Failed to start screen capture stream: "
139 << streamResult.error();
140 return q23::unexpected{ ErrorPair{
141 QPlatformSurfaceCapture::Error::CaptureFailed,
142 u"Failed to start stream due to unknown issue"_s } };
145 std::unique_ptr<QMacScreenCaptureKit> &macScreenCaptureKit = *streamResult;
146 setupQMacScreenCaptureKitConnections(
148 *macScreenCaptureKit.get());
150 auto newActiveData = std::make_unique<ActiveData>();
151 newActiveData->macScreenCaptureKit = std::move(macScreenCaptureKit);
152 newActiveData->scWindow = std::move(scWindow);
153 newActiveData->streamId = newStreamId;
154 return newActiveData;
158 Q_ASSERT(!m_activeData);
160 TryStartResult result = tryStartStream();
162 const ErrorPair &error = result.error();
163 QPlatformSurfaceCapture::updateError(error.err, error.msg);
167 m_activeData = std::move(*result);
169 m_activeData.reset();
187 int64_t incomingStreamId,
190 Q_ASSERT(thread()->isCurrentThread());
192 qCDebug(qLcMacScreenCapture)
195 <<
" stopped with error: "
198 std::optional<int64_t> activeStreamIdOpt = activeStreamId();
199 if (!activeStreamIdOpt || *activeStreamIdOpt != incomingStreamId)
204 m_activeData.reset();
205 m_videoFrameFormat.reset();
207 QPlatformSurfaceCapture::updateError(
208 Error::CaptureFailed,
209 u"The capture stream was closed by the system"_s);