- Revision
- 200431
- Author
- [email protected]
- Date
- 2016-05-04 14:33:45 -0700 (Wed, 04 May 2016)
Log Message
Media elements should not be paused right away when removed from the document
https://bugs.webkit.org/show_bug.cgi?id=157347
<rdar://problem/25888758>
Reviewed by Alex Christensen.
LayoutTests/imported/w3c:
Rebaseline now that more W3C tests are passing.
* web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-to-other-document-expected.txt:
* web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-within-document-expected.txt:
* web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-remove-from-document-expected.txt:
Source/WebCore:
Media elements should not be paused right away when removed from the document.
Instead we should allow the task that removed the media element from the
document to finish because considering pausing.
This avoid inadvertently pausing media elements when the JS merely moves them
on the page (e.g. using Node.replaceChild()).
Text from the HTML specification:
"""
When a media element is removed from a Document, the user agent must run the
following steps:
1. Await a stable state, allowing the task that removed the media element
from the Document to continue. The synchronous section consists of all the
remaining steps of this algorithm. (Steps in the synchronous section are
marked with ⌛.)
2. ⌛ If the media element is in a Document, abort these steps.
3. ⌛ Run the internal pause steps for the media element.
"""
c.f. https://html.spec.whatwg.org/multipage/embedded-content.html#htmlmediaelement
Test: media/replaceChild-should-not-pause-video.html
* dom/GenericEventQueue.cpp:
(WebCore::GenericEventQueue::sharedTimerFired):
Copy the queue of events before processing it so that we don't fire events that
get scheduled by the event handlers as a result of us firing the pending events.
Otherwise, we end up firing events synchronously right after they've been queued,
which is wrong. This was causing several W3C tests to fail.
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::HTMLMediaElement):
(WebCore::HTMLMediaElement::pauseAfterDetachedTimerFired):
(WebCore::HTMLMediaElement::removedFrom):
* html/HTMLMediaElement.h:
After the media element gets removed from the document, schedule a 0 timer before
pausing the media element, to give the task that removed us a chance to finish.
When the timer fires, we check if we were added back into an active document and
avoid pausing in such case.
LayoutTests:
* media/remove-from-document-expected.txt:
* media/remove-from-document.html:
Check asynchronously if the video has been paused after removing it from
the document instead of synchronously as we no longer pause the video
synchronously in this case.
* media/replaceChild-should-not-pause-video-expected.txt: Added.
* media/replaceChild-should-not-pause-video.html: Added.
Add test case to make sure that calling replaceChild() on with a video
element as newChild does not pause the video if it is already playing.
This is a regression test for <rdar://problem/25888758>.
* webaudio/audiocontext-state-interrupted-expected.txt:
* webaudio/audiocontext-state-interrupted.html:
Add a missing call to
internals.setMediaSessionRestrictions("WebAudio", "InterruptedPlaybackNotPermitted")
before the fourth test. The fourth test was previously passing by
chance, due to a bug in GenericEventQueue sometimes firing events
synchronously after they are scheduled.
Modified Paths
Added Paths
Diff
Modified: trunk/LayoutTests/ChangeLog (200430 => 200431)
--- trunk/LayoutTests/ChangeLog 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/ChangeLog 2016-05-04 21:33:45 UTC (rev 200431)
@@ -1,3 +1,31 @@
+2016-05-04 Chris Dumez <[email protected]>
+
+ Media elements should not be paused right away when removed from the document
+ https://bugs.webkit.org/show_bug.cgi?id=157347
+ <rdar://problem/25888758>
+
+ Reviewed by Alex Christensen.
+
+ * media/remove-from-document-expected.txt:
+ * media/remove-from-document.html:
+ Check asynchronously if the video has been paused after removing it from
+ the document instead of synchronously as we no longer pause the video
+ synchronously in this case.
+
+ * media/replaceChild-should-not-pause-video-expected.txt: Added.
+ * media/replaceChild-should-not-pause-video.html: Added.
+ Add test case to make sure that calling replaceChild() on with a video
+ element as newChild does not pause the video if it is already playing.
+ This is a regression test for <rdar://problem/25888758>.
+
+ * webaudio/audiocontext-state-interrupted-expected.txt:
+ * webaudio/audiocontext-state-interrupted.html:
+ Add a missing call to
+ internals.setMediaSessionRestrictions("WebAudio", "InterruptedPlaybackNotPermitted")
+ before the fourth test. The fourth test was previously passing by
+ chance, due to a bug in GenericEventQueue sometimes firing events
+ synchronously after they are scheduled.
+
2016-05-04 Filip Pizlo <[email protected]>
Speed up JSGlobalObject initialization by making some properties lazy
Modified: trunk/LayoutTests/imported/w3c/ChangeLog (200430 => 200431)
--- trunk/LayoutTests/imported/w3c/ChangeLog 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/imported/w3c/ChangeLog 2016-05-04 21:33:45 UTC (rev 200431)
@@ -1,3 +1,17 @@
+2016-05-04 Chris Dumez <[email protected]>
+
+ Media elements should not be paused right away when removed from the document
+ https://bugs.webkit.org/show_bug.cgi?id=157347
+ <rdar://problem/25888758>
+
+ Reviewed by Alex Christensen.
+
+ Rebaseline now that more W3C tests are passing.
+
+ * web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-to-other-document-expected.txt:
+ * web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-within-document-expected.txt:
+ * web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-remove-from-document-expected.txt:
+
2016-05-04 Joseph Pecoraro <[email protected]>
Improve the grammar of some error messages 'a argument list' => 'an argument list'
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-to-other-document-expected.txt (200430 => 200431)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-to-other-document-expected.txt 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-to-other-document-expected.txt 2016-05-04 21:33:45 UTC (rev 200431)
@@ -1,3 +1,3 @@
-FAIL paused state when moving to other document assert_false: paused after moving expected false got true
+PASS paused state when moving to other document
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-within-document-expected.txt (200430 => 200431)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-within-document-expected.txt 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-move-within-document-expected.txt 2016-05-04 21:33:45 UTC (rev 200431)
@@ -1,3 +1,3 @@
-FAIL paused state when moving within a document assert_false: paused after moving expected false got true
+PASS paused state when moving within a document
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-remove-from-document-expected.txt (200430 => 200431)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-remove-from-document-expected.txt 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/playing-the-media-resource/pause-remove-from-document-expected.txt 2016-05-04 21:33:45 UTC (rev 200431)
@@ -1,3 +1,3 @@
-FAIL paused state when removing from a document assert_false: paused after removing expected false got true
+PASS paused state when removing from a document
Modified: trunk/LayoutTests/media/remove-from-document-expected.txt (200430 => 200431)
--- trunk/LayoutTests/media/remove-from-document-expected.txt 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/media/remove-from-document-expected.txt 2016-05-04 21:33:45 UTC (rev 200431)
@@ -5,6 +5,8 @@
EXPECTED (video.paused == 'false') OK
RUN(document.body.removeChild(video))
EXPECTED (video.networkState != '0') OK
+EXPECTED (video.paused == 'false') OK
+EXPECTED (video.networkState != '0') OK
EXPECTED (video.paused == 'true') OK
END OF TEST
Modified: trunk/LayoutTests/media/remove-from-document.html (200430 => 200431)
--- trunk/LayoutTests/media/remove-from-document.html 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/media/remove-from-document.html 2016-05-04 21:33:45 UTC (rev 200431)
@@ -19,10 +19,15 @@
run("document.body.removeChild(video)");
testExpected("video.networkState", HTMLMediaElement.NETWORK_EMPTY, "!=");
- testExpected("video.paused", true);
-
- document.body.offsetTop;
- endTest();
+ testExpected("video.paused", false);
+
+ setTimeout(function() {
+ testExpected("video.networkState", HTMLMediaElement.NETWORK_EMPTY, "!=");
+ testExpected("video.paused", true);
+
+ document.body.offsetTop;
+ endTest();
+ }, 0);
}
window.addEventListener('load', doSetup, false);
Added: trunk/LayoutTests/media/replaceChild-should-not-pause-video-expected.txt (0 => 200431)
--- trunk/LayoutTests/media/replaceChild-should-not-pause-video-expected.txt (rev 0)
+++ trunk/LayoutTests/media/replaceChild-should-not-pause-video-expected.txt 2016-05-04 21:33:45 UTC (rev 200431)
@@ -0,0 +1,17 @@
+Test that a video element does not get paused when moved using replaceChild().
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+EVENT(canplay)
+video.play()
+EVENT(playing)
+PASS video.paused is false
+parentA.replaceChild(video, childToReplace)
+PASS video.paused is false
+PASS video.paused is false
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
+
Added: trunk/LayoutTests/media/replaceChild-should-not-pause-video.html (0 => 200431)
--- trunk/LayoutTests/media/replaceChild-should-not-pause-video.html (rev 0)
+++ trunk/LayoutTests/media/replaceChild-should-not-pause-video.html 2016-05-04 21:33:45 UTC (rev 200431)
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+<script src=""
+</head>
+<body _onload_="start()">
+<div id="parentA">
+ <div id="childToReplace"></div>
+</div>
+<div id="parentB">
+ <video id="vid"></video>
+</div>
+
+<script>
+description("Test that a video element does not get paused when moved using replaceChild().");
+jsTestIsAsync = true;
+
+var video = document.getElementsByTagName('video')[0];
+
+function paused()
+{
+ testFailed("Video was paused");
+}
+
+video.addEventListener("pause", paused);
+
+function playing()
+{
+ debug("EVENT(playing)");
+ shouldBeFalse("video.paused");
+ parentA = document.getElementById("parentA");
+ childToReplace = document.getElementById("childToReplace");
+ evalAndLog("parentA.replaceChild(video, childToReplace)");
+ shouldBeFalse("video.paused");
+ setTimeout(function() {
+ shouldBeFalse("video.paused");
+ finishJSTest();
+ }, 0);
+}
+
+function canplay()
+{
+ debug("EVENT(canplay)");
+ video.addEventListener("playing", playing);
+ evalAndLog("video.play()");
+}
+
+function start()
+{
+ video.addEventListener("canplay", canplay);
+ video.src = "" "content/test");
+}
+</script>
+<script src=""
+</body>
+</html>
+
Modified: trunk/LayoutTests/webaudio/audiocontext-state-interrupted-expected.txt (200430 => 200431)
--- trunk/LayoutTests/webaudio/audiocontext-state-interrupted-expected.txt 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/webaudio/audiocontext-state-interrupted-expected.txt 2016-05-04 21:33:45 UTC (rev 200431)
@@ -37,6 +37,7 @@
PASS context.state is "suspended"
Test 4: resume() while interrupted will not resume playback after an interruption.
+internals.setMediaSessionRestrictions("WebAudio", "InterruptedPlaybackNotPermitted")
internals.beginMediaSessionInterruption("System")
EVENT statechange
PASS context.state is "interrupted"
Modified: trunk/LayoutTests/webaudio/audiocontext-state-interrupted.html (200430 => 200431)
--- trunk/LayoutTests/webaudio/audiocontext-state-interrupted.html 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/LayoutTests/webaudio/audiocontext-state-interrupted.html 2016-05-04 21:33:45 UTC (rev 200431)
@@ -117,6 +117,9 @@
debug('');
debug('Test 4: resume() while interrupted will not resume playback after an interruption.')
+ if (window.internals)
+ evalAndLog('internals.setMediaSessionRestrictions("WebAudio", "InterruptedPlaybackNotPermitted")');
+
context._onstatechange_ = fourthInterruptionStarted;
if (window.internals)
evalAndLog('internals.beginMediaSessionInterruption("System")');
Modified: trunk/Source/WebCore/ChangeLog (200430 => 200431)
--- trunk/Source/WebCore/ChangeLog 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/Source/WebCore/ChangeLog 2016-05-04 21:33:45 UTC (rev 200431)
@@ -1,3 +1,51 @@
+2016-05-04 Chris Dumez <[email protected]>
+
+ Media elements should not be paused right away when removed from the document
+ https://bugs.webkit.org/show_bug.cgi?id=157347
+ <rdar://problem/25888758>
+
+ Reviewed by Alex Christensen.
+
+ Media elements should not be paused right away when removed from the document.
+ Instead we should allow the task that removed the media element from the
+ document to finish because considering pausing.
+
+ This avoid inadvertently pausing media elements when the JS merely moves them
+ on the page (e.g. using Node.replaceChild()).
+
+ Text from the HTML specification:
+ """
+ When a media element is removed from a Document, the user agent must run the
+ following steps:
+ 1. Await a stable state, allowing the task that removed the media element
+ from the Document to continue. The synchronous section consists of all the
+ remaining steps of this algorithm. (Steps in the synchronous section are
+ marked with ⌛.)
+ 2. ⌛ If the media element is in a Document, abort these steps.
+ 3. ⌛ Run the internal pause steps for the media element.
+ """
+
+ c.f. https://html.spec.whatwg.org/multipage/embedded-content.html#htmlmediaelement
+
+ Test: media/replaceChild-should-not-pause-video.html
+
+ * dom/GenericEventQueue.cpp:
+ (WebCore::GenericEventQueue::sharedTimerFired):
+ Copy the queue of events before processing it so that we don't fire events that
+ get scheduled by the event handlers as a result of us firing the pending events.
+ Otherwise, we end up firing events synchronously right after they've been queued,
+ which is wrong. This was causing several W3C tests to fail.
+
+ * html/HTMLMediaElement.cpp:
+ (WebCore::HTMLMediaElement::HTMLMediaElement):
+ (WebCore::HTMLMediaElement::pauseAfterDetachedTimerFired):
+ (WebCore::HTMLMediaElement::removedFrom):
+ * html/HTMLMediaElement.h:
+ After the media element gets removed from the document, schedule a 0 timer before
+ pausing the media element, to give the task that removed us a chance to finish.
+ When the timer fires, we check if we were added back into an active document and
+ avoid pausing in such case.
+
2016-05-04 Filip Pizlo <[email protected]>
Speed up JSGlobalObject initialization by making some properties lazy
Modified: trunk/Source/WebCore/dom/GenericEventQueue.cpp (200430 => 200431)
--- trunk/Source/WebCore/dom/GenericEventQueue.cpp 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/Source/WebCore/dom/GenericEventQueue.cpp 2016-05-04 21:33:45 UTC (rev 200431)
@@ -75,15 +75,16 @@
ASSERT(!sharedTimer().isActive());
ASSERT(!pendingQueues().isEmpty());
- while (!pendingQueues().isEmpty()) {
- WeakPtr<GenericEventQueue> queue = pendingQueues().takeFirst();
+ // Copy the pending events first because we don't want to process synchronously the new events
+ // queued by the JS events handlers that are executed in the loop below.
+ Deque<WeakPtr<GenericEventQueue>> queuedEvents;
+ std::swap(queuedEvents, pendingQueues());
+ while (!queuedEvents.isEmpty()) {
+ WeakPtr<GenericEventQueue> queue = queuedEvents.takeFirst();
if (!queue)
continue;
queue->dispatchOneEvent();
}
-
- if (sharedTimer().isActive())
- sharedTimer().stop();
}
Deque<WeakPtr<GenericEventQueue>>& GenericEventQueue::pendingQueues()
Modified: trunk/Source/WebCore/html/HTMLMediaElement.cpp (200430 => 200431)
--- trunk/Source/WebCore/html/HTMLMediaElement.cpp 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/Source/WebCore/html/HTMLMediaElement.cpp 2016-05-04 21:33:45 UTC (rev 200431)
@@ -348,6 +348,7 @@
, m_progressEventTimer(*this, &HTMLMediaElement::progressEventTimerFired)
, m_playbackProgressTimer(*this, &HTMLMediaElement::playbackProgressTimerFired)
, m_scanTimer(*this, &HTMLMediaElement::scanTimerFired)
+ , m_pauseAfterDetachedTimer(*this, &HTMLMediaElement::pauseAfterDetachedTimerFired)
, m_seekTaskQueue(document)
, m_resizeTaskQueue(document)
, m_shadowDOMTaskQueue(document)
@@ -783,32 +784,43 @@
return InsertionDone;
}
+void HTMLMediaElement::pauseAfterDetachedTimerFired()
+{
+ // If we were re-inserted into an active document, no need to pause.
+ if (m_inActiveDocument)
+ return;
+
+ if (hasMediaControls())
+ mediaControls()->hide();
+ if (m_networkState > NETWORK_EMPTY)
+ pause();
+ if (m_videoFullscreenMode != VideoFullscreenModeNone)
+ exitFullscreen();
+
+ if (!m_player)
+ return;
+
+ size_t extraMemoryCost = m_player->extraMemoryCost();
+ if (extraMemoryCost > m_reportedExtraMemoryCost) {
+ JSC::VM& vm = JSDOMWindowBase::commonVM();
+ JSC::JSLockHolder lock(vm);
+
+ size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
+ m_reportedExtraMemoryCost = extraMemoryCost;
+ // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
+ // https://bugs.webkit.org/show_bug.cgi?id=142595
+ vm.heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
+ }
+}
+
void HTMLMediaElement::removedFrom(ContainerNode& insertionPoint)
{
LOG(Media, "HTMLMediaElement::removedFrom(%p)", this);
m_inActiveDocument = false;
if (insertionPoint.inDocument()) {
- if (hasMediaControls())
- mediaControls()->hide();
- if (m_networkState > NETWORK_EMPTY)
- pause();
- if (m_videoFullscreenMode != VideoFullscreenModeNone)
- exitFullscreen();
-
- if (m_player) {
- size_t extraMemoryCost = m_player->extraMemoryCost();
- if (extraMemoryCost > m_reportedExtraMemoryCost) {
- JSC::VM& vm = JSDOMWindowBase::commonVM();
- JSC::JSLockHolder lock(vm);
-
- size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
- m_reportedExtraMemoryCost = extraMemoryCost;
- // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
- // https://bugs.webkit.org/show_bug.cgi?id=142595
- vm.heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
- }
- }
+ // Pause asynchronously to let the operation that removed us finish, in case we get inserted back into a document.
+ m_pauseAfterDetachedTimer.startOneShot(0);
}
HTMLElement::removedFrom(insertionPoint);
Modified: trunk/Source/WebCore/html/HTMLMediaElement.h (200430 => 200431)
--- trunk/Source/WebCore/html/HTMLMediaElement.h 2016-05-04 21:21:36 UTC (rev 200430)
+++ trunk/Source/WebCore/html/HTMLMediaElement.h 2016-05-04 21:33:45 UTC (rev 200431)
@@ -772,10 +772,13 @@
void isVisibleInViewportChanged() final;
void updateShouldAutoplay();
+ void pauseAfterDetachedTimerFired();
+
Timer m_pendingActionTimer;
Timer m_progressEventTimer;
Timer m_playbackProgressTimer;
Timer m_scanTimer;
+ Timer m_pauseAfterDetachedTimer;
GenericTaskQueue<ScriptExecutionContext> m_seekTaskQueue;
GenericTaskQueue<ScriptExecutionContext> m_resizeTaskQueue;
GenericTaskQueue<ScriptExecutionContext> m_shadowDOMTaskQueue;