- Revision
- 252692
- Author
- [email protected]
- Date
- 2019-11-20 10:32:02 -0800 (Wed, 20 Nov 2019)
Log Message
HTML5 audio .ended event not fired when app in background or phone screen is off
https://bugs.webkit.org/show_bug.cgi?id=173332
<rdar://problem/32757402>
Reviewed by Eric Carlson.
Source/WebCore:
Test: media/audio-background-playback-playlist.html
In addition to the necessary WebKit part of this patch, there are behaviors which prevent
websites from enqueuing new playback while in the background. Namely, the platform will
prevent background playback from any application which is not currently the "Now Playing"
application, so in order to allow pages to switch sources, we must ensure we do not give
up "Now Playing" status. To do so, we will change the implementation of canProduceAudio()
to include any media element which previously could produce audio but currently has no
source.
Also, MediaElementSession::canShowControlsManager() will be modified to only check for
a RequireUserGestureToControlControlsManager restriction if the purpose passed in is
is "ControlsManager" and not "NowPlaying".
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::canProduceAudio const):
* html/MediaElementSession.cpp:
(WebCore::MediaElementSession::canShowControlsManager const):
Source/WebKit:
When a WebPage goes from audible to inaudible, allow a short grace period before removing
the activity token, to give the page a chance to (e.g.) move to the next item in a playlist
before the process is suspended when in the background.
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::close):
(WebKit::WebPageProxy::updateThrottleState):
(WebKit::WebPageProxy::clearAudibleActivity):
* UIProcess/WebPageProxy.h:
LayoutTests:
* media/audio-background-playback-playlist-expected.txt: Added.
* media/audio-background-playback-playlist.html: Added.
Modified Paths
Added Paths
Diff
Modified: trunk/LayoutTests/ChangeLog (252691 => 252692)
--- trunk/LayoutTests/ChangeLog 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/LayoutTests/ChangeLog 2019-11-20 18:32:02 UTC (rev 252692)
@@ -1,3 +1,14 @@
+2019-11-20 Jer Noble <[email protected]>
+
+ HTML5 audio .ended event not fired when app in background or phone screen is off
+ https://bugs.webkit.org/show_bug.cgi?id=173332
+ <rdar://problem/32757402>
+
+ Reviewed by Eric Carlson.
+
+ * media/audio-background-playback-playlist-expected.txt: Added.
+ * media/audio-background-playback-playlist.html: Added.
+
2019-11-20 Simon Fraser <[email protected]>
REGRESSION (r252161): box-shadow with inset and rounded borders is clipped
Added: trunk/LayoutTests/media/audio-background-playback-playlist-expected.txt (0 => 252692)
--- trunk/LayoutTests/media/audio-background-playback-playlist-expected.txt (rev 0)
+++ trunk/LayoutTests/media/audio-background-playback-playlist-expected.txt 2019-11-20 18:32:02 UTC (rev 252692)
@@ -0,0 +1,21 @@
+
+RUN(internals.setMediaElementRestrictions(audio, "RequireUserGestureForAudioRateChange,RequireUserGestureToControlControlsManager"))
+RUN(audio.src = "" "content/test"))
+EVENT(canplaythrough)
+RUN(audio.play())
+EVENT(playing)
+EXPECTED (internals.bestMediaElementForShowingPlaybackControlsManager("NowPlaying") == '[object HTMLAudioElement]') OK
+RUN(internals.applicationDidEnterBackground(true))
+RUN(audio.currentTime = audio.duration - 0.1)
+EVENT(ended)
+RUN(audio.src = ""
+RUN(audio.load())
+EXPECTED (internals.bestMediaElementForShowingPlaybackControlsManager("NowPlaying") == '[object HTMLAudioElement]') OK
+RUN(audio.src = "" "content/test"))
+RUN(audio.load())
+EVENT(canplaythrough)
+RUN(audio.play())
+EVENT(playing)
+EXPECTED (internals.bestMediaElementForShowingPlaybackControlsManager("NowPlaying") == '[object HTMLAudioElement]') OK
+END OF TEST
+
Added: trunk/LayoutTests/media/audio-background-playback-playlist.html (0 => 252692)
--- trunk/LayoutTests/media/audio-background-playback-playlist.html (rev 0)
+++ trunk/LayoutTests/media/audio-background-playback-playlist.html 2019-11-20 18:32:02 UTC (rev 252692)
@@ -0,0 +1,35 @@
+<html>
+<head>
+ <script src=""
+ <script src=""
+ <script>
+ window.addEventListener('load', async event => {
+ window.audio = document.querySelector('audio');
+ run('internals.setMediaElementRestrictions(audio, "RequireUserGestureForAudioRateChange,RequireUserGestureToControlControlsManager")');
+ run('audio.src = "" "content/test")');
+
+ await waitFor(audio, 'canplaythrough');
+ runWithKeyDown('audio.play()');
+ await waitFor(audio, 'playing');
+ testExpected('internals.bestMediaElementForShowingPlaybackControlsManager("NowPlaying")', audio);
+ run('internals.applicationDidEnterBackground(true)');
+ run('audio.currentTime = audio.duration - 0.1')
+ await waitFor(audio, 'ended');
+ run('audio.src = ""
+ run('audio.load()');
+ testExpected('internals.bestMediaElementForShowingPlaybackControlsManager("NowPlaying")', audio);
+ run('audio.src = "" "content/test")');
+ run('audio.load()');
+ await waitFor(audio, 'canplaythrough');
+ run('audio.play()');
+ await waitFor(audio, 'playing');
+ testExpected('internals.bestMediaElementForShowingPlaybackControlsManager("NowPlaying")', audio);
+ endTest();
+ });
+ </script>
+</head>
+
+<body>
+ <audio controls><audio>
+</body>
+</html>
Modified: trunk/Source/WebCore/ChangeLog (252691 => 252692)
--- trunk/Source/WebCore/ChangeLog 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/Source/WebCore/ChangeLog 2019-11-20 18:32:02 UTC (rev 252692)
@@ -1,3 +1,30 @@
+2019-11-20 Jer Noble <[email protected]>
+
+ HTML5 audio .ended event not fired when app in background or phone screen is off
+ https://bugs.webkit.org/show_bug.cgi?id=173332
+ <rdar://problem/32757402>
+
+ Reviewed by Eric Carlson.
+
+ Test: media/audio-background-playback-playlist.html
+
+ In addition to the necessary WebKit part of this patch, there are behaviors which prevent
+ websites from enqueuing new playback while in the background. Namely, the platform will
+ prevent background playback from any application which is not currently the "Now Playing"
+ application, so in order to allow pages to switch sources, we must ensure we do not give
+ up "Now Playing" status. To do so, we will change the implementation of canProduceAudio()
+ to include any media element which previously could produce audio but currently has no
+ source.
+
+ Also, MediaElementSession::canShowControlsManager() will be modified to only check for
+ a RequireUserGestureToControlControlsManager restriction if the purpose passed in is
+ is "ControlsManager" and not "NowPlaying".
+
+ * html/HTMLMediaElement.cpp:
+ (WebCore::HTMLMediaElement::canProduceAudio const):
+ * html/MediaElementSession.cpp:
+ (WebCore::MediaElementSession::canShowControlsManager const):
+
2019-11-20 Simon Fraser <[email protected]>
REGRESSION (r252161): box-shadow with inset and rounded borders is clipped
Modified: trunk/Source/WebCore/html/HTMLMediaElement.cpp (252691 => 252692)
--- trunk/Source/WebCore/html/HTMLMediaElement.cpp 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/Source/WebCore/html/HTMLMediaElement.cpp 2019-11-20 18:32:02 UTC (rev 252692)
@@ -7601,7 +7601,10 @@
if (muted())
return false;
- return m_player && m_readyState >= HAVE_METADATA && hasAudio();
+ if (m_player && m_readyState >= HAVE_METADATA)
+ return hasAudio();
+
+ return hasEverHadAudio();
}
bool HTMLMediaElement::isSuspended() const
Modified: trunk/Source/WebCore/html/MediaElementSession.cpp (252691 => 252692)
--- trunk/Source/WebCore/html/MediaElementSession.cpp 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/Source/WebCore/html/MediaElementSession.cpp 2019-11-20 18:32:02 UTC (rev 252692)
@@ -436,7 +436,7 @@
return true;
}
- if (client().presentationType() == Audio) {
+ if (client().presentationType() == Audio && purpose == PlaybackControlsPurpose::ControlsManager) {
if (!hasBehaviorRestriction(RequireUserGestureToControlControlsManager) || m_element.document().processingUserGestureForMedia()) {
INFO_LOG(LOGIDENTIFIER, "returning TRUE: audio element with user gesture");
return true;
Modified: trunk/Source/WebKit/ChangeLog (252691 => 252692)
--- trunk/Source/WebKit/ChangeLog 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/Source/WebKit/ChangeLog 2019-11-20 18:32:02 UTC (rev 252692)
@@ -1,3 +1,21 @@
+2019-11-20 Jer Noble <[email protected]>
+
+ HTML5 audio .ended event not fired when app in background or phone screen is off
+ https://bugs.webkit.org/show_bug.cgi?id=173332
+ <rdar://problem/32757402>
+
+ Reviewed by Eric Carlson.
+
+ When a WebPage goes from audible to inaudible, allow a short grace period before removing
+ the activity token, to give the page a chance to (e.g.) move to the next item in a playlist
+ before the process is suspended when in the background.
+
+ * UIProcess/WebPageProxy.cpp:
+ (WebKit::WebPageProxy::close):
+ (WebKit::WebPageProxy::updateThrottleState):
+ (WebKit::WebPageProxy::clearAudibleActivity):
+ * UIProcess/WebPageProxy.h:
+
2019-11-19 Chris Dumez <[email protected]>
Unreviewed, fix webkitpy failures after r252655.
Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (252691 => 252692)
--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp 2019-11-20 18:32:02 UTC (rev 252692)
@@ -269,6 +269,7 @@
static const Seconds resetRecentCrashCountDelay = 30_s;
static unsigned maximumWebProcessRelaunchAttempts = 1;
+static const Seconds audibleActivityClearDelay = 10_s;
namespace WebKit {
using namespace WebCore;
@@ -429,6 +430,7 @@
, m_notificationPermissionRequestManager(*this)
#if PLATFORM(IOS_FAMILY)
, m_alwaysRunsAtForegroundPriority(m_configuration->alwaysRunsAtForegroundPriority())
+ , m_audibleActivityTimer(RunLoop::main(), this, &WebPageProxy::clearAudibleActivity)
#endif
, m_initialCapitalizationEnabled(m_configuration->initialCapitalizationEnabled())
, m_cpuLimit(m_configuration->cpuLimit())
@@ -1059,6 +1061,7 @@
m_isAudibleActivity = nullptr;
m_isCapturingActivity = nullptr;
m_alwaysRunsAtForegroundPriorityActivity = nullptr;
+ m_audibleActivityTimer.stop();
#endif
stopAllURLSchemeTasks();
@@ -1949,9 +1952,11 @@
RELEASE_LOG_IF_ALLOWED(ProcessSuspension, "updateThrottleState: UIProcess is taking a foreground assertion because we are playing audio");
m_isAudibleActivity = m_process->throttler().foregroundActivity("View is playing audio"_s).moveToUniquePtr();
}
+ m_audibleActivityTimer.stop();
} else if (m_isAudibleActivity) {
- RELEASE_LOG_IF_ALLOWED(ProcessSuspension, "updateThrottleState: UIProcess is releasing a foreground assertion because we are no longer playing audio");
- m_isAudibleActivity = nullptr;
+ RELEASE_LOG_IF_ALLOWED(ProcessSuspension, "updateThrottleState: UIProcess will release a foreground assertion in %g seconds because we are no longer playing audio", audibleActivityClearDelay.seconds());
+ if (!m_audibleActivityTimer.isActive())
+ m_audibleActivityTimer.startOneShot(audibleActivityClearDelay);
}
bool isCapturingMedia = m_activityState.contains(ActivityState::IsCapturingMedia);
@@ -1977,6 +1982,14 @@
#endif
}
+#if PLATFORM(IOS_FAMILY)
+void WebPageProxy::clearAudibleActivity()
+{
+ RELEASE_LOG_IF_ALLOWED(ProcessSuspension, "updateThrottleState: UIProcess is releasing a foreground assertion because we are no longer playing audio");
+ m_isAudibleActivity = nullptr;
+}
+#endif
+
void WebPageProxy::updateHiddenPageThrottlingAutoIncreases()
{
if (!m_preferences->hiddenPageDOMTimerThrottlingAutoIncreases())
Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (252691 => 252692)
--- trunk/Source/WebKit/UIProcess/WebPageProxy.h 2019-11-20 18:25:20 UTC (rev 252691)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h 2019-11-20 18:32:02 UTC (rev 252692)
@@ -2169,6 +2169,7 @@
#if PLATFORM(IOS_FAMILY)
static bool isInHardwareKeyboardMode();
+ void clearAudibleActivity();
#endif
void makeStorageSpaceRequest(WebCore::FrameIdentifier, const String& originIdentifier, const String& databaseName, const String& displayName, uint64_t currentQuota, uint64_t currentOriginUsage, uint64_t currentDatabaseUsage, uint64_t expectedUsage, CompletionHandler<void(uint64_t)>&&);
@@ -2300,6 +2301,7 @@
std::unique_ptr<ProcessThrottler::ForegroundActivity> m_isAudibleActivity;
std::unique_ptr<ProcessThrottler::ForegroundActivity> m_isCapturingActivity;
std::unique_ptr<ProcessThrottler::ForegroundActivity> m_alwaysRunsAtForegroundPriorityActivity;
+ RunLoop::Timer<WebPageProxy> m_audibleActivityTimer;
#endif
bool m_initialCapitalizationEnabled { false };
Optional<double> m_cpuLimit;