Title: [271779] trunk
Revision
271779
Author
eric.carl...@apple.com
Date
2021-01-23 12:31:36 -0800 (Sat, 23 Jan 2021)

Log Message

[iOS] Set background playback restriction for WebAudio
https://bugs.webkit.org/show_bug.cgi?id=220879
<rdar://72949281>

Reviewed by Jer Noble.
Source/WebCore:

WebAudio does not play in the background on iOS both because audio doesn't play
when the category is set to `AVAudioSessionCategoryAmbient`, and because the
AudioContext is paused when the web process is forwarded the
`UIApplicationWillResignActiveNotification` notification.

This notification isn't forwarded when the WebView is hosted in SafariViewController
so the AudioContext is not paused, so it resumes playing when the application is
foregrounded even if the view controller is not in the foreground.

Fix this by setting the `BackgroundProcessPlaybackRestricted` restriction for
WebAudio sessions so the context is always paused in the background.

Test: media/webaudio-background-playback.html

* platform/audio/ios/MediaSessionManagerIOS.mm:
(WebCore::MediaSessionManageriOS::resetRestrictions):

LayoutTests:

* media/media-session-restrictions-expected.txt: Update expectations.
* media/webaudio-background-playback-expected.txt: Added.
* media/webaudio-background-playback.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (271778 => 271779)


--- trunk/LayoutTests/ChangeLog	2021-01-23 15:35:06 UTC (rev 271778)
+++ trunk/LayoutTests/ChangeLog	2021-01-23 20:31:36 UTC (rev 271779)
@@ -1,3 +1,15 @@
+2021-01-23  Eric Carlson  <eric.carl...@apple.com>
+
+        [iOS] Set background playback restriction for WebAudio
+        https://bugs.webkit.org/show_bug.cgi?id=220879
+        <rdar://72949281>
+
+        Reviewed by Jer Noble.
+
+        * media/media-session-restrictions-expected.txt: Update expectations.
+        * media/webaudio-background-playback-expected.txt: Added.
+        * media/webaudio-background-playback.html: Added.
+
 2021-01-23  Per Arne Vollan  <pvol...@apple.com>
 
         [macOS] Deny mach-lookup to the service 'com.apple.iconservices.store' in the WebContent process

Added: trunk/LayoutTests/media/webaudio-background-playback-expected.txt (0 => 271779)


--- trunk/LayoutTests/media/webaudio-background-playback-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/media/webaudio-background-playback-expected.txt	2021-01-23 20:31:36 UTC (rev 271779)
@@ -0,0 +1,3 @@
+
+PASS Ensure WebAudio stops playing in the background when the 'BackgroundProcessPlaybackRestricted' restriction is set
+

Added: trunk/LayoutTests/media/webaudio-background-playback.html (0 => 271779)


--- trunk/LayoutTests/media/webaudio-background-playback.html	                        (rev 0)
+++ trunk/LayoutTests/media/webaudio-background-playback.html	2021-01-23 20:31:36 UTC (rev 271779)
@@ -0,0 +1,96 @@
+<!doctype html>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <title>Test WebAudio background playback restriction</title>
+        <script src=""
+        <script src=""
+        <script src=""
+    </head>
+    <body>
+        <script>
+
+promise_test(async (test) => {
+    if (!window.internals)
+        return Promise.reject("Test requires internals API");
+
+    if (window.internals)
+        internals.setMediaSessionRestrictions('webaudio', 'BackgroundProcessPlaybackRestricted');
+
+    let context = new AudioContext();
+    let source = context.createBufferSource();
+    let analyser = context.createAnalyser();
+    let processor = context.createScriptProcessor(1024, 1, 1);
+
+    source.connect(context.destination);
+    source.connect(analyser);
+    analyser.connect(processor);
+    processor.connect(context.destination);
+
+    let _onProcessCount_ = 0;
+    processor._onaudioprocess_ = () => {
+        ++onProcessCount;
+    }
+
+    audioBuffer = await new Promise((resolve, reject) => {
+        let request = new XMLHttpRequest();
+        let url = "" "content/silence");
+        request.open('GET', url, true);
+        request.responseType = 'arraybuffer';
+
+        request._onload_ = () => {
+            if (request.status === 200 || request.status === 0) {
+                context.decodeAudioData(request.response, (buffer) => {
+                    resolve(buffer);
+                }, () => {
+                    reject("Error loading media file");
+                });
+            }
+        }
+
+        request._onerror_ = (event) => {
+          reject("Error loading media file");
+        }
+
+        request.send();
+    });
+
+    source.buffer = audioBuffer;
+    source.loop = true;
+    source.start(0);
+    
+    let counter = 0;
+    while (!onProcessCount && ++counter < 20)
+        await new Promise(resolve => setTimeout(resolve, 50));
+
+    assert_true(onProcessCount > 0, "audio is playing");
+
+    let oldOnProcessCount = onProcessCount;
+    await new Promise(resolve => setTimeout(resolve, 100));
+    assert_true(onProcessCount > oldOnProcessCount, "audio continues to play");
+
+    if (window.internals)
+        internals.applicationDidEnterBackground();
+
+    counter = 0;
+    while (++counter < 20) {
+        oldOnProcessCount = onProcessCount;
+        await new Promise(resolve => setTimeout(resolve, 50));
+        if (oldOnProcessCount == onProcessCount)
+            break;
+    }
+
+    assert_true(_onProcessCount_ == oldOnProcessCount, "audio playback suspended in background");
+
+    if (window.internals)
+        internals.applicationWillEnterForeground();
+    oldOnProcessCount = onProcessCount;
+    await new Promise(resolve => setTimeout(resolve, 100));
+    assert_true(onProcessCount > oldOnProcessCount, "audio resumed playing in the foreground");
+
+
+}, "Ensure WebAudio stops playing in the background when the 'BackgroundProcessPlaybackRestricted' restriction is set");
+
+        </script>
+    </body>
+</html>
\ No newline at end of file

Modified: trunk/LayoutTests/platform/ios/media/media-session-restrictions-expected.txt (271778 => 271779)


--- trunk/LayoutTests/platform/ios/media/media-session-restrictions-expected.txt	2021-01-23 15:35:06 UTC (rev 271778)
+++ trunk/LayoutTests/platform/ios/media/media-session-restrictions-expected.txt	2021-01-23 20:31:36 UTC (rev 271779)
@@ -1,6 +1,6 @@
 mediaSessionRestrictions["video"] = "backgroundprocessplaybackrestricted,backgroundtabplaybackrestricted"
 mediaSessionRestrictions["audio"] = ""
 mediaSessionRestrictions["videoaudio"] = "concurrentplaybacknotpermitted,backgroundprocessplaybackrestricted"
-mediaSessionRestrictions["webaudio"] = ""
+mediaSessionRestrictions["webaudio"] = "backgroundprocessplaybackrestricted"
 END OF TEST
 

Modified: trunk/Source/WebCore/ChangeLog (271778 => 271779)


--- trunk/Source/WebCore/ChangeLog	2021-01-23 15:35:06 UTC (rev 271778)
+++ trunk/Source/WebCore/ChangeLog	2021-01-23 20:31:36 UTC (rev 271779)
@@ -1,3 +1,28 @@
+2021-01-23  Eric Carlson  <eric.carl...@apple.com>
+
+        [iOS] Set background playback restriction for WebAudio
+        https://bugs.webkit.org/show_bug.cgi?id=220879
+        <rdar://72949281>
+
+        Reviewed by Jer Noble.
+        
+        WebAudio does not play in the background on iOS both because audio doesn't play
+        when the category is set to `AVAudioSessionCategoryAmbient`, and because the
+        AudioContext is paused when the web process is forwarded the 
+        `UIApplicationWillResignActiveNotification` notification. 
+
+        This notification isn't forwarded when the WebView is hosted in SafariViewController
+        so the AudioContext is not paused, so it resumes playing when the application is
+        foregrounded even if the view controller is not in the foreground.
+
+        Fix this by setting the `BackgroundProcessPlaybackRestricted` restriction for 
+        WebAudio sessions so the context is always paused in the background.
+
+        Test: media/webaudio-background-playback.html
+
+        * platform/audio/ios/MediaSessionManagerIOS.mm:
+        (WebCore::MediaSessionManageriOS::resetRestrictions):
+
 2021-01-23  Zalan Bujtas  <za...@apple.com>
 
         [LFC][Integration] Disable inline boxes with layers

Modified: trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm (271778 => 271779)


--- trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm	2021-01-23 15:35:06 UTC (rev 271778)
+++ trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm	2021-01-23 20:31:36 UTC (rev 271779)
@@ -77,6 +77,7 @@
     }
 
     addRestriction(PlatformMediaSession::MediaType::Video, BackgroundProcessPlaybackRestricted);
+    addRestriction(PlatformMediaSession::MediaType::WebAudio, BackgroundProcessPlaybackRestricted);
     addRestriction(PlatformMediaSession::MediaType::VideoAudio, ConcurrentPlaybackNotPermitted | BackgroundProcessPlaybackRestricted | SuspendedUnderLockPlaybackRestricted);
 }
 #endif
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to