Title: [293609] trunk
Revision
293609
Author
[email protected]
Date
2022-04-29 00:57:59 -0700 (Fri, 29 Apr 2022)

Log Message

HTMLMediaElement can get multiple interruptions for invisible autoplay
https://bugs.webkit.org/show_bug.cgi?id=239842
<rdar://91809550>

Reviewed by Eric Carlson.

Source/WebCore:

A media element may be interrupted for invisible autoplay.
In some cases, like by calling play on the media element, the media element will no longer be in interrupted state.
This will lead to multiple invisible autoplay interruptions to be added to the same media element.
There will be only one end of interruption for invisible autoplay, which will lead to the media element to not restart as expected.

To prevent this, we store a boolean in HTMLMediaElement that tells us whether the media element has an ongoing invisible autoplay interruption.
Based on that, we make sure to always balance invisible autoplay interruption begin and end.

Test: media/video-mediastream-restricted-invisible-autoplay-not-allowed.html

* html/HTMLMediaElement.cpp:
* html/HTMLMediaElement.h:

LayoutTests:

* fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed-expected.txt: Added.
* fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (293608 => 293609)


--- trunk/LayoutTests/ChangeLog	2022-04-29 07:50:27 UTC (rev 293608)
+++ trunk/LayoutTests/ChangeLog	2022-04-29 07:57:59 UTC (rev 293609)
@@ -1,3 +1,14 @@
+2022-04-29  Youenn Fablet  <[email protected]>
+
+        HTMLMediaElement can get multiple interruptions for invisible autoplay
+        https://bugs.webkit.org/show_bug.cgi?id=239842
+        <rdar://91809550>
+
+        Reviewed by Eric Carlson.
+
+        * fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed-expected.txt: Added.
+        * fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed.html: Added.
+
 2022-04-28  Patrick Griffis  <[email protected]>
 
         CSP: Fix mixing strict-dynamic and unsafe-inline policies

Added: trunk/LayoutTests/fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed-expected.txt (0 => 293609)


--- trunk/LayoutTests/fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed-expected.txt	2022-04-29 07:57:59 UTC (rev 293609)
@@ -0,0 +1,21 @@
+
+Test that "invisible autoplay not allowed restriction" pauses realtime media when scrolled out of view and restarts as expected.
+
+** setting video.srcObject
+
+EVENT(play)
+RUN(video.style.display = "none")
+EVENT(pause)
+RUN(video.style.removeProperty("display"))
+EVENT(play)
+RUN(video.style.visibility = "hidden")
+EVENT(pause)
+RUN(video.style.removeProperty("visibility"))
+EVENT(play)
+RUN(document.documentElement.style.height = window.innerHeight + 20 + video.offsetHeight)
+RUN(window.scrollBy(0, 20 + video.offsetHeight))
+EVENT(pause)
+RUN(window.scrollTo(0, 0))
+EVENT(play)
+END OF TEST
+

Added: trunk/LayoutTests/fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed.html (0 => 293609)


--- trunk/LayoutTests/fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed.html	                        (rev 0)
+++ trunk/LayoutTests/fast/mediastream/video-mediastream-restricted-invisible-autoplay-not-allowed.html	2022-04-29 07:57:59 UTC (rev 293609)
@@ -0,0 +1,71 @@
+<html>
+    <head>
+        <script src=""
+        <script>
+            if (window.internals)
+                internals.settings.setInvisibleAutoplayNotPermitted(true);
+            async function start()
+            {
+                findMediaElement();
+                consoleWrite('** setting video.srcObject');
+                video.srcObject = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
+                video.muted = true;
+
+                waitForEventOnce('play', play1);
+                consoleWrite('');
+            }
+
+            function play1()
+            {
+                run('video.style.display = "none"');
+                waitForEventOnce('pause', pause1);
+            }
+
+            function pause1()
+            {
+                run('video.style.removeProperty("display")')
+                waitForEventOnce('play', play2);
+            }
+
+            async function play2()
+            {
+                run('video.style.visibility = "hidden"');
+                waitForEventOnce('pause', pause2);
+            }
+
+            async function pause2()
+            {
+                // At this point, we are interrupted, calling play should be a no-op and should not confuse our interruption state tracking.
+                video.play();
+
+                run('video.style.removeProperty("visibility")');
+                waitForEventOnce('play', play3);
+            }
+
+            function play3()
+            {
+                run('document.documentElement.style.height = window.innerHeight + 20 + video.offsetHeight');
+                run('window.scrollBy(0, 20 + video.offsetHeight)');
+                waitForEventOnce('pause', pause3);
+            }
+
+            function pause3()
+            {
+                run('window.scrollTo(0, 0)');
+                waitForEventOnce('play', finish);
+            }
+
+            function finish()
+            {
+                video.srcObject.getVideoTracks()[0].stop();
+                video.srcObject = null;
+                endTest();
+            }
+        </script>
+    </head>
+
+    <body _onload_="start()">
+        <video controls autoplay></video>
+        <p>Test that "invisible autoplay not allowed restriction" pauses realtime media when scrolled out of view and restarts as expected.</p>
+    </body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (293608 => 293609)


--- trunk/Source/WebCore/ChangeLog	2022-04-29 07:50:27 UTC (rev 293608)
+++ trunk/Source/WebCore/ChangeLog	2022-04-29 07:57:59 UTC (rev 293609)
@@ -1,5 +1,26 @@
 2022-04-29  Youenn Fablet  <[email protected]>
 
+        HTMLMediaElement can get multiple interruptions for invisible autoplay
+        https://bugs.webkit.org/show_bug.cgi?id=239842
+        <rdar://91809550>
+
+        Reviewed by Eric Carlson.
+
+        A media element may be interrupted for invisible autoplay.
+        In some cases, like by calling play on the media element, the media element will no longer be in interrupted state.
+        This will lead to multiple invisible autoplay interruptions to be added to the same media element.
+        There will be only one end of interruption for invisible autoplay, which will lead to the media element to not restart as expected.
+
+        To prevent this, we store a boolean in HTMLMediaElement that tells us whether the media element has an ongoing invisible autoplay interruption.
+        Based on that, we make sure to always balance invisible autoplay interruption begin and end.
+
+        Test: media/video-mediastream-restricted-invisible-autoplay-not-allowed.html
+
+        * html/HTMLMediaElement.cpp:
+        * html/HTMLMediaElement.h:
+
+2022-04-29  Youenn Fablet  <[email protected]>
+
         Make sure to fail importScripts as per https://w3c.github.io/ServiceWorker/#importscripts step 4
         https://bugs.webkit.org/show_bug.cgi?id=239847
 

Modified: trunk/Source/WebCore/html/HTMLMediaElement.cpp (293608 => 293609)


--- trunk/Source/WebCore/html/HTMLMediaElement.cpp	2022-04-29 07:50:27 UTC (rev 293608)
+++ trunk/Source/WebCore/html/HTMLMediaElement.cpp	2022-04-29 07:57:59 UTC (rev 293609)
@@ -8379,15 +8379,26 @@
     bool canAutoplay = mediaSession().autoplayPermitted();
 
     if (canAutoplay) {
-        if (mediaSession().state() == PlatformMediaSession::Interrupted) {
-            if (mediaSession().interruptionType() == PlatformMediaSession::InvisibleAutoplay)
-                mediaSession().endInterruption(PlatformMediaSession::MayResumePlaying);
-        } else if (!isPlaying())
+        if (m_wasInterruptedForInvisibleAutoplay) {
+            m_wasInterruptedForInvisibleAutoplay = false;
+            mediaSession().endInterruption(PlatformMediaSession::MayResumePlaying);
+            return;
+        }
+        if (!isPlaying())
             resumeAutoplaying();
         return;
     }
-    if (mediaSession().state() != PlatformMediaSession::Interrupted)
-        mediaSession().beginInterruption(PlatformMediaSession::InvisibleAutoplay);
+
+    if (mediaSession().state() == PlatformMediaSession::Interrupted)
+        return;
+
+    if (m_wasInterruptedForInvisibleAutoplay) {
+        m_wasInterruptedForInvisibleAutoplay = false;
+        mediaSession().endInterruption(PlatformMediaSession::NoFlags);
+    }
+
+    m_wasInterruptedForInvisibleAutoplay = true;
+    mediaSession().beginInterruption(PlatformMediaSession::InvisibleAutoplay);
 }
 
 void HTMLMediaElement::updateShouldPlay()

Modified: trunk/Source/WebCore/html/HTMLMediaElement.h (293608 => 293609)


--- trunk/Source/WebCore/html/HTMLMediaElement.h	2022-04-29 07:50:27 UTC (rev 293608)
+++ trunk/Source/WebCore/html/HTMLMediaElement.h	2022-04-29 07:57:59 UTC (rev 293609)
@@ -1248,6 +1248,7 @@
 #if USE(AUDIO_SESSION)
     AudioSessionCategory m_categoryAtMostRecentPlayback;
 #endif
+    bool m_wasInterruptedForInvisibleAutoplay { false };
 };
 
 String convertEnumerationToString(HTMLMediaElement::AutoplayEventPlaybackState);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to