Skip to content

Commit

Permalink
Split code into fiels and fix implementations to separate texture app…
Browse files Browse the repository at this point in the history
…roach from platform view approach
  • Loading branch information
FirentisTFW committed Nov 26, 2024
1 parent fc88fc8 commit 282e746
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import <AVFoundation/AVFoundation.h>

#import "FVPAVFactory.h"

@implementation FVPDefaultAVFactory
- (AVPlayer *)playerWithPlayerItem:(AVPlayerItem *)playerItem {
return [AVPlayer playerWithPlayerItem:playerItem];
}
- (AVPlayerItemVideoOutput *)videoOutputWithPixelBufferAttributes:
(NSDictionary<NSString *, id> *)attributes {
return [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:attributes];
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#import <AVFoundation/AVFoundation.h>

#import "./include/video_player_avfoundation/FVPAVFactory.h"
#import "./include/video_player_avfoundation/FVPDisplayLink.h"
#import "./include/video_player_avfoundation/FVPFrameUpdater.h"
#import "./include/video_player_avfoundation/FVPVideoPlayer.h"
Expand All @@ -24,19 +25,6 @@
#error Code Requires ARC.
#endif

@interface FVPDefaultAVFactory : NSObject <FVPAVFactory>
@end

@implementation FVPDefaultAVFactory
- (AVPlayer *)playerWithPlayerItem:(AVPlayerItem *)playerItem {
return [AVPlayer playerWithPlayerItem:playerItem];
}
- (AVPlayerItemVideoOutput *)videoOutputWithPixelBufferAttributes:
(NSDictionary<NSString *, id> *)attributes {
return [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:attributes];
}
@end

/// Non-test implementation of the diplay link factory.
@interface FVPDefaultDisplayLinkFactory : NSObject <FVPDisplayLinkFactory>
@end
Expand Down Expand Up @@ -102,8 +90,8 @@ - (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registr
- (int64_t)onPlayerSetup:(FVPVideoPlayer *)player frameUpdater:(FVPFrameUpdater *)frameUpdater {
// FIXME Rename textureId to playerId, in all other places as well.
int64_t textureId;
if (frameUpdater) {
textureId = [self.registry registerTexture:player];
if (frameUpdater && [player isKindOfClass:[FVPVideoPlayerTextureApproach class]]) {
textureId = [self.registry registerTexture:(FVPVideoPlayerTextureApproach *)player];
frameUpdater.textureId = textureId;
} else {
// FIXME Possibly start with a predefined prefix and then increment it to avoid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import <AVFoundation/AVFoundation.h>

// Protocol for AVFoundation object instance factory. Used for injecting framework objects in tests.
@protocol FVPAVFactory
@required
- (AVPlayer *)playerWithPlayerItem:(AVPlayerItem *)playerItem;
- (AVPlayerItemVideoOutput *)videoOutputWithPixelBufferAttributes:
(NSDictionary<NSString *, id> *)attributes;
@end

@interface FVPDefaultAVFactory : NSObject <FVPAVFactory>
@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

#import <AVFoundation/AVFoundation.h>

#import "FVPAVFactory.h"
#import "FVPDisplayLink.h"
#import "FVPFrameUpdater.h"
#import "FVPVideoPlayerPlugin_Test.h"

/// FVPVideoPlayer is responsible for managing video playback using AVPlayer.
/// It provides methods to control playback, adjust volume, handle seeking, and
/// notify the Flutter engine about new video frames.
@interface FVPVideoPlayer ()
@interface FVPVideoPlayer : NSObject <FlutterStreamHandler>
/// The AVPlayerItemVideoOutput associated with this video player.
@property(readonly, nonatomic, nonnull) AVPlayerItemVideoOutput *videoOutput;
/// The plugin registrar, to obtain view information from.
Expand All @@ -28,9 +28,13 @@
@property(nonatomic, nonnull) FlutterEventSink eventSink;
/// The preferred transform for the video. It can be used to handle the rotation of the video.
@property(nonatomic) CGAffineTransform preferredTransform;
/// The AVPlayer instance used for video playback.
@property(readonly, nonatomic, nonnull) AVPlayer *player;
/// The layer used to display the video content from the AVPlayer. It's responsible for rendering
/// the video output of the associated AVPlayer.
@property(nonatomic, nonnull) AVPlayerLayer *playerLayer;
/// The current playback position of the video, in milliseconds.
@property(readonly, nonatomic) int64_t position;
/// Indicates whether the video player has been disposed.
@property(nonatomic, readonly) BOOL disposed;
/// Indicates whether the video player is currently playing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "FVPVideoPlayerPlugin.h"

#import <AVFoundation/AVFoundation.h>

#import "FVPAVFactory.h"
#import "FVPDisplayLink.h"
#import "FVPVideoPlayer.h"
#import "FVPVideoPlayerPlugin.h"
#import "messages.g.h"

// Protocol for AVFoundation object instance factory. Used for injecting framework objects in tests.
@protocol FVPAVFactory
@required
- (AVPlayer *)playerWithPlayerItem:(AVPlayerItem *)playerItem;
- (AVPlayerItemVideoOutput *)videoOutputWithPixelBufferAttributes:
(NSDictionary<NSString *, id> *)attributes;
@end

// Protocol for an AVPlayer instance factory. Used for injecting display links in tests.
@protocol FVPDisplayLinkFactory
- (FVPDisplayLink *)displayLinkWithRegistrar:(id<FlutterPluginRegistrar>)registrar
Expand All @@ -25,22 +16,6 @@

#pragma mark -

// TODO(stuartmorgan): Move this whole class to its own files.
@interface FVPVideoPlayer : NSObject <FlutterStreamHandler, FlutterTexture>
@property(readonly, nonatomic) AVPlayer *player;
// This is to fix 2 bugs: 1. blank video for encrypted video streams on iOS 16
// (https://github.com/flutter/flutter/issues/111457) and 2. swapped width and height for some video
// streams (not just iOS 16). (https://github.com/flutter/flutter/issues/109116).
// An invisible AVPlayerLayer is used to overwrite the protection of pixel buffers in those streams
// for issue #1, and restore the correct width and height for issue #2.
@property(readonly, nonatomic) AVPlayerLayer *playerLayer;
@property(readonly, nonatomic) int64_t position;

- (void)onTextureUnregistered:(NSObject<FlutterTexture> *)texture;
@end

#pragma mark -

@interface FVPVideoPlayerPlugin () <FVPAVFoundationVideoPlayerApi>

@property(readonly, strong, nonatomic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// A subclass of FVPVideoPlayer that adds functionality related to texture-based view as a way of
/// displaying the video in the app. It manages the CALayer associated with the Flutter view,
/// updates frames, and handles display link callbacks.
@interface FVPVideoPlayerTextureApproach : FVPVideoPlayer
@interface FVPVideoPlayerTextureApproach : FVPVideoPlayer <FlutterTexture>
// The CALayer associated with the Flutter view this plugin is associated with, if any.
@property(nonatomic, readonly, nullable) CALayer *flutterViewLayer;
// The updater that drives callbacks to the engine to indicate that a new frame is ready.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#import "../video_player_avfoundation/include/video_player_avfoundation/FVPVideoPlayer.h"
#import "../video_player_avfoundation/include/video_player_avfoundation/messages.g.h"

#import "./include/FVPNativeVideoView.h"
#import "./include/FVPNativeVideoViewFactory.h"
Expand Down

0 comments on commit 282e746

Please sign in to comment.