Bug 234131 - [Cocoa] -[AVPlayerItem liveUpdateTime] can hang the main thread for ~60ms
Summary: [Cocoa] -[AVPlayerItem liveUpdateTime] can hang the main thread for ~60ms
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Media (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Jer Noble
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-12-10 01:28 PST by Jer Noble
Modified: 2021-12-11 00:48 PST (History)
6 users (show)

See Also:


Attachments
Patch (17.67 KB, patch)
2021-12-10 01:34 PST, Jer Noble
eric.carlson: review+
Details | Formatted Diff | Diff
Patch for landing (18.96 KB, patch)
2021-12-10 10:30 PST, Jer Noble
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jer Noble 2021-12-10 01:28:48 PST
[Cocoa] -[AVPlayerItem liveUpdateThread] can hang the main thread for ~60ms
Comment 1 Jer Noble 2021-12-10 01:29:17 PST
<rdar://86307593>
Comment 2 Jer Noble 2021-12-10 01:34:04 PST
Created attachment 446682 [details]
Patch
Comment 3 Eric Carlson 2021-12-10 08:39:36 PST
Comment on attachment 446682 [details]
Patch

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

> Source/WebCore/ChangeLog:3
> +        [Cocoa] -[AVPlayerItem liveUpdateThread] can hang the main thread for ~60ms

Do you mean -[AVPlayerItem liveUpdateInterval]?

> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3839
> +    auto queueTaskOnEventLoopWithPlayer = [self, strongSelf = retainPtr(self)] (Function<void(MediaPlayerPrivateAVFoundationObjC&)>&& function) mutable {

s/strongSelf = retainPtr(self)/strongSelf = RetainPtr { self }/

> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3856
> +        auto seekableTimeRanges = retainPtr((NSArray*)newValue);

s/ = retainPtr( ... )/ = RetainPtr { ... }/

> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3858
> +        auto playerItem = retainPtr((AVPlayerItem*)object);

The player doesn't need to be kept alive so you could avoid the refcount churn by just casting to an AVPlayerItem

> Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3866
> +    queueTaskOnEventLoopWithPlayer([keyPath = retainPtr(keyPath), change = retainPtr(change), object = retainPtr(object), context] (auto& player) mutable {

s/ = retainPtr( ... )/ = RetainPtr { ... }/
Comment 4 Jer Noble 2021-12-10 09:20:40 PST
(In reply to Eric Carlson from comment #3)
> Comment on attachment 446682 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=446682&action=review
> 
> > Source/WebCore/ChangeLog:3
> > +        [Cocoa] -[AVPlayerItem liveUpdateThread] can hang the main thread for ~60ms
> 
> Do you mean -[AVPlayerItem liveUpdateInterval]?

Whoops, yes I do.

> > Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3839
> > +    auto queueTaskOnEventLoopWithPlayer = [self, strongSelf = retainPtr(self)] (Function<void(MediaPlayerPrivateAVFoundationObjC&)>&& function) mutable {
> 
> s/strongSelf = retainPtr(self)/strongSelf = RetainPtr { self }/

Ok.

> > Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3856
> > +        auto seekableTimeRanges = retainPtr((NSArray*)newValue);
> 
> s/ = retainPtr( ... )/ = RetainPtr { ... }/
> 
> > Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:3858
> > +        auto playerItem = retainPtr((AVPlayerItem*)object);
> 
> The player doesn't need to be kept alive so you could avoid the refcount
> churn by just casting to an AVPlayerItem

So, I think I'm going to have to modify the patch to explicitly do this query on a background queue. The KVO will happen on the main thread most of the time, in which case, I need to keep this a RetainPtr so I can pass it through to a background queue.
Comment 5 Jer Noble 2021-12-10 10:30:11 PST
Created attachment 446753 [details]
Patch for landing
Comment 6 EWS 2021-12-11 00:48:37 PST
Committed r286907 (245133@main): <https://commits.webkit.org/245133@main>

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