Bug 229688

Summary: REGRESSION (iOS 15): HTMLAudioElement fails to load new audio when device is locked or safari is in background
Product: WebKit Reporter: Rick Li <racke1983cn>
Component: Web AudioAssignee: Eric Carlson <eric.carlson>
Status: RESOLVED FIXED    
Severity: Major CC: cdumez, darin, eric.carlson, ews-watchlist, glenn, hta, jer.noble, peng.liu6, philipj, sergio, tommyw, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: Safari Technology Preview   
Hardware: iPhone / iPad   
OS: Other   
See Also: https://bugs.webkit.org/show_bug.cgi?id=239812
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch for landing
none
Address post-review feedback
none
Patch none

Description Rick Li 2021-08-30 15:11:01 PDT
Hi, we are currently attempting to get Safari to auto move on to the next audio track when a track ends. By executing JavaScript that replaces the src attribute with a new mp3, it worked fine with IOS 14.
However in IOS 15 Beta - 15.0 (19A5325f) if the screen is locked or safari is in the background, playback terminates at the end of the first track and does not progress to the second.  No error is raised.

To test, please use https://521dimensions.com/open-source/amplitudejs
Comment 1 Radar WebKit Bug Importer 2021-08-30 16:55:01 PDT
<rdar://problem/82554226>
Comment 2 Eric Carlson 2022-01-21 12:23:45 PST
Created attachment 449683 [details]
Patch
Comment 3 Eric Carlson 2022-01-21 15:09:44 PST
Created attachment 449698 [details]
Patch
Comment 4 Jer Noble 2022-01-21 17:47:47 PST
Comment on attachment 449698 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=449698&action=review

> Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:146
> +    if (!hasAudibleAudioOrVideoMediaType && m_previousHadAudibleAudioOrVideoMediaType) {
> +        if (!m_delayCategoryChangeTimer.isActive()) {
> +            m_delayCategoryChangeTimer.startOneShot(delayBeforeSettingCategoryNone);
> +            ALWAYS_LOG(LOGIDENTIFIER, "setting timer");
> +        }
> +        hasAudibleAudioOrVideoMediaType = true;
> +    }
> +    m_previousHadAudibleAudioOrVideoMediaType = hasAudibleAudioOrVideoMediaType;
> +

I wonder what will happen when a page is using WebAudio; we may go from Playback -> Ambient without hitting this timer.

> Source/WebKit/WebProcess/GPU/media/RemoteAudioSession.cpp:-148
> -#if PLATFORM(COCOA)
>      return m_category;
> -#else
> -    return AudioSession::CategoryType::None;
> -#endif

Is this necessary?

> LayoutTests/ChangeLog:12
> +        * media/audio-session-category-expected.txt: Added.
> +        * media/audio-session-category.html: Added.

Should these go into platform/ios/media or platform/mac/media?
Comment 5 Jer Noble 2022-01-21 17:55:36 PST
Comment on attachment 449698 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=449698&action=review

> Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:133
> +                m_delayCategoryChangeTimer.stop();

I think this may be in the wrong place. It'll only get hit if there are no audible <video> or <audio> elements, but the session is "potentially audible".

> Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:145
> +    if (!hasAudibleAudioOrVideoMediaType && m_previousHadAudibleAudioOrVideoMediaType) {
> +        if (!m_delayCategoryChangeTimer.isActive()) {
> +            m_delayCategoryChangeTimer.startOneShot(delayBeforeSettingCategoryNone);
> +            ALWAYS_LOG(LOGIDENTIFIER, "setting timer");
> +        }
> +        hasAudibleAudioOrVideoMediaType = true;
> +    }
> +    m_previousHadAudibleAudioOrVideoMediaType = hasAudibleAudioOrVideoMediaType;

I think it should move down here, in an "else" statement after the "if" (which may need to be restructured).  Maybe something like:

if (hasAudibleAudioOrVideoMediaType)
    m_delayCategoryChangeTimer.stop();
else if (m_previousHadAudibleAudioOrVideoMediaType) {
    ....
}
Comment 6 Eric Carlson 2022-01-22 13:49:07 PST
Created attachment 449735 [details]
Patch
Comment 7 Eric Carlson 2022-01-24 08:41:23 PST
Created attachment 449821 [details]
Patch for landing
Comment 8 Eric Carlson 2022-01-24 13:24:19 PST
Comment on attachment 449821 [details]
Patch for landing

Test failures are unrelated.
Comment 9 EWS 2022-01-24 14:06:36 PST
Committed r288466 (246352@main): <https://commits.webkit.org/246352@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 449821 [details].
Comment 10 Darin Adler 2022-01-24 14:10:18 PST
Comment on attachment 449821 [details]
Patch for landing

View in context: https://bugs.webkit.org/attachment.cgi?id=449821&action=review

> Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:97
> +    static const Seconds delayBeforeSettingCategoryNone = { 2_s };

constexpr auto delayBeforeSettingCategoryNone = 2_s;
Comment 11 Eric Carlson 2022-01-24 15:08:04 PST
Reopening to attach new patch.
Comment 12 Eric Carlson 2022-01-24 15:08:05 PST
Created attachment 449867 [details]
Address post-review feedback
Comment 13 EWS 2022-01-24 17:25:05 PST
Committed r288493 (246365@main): <https://commits.webkit.org/246365@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 449867 [details].
Comment 14 Eric Carlson 2022-04-27 07:35:27 PDT
Reopening to attach new patch.
Comment 15 Eric Carlson 2022-04-27 07:35:29 PDT
Created attachment 458443 [details]
Patch
Comment 16 Eric Carlson 2022-04-27 08:53:55 PDT
Comment on attachment 458443 [details]
Patch

Nothing to see here, I attached the patch to the wrong bug.