Skip to content
Permalink
Browse files
[macOS] Screen recording in background tab only works for about 1 minute
https://bugs.webkit.org/show_bug.cgi?id=244167
rdar://problem/98966467

Reviewed by Chris Dumez and Eric Carlson.

Disable app nap for processes that run DisplayCaptureSourceCocoa, in our case the UIProcess, since it uses a timer.
We do so by adding a UserActivity to DisplayCaptureSourceCocoa and starting/stopping the user activity when starting/stopping the source.

We also disable app nap for pages that do screen capture (previously we were only doing this for pages doing microphone or camera capture).

* Source/WebCore/platform/mediastream/cocoa/DisplayCaptureSourceCocoa.cpp:
(WebCore::DisplayCaptureSourceCocoa::DisplayCaptureSourceCocoa):
(WebCore::DisplayCaptureSourceCocoa::startProducingData):
(WebCore::DisplayCaptureSourceCocoa::stopProducingData):
* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::updateActivityState):

Canonical link: https://commits.webkit.org/253769@main
  • Loading branch information
youennf committed Aug 25, 2022
1 parent fccee31 commit c888c485b787e204057b56d69536aae567ab8b3a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
@@ -109,6 +109,7 @@ DisplayCaptureSourceCocoa::DisplayCaptureSourceCocoa(UniqueRef<Capturer>&& captu
: RealtimeMediaSource(RealtimeMediaSource::Type::Video, WTFMove(name), WTFMove(deviceID), WTFMove(hashSalt), pageIdentifier)
, m_capturer(WTFMove(capturer))
, m_timer(RunLoop::current(), this, &DisplayCaptureSourceCocoa::emitFrame)
, m_userActivity("App nap disabled for screen capture")
{
m_capturer->setObserver(this);
}
@@ -173,6 +174,7 @@ void DisplayCaptureSourceCocoa::startProducingData()
{
m_startTime = MonotonicTime::now();
m_timer.startRepeating(1_s / frameRate());
m_userActivity.start();

commitConfiguration();
if (!m_capturer->start())
@@ -182,6 +184,7 @@ void DisplayCaptureSourceCocoa::startProducingData()
void DisplayCaptureSourceCocoa::stopProducingData()
{
m_timer.stop();
m_userActivity.stop();
m_elapsedTime += MonotonicTime::now() - m_startTime;
m_startTime = MonotonicTime::nan();

@@ -30,6 +30,7 @@
#include "CaptureDevice.h"
#include "RealtimeMediaSource.h"
#include "RealtimeMediaSourceSettings.h"
#include "UserActivity.h"
#include <wtf/Observer.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
@@ -152,6 +153,7 @@ class DisplayCaptureSourceCocoa final
Seconds m_elapsedTime { 0_s };

RunLoop::Timer<DisplayCaptureSourceCocoa> m_timer;
UserActivity m_userActivity;

std::unique_ptr<ImageTransferSessionVT> m_imageTransferSession;
};
@@ -2184,7 +2184,7 @@ void WebPageProxy::updateActivityState(OptionSet<ActivityState::Flag> flagsToUpd
m_activityState.add(ActivityState::IsAudible);
if (flagsToUpdate & ActivityState::IsLoading && m_pageLoadState.isLoading())
m_activityState.add(ActivityState::IsLoading);
if (flagsToUpdate & ActivityState::IsCapturingMedia && m_mediaState.containsAny({ MediaProducerMediaState::HasActiveAudioCaptureDevice, MediaProducerMediaState::HasActiveVideoCaptureDevice }))
if (flagsToUpdate & ActivityState::IsCapturingMedia && m_mediaState.containsAny(MediaProducer::ActiveCaptureMask))
m_activityState.add(ActivityState::IsCapturingMedia);
}

0 comments on commit c888c48

Please sign in to comment.