-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,10 +209,8 @@ - (AVAsset *)asset | |
- (void)didLoad | ||
{ | ||
[super didLoad]; | ||
{ | ||
ASLockScopeSelf(); | ||
[self createControls]; | ||
} | ||
|
||
[self createControls]; | ||
} | ||
|
||
- (void)didEnterPreloadState | ||
|
@@ -284,15 +282,23 @@ - (void)createControls | |
|
||
if (_delegateFlags.delegateCustomControls && _delegateFlags.delegateLayoutSpecForControls) { | ||
NSDictionary *customControls = [_delegate videoPlayerNodeCustomControls:self]; | ||
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]; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
|
||
|
@@ -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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's safe to do this before calling |
||
} | ||
|
||
- (void)cleanCachedControls | ||
- (void)__locked_cleanCachedControls | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
{ | ||
[_cachedControls removeAllObjects]; | ||
|
||
|
@@ -355,7 +369,10 @@ - (void)_locked_createPlaybackButton | |
[_cachedControls setObject:_playbackButtonNode forKey:@(ASVideoPlayerNodeControlTypePlaybackButton)]; | ||
} | ||
|
||
[self addSubnode:_playbackButtonNode]; | ||
{ | ||
ASUnlockScope(self); | ||
[self addSubnode:_playbackButtonNode]; | ||
} | ||
} | ||
|
||
- (void)_locked_createFullScreenButton | ||
|
@@ -374,7 +391,10 @@ - (void)_locked_createFullScreenButton | |
[_cachedControls setObject:_fullScreenButtonNode forKey:@(ASVideoPlayerNodeControlTypeFullScreenButton)]; | ||
} | ||
|
||
[self addSubnode:_fullScreenButtonNode]; | ||
{ | ||
ASUnlockScope(self); | ||
[self addSubnode:_fullScreenButtonNode]; | ||
} | ||
} | ||
|
||
- (void)_locked_createElapsedTextField | ||
|
@@ -389,7 +409,10 @@ - (void)_locked_createElapsedTextField | |
|
||
[_cachedControls setObject:_elapsedTextNode forKey:@(ASVideoPlayerNodeControlTypeElapsedText)]; | ||
} | ||
[self addSubnode:_elapsedTextNode]; | ||
{ | ||
ASUnlockScope(self); | ||
[self addSubnode:_elapsedTextNode]; | ||
} | ||
} | ||
|
||
- (void)_locked_createDurationTextField | ||
|
@@ -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 | ||
|
@@ -450,8 +476,10 @@ - (void)_locked_createScrubber | |
|
||
[_cachedControls setObject:_scrubberNode forKey:@(ASVideoPlayerNodeControlTypeScrubber)]; | ||
} | ||
|
||
[self addSubnode:_scrubberNode]; | ||
{ | ||
ASUnlockScope(self); | ||
[self addSubnode:_scrubberNode]; | ||
} | ||
} | ||
|
||
- (void)_locked_createControlFlexGrowSpacer | ||
|
@@ -623,7 +651,6 @@ - (void)showSpinner | |
ASLockScopeSelf(); | ||
|
||
if (!_spinnerNode) { | ||
|
||
__weak __typeof__(self) weakSelf = self; | ||
_spinnerNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{ | ||
__typeof__(self) strongSelf = weakSelf; | ||
|
@@ -644,9 +671,11 @@ - (void)showSpinner | |
}]; | ||
|
||
_spinnerNode.style.preferredSize = CGSizeMake(44.0, 44.0); | ||
|
||
[self addSubnode:_spinnerNode]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
[self setNeedsLayout]; | ||
{ | ||
ASUnlockScope(self); | ||
[self addSubnode:_spinnerNode]; | ||
[self setNeedsLayout]; | ||
} | ||
} | ||
[(UIActivityIndicatorView *)_spinnerNode.view startAnimating]; | ||
} | ||
|
@@ -658,7 +687,10 @@ - (void)removeSpinner | |
if (!_spinnerNode) { | ||
return; | ||
} | ||
[_spinnerNode removeFromSupernode]; | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may should grab the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.