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();
155 createInfo.streamSettings.overrideIgnoreDropShadow = ignoreDropShadow();
158 using ResultType = q23::expected<std::unique_ptr<QMacScreenCaptureKit>, QString>;
159 std::future<ResultType> streamResultFuture = QMacScreenCaptureKit::createStreamFromWindow(
163 ResultType streamResult = streamResultFuture.get();
167 m_session->onSourceActivationFailure(*
this);
169 qCWarning(qLcMacScreenCapture)
170 <<
"Failed to start screen capture stream:"
171 << streamResult.error();
172 return q23::unexpected{ ErrorPair{
173 QPlatformSurfaceCapture::Error::CaptureFailed,
174 u"Failed to start stream due to unknown issue"_s } };
177 std::unique_ptr<QMacScreenCaptureKit> &macScreenCaptureKit = *streamResult;
179 auto newActiveData = std::make_unique<ActiveData>();
180 newActiveData->macScreenCaptureKit = std::move(macScreenCaptureKit);
181 newActiveData->scWindow = std::move(scWindow);
182 newActiveData->streamId = newStreamId;
183 return newActiveData;
187 Q_ASSERT(!m_activeData);
189 TryStartResult result = tryStartStream();
191 const ErrorPair &error = result.error();
192 QPlatformSurfaceCapture::updateError(error.err, error.msg);
196 m_activeData = std::move(*result);
198 m_activeData.reset();
214 QMacScreenCaptureKit::StreamId incomingStreamId,
217 Q_ASSERT(thread()->isCurrentThread());
219 qCDebug(qLcMacScreenCapture)
221 <<
static_cast<int64_t>(incomingStreamId)
222 <<
"stopped with error:"
225 if (activeStreamId() != incomingStreamId)
230 m_activeData.reset();
231 m_videoFrameFormat.reset();
233 QPlatformSurfaceCapture::updateError(
234 Error::CaptureFailed,
235 u"The capture stream was closed by the system"_s);