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

Patch by Youenn Fablet <> 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:

Canonical link:
git-svn-id: 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>
@@ -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 @@
if (window.testRunner) {
if (window.internals)
<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>
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;

async function play1()
myVideo.onplay = undefined;

doLog('** element played'); = "none";
await new Promise(resolve => setTimeout(resolve, 500));
if (myVideo.paused) {
myVideo.onpause = pause1;

async function pause1()
myVideo.onpause = undefined;

doLog('** element paused');
myVideo.onplay = finish;

function finish()
myVideo.onplay = undefined;
doLog('** element played');
doLog('** test finished');
if (window.testRunner)
@@ -869,7 +869,7 @@ void HTMLMediaElement::pauseAfterDetachedTask()
if (m_inActiveDocument)

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

0 comments on commit f137908

Please sign in to comment.