This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[video_player_avfoundation] Applies the standardized transform for videos with different orientations #5069
Merged
fluttergithubbot
merged 11 commits into
flutter:main
from
AlexV525:fix-ios-video-transform
Apr 19, 2022
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f69f79e
Apply the correct transform for videos with different orientations
AlexV525 85da558
✅ `testTransformFixByOrientation` -> `validateTransformFixForOrientat…
AlexV525 bc4b06d
🎨 Extra blank lines
AlexV525 7a74c2a
💡 Rewrite and move around comments
AlexV525 2ae271b
♻️ Rewrite the category as a C function
AlexV525 7c91a3f
Apply suggestions from code review
AlexV525 1c7bc9a
♻️ Rewrite the category as a C function x2
AlexV525 2bb2f41
🎨 Format fix
AlexV525 e452c89
Apply suggestions from code review
AlexV525 07cb30e
💡 Remove the comment about iOS 14+
AlexV525 b801da1
Apply suggestions from code review
AlexV525 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,63 @@ | |
// found in the LICENSE file. | ||
|
||
#import "FLTVideoPlayerPlugin.h" | ||
|
||
#import <AVFoundation/AVFoundation.h> | ||
#import <GLKit/GLKit.h> | ||
|
||
#import "messages.g.h" | ||
|
||
#if !__has_feature(objc_arc) | ||
#error Code Requires ARC. | ||
#endif | ||
|
||
/** | ||
* Note: https://stackoverflow.com/questions/64161544 | ||
* `AVAssetTrack.preferredTransform` can have wrong `tx` and `ty` | ||
* on iOS 14 and above. This method provide a standardized transform | ||
* according to the orientation of the track. | ||
*/ | ||
static CGAffineTransform FLTGetStandardizedTransformForTrack(AVAssetTrack *track) { | ||
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. A static function is internal to the compilation unit (this file), so can't be access from the test. You need a public declaration of this in a header, just as you do for ObjC classes you want to use in other files. You should have a separate AVAssetTrackUtils.h and AVAssetTrackUtils.m where you declare and implement this, just as you did when you had it as a category, just using a (non-static) function like this instead of a category. 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. Thanks! That helps a lot |
||
CGAffineTransform t = track.preferredTransform; | ||
CGSize size = track.naturalSize; | ||
// Each case of control flows corresponding to | ||
// a specific `UIImageOrientation`, with 8 cases in total. | ||
if (t.a == 1 && t.b == 0 && t.c == 0 && t.d == 1) { | ||
// UIImageOrientationUp | ||
t.tx = 0; | ||
t.ty = 0; | ||
} else if (t.a == -1 && t.b == 0 && t.c == 0 && t.d == -1) { | ||
// UIImageOrientationDown | ||
t.tx = size.width; | ||
t.ty = size.height; | ||
} else if (t.a == 0 && t.b == -1 && t.c == 1 && t.d == 0) { | ||
// UIImageOrientationLeft | ||
t.tx = 0; | ||
t.ty = size.width; | ||
} else if (t.a == 0 && t.b == 1 && t.c == -1 && t.d == 0) { | ||
// UIImageOrientationRight | ||
t.tx = size.height; | ||
t.ty = 0; | ||
} else if (t.a == -1 && t.b == 0 && t.c == 0 && t.d == 1) { | ||
// UIImageOrientationUpMirrored | ||
t.tx = size.width; | ||
t.ty = 0; | ||
} else if (t.a == 1 && t.b == 0 && t.c == 0 && t.d == -1) { | ||
// UIImageOrientationDownMirrored | ||
t.tx = 0; | ||
t.ty = size.height; | ||
} else if (t.a == 0 && t.b == -1 && t.c == -1 && t.d == 0) { | ||
// UIImageOrientationLeftMirrored | ||
t.tx = size.height; | ||
t.ty = size.width; | ||
} else if (t.a == 0 && t.b == 1 && t.c == 1 && t.d == 0) { | ||
// UIImageOrientationRightMirrored | ||
t.tx = 0; | ||
t.ty = 0; | ||
} | ||
return t; | ||
} | ||
|
||
@interface FLTFrameUpdater : NSObject | ||
@property(nonatomic) int64_t textureId; | ||
@property(nonatomic, weak, readonly) NSObject<FlutterTextureRegistry> *registry; | ||
|
@@ -187,29 +236,6 @@ - (instancetype)initWithURL:(NSURL *)url | |
return [self initWithPlayerItem:item frameUpdater:frameUpdater]; | ||
} | ||
|
||
- (CGAffineTransform)fixTransform:(AVAssetTrack *)videoTrack { | ||
CGAffineTransform transform = videoTrack.preferredTransform; | ||
// TODO(@recastrodiaz): why do we need to do this? Why is the preferredTransform incorrect? | ||
// At least 2 user videos show a black screen when in portrait mode if we directly use the | ||
// videoTrack.preferredTransform Setting tx to the height of the video instead of 0, properly | ||
// displays the video https://github.com/flutter/flutter/issues/17606#issuecomment-413473181 | ||
if (transform.tx == 0 && transform.ty == 0) { | ||
NSInteger rotationDegrees = (NSInteger)round(radiansToDegrees(atan2(transform.b, transform.a))); | ||
NSLog(@"TX and TY are 0. Rotation: %ld. Natural width,height: %f, %f", (long)rotationDegrees, | ||
videoTrack.naturalSize.width, videoTrack.naturalSize.height); | ||
if (rotationDegrees == 90) { | ||
NSLog(@"Setting transform tx"); | ||
transform.tx = videoTrack.naturalSize.height; | ||
transform.ty = 0; | ||
} else if (rotationDegrees == 270) { | ||
NSLog(@"Setting transform ty"); | ||
transform.tx = 0; | ||
transform.ty = videoTrack.naturalSize.width; | ||
} | ||
} | ||
return transform; | ||
} | ||
|
||
- (instancetype)initWithPlayerItem:(AVPlayerItem *)item | ||
frameUpdater:(FLTFrameUpdater *)frameUpdater { | ||
self = [super init]; | ||
|
@@ -226,7 +252,7 @@ - (instancetype)initWithPlayerItem:(AVPlayerItem *)item | |
if ([videoTrack statusOfValueForKey:@"preferredTransform" | ||
error:nil] == AVKeyValueStatusLoaded) { | ||
// Rotate the video by using a videoComposition and the preferredTransform | ||
self->_preferredTransform = [self fixTransform:videoTrack]; | ||
self->_preferredTransform = FLTGetStandardizedTransformForTrack(videoTrack); | ||
// Note: | ||
// https://developer.apple.com/documentation/avfoundation/avplayeritem/1388818-videocomposition | ||
// Video composition can only be used with file-based media and is not supported for | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.