Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 25e10b2

Browse files
authored
[video_player] Fixes a bug where the aspect ratio of some HLS videos are incorrectly inverted (#6507)
1 parent a295dc1 commit 25e10b2

File tree

4 files changed

+31
-41
lines changed

4 files changed

+31
-41
lines changed

packages/video_player/video_player_avfoundation/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.3.7
22

3+
* Fixes a bug where the aspect ratio of some HLS videos are incorrectly inverted.
34
* Updates code for `no_leading_underscores_for_local_identifiers` lint.
45

56
## 2.3.6

packages/video_player/video_player_avfoundation/example/ios/RunnerTests/VideoPlayerTests.m

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
@interface FLTVideoPlayer : NSObject <FlutterStreamHandler>
1313
@property(readonly, nonatomic) AVPlayer *player;
14-
// This is to fix a bug (https://github.com/flutter/flutter/issues/111457) in iOS 16 with blank
15-
// video for encrypted video streams. An invisible AVPlayerLayer is used to overwrite the
16-
// protection of pixel buffers in those streams.
1714
@property(readonly, nonatomic) AVPlayerLayer *playerLayer;
1815
@end
1916

@@ -65,13 +62,12 @@ @interface VideoPlayerTests : XCTestCase
6562

6663
@implementation VideoPlayerTests
6764

68-
- (void)testIOS16BugWithEncryptedVideoStream {
69-
// This is to fix a bug (https://github.com/flutter/flutter/issues/111457) in iOS 16 with blank
70-
// video for encrypted video streams. An invisible AVPlayerLayer is used to overwrite the
71-
// protection of pixel buffers in those streams.
72-
// Note that a better unit test is to validate that `copyPixelBuffer` API returns the pixel
73-
// buffers as expected, which requires setting up the video player properly with a protected video
74-
// stream (.m3u8 file).
65+
- (void)testBlankVideoBugWithEncryptedVideoStreamAndInvertedAspectRatioBugForSomeVideoStream {
66+
// This is to fix 2 bugs: 1. blank video for encrypted video streams on iOS 16
67+
// (https://github.com/flutter/flutter/issues/111457) and 2. swapped width and height for some
68+
// video streams (not just iOS 16). (https://github.com/flutter/flutter/issues/109116). An
69+
// invisible AVPlayerLayer is used to overwrite the protection of pixel buffers in those streams
70+
// for issue #1, and restore the correct width and height for issue #2.
7571
NSObject<FlutterPluginRegistry> *registry =
7672
(NSObject<FlutterPluginRegistry> *)[[UIApplication sharedApplication] delegate];
7773
NSObject<FlutterPluginRegistrar> *registrar =
@@ -95,13 +91,8 @@ - (void)testIOS16BugWithEncryptedVideoStream {
9591
FLTVideoPlayer *player = videoPlayerPlugin.playersByTextureId[textureMessage.textureId];
9692
XCTAssertNotNil(player);
9793

98-
if (@available(iOS 16.0, *)) {
99-
XCTAssertNotNil(player.playerLayer, @"AVPlayerLayer should be present for iOS 16.");
100-
XCTAssertNotNil(player.playerLayer.superlayer,
101-
@"AVPlayerLayer should be added on screen for iOS 16.");
102-
} else {
103-
XCTAssertNil(player.playerLayer, @"AVPlayerLayer should not be present before iOS 16.");
104-
}
94+
XCTAssertNotNil(player.playerLayer, @"AVPlayerLayer should be present.");
95+
XCTAssertNotNil(player.playerLayer.superlayer, @"AVPlayerLayer should be added on screen.");
10596
}
10697

10798
- (void)testSeekToInvokesTextureFrameAvailableOnTextureRegistry {

packages/video_player/video_player_avfoundation/ios/Classes/FLTVideoPlayerPlugin.m

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ - (void)onDisplayLink:(CADisplayLink *)link {
3636
@interface FLTVideoPlayer : NSObject <FlutterTexture, FlutterStreamHandler>
3737
@property(readonly, nonatomic) AVPlayer *player;
3838
@property(readonly, nonatomic) AVPlayerItemVideoOutput *videoOutput;
39-
/// An invisible player layer used to access the pixel buffers in protected video streams in iOS 16.
39+
// This is to fix 2 bugs: 1. blank video for encrypted video streams on iOS 16
40+
// (https://github.com/flutter/flutter/issues/111457) and 2. swapped width and height for some video
41+
// streams (not just iOS 16). (https://github.com/flutter/flutter/issues/109116).
42+
// An invisible AVPlayerLayer is used to overwrite the protection of pixel buffers in those streams
43+
// for issue #1, and restore the correct width and height for issue #2.
4044
@property(readonly, nonatomic) AVPlayerLayer *playerLayer;
4145
@property(readonly, nonatomic) CADisplayLink *displayLink;
4246
@property(nonatomic) FlutterEventChannel *eventChannel;
@@ -134,17 +138,13 @@ NS_INLINE CGFloat radiansToDegrees(CGFloat radians) {
134138
return degrees;
135139
};
136140

137-
NS_INLINE UIViewController *rootViewController() API_AVAILABLE(ios(16.0)) {
138-
for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
139-
if ([scene isKindOfClass:UIWindowScene.class]) {
140-
for (UIWindow *window in ((UIWindowScene *)scene).windows) {
141-
if (window.isKeyWindow) {
142-
return window.rootViewController;
143-
}
144-
}
145-
}
146-
}
147-
return nil;
141+
NS_INLINE UIViewController *rootViewController() {
142+
#pragma clang diagnostic push
143+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
144+
// TODO: (hellohuanlin) Provide a non-deprecated codepath. See
145+
// https://github.com/flutter/flutter/issues/104117
146+
return UIApplication.sharedApplication.keyWindow.rootViewController;
147+
#pragma clang diagnostic pop
148148
}
149149

150150
- (AVMutableVideoComposition *)getVideoCompositionWithTransform:(CGAffineTransform)transform
@@ -242,13 +242,13 @@ - (instancetype)initWithPlayerItem:(AVPlayerItem *)item
242242
_player = [AVPlayer playerWithPlayerItem:item];
243243
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
244244

245-
// This is to fix a bug (https://github.com/flutter/flutter/issues/111457) in iOS 16 with blank
246-
// video for encrypted video streams. An invisible AVPlayerLayer is used to overwrite the
247-
// protection of pixel buffers in those streams.
248-
if (@available(iOS 16.0, *)) {
249-
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
250-
[rootViewController().view.layer addSublayer:_playerLayer];
251-
}
245+
// This is to fix 2 bugs: 1. blank video for encrypted video streams on iOS 16
246+
// (https://github.com/flutter/flutter/issues/111457) and 2. swapped width and height for some
247+
// video streams (not just iOS 16). (https://github.com/flutter/flutter/issues/109116). An
248+
// invisible AVPlayerLayer is used to overwrite the protection of pixel buffers in those streams
249+
// for issue #1, and restore the correct width and height for issue #2.
250+
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
251+
[rootViewController().view.layer addSublayer:_playerLayer];
252252

253253
[self createVideoOutputAndDisplayLink:frameUpdater];
254254

@@ -481,9 +481,7 @@ - (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
481481
/// so the channel is going to die or is already dead.
482482
- (void)disposeSansEventChannel {
483483
_disposed = YES;
484-
if (@available(iOS 16.0, *)) {
485-
[_playerLayer removeFromSuperlayer];
486-
}
484+
[_playerLayer removeFromSuperlayer];
487485
[_displayLink invalidate];
488486
AVPlayerItem *currentItem = self.player.currentItem;
489487
[currentItem removeObserver:self forKeyPath:@"status"];

packages/video_player/video_player_avfoundation/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: video_player_avfoundation
22
description: iOS implementation of the video_player plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/video_player/video_player_avfoundation
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
5-
version: 2.3.6
5+
version: 2.3.7
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)