-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[video_player_avfoundation] removes unnecessary duration and size check before sending the initialized event #9534
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
base: main
Are you sure you want to change the base?
Changes from 25 commits
42a5ba4
375ec1f
bb89c41
1ad1d2e
63d240b
0cfe3f4
60fc5d2
f3fbff5
bcc2725
733f5ea
c86f933
35c9a08
3b0cd77
4315c4a
47ff5d0
e2ce9fd
6f232f5
11e8815
f88925e
72b7474
2179d4e
5392116
7769699
5457167
b6ad65a
349f248
48ecaec
ddf7236
a74417f
0d58f8c
42cc71f
04d3a83
ebe1856
f3eeb7b
11a2ae5
789dfe6
ece7c3f
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 |
|---|---|---|
|
|
@@ -11,8 +11,6 @@ | |
|
|
||
| static void *timeRangeContext = &timeRangeContext; | ||
| static void *statusContext = &statusContext; | ||
| static void *presentationSizeContext = &presentationSizeContext; | ||
| static void *durationContext = &durationContext; | ||
| static void *playbackLikelyToKeepUpContext = &playbackLikelyToKeepUpContext; | ||
| static void *rateContext = &rateContext; | ||
|
|
||
|
|
@@ -64,8 +62,6 @@ static void FVPRemoveKeyValueObservers(NSObject *observer, | |
| return @{ | ||
| @"loadedTimeRanges" : [NSValue valueWithPointer:timeRangeContext], | ||
| @"status" : [NSValue valueWithPointer:statusContext], | ||
| @"presentationSize" : [NSValue valueWithPointer:presentationSizeContext], | ||
| @"duration" : [NSValue valueWithPointer:durationContext], | ||
| @"playbackLikelyToKeepUp" : [NSValue valueWithPointer:playbackLikelyToKeepUpContext], | ||
| }; | ||
| } | ||
|
|
@@ -277,14 +273,6 @@ - (void)observeValueForKeyPath:(NSString *)path | |
| } else if (context == statusContext) { | ||
| AVPlayerItem *item = (AVPlayerItem *)object; | ||
| [self reportStatusForPlayerItem:item]; | ||
| } else if (context == presentationSizeContext || context == durationContext) { | ||
| AVPlayerItem *item = (AVPlayerItem *)object; | ||
| if (item.status == AVPlayerItemStatusReadyToPlay) { | ||
| // Due to an apparent bug, when the player item is ready, it still may not have determined | ||
| // its presentation size or duration. When these properties are finally set, re-check if | ||
| // all required properties and instantiate the event sink if it is not already set up. | ||
| [self reportInitializedIfReadyToPlay]; | ||
| } | ||
| } else if (context == playbackLikelyToKeepUpContext) { | ||
| [self updatePlayingState]; | ||
| if ([[_player currentItem] isPlaybackLikelyToKeepUp]) { | ||
|
|
@@ -301,6 +289,8 @@ - (void)observeValueForKeyPath:(NSString *)path | |
| } | ||
|
|
||
| - (void)reportStatusForPlayerItem:(AVPlayerItem *)item { | ||
| NSAssert(self.eventListener, | ||
| @"reportStatusForPlayerItem was called when the event listener was not set."); | ||
| switch (item.status) { | ||
| case AVPlayerItemStatusFailed: | ||
| [self sendFailedToLoadVideoEvent]; | ||
|
|
@@ -383,52 +373,15 @@ - (void)sendFailedToLoadVideoEvent { | |
| } | ||
|
|
||
| - (void)reportInitializedIfReadyToPlay { | ||
| if (!_isInitialized) { | ||
|
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. Does 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. Yeah the documentation doesn't seem to make that guarantee (nor the sample code). I tried to update the code to allow assert(
!initializingCompleter.isCompleted,
'VideoPlayerController already initialized. This is typically a '
'sign that an implementation of the VideoPlayerPlatform '
'(${_videoPlayerPlatform.runtimeType}) has a bug and is sending '
'more than one initialized event per instance.',
);So for now I added an |
||
| AVPlayerItem *currentItem = self.player.currentItem; | ||
| CGSize size = currentItem.presentationSize; | ||
| CGFloat width = size.width; | ||
| CGFloat height = size.height; | ||
|
|
||
| // Wait until tracks are loaded to check duration or if there are any videos. | ||
| AVAsset *asset = currentItem.asset; | ||
| if ([asset statusOfValueForKey:@"tracks" error:nil] != AVKeyValueStatusLoaded) { | ||
| void (^trackCompletionHandler)(void) = ^{ | ||
| if ([asset statusOfValueForKey:@"tracks" error:nil] != AVKeyValueStatusLoaded) { | ||
| // Cancelled, or something failed. | ||
| return; | ||
| } | ||
| // This completion block will run on an AVFoundation background queue. | ||
| // Hop back to the main thread to set up event sink. | ||
| [self performSelector:_cmd onThread:NSThread.mainThread withObject:self waitUntilDone:NO]; | ||
| }; | ||
| [asset loadValuesAsynchronouslyForKeys:@[ @"tracks" ] | ||
| completionHandler:trackCompletionHandler]; | ||
| return; | ||
| } | ||
| AVPlayerItem *currentItem = self.player.currentItem; | ||
| NSAssert(currentItem.status == AVPlayerItemStatusReadyToPlay, | ||
| @"reportInitializedIfReadyToPlay was called when the item wasn't ready to play."); | ||
| NSAssert(!_isInitialized, @"reportInitializedIfReadyToPlay should only be called once."); | ||
|
|
||
|
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. Removed the duration / size checks since now the caller has to guarantee the item is Before this you may enter this |
||
| BOOL hasVideoTracks = [asset tracksWithMediaType:AVMediaTypeVideo].count != 0; | ||
| // Audio-only HLS files have no size, so `currentItem.tracks.count` must be used to check for | ||
| // track presence, as AVAsset does not always provide track information in HLS streams. | ||
| BOOL hasNoTracks = currentItem.tracks.count == 0 && asset.tracks.count == 0; | ||
|
|
||
| // The player has not yet initialized when it has no size, unless it is an audio-only track. | ||
| // HLS m3u8 video files never load any tracks, and are also not yet initialized until they have | ||
| // a size. | ||
| if ((hasVideoTracks || hasNoTracks) && height == CGSizeZero.height && | ||
| width == CGSizeZero.width) { | ||
| return; | ||
| } | ||
| // The player may be initialized but still needs to determine the duration. | ||
| int64_t duration = [self duration]; | ||
| if (duration == 0) { | ||
| return; | ||
| } | ||
|
|
||
| _isInitialized = YES; | ||
| [self updatePlayingState]; | ||
|
|
||
| [self.eventListener videoPlayerDidInitializeWithDuration:duration size:size]; | ||
| } | ||
| _isInitialized = YES; | ||
|
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. looks like the flag is still used by 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. If |
||
| [self updatePlayingState]; | ||
|
||
| [self.eventListener videoPlayerDidInitializeWithDuration:self.duration | ||
| size:currentItem.presentationSize]; | ||
| } | ||
|
|
||
| #pragma mark - FVPVideoPlayerInstanceApi | ||
|
|
||
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.
This should just be
reportInitializednow, as there is no more "if" aspect to its behavior.