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: http://svn.webkit.org/repository/webkit/trunk@295590 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/http/tests/webrtc/resources/utility-frame.html b/LayoutTests/http/tests/webrtc/resources/utility-frame.html
new file mode 100644
index 0000000..eaca3f4
--- /dev/null
+++ b/LayoutTests/http/tests/webrtc/resources/utility-frame.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<html>
+<body>
+</body>
+</html>
diff --git a/LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached-expected.txt b/LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached-expected.txt
new file mode 100644
index 0000000..d84ca16
--- /dev/null
+++ b/LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached-expected.txt
@@ -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
+
diff --git a/LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached.html b/LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached.html
new file mode 100644
index 0000000..136fc7b
--- /dev/null
+++ b/LayoutTests/http/tests/webrtc/video-mediastream-invisible-autoplay-detached.html
@@ -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>
diff --git a/Source/WebCore/html/HTMLMediaElement.cpp b/Source/WebCore/html/HTMLMediaElement.cpp
index fd31b69..90a14f3 100644
--- a/Source/WebCore/html/HTMLMediaElement.cpp
+++ b/Source/WebCore/html/HTMLMediaElement.cpp
@@ -869,7 +869,7 @@
     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();