- Revision
- 288466
- Author
- [email protected]
- Date
- 2022-01-24 14:05:22 -0800 (Mon, 24 Jan 2022)
Log Message
REGRESSION (iOS 15): HTMLAudioElement fails to load new audio when device is locked or safari is in background
https://bugs.webkit.org/show_bug.cgi?id=229688
<rdar://problem/82554226>
Reviewed by Jer Noble.
Source/WebCore:
An iOS application loses its NowPlaying status as soon as the AudioSession category
is set to Ambient, and it can only get it back again by changing the category
to Playback *and* starting playback. When an application is in the background, however,
it can't start playback unless it *already* has NowPlaying status. So, ipso facto,
background playback will always fail once the category is set to Ambient.
If an AudioSession's category is set to Playback when it is activated, it will
interrupt another application playing audio, activate Bluetooth routes, etc.
This means WebKit has to relinquish Playback when a page is unable to produce audio
because AVFoundation automatically activates the audio session when playback starts
- even if the AVPlayer is muted or has no audio.
To allow pages that were playing audio when WebKit went into the background to
continue to play after switching urls or activating another media element, add a
grace period when switch from Playback to Ambient.
Test: media/audio-session-category.html
* platform/audio/cocoa/MediaSessionManagerCocoa.mm:
(WebCore::MediaSessionManagerCocoa::updateSessionState):
Source/WebKit:
* WebProcess/GPU/media/RemoteAudioSession.cpp:
(WebKit::RemoteAudioSession::category const): Return the most recently set category
on all platforms so it is possible to test.
LayoutTests:
* fast/mediasession/metadata/audio-track-enabled.html: Wait longer for the audio session
category to be reset.
* media/audio-session-category-expected.txt: Added.
* media/audio-session-category.html: Added.
* platform/gtk/TestExpectations: Skip new test.
* platform/mac-wk1/TestExpectations: Skip new test, MediaStream isn't supported
on WK1.
Modified Paths
Added Paths
Diff
Modified: trunk/LayoutTests/ChangeLog (288465 => 288466)
--- trunk/LayoutTests/ChangeLog 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/LayoutTests/ChangeLog 2022-01-24 22:05:22 UTC (rev 288466)
@@ -1,3 +1,19 @@
+2022-01-24 Eric Carlson <[email protected]>
+
+ REGRESSION (iOS 15): HTMLAudioElement fails to load new audio when device is locked or safari is in background
+ https://bugs.webkit.org/show_bug.cgi?id=229688
+ <rdar://problem/82554226>
+
+ Reviewed by Jer Noble.
+
+ * fast/mediasession/metadata/audio-track-enabled.html: Wait longer for the audio session
+ category to be reset.
+ * media/audio-session-category-expected.txt: Added.
+ * media/audio-session-category.html: Added.
+ * platform/gtk/TestExpectations: Skip new test.
+ * platform/mac-wk1/TestExpectations: Skip new test, MediaStream isn't supported
+ on WK1.
+
2022-01-24 Youenn Fablet <[email protected]>
[ iOS Release ] fast/mediastream/mediaElement-gc.html is a flaky failure
Modified: trunk/LayoutTests/fast/mediastream/audio-track-enabled.html (288465 => 288466)
--- trunk/LayoutTests/fast/mediastream/audio-track-enabled.html 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/LayoutTests/fast/mediastream/audio-track-enabled.html 2022-01-24 22:05:22 UTC (rev 288466)
@@ -32,10 +32,10 @@
assert_equals(internals.audioSessionCategory(), "PlayAndRecord");
audioTrack.stop();
- const maxTries = 100;
+ const maxTries = 300;
let counter = 0;
while (++counter < maxTries) {
- if (internals.audioSessionCategory() != "PlayAndRecord")
+ if (internals.audioSessionCategory() == defaultCategory)
break;
await new Promise(resolve => setTimeout(resolve, 10));
}
Added: trunk/LayoutTests/media/audio-session-category-expected.txt (0 => 288466)
--- trunk/LayoutTests/media/audio-session-category-expected.txt (rev 0)
+++ trunk/LayoutTests/media/audio-session-category-expected.txt 2022-01-24 22:05:22 UTC (rev 288466)
@@ -0,0 +1,72 @@
+
+
+
+** <audio> element test **
+
+** Check category before anything has loaded.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+** Check category when a muted, paused, element has loaded.
+EVENT(canplaythrough)
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+** Check category when a muted element is playing.
+RUN(video.play())
+EVENT(playing)
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+** Check category when an unmuted element is playing.
+RUN(video.muted = false)
+EVENT(volumechange)
+EXPECTED (internals.audioSessionCategory() == 'MediaPlayback') OK
+
+** Mute the element, check again after 500ms.
+RUN(video.pause())
+RUN(video.muted = true)
+EXPECTED (internals.audioSessionCategory() == 'MediaPlayback') OK
+
+** And check again after 3 seconds.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+
+** AudioContext test **
+
+** Check category before creating AudioContext.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+** Check category after oscillator graph has been connected but not started.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+** Check category after starting oscillator.
+EXPECTED (internals.audioSessionCategory() == 'AmbientSound') OK
+
+** Close the context, check again after 500ms.
+EXPECTED (internals.audioSessionCategory() == 'AmbientSound') OK
+
+** And check again after 3 seconds.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+
+** MediaStream test **
+
+** Check category before capture begins.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+
+** Check category when capturing.
+EXPECTED (internals.audioSessionCategory() == 'PlayAndRecord') OK
+
+** Check after MediaStream is attached to audio element.
+RUN(video.play())
+EVENT(playing)
+EXPECTED (internals.audioSessionCategory() == 'PlayAndRecord') OK
+
+** Check after MediaStream muting audio track.
+EXPECTED (internals.audioSessionCategory() == 'PlayAndRecord') OK
+
+** Check 500ms after MediaStream stopping capture.
+EXPECTED (internals.audioSessionCategory() == 'PlayAndRecord') OK
+
+** And check again after 3 seconds.
+EXPECTED (internals.audioSessionCategory() == 'None') OK
+END OF TEST
+
Added: trunk/LayoutTests/media/audio-session-category.html (0 => 288466)
--- trunk/LayoutTests/media/audio-session-category.html (rev 0)
+++ trunk/LayoutTests/media/audio-session-category.html 2022-01-24 22:05:22 UTC (rev 288466)
@@ -0,0 +1,142 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>audio-session-category</title>
+ <script src=''></script>
+ <script src=''></script>
+ <script>
+
+ async function waitForCategory(category, duration, message)
+ {
+ consoleWrite(message);
+
+ const maxTries = duration * 1000 / 10;
+ let counter = 0;
+ while (++counter < maxTries) {
+ if (internals.audioSessionCategory() == category)
+ break;
+ await new Promise(resolve => setTimeout(resolve, 10));
+ }
+
+ testExpected('internals.audioSessionCategory()', category);
+ }
+
+ async function testAudioElement()
+ {
+ consoleWrite('<br><br>** <audio> element test **');
+ await waitForCategory('None', 10, '<br>** Check category before anything has loaded.');
+
+ consoleWrite('<br>** Check category when a muted, paused, element has loaded.');
+ video.src = "" 'content/test');
+
+ await waitFor(video, 'canplaythrough');
+ testExpected('internals.audioSessionCategory()', 'None');
+
+ consoleWrite('<br>** Check category when a muted element is playing.');
+ runWithKeyDown(() => { run('video.play()') });
+ await waitFor(video, 'playing');
+ testExpected('internals.audioSessionCategory()', 'None');
+
+ consoleWrite('<br>** Check category when an unmuted element is playing.');
+ runWithKeyDown(() => { run('video.muted = false') });
+ await waitFor(video, 'volumechange');
+ testExpected('internals.audioSessionCategory()', 'MediaPlayback');
+
+ consoleWrite('<br>** Mute the element, check again after 500ms.');
+ run('video.pause()');
+ runWithKeyDown(() => { run('video.muted = true') });
+ await sleepFor(500);
+ testExpected('internals.audioSessionCategory()', 'MediaPlayback');
+
+ await waitForCategory('None', 3, '<br>** And check again after 3 seconds.');
+ video.src = '';
+ video.load();
+ }
+
+ async function testWebAudio()
+ {
+ consoleWrite('<br><br>** AudioContext test **');
+ await waitForCategory('None', 10, '<br>** Check category before creating AudioContext.');
+
+ consoleWrite('<br>** Check category after oscillator graph has been connected but not started.');
+ let context = new AudioContext();
+ let oscillator = null;
+ let gainNode = context.createGain();
+
+ oscillator = context.createOscillator();
+ oscillator.type = 'square';
+ oscillator.frequency.setValueAtTime(440, context.currentTime);
+
+ oscillator.connect(gainNode);
+ gainNode.connect(context.destination);
+ gainNode.gain.value = 0.1
+ testExpected('internals.audioSessionCategory()', 'None');
+
+ consoleWrite('<br>** Check category after starting oscillator.');
+ oscillator.start(0);
+ await sleepFor(500);
+ testExpected('internals.audioSessionCategory()', 'AmbientSound');
+
+ consoleWrite('<br>** Close the context, check again after 500ms.');
+ await context.close();
+ await sleepFor(500);
+ testExpected('internals.audioSessionCategory()', 'AmbientSound');
+
+ await waitForCategory('None', 3, '<br>** And check again after 3 seconds.');
+ }
+
+ async function testMediaStream()
+ {
+ consoleWrite('<br><br>** MediaStream test **');
+ await waitForCategory('None', 10, '<br>** Check category before capture begins.');
+
+ consoleWrite('<br>** Check category when capturing.');
+ let stream = await navigator.mediaDevices.getUserMedia({audio : true});
+ testExpected('internals.audioSessionCategory()', 'PlayAndRecord');
+
+ consoleWrite('<br>** Check after MediaStream is attached to audio element.');
+ video.srcObject = stream;
+ runWithKeyDown(() => { run('video.play()') });
+ await waitFor(video, 'playing');
+ testExpected('internals.audioSessionCategory()', 'PlayAndRecord');
+
+ consoleWrite('<br>** Check after MediaStream muting audio track.');
+ const audioTrack = stream.getAudioTracks()[0];
+ audioTrack.enabled = false;
+ await sleepFor(500);
+ testExpected('internals.audioSessionCategory()', 'PlayAndRecord');
+
+ consoleWrite('<br>** Check 500ms after MediaStream stopping capture.');
+ audioTrack.stop();
+ await sleepFor(500);
+ testExpected('internals.audioSessionCategory()', 'PlayAndRecord');
+
+ await waitForCategory('None', 3, '<br>** And check again after 3 seconds.');
+ video.src = '';
+ video.load();
+ }
+
+
+ window.addEventListener('load', async event => {
+ if (!window.internals) {
+ failTest(`<br>This test requires internals!`);
+ return;
+ }
+
+ internals.settings.setShouldManageAudioSessionCategory(true);
+
+ video = document.getElementsByTagName('audio')[0];
+
+ failTestIn(20000);
+ await testAudioElement();
+ await testWebAudio();
+ await testMediaStream();
+
+ endTest();
+ });
+ </script>
+</head>
+<body>
+ <audio muted controls></audio>
+</body>
+</html>
\ No newline at end of file
Modified: trunk/LayoutTests/platform/gtk/TestExpectations (288465 => 288466)
--- trunk/LayoutTests/platform/gtk/TestExpectations 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/LayoutTests/platform/gtk/TestExpectations 2022-01-24 22:05:22 UTC (rev 288466)
@@ -946,6 +946,9 @@
# End of Crashing tests
#////////////////////////////////////////////////////////////////////////////////////////
+# Tests behavior specific to MediaSessionManagerCocoa
+media/audio-session-category.html [ Skip ]
+
#////////////////////////////////////////////////////////////////////////////////////////
# 4. FLAKY TESTS
#////////////////////////////////////////////////////////////////////////////////////////
Modified: trunk/LayoutTests/platform/mac-wk1/TestExpectations (288465 => 288466)
--- trunk/LayoutTests/platform/mac-wk1/TestExpectations 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/LayoutTests/platform/mac-wk1/TestExpectations 2022-01-24 22:05:22 UTC (rev 288466)
@@ -64,6 +64,7 @@
inspector/page/overrideSetting-MockCaptureDevicesEnabled.html [ Skip ]
http/tests/navigation/page-cache-getUserMedia-pending-promise.html [ Skip ]
http/tests/navigation/page-cache-mediastream.html [ Skip ]
+media/audio-session-category.html [ Skip ]
http/wpt/mediarecorder [ Skip ]
imported/w3c/web-platform-tests/mediacapture-record [ Skip ]
Modified: trunk/Source/WebCore/ChangeLog (288465 => 288466)
--- trunk/Source/WebCore/ChangeLog 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/Source/WebCore/ChangeLog 2022-01-24 22:05:22 UTC (rev 288466)
@@ -1,3 +1,32 @@
+2022-01-24 Eric Carlson <[email protected]>
+
+ REGRESSION (iOS 15): HTMLAudioElement fails to load new audio when device is locked or safari is in background
+ https://bugs.webkit.org/show_bug.cgi?id=229688
+ <rdar://problem/82554226>
+
+ Reviewed by Jer Noble.
+
+ An iOS application loses its NowPlaying status as soon as the AudioSession category
+ is set to Ambient, and it can only get it back again by changing the category
+ to Playback *and* starting playback. When an application is in the background, however,
+ it can't start playback unless it *already* has NowPlaying status. So, ipso facto,
+ background playback will always fail once the category is set to Ambient.
+
+ If an AudioSession's category is set to Playback when it is activated, it will
+ interrupt another application playing audio, activate Bluetooth routes, etc.
+ This means WebKit has to relinquish Playback when a page is unable to produce audio
+ because AVFoundation automatically activates the audio session when playback starts
+ - even if the AVPlayer is muted or has no audio.
+
+ To allow pages that were playing audio when WebKit went into the background to
+ continue to play after switching urls or activating another media element, add a
+ grace period when switch from Playback to Ambient.
+
+ Test: media/audio-session-category.html
+
+ * platform/audio/cocoa/MediaSessionManagerCocoa.mm:
+ (WebCore::MediaSessionManagerCocoa::updateSessionState):
+
2022-01-24 Antti Koivisto <[email protected]>
[CSS Container Queries] Basic parsing support for container-type property
Modified: trunk/Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.h (288465 => 288466)
--- trunk/Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.h 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.h 2022-01-24 22:05:22 UTC (rev 288466)
@@ -28,9 +28,11 @@
#if PLATFORM(COCOA)
#include "AudioHardwareListener.h"
+#include "AudioSession.h"
#include "NowPlayingManager.h"
#include "PlatformMediaSessionManager.h"
#include "RemoteCommandListener.h"
+#include <wtf/RunLoop.h>
namespace WebCore {
@@ -106,6 +108,8 @@
void audioHardwareDidBecomeInactive() final { }
void audioOutputDeviceChanged() final;
+ void possiblyChangeAudioCategory();
+
bool m_nowPlayingActive { false };
bool m_registeredAsNowPlayingApplication { false };
bool m_haveEverRegisteredAsNowPlayingApplication { false };
@@ -121,6 +125,10 @@
AudioHardwareListener::BufferSizeRange m_supportedAudioHardwareBufferSizes;
size_t m_defaultBufferSize;
+
+ RunLoop::Timer<MediaSessionManagerCocoa> m_delayCategoryChangeTimer;
+ AudioSession::CategoryType m_previousCategory { AudioSession::CategoryType::None };
+ bool m_previousHadAudibleAudioOrVideoMediaType { false };
};
}
Modified: trunk/Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm (288465 => 288466)
--- trunk/Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm 2022-01-24 22:05:22 UTC (rev 288466)
@@ -28,7 +28,6 @@
#if USE(AUDIO_SESSION) && PLATFORM(COCOA)
-#import "AudioSession.h"
#import "AudioUtilities.h"
#import "DeprecatedGlobalSettings.h"
#import "HTMLMediaElement.h"
@@ -60,6 +59,7 @@
MediaSessionManagerCocoa::MediaSessionManagerCocoa()
: m_nowPlayingManager(platformStrategies()->mediaStrategy().createNowPlayingManager())
, m_defaultBufferSize(AudioSession::sharedSession().preferredBufferSize())
+ , m_delayCategoryChangeTimer(RunLoop::main(), this, &MediaSessionManagerCocoa::possiblyChangeAudioCategory)
{
ensureCodecsRegistered();
}
@@ -94,6 +94,7 @@
void MediaSessionManagerCocoa::updateSessionState()
{
+ static const Seconds delayBeforeSettingCategoryNone = { 2_s };
int videoCount = 0;
int videoAudioCount = 0;
int audioCount = 0;
@@ -164,10 +165,28 @@
} else if (webAudioCount)
category = AudioSession::CategoryType::AmbientSound;
+ if (category == AudioSession::CategoryType::None && m_previousCategory != AudioSession::CategoryType::None) {
+ if (!m_delayCategoryChangeTimer.isActive()) {
+ m_delayCategoryChangeTimer.startOneShot(delayBeforeSettingCategoryNone);
+ ALWAYS_LOG(LOGIDENTIFIER, "setting timer");
+ }
+ category = m_previousCategory;
+ } else
+ m_delayCategoryChangeTimer.stop();
+
+ m_previousCategory = category;
+
ALWAYS_LOG(LOGIDENTIFIER, "setting category = ", category, ", policy = ", policy);
AudioSession::sharedSession().setCategory(category, policy);
}
+void MediaSessionManagerCocoa::possiblyChangeAudioCategory()
+{
+ ALWAYS_LOG(LOGIDENTIFIER);
+ m_previousCategory = AudioSession::CategoryType::None;
+ updateSessionState();
+}
+
void MediaSessionManagerCocoa::beginInterruption(PlatformMediaSession::InterruptionType type)
{
if (type == PlatformMediaSession::InterruptionType::SystemInterruption) {
Modified: trunk/Source/WebKit/ChangeLog (288465 => 288466)
--- trunk/Source/WebKit/ChangeLog 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/Source/WebKit/ChangeLog 2022-01-24 22:05:22 UTC (rev 288466)
@@ -1,3 +1,15 @@
+2022-01-24 Eric Carlson <[email protected]>
+
+ REGRESSION (iOS 15): HTMLAudioElement fails to load new audio when device is locked or safari is in background
+ https://bugs.webkit.org/show_bug.cgi?id=229688
+ <rdar://problem/82554226>
+
+ Reviewed by Jer Noble.
+
+ * WebProcess/GPU/media/RemoteAudioSession.cpp:
+ (WebKit::RemoteAudioSession::category const): Return the most recently set category
+ on all platforms so it is possible to test.
+
2022-01-24 David Quesada <[email protected]>
Simplify accesses to SystemVersion.plist
Modified: trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioSession.cpp (288465 => 288466)
--- trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioSession.cpp 2022-01-24 22:02:34 UTC (rev 288465)
+++ trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioSession.cpp 2022-01-24 22:05:22 UTC (rev 288466)
@@ -141,11 +141,7 @@
AudioSession::CategoryType RemoteAudioSession::category() const
{
-#if PLATFORM(COCOA)
return m_category;
-#else
- return AudioSession::CategoryType::None;
-#endif
}
void RemoteAudioSession::configurationChanged(RemoteAudioSessionConfiguration&& configuration)