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 auto setupConnections = [&](QMacScreenCaptureKit &newObject) {
149 setupQMacScreenCaptureKitConnections(*
this, newObject);
153 using ResultType = q23::expected<std::unique_ptr<QMacScreenCaptureKit>, QString>;
154 std::future<ResultType> streamResultFuture = QMacScreenCaptureKit::createStreamFromWindow(
160 ResultType streamResult = streamResultFuture.get();
164 m_session->onSourceActivationFailure(*
this);
166 qCWarning(qLcMacScreenCapture)
167 <<
"Failed to start screen capture stream:"
168 << streamResult.error();
169 return q23::unexpected{ ErrorPair{
170 QPlatformSurfaceCapture::Error::CaptureFailed,
171 u"Failed to start stream due to unknown issue"_s } };
174 std::unique_ptr<QMacScreenCaptureKit> &macScreenCaptureKit = *streamResult;
176 auto newActiveData = std::make_unique<ActiveData>();
177 newActiveData->macScreenCaptureKit = std::move(macScreenCaptureKit);
178 newActiveData->scWindow = std::move(scWindow);
179 newActiveData->streamId = newStreamId;
180 return newActiveData;
184 Q_ASSERT(!m_activeData);
186 TryStartResult result = tryStartStream();
188 const ErrorPair &error = result.error();
189 QPlatformSurfaceCapture::updateError(error.err, error.msg);
193 m_activeData = std::move(*result);
195 m_activeData.reset();
211 QMacScreenCaptureKit::StreamId incomingStreamId,
214 Q_ASSERT(thread()->isCurrentThread());
216 qCDebug(qLcMacScreenCapture)
218 <<
static_cast<int64_t>(incomingStreamId)
219 <<
"stopped with error:"
222 if (activeStreamId() != incomingStreamId)
227 m_activeData.reset();
228 m_videoFrameFormat.reset();
230 QPlatformSurfaceCapture::updateError(
231 Error::CaptureFailed,
232 u"The capture stream was closed by the system"_s);