Bug 216299

Summary: [Cocoa] PERF: Don't instantiate AVPlayer-based audio decoders or renderers if an element is initially muted.
Product: WebKit Reporter: Jer Noble <jer.noble>
Component: New BugsAssignee: Jer Noble <jer.noble>
Status: RESOLVED FIXED    
Severity: Normal CC: benjamin, calvaris, cdumez, changseok, cmarcelo, eric.carlson, esprehn+autocc, ews-watchlist, glenn, gyuyoung.kim, nham, peng.liu6, philipj, sergio, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
eric.carlson: review+, ews-feeder: commit-queue-
Patch for Landing none

Description Jer Noble 2020-09-08 17:35:57 PDT
[Cocoa] Don't instantiate AVPlayer-based audio decoders or renderers if an element is initially muted.
Comment 1 Jer Noble 2020-09-08 17:46:39 PDT
Created attachment 408296 [details]
Patch
Comment 2 Peng Liu 2020-09-08 18:37:48 PDT
Comment on attachment 408296 [details]
Patch

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

> Source/WebCore/ChangeLog:14
> +        audio decoding and rendering until the first time the AVPlayer is unmuted. ThisThe  means the first un-mute may

s/ThisThe/This/

> Source/WebCore/PAL/pal/spi/cocoa/AVFoundationSPI.h:77
> +@property (nonatomic, getter=_suppressesAudioRendering, setter=_setSuppressesAudioRendering:) BOOL suppressesAudioRendering

Miss a ";"?
Comment 3 Jer Noble 2020-09-09 14:10:37 PDT
Created attachment 408366 [details]
Patch
Comment 4 Eric Carlson 2020-09-09 14:57:39 PDT
Comment on attachment 408366 [details]
Patch

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

r=me once the bots are happy

> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:946
> +        m_avPlayer.get().suppressesAudioRendering = YES;

We should always know if an element is muted or not with the above changes, and setMuted() will always be called if the muted state changes, so how about something like `m_avPlayer.get().suppressesAudioRendering = m_muted`?

> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:1334
> +    if (!m_muted)
> +        m_avPlayer.get().suppressesAudioRendering = NO;

setMuted(true) may be called before playback starts, so something like `m_avPlayer.get().suppressesAudioRendering = m_muted` might be better.
Comment 5 Eric Carlson 2020-09-09 14:58:59 PDT
Comment on attachment 408366 [details]
Patch

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

> Source/WebCore/PAL/pal/spi/cocoa/AVFoundationSPI.h:77
> +@property (nonatomic, getter=_suppressesAudioRendering, setter=_setSuppressesAudioRendering:) BOOL suppressesAudioRendering

Looks like you are missing a semi-colon
Comment 6 Jer Noble 2020-09-09 15:30:48 PDT
Comment on attachment 408366 [details]
Patch

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

>> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:946
>> +        m_avPlayer.get().suppressesAudioRendering = YES;
> 
> We should always know if an element is muted or not with the above changes, and setMuted() will always be called if the muted state changes, so how about something like `m_avPlayer.get().suppressesAudioRendering = m_muted`?

If you look deeper in the context for this change, this is already inside a `if (m_muted)` block. And three lines above m_muted is set to `false`, so we literally have no choice but to set this unconditionally to YES on this line.

>> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:1334
>> +        m_avPlayer.get().suppressesAudioRendering = NO;
> 
> setMuted(true) may be called before playback starts, so something like `m_avPlayer.get().suppressesAudioRendering = m_muted` might be better.

That would cause suppression to kick in if the video was muted during playback.  The existing logic definitely has edge cases where suppression won't kick in (I.e., if you do `video.src = foo; video.addEventListener('loadedmetadata', event => { video.muted = true; });`), but those edge cases feel less common, and we can always expand this behavior.
Comment 7 Jer Noble 2020-09-09 15:34:36 PDT
Created attachment 408375 [details]
Patch for Landing
Comment 8 EWS 2020-09-10 11:23:56 PDT
Committed r266844: <https://trac.webkit.org/changeset/266844>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 408375 [details].
Comment 9 Radar WebKit Bug Importer 2020-09-10 11:24:31 PDT
<rdar://problem/68654077>
Comment 10 Chris Dumez 2020-09-14 13:18:51 PDT
I suspect this has caused <rdar://problem/68743466>.