111 QPlatformSurfaceCapture::Error err;
115 using TryStartResult = q23::expected<std::unique_ptr<ActiveData>, ErrorPair>;
117 auto tryStartStream = [&]() -> TryStartResult {
118 QCapturableWindow capturableWindow = source<WindowSource>();
119 const QCapturableWindowPrivate *handle = QCapturableWindowPrivate::handle(capturableWindow);
121 return q23::unexpected{ ErrorPair{
122 QPlatformSurfaceCapture::Error::NotFound,
123 u"Selected window is null"_s } };
125 CGWindowID cgWindowId =
static_cast<CGWindowID>(handle->id);
129 q23::expected<AVFScopedPointer<SCWindow>, QString> scWindowResult = findScWindow(cgWindowId);
130 if (!scWindowResult) {
131 qCWarning(qLcMacScreenCapture)
132 <<
"Could not find associated SCWindow:"
133 << scWindowResult.error();
134 return q23::unexpected{ ErrorPair {
135 QPlatformSurfaceCapture::Error::NotFound,
136 u"Backend was unable to find selected QCapturableWindow"_s } };
139 QMacScreenCaptureKit::StreamId newStreamId = allocateStreamId();
141 AVFScopedPointer<SCWindow> &scWindow = *scWindowResult;
146 m_session->onSourceActivating(*
this);
148 QMacScreenCaptureKit::CreateStreamInfo createInfo = {};
149 createInfo.streamId = newStreamId;
150 createInfo.connectionSetupFn = [&](QMacScreenCaptureKit &newObject) {
151 setupQMacScreenCaptureKitConnections(*
this, newObject);
153 createInfo.streamSettings.frameRate = frameRate();
154 createInfo.streamSettings.overrideIgnoreCursor = ignoreCursor();
157 using ResultType = q23::expected<std::unique_ptr<QMacScreenCaptureKit>, QString>;
158 std::future<ResultType> streamResultFuture = QMacScreenCaptureKit::createStreamFromWindow(
162 ResultType streamResult = streamResultFuture.get();
166 m_session->onSourceActivationFailure(*
this);
168 qCWarning(qLcMacScreenCapture)
169 <<
"Failed to start screen capture stream:"
170 << streamResult.error();
171 return q23::unexpected{ ErrorPair{
172 QPlatformSurfaceCapture::Error::CaptureFailed,
173 u"Failed to start stream due to unknown issue"_s } };
176 std::unique_ptr<QMacScreenCaptureKit> &macScreenCaptureKit = *streamResult;
178 auto newActiveData = std::make_unique<ActiveData>();
179 newActiveData->macScreenCaptureKit = std::move(macScreenCaptureKit);
180 newActiveData->scWindow = std::move(scWindow);
181 newActiveData->streamId = newStreamId;
182 return newActiveData;
186 Q_ASSERT(!m_activeData);
188 TryStartResult result = tryStartStream();
190 const ErrorPair &error = result.error();
191 QPlatformSurfaceCapture::updateError(error.err, error.msg);
195 m_activeData = std::move(*result);
197 m_activeData.reset();
213 QMacScreenCaptureKit::StreamId incomingStreamId,
216 Q_ASSERT(thread()->isCurrentThread());
218 qCDebug(qLcMacScreenCapture)
220 <<
static_cast<int64_t>(incomingStreamId)
221 <<
"stopped with error:"
224 if (activeStreamId() != incomingStreamId)
229 m_activeData.reset();
230 m_videoFrameFormat.reset();
232 QPlatformSurfaceCapture::updateError(
233 Error::CaptureFailed,
234 u"The capture stream was closed by the system"_s);