Skip to content
Permalink
Browse files
Moving a media element from one document to another should not break …
…autoplay

https://bugs.webkit.org/show_bug.cgi?id=241631
rdar://95060381

Patch by Youenn Fablet <youennf@gmail.com> on 2022-06-16
Reviewed by Eric Carlson.

When pausing a video element when being detached from a document, autoplay might get broken if the element is interrupted due to invisibility.
In that case, the session will store the fact that the element is paused and when the end of invisibility interruption happens, the session state is Paused instead of Autoplay.
To prevent this, we do not pause when being detached if we are alread interrupted due to invisibility.

Covered by added test.

* LayoutTests/http/tests/webrtc/resources/utility-frame.html: Added.
* LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached-expected.txt: Added.
* LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached.html: Added.
* Source/WebCore/html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::pauseAfterDetachedTask):

Canonical link: https://commits.webkit.org/251595@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295590 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
youennf authored and webkit-commit-queue committed Jun 16, 2022
1 parent b13a5ff commit f137908f57f04b737f24a764216001d3b0c562a9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<html>
<body>
</body>
</html>
@@ -0,0 +1,10 @@
Test that "invisible autoplay not allowed restriction" pauses realtime media when scrolled out of view and restarts as expected.

** setting video.srcObject
** waiting to play

** element played
** element paused
** element played
** test finished

@@ -0,0 +1,71 @@
<html>
<head>
</head>
<script>
if (window.testRunner) {
testRunner.dumpAsText();
testRunner.waitUntilDone();
}
if (window.internals)
internals.settings.setInvisibleAutoplayNotPermitted(true);
</script>
<body>
<video autoplay width=320px height=240px id=videoElement controls playsInline></video>
<p>Test that "invisible autoplay not allowed restriction" pauses realtime media when scrolled out of view and restarts as expected.</p>
<div id="log"></div>
<iframe wdith=640px height=480px id="testFrame" src="resources/utility-frame.html"></iframe>
<script>
let myVideo = videoElement;

function doLog(msg)
{
log.innerHTML += msg + "<br>";
}

async function start()
{
doLog('** setting video.srcObject');
myVideo.srcObject = await navigator.mediaDevices.getUserMedia({ video: true });
doLog('** waiting to play');
myVideo.onplay = play1;
doLog('');
}

async function play1()
{
myVideo.onplay = undefined;

doLog('** element played');

myVideo.style.display = "none";
document.body.removeChild(myVideo);
await new Promise(resolve => setTimeout(resolve, 500));
if (myVideo.paused) {
pause1();
return;
}
myVideo.onpause = pause1;
}

async function pause1()
{
myVideo.onpause = undefined;

doLog('** element paused');
testFrame.contentDocument.body.appendChild(myVideo);
myVideo.style.removeProperty("display");
myVideo.onplay = finish;
}

function finish()
{
myVideo.onplay = undefined;
doLog('** element played');
doLog('** test finished');
if (window.testRunner)
testRunner.notifyDone();
}
start();
</script>
</body>
</html>
@@ -869,7 +869,7 @@ void HTMLMediaElement::pauseAfterDetachedTask()
if (m_inActiveDocument)
return;

if (m_videoFullscreenMode != VideoFullscreenModePictureInPicture && m_networkState > NETWORK_EMPTY)
if (m_videoFullscreenMode != VideoFullscreenModePictureInPicture && m_networkState > NETWORK_EMPTY && !m_wasInterruptedForInvisibleAutoplay)
pause();
if (m_videoFullscreenMode == VideoFullscreenModeStandard)
exitFullscreen();

0 comments on commit f137908

Please sign in to comment.