Skip to content

Commit

Permalink
Cherry-pick 5a6b1df. rdar://122187886
Browse files Browse the repository at this point in the history
    [macOS] Screen or Window sharing in Safari stops working once paused, resume option does not work - WebRTC getDisplayMedia
    https://bugs.webkit.org/show_bug.cgi?id=269566
    rdar://122187886

    Reviewed by Eric Carlson.

    When muting the source, we stop it.
    At that point, we were forgetting which content filter we were using, which is necessary to restart the stream.
    We are now storing it in ScreenCaptureKitCaptureSource by querying ScreenCaptureKitSharingSessionManager.
    When unmuting, we already have it and we can construct successfully a new session source.

    * Source/WebCore/platform/mediastream/mac/ScreenCaptureKitCaptureSource.h:
    * Source/WebCore/platform/mediastream/mac/ScreenCaptureKitCaptureSource.mm:
    (WebCore::ScreenCaptureKitCaptureSource::startContentStream):
    * Source/WebCore/platform/mediastream/mac/ScreenCaptureKitSharingSessionManager.h:
    * Source/WebCore/platform/mediastream/mac/ScreenCaptureKitSharingSessionManager.mm:
    (WebCore::ScreenCaptureKitSharingSessionManager::cancelPendingSessionForDevice):
    (WebCore::ScreenCaptureKitSharingSessionManager::contentFilterFromCaptureDevice):
    (WebCore::ScreenCaptureKitSharingSessionManager::createSessionSourceForDevice):

    Canonical link: https://commits.webkit.org/274943@main

Identifier: 272448.618@safari-7618-branch
  • Loading branch information
youennf authored and MyahCobbs committed Feb 24, 2024
1 parent a72205e commit a0a021c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ScreenCaptureKitCaptureSource final

std::optional<Content> m_content;
RetainPtr<WebCoreScreenCaptureKitHelper> m_captureHelper;
RetainPtr<SCContentFilter> m_contentFilter;
RetainPtr<CMSampleBufferRef> m_currentFrame;
RefPtr<ScreenCaptureSessionSource> m_sessionSource;
RetainPtr<SCStreamConfiguration> m_streamConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,17 @@ - (void)stream:(SCStream *)stream didOutputSampleBuffer:(CMSampleBufferRef)sampl
if (!m_captureHelper)
m_captureHelper = adoptNS([[WebCoreScreenCaptureKitHelper alloc] initWithCallback:this]);

m_sessionSource = ScreenCaptureKitSharingSessionManager::singleton().createSessionSourceForDevice(*this, m_captureDevice, streamConfiguration().get(), (SCStreamDelegate*)m_captureHelper.get());
if (!m_contentFilter)
m_contentFilter = ScreenCaptureKitSharingSessionManager::singleton().contentFilterFromCaptureDevice(m_captureDevice);

#if HAVE(SC_CONTENT_SHARING_PICKER)
if (!m_contentFilter) {
sessionFailedWithError(nil, "Unkown display device"_s);
return;
}
#endif

m_sessionSource = ScreenCaptureKitSharingSessionManager::singleton().createSessionSourceForDevice(*this, m_contentFilter.get(), streamConfiguration().get(), (SCStreamDelegate*)m_captureHelper.get());
if (!m_sessionSource) {
sessionFailedWithError(nil, "Failed to allocate stream"_s);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class ScreenCaptureKitSharingSessionManager : public CanMakeWeakPtr<ScreenCaptur

void cancelPicking();

RefPtr<ScreenCaptureSessionSource> createSessionSourceForDevice(WeakPtr<ScreenCaptureSessionSource::Observer>, const CaptureDevice&, SCStreamConfiguration*, SCStreamDelegate*);
RetainPtr<SCContentFilter> contentFilterFromCaptureDevice(const CaptureDevice&);
RefPtr<ScreenCaptureSessionSource> createSessionSourceForDevice(WeakPtr<ScreenCaptureSessionSource::Observer>, SCContentFilter*, SCStreamConfiguration*, SCStreamDelegate*);
void cancelPendingSessionForDevice(const CaptureDevice&);

WEBCORE_EXPORT void promptForGetDisplayMedia(DisplayCapturePromptType, CompletionHandler<void(std::optional<CaptureDevice>)>&&);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,27 +522,33 @@ - (void)stopObservingPicker:(SCContentSharingPicker *)picker
ASSERT(isMainThread());

if (m_pendingContentFilter.get() != device) {
RELEASE_LOG_ERROR(WebRTC, "ScreenCaptureKitSharingSessionManager::createSessionSourceForDevice - unknown capture device.");
RELEASE_LOG_ERROR(WebRTC, "ScreenCaptureKitSharingSessionManager::cancelPendingSessionForDevice - unknown capture device.");
return;
}

m_pendingContentFilter = nullptr;
cancelPicking();
}

RefPtr<ScreenCaptureSessionSource> ScreenCaptureKitSharingSessionManager::createSessionSourceForDevice(WeakPtr<ScreenCaptureSessionSource::Observer> observer, const CaptureDevice& device, SCStreamConfiguration* configuration, SCStreamDelegate* delegate)
RetainPtr<SCContentFilter> ScreenCaptureKitSharingSessionManager::contentFilterFromCaptureDevice(const CaptureDevice& device)
{
ASSERT(isMainThread());

if (m_pendingContentFilter.get() != device) {
RELEASE_LOG_ERROR(WebRTC, "ScreenCaptureKitSharingSessionManager::createSessionSourceForDevice - unknown capture device.");
RELEASE_LOG_ERROR(WebRTC, "ScreenCaptureKitSharingSessionManager::contentFilterFromCaptureDevice - unknown capture device.");
return nullptr;
}
return std::exchange(m_pendingContentFilter, { });
}

RefPtr<ScreenCaptureSessionSource> ScreenCaptureKitSharingSessionManager::createSessionSourceForDevice(WeakPtr<ScreenCaptureSessionSource::Observer> observer, SCContentFilter* contentFilter, SCStreamConfiguration* configuration, SCStreamDelegate* delegate)
{
ASSERT(isMainThread());

RetainPtr<SCStream> stream;
#if HAVE(SC_CONTENT_SHARING_PICKER)
if (useSCContentSharingPicker())
stream = adoptNS([PAL::allocSCStreamInstance() initWithFilter:m_pendingContentFilter.get() configuration:configuration delegate:(id<SCStreamDelegate> _Nullable)delegate]);
stream = adoptNS([PAL::allocSCStreamInstance() initWithFilter:contentFilter configuration:configuration delegate:(id<SCStreamDelegate> _Nullable)delegate]);
else
#endif
stream = adoptNS([PAL::allocSCStreamInstance() initWithSharingSession:m_pendingSession.get() captureOutputProperties:configuration delegate:(id<SCStreamDelegate> _Nullable)delegate]);
Expand All @@ -559,7 +565,7 @@ - (void)stopObservingPicker:(SCContentSharingPicker *)picker
cleanupSessionSource(source);
};

auto newSession = ScreenCaptureSessionSource::create(WTFMove(observer), WTFMove(stream), WTFMove(m_pendingContentFilter), WTFMove(m_pendingSession), WTFMove(cleanupFunction));
auto newSession = ScreenCaptureSessionSource::create(WTFMove(observer), WTFMove(stream), contentFilter, WTFMove(m_pendingSession), WTFMove(cleanupFunction));
m_activeSources.append(newSession);

return newSession;
Expand Down

0 comments on commit a0a021c

Please sign in to comment.