Bug 229327

Summary: Prevent AudioSession category from moving out of PlayAndRecord too quickly
Product: WebKit Reporter: youenn fablet <youennf>
Component: WebRTCAssignee: youenn fablet <youennf>
Status: RESOLVED FIXED    
Severity: Normal CC: calvaris, cdumez, changseok, eric.carlson, esprehn+autocc, ews-watchlist, glenn, gyuyoung.kim, hta, jer.noble, kangil.han, philipj, sergio, tommyw, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch for landing none

Description youenn fablet 2021-08-20 01:50:29 PDT
Prevent AudioSession category to move out of PlayAndRecord to quickly
Comment 1 youenn fablet 2021-08-20 01:56:02 PDT
<rdar://81997024>
Comment 2 youenn fablet 2021-08-20 02:01:50 PDT
Created attachment 435954 [details]
Patch
Comment 3 youenn fablet 2021-08-20 02:05:44 PDT
<rdar://81283764>
Comment 4 youenn fablet 2021-08-20 02:09:08 PDT
Created attachment 435955 [details]
Patch
Comment 5 youenn fablet 2021-08-20 02:21:07 PDT
We could remove individual MediaStreamTrack registration by making sure the Document media state is updated synchronously. I left this as a follow-up to make this patch small.
Comment 6 youenn fablet 2021-08-20 04:41:33 PDT
Created attachment 435971 [details]
Patch
Comment 7 youenn fablet 2021-08-20 05:48:33 PDT
Created attachment 435976 [details]
Patch
Comment 8 youenn fablet 2021-08-20 07:15:00 PDT
Created attachment 435981 [details]
Patch
Comment 9 youenn fablet 2021-08-20 07:15:55 PDT
(In reply to youenn fablet from comment #5)
> We could remove individual MediaStreamTrack registration by making sure the
> Document media state is updated synchronously. I left this as a follow-up to
> make this patch small.

Initial version was using Document to ensure PlayAndRecord sticks.
New version is moving the logic inside MediaSessionManagerCocoa which is more appropriate.
Comment 10 Eric Carlson 2021-08-20 08:57:00 PDT
Comment on attachment 435981 [details]
Patch

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

> Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:154
> +    if (AudioSession::sharedSession().category() == AudioSession::CategoryType::PlayAndRecord && isPlayingAudio)
> +        category = AudioSession::CategoryType::PlayAndRecord;

Nit: it would be easier to understand if this was first:

    if (captureCount  || AudioSession::sharedSession().category() == AudioSession::CategoryType::PlayAndRecord && isPlayingAudio)
        category = AudioSession::CategoryType::PlayAndRecord;
    else if (...

> LayoutTests/http/tests/media/media-stream/audio-capture-and-category.https.html:45
> +     assert_equals(internals.audioSessionCategory(), "PlayAndRecord", "category when capturing");

Nit: it would be good to make this assert message different from the one above so if the test fails we can tell which test was the problem.
Comment 11 youenn fablet 2021-08-20 09:02:43 PDT
Created attachment 435995 [details]
Patch
Comment 12 youenn fablet 2021-08-20 09:06:33 PDT
Created attachment 435996 [details]
Patch
Comment 13 youenn fablet 2021-08-20 09:07:14 PDT
> > Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:154
> > +    if (AudioSession::sharedSession().category() == AudioSession::CategoryType::PlayAndRecord && isPlayingAudio)
> > +        category = AudioSession::CategoryType::PlayAndRecord;
> 
> Nit: it would be easier to understand if this was first:

Done

> > LayoutTests/http/tests/media/media-stream/audio-capture-and-category.https.html:45
> > +     assert_equals(internals.audioSessionCategory(), "PlayAndRecord", "category when capturing");
> 
> Nit: it would be good to make this assert message different from the one
> above so if the test fails we can tell which test was the problem.

Done as well.
Comment 14 Eric Carlson 2021-08-20 09:51:30 PDT
Comment on attachment 435996 [details]
Patch

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

r=me once the bots are happy

> LayoutTests/http/tests/media/media-stream/audio-capture-and-category.https.html:46
> +     assert_equals(internals.audioSessionCategory(), "PlayAndRecord", "category when capturing");

Maybe "category when capturing and playing audio"

> LayoutTests/http/tests/media/media-stream/audio-capture-and-category.https.html:48
> +     stream.getAudioTracks()[0].stop();

As we discussed, it would be good to either comment here or use a url src for the audio element to make it obvious that audio will continue to play when the capture track is stopped.

> LayoutTests/http/tests/media/media-stream/audio-capture-and-category.https.html:51
> +     assert_equals(internals.audioSessionCategory(), "PlayAndRecord", "category when document did capture but is not capturing anymore");

Maybe "category when capture has stopped but audio continues to play"
Comment 15 youenn fablet 2021-08-20 10:57:19 PDT
Created attachment 436008 [details]
Patch for landing
Comment 16 EWS 2021-08-21 00:01:30 PDT
Committed r281367 (240782@main): <https://commits.webkit.org/240782@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 436008 [details].