Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve locking situation in ASVideoPlayerNode #1042

Merged
merged 2 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
- Split MapKit, Photos, and AssetsLibrary dependent code into separate subspecs to improve binary size and start time when they're not needed. The default subspec includes all three for backwards compatibility, but this **will change in 3.0**. When using non-Cocoapods build environments, define `AS_USE_PHOTOS, AS_USE_MAPKIT, AS_USE_ASSETS_LIBRARY` to 1 respectively to signal their use. [Adlai Holler](https://github.com/Adlai-Holler)
- Optimization: Removed an NSMutableArray in flattened layouts. [Adlai Holler](https://github.com/Adlai-Holler)
- Reduced binary size by disabling exception support (which we don't use.) [Adlai Holler](https://github.com/Adlai-Holler)
- Create and set delegate for clip corner layers within ASDisplayNode [Michael Schneider](https://github.com/maicki) [#1024](https://github.com/TextureGroup/Texture/pull/1029)
- Create and set delegate for clip corner layers within ASDisplayNode [Michael Schneider](https://github.com/maicki) [#1029](https://github.com/TextureGroup/Texture/pull/1029)
- Improve locking situation in ASVideoPlayerNode [Michael Schneider](https://github.com/maicki) [#1042](https://github.com/TextureGroup/Texture/pull/1042)


## 2.7
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)
Expand Down
72 changes: 52 additions & 20 deletions Source/ASVideoPlayerNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,8 @@ - (AVAsset *)asset
- (void)didLoad
{
[super didLoad];
{
ASLockScopeSelf();
[self createControls];
}

[self createControls];
}

- (void)didEnterPreloadState
Expand Down Expand Up @@ -284,15 +282,23 @@ - (void)createControls

if (_delegateFlags.delegateCustomControls && _delegateFlags.delegateLayoutSpecForControls) {
NSDictionary *customControls = [_delegate videoPlayerNodeCustomControls:self];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we should avoid calling delegates with the lock held. May not worth address in this PR though.

std::vector<ASDisplayNode *> subnodes;
for (id key in customControls) {
id node = customControls[key];
if (![node isKindOfClass:[ASDisplayNode class]]) {
continue;
}

[self addSubnode:node];
subnodes.push_back(node);
[_cachedControls setObject:node forKey:key];
}

{
ASUnlockScope(self);
for (var subnode : subnodes) {
[self addSubnode:subnode];
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid a pair of lock and unlock calls if we add subnodes outside of the outmost lock scope.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked to @maicki offline. Let's leave this as is since my suggested approach is a bit more complicated and in case of early return(s), the vector will not be used.

}
}

Expand All @@ -315,14 +321,22 @@ - (NSArray *)createDefaultControlElementArray

- (void)removeControls
{
for (ASDisplayNode *node in [_cachedControls objectEnumerator]) {
[node removeFromSupernode];
ASLockScope(self);

// Grab the cached controls for removing it
NSMutableDictionary *cachedControls = [_cachedControls copy];

{
ASUnlockScope(self);
for (ASDisplayNode *node in [cachedControls objectEnumerator]) {
[node removeFromSupernode];
}
}

[self cleanCachedControls];
[self __locked_cleanCachedControls];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to do this before calling removeFromSupernode on cached controls? If yes we can avoid the unlock scope.

}

- (void)cleanCachedControls
- (void)__locked_cleanCachedControls
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha!

Copy link
Member

@nguyenhuy nguyenhuy Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we use 1 underscore at the beginning of this method's name (i.e - (void)_locked_cleanCachedControls)?

{
[_cachedControls removeAllObjects];

Expand Down Expand Up @@ -355,7 +369,10 @@ - (void)_locked_createPlaybackButton
[_cachedControls setObject:_playbackButtonNode forKey:@(ASVideoPlayerNodeControlTypePlaybackButton)];
}

[self addSubnode:_playbackButtonNode];
{
ASUnlockScope(self);
[self addSubnode:_playbackButtonNode];
}
}

- (void)_locked_createFullScreenButton
Expand All @@ -374,7 +391,10 @@ - (void)_locked_createFullScreenButton
[_cachedControls setObject:_fullScreenButtonNode forKey:@(ASVideoPlayerNodeControlTypeFullScreenButton)];
}

[self addSubnode:_fullScreenButtonNode];
{
ASUnlockScope(self);
[self addSubnode:_fullScreenButtonNode];
}
}

- (void)_locked_createElapsedTextField
Expand All @@ -389,7 +409,10 @@ - (void)_locked_createElapsedTextField

[_cachedControls setObject:_elapsedTextNode forKey:@(ASVideoPlayerNodeControlTypeElapsedText)];
}
[self addSubnode:_elapsedTextNode];
{
ASUnlockScope(self);
[self addSubnode:_elapsedTextNode];
}
}

- (void)_locked_createDurationTextField
Expand All @@ -405,7 +428,10 @@ - (void)_locked_createDurationTextField
[_cachedControls setObject:_durationTextNode forKey:@(ASVideoPlayerNodeControlTypeDurationText)];
}
[self updateDurationTimeLabel];
[self addSubnode:_durationTextNode];
{
ASUnlockScope(self);
[self addSubnode:_durationTextNode];
}
}

- (void)_locked_createScrubber
Expand Down Expand Up @@ -450,8 +476,10 @@ - (void)_locked_createScrubber

[_cachedControls setObject:_scrubberNode forKey:@(ASVideoPlayerNodeControlTypeScrubber)];
}

[self addSubnode:_scrubberNode];
{
ASUnlockScope(self);
[self addSubnode:_scrubberNode];
}
}

- (void)_locked_createControlFlexGrowSpacer
Expand Down Expand Up @@ -623,7 +651,6 @@ - (void)showSpinner
ASLockScopeSelf();

if (!_spinnerNode) {

__weak __typeof__(self) weakSelf = self;
_spinnerNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
__typeof__(self) strongSelf = weakSelf;
Expand All @@ -644,9 +671,11 @@ - (void)showSpinner
}];

_spinnerNode.style.preferredSize = CGSizeMake(44.0, 44.0);

[self addSubnode:_spinnerNode];
Copy link
Contributor Author

@maicki maicki Jul 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spinner can be removed and added on different threads we may should grab the _spinnerNode before unlocking and adding it.

[self setNeedsLayout];
{
ASUnlockScope(self);
[self addSubnode:_spinnerNode];
[self setNeedsLayout];
}
}
[(UIActivityIndicatorView *)_spinnerNode.view startAnimating];
}
Expand All @@ -658,7 +687,10 @@ - (void)removeSpinner
if (!_spinnerNode) {
return;
}
[_spinnerNode removeFromSupernode];
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may should grab the _spinnerNode here before unlocking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this at the end so we don't need to re-acquire the lock.

ASUnlockScope(self);
[_spinnerNode removeFromSupernode];
}
_spinnerNode = nil;
}

Expand Down