Skip to content

Commit 4a231ae

Browse files
[video_player] Adds platform view support on macOS (#9576)
Adds macOS support for the platform view display option, brining macOS in line with the iOS implementation. This: - extracts the macOS additions from #8558 as cherry picks, - updates it for some changes that happened during iOS review, and - makes some minor adjustments (mostly combining the factory classes into one shared file) Part of flutter/flutter#86613 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent ccaac1f commit 4a231ae

File tree

12 files changed

+55
-84
lines changed

12 files changed

+55
-84
lines changed

packages/video_player/video_player_avfoundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.8.0
2+
3+
* Adds platform view support for macOS.
4+
15
## 2.7.3
26

37
* Restructures the communication between Dart and native code.

packages/video_player/video_player_avfoundation/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ should add it to your `pubspec.yaml` as usual.
1313

1414
[1]: https://pub.dev/packages/video_player
1515
[2]: https://flutter.dev/to/endorsed-federated-plugin
16-
17-
## Platform limitations
18-
19-
On macOS, the plugin does not currently support platform views. Instead, a texture view is always used to display the video player, even if `VideoViewType.platformView` is specified as a parameter.

packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88

99
#import <OCMock/OCMock.h>
1010
#import <video_player_avfoundation/AVAssetTrackUtils.h>
11+
#import <video_player_avfoundation/FVPNativeVideoViewFactory.h>
1112
#import <video_player_avfoundation/FVPTextureBasedVideoPlayer_Test.h>
1213
#import <video_player_avfoundation/FVPVideoPlayerPlugin_Test.h>
1314

14-
#if TARGET_OS_IOS
15-
#import <video_player_avfoundation/FVPNativeVideoViewFactory.h>
16-
#endif
17-
1815
#if TARGET_OS_IOS
1916
@interface FakeAVAssetTrack : AVAssetTrack
2017
@property(readonly, nonatomic) CGAffineTransform preferredTransform;
@@ -801,7 +798,6 @@ - (void)testHotReloadDoesNotCrash {
801798
handler:nil]; // No assertions needed. Lack of crash is a success.
802799
}
803800

804-
#if TARGET_OS_IOS
805801
- (void)testNativeVideoViewFactoryRegistration {
806802
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
807803

@@ -811,7 +807,6 @@ - (void)testNativeVideoViewFactoryRegistration {
811807

812808
OCMVerifyAll(registrar);
813809
}
814-
#endif
815810

816811
- (void)testPublishesInRegistration {
817812
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ - (instancetype)initWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger
2525
return self;
2626
}
2727

28+
#pragma mark - FlutterPlatformViewFactory
29+
30+
#if TARGET_OS_OSX
31+
- (NSView *)createWithViewIdentifier:(int64_t)viewIdentifier
32+
arguments:(FVPPlatformVideoViewCreationParams *)args {
33+
#else
2834
- (NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame
2935
viewIdentifier:(int64_t)viewIdentifier
3036
arguments:(FVPPlatformVideoViewCreationParams *)args {
37+
#endif
3138
NSNumber *playerIdentifier = @(args.playerId);
3239
FVPVideoPlayer *player = self.playerByIdProvider(playerIdentifier);
3340
return [[FVPNativeVideoView alloc] initWithPlayer:player.player];

packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/FVPVideoPlayerPlugin.m

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010
#import "./include/video_player_avfoundation/FVPAVFactory.h"
1111
#import "./include/video_player_avfoundation/FVPDisplayLink.h"
1212
#import "./include/video_player_avfoundation/FVPFrameUpdater.h"
13+
#import "./include/video_player_avfoundation/FVPNativeVideoViewFactory.h"
1314
#import "./include/video_player_avfoundation/FVPTextureBasedVideoPlayer.h"
1415
#import "./include/video_player_avfoundation/FVPVideoPlayer.h"
1516
// Relative path is needed for messages.g.h. See
1617
// https://github.com/flutter/packages/pull/6675/#discussion_r1591210702
1718
#import "./include/video_player_avfoundation/messages.g.h"
1819

19-
#if TARGET_OS_IOS
20-
// Platform views are only supported on iOS as of now.
21-
#import "./include/video_player_avfoundation/FVPNativeVideoViewFactory.h"
22-
#endif
23-
2420
#if !__has_feature(objc_arc)
2521
#error Code Requires ARC.
2622
#endif
@@ -61,15 +57,12 @@ @implementation FVPVideoPlayerPlugin
6157
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
6258
FVPVideoPlayerPlugin *instance = [[FVPVideoPlayerPlugin alloc] initWithRegistrar:registrar];
6359
[registrar publish:instance];
64-
#if TARGET_OS_IOS
65-
// Platform views are only supported on iOS as of now.
6660
FVPNativeVideoViewFactory *factory = [[FVPNativeVideoViewFactory alloc]
6761
initWithMessenger:registrar.messenger
6862
playerByIdentifierProvider:^FVPVideoPlayer *(NSNumber *playerIdentifier) {
6963
return instance->_playersByIdentifier[playerIdentifier];
7064
}];
7165
[registrar registerViewFactory:factory withId:@"plugins.flutter.dev/video_player_ios"];
72-
#endif
7366
SetUpFVPAVFoundationVideoPlayerApi(registrar.messenger, instance);
7467
}
7568

packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/include/video_player_avfoundation/FVPNativeVideoView.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// Platform views are only supported on iOS as of now. Ifdefs are used to avoid compilation errors.
6-
75
#import <AVFoundation/AVFoundation.h>
86

97
#if TARGET_OS_OSX

packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/include/video_player_avfoundation/FVPNativeVideoViewFactory.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// Platform views are only supported on iOS as of now. Ifdef is used to avoid compilation errors.
6-
75
#import <Foundation/Foundation.h>
86

97
#import "FVPVideoPlayer.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "../video_player_avfoundation/include/video_player_avfoundation/FVPNativeVideoView.h"
6+
7+
#import <AVFoundation/AVFoundation.h>
8+
#import <QuartzCore/QuartzCore.h>
9+
10+
@implementation FVPNativeVideoView
11+
12+
- (instancetype)initWithPlayer:(AVPlayer *)player {
13+
self = [super init];
14+
if (self) {
15+
self.wantsLayer = YES;
16+
((AVPlayerLayer *)self.layer).player = player;
17+
}
18+
return self;
19+
}
20+
21+
- (CALayer *)makeBackingLayer {
22+
return [[AVPlayerLayer alloc] init];
23+
}
24+
25+
@end

packages/video_player/video_player_avfoundation/example/lib/main.dart

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
// ignore_for_file: public_member_api_docs
66

7-
import 'dart:io';
8-
97
import 'package:flutter/material.dart';
108
import 'package:video_player_platform_interface/video_player_platform_interface.dart';
119

@@ -44,27 +42,20 @@ class _App extends StatelessWidget {
4442
),
4543
),
4644
body: TabBarView(
47-
children: Platform.isIOS
48-
? <Widget>[
49-
_ViewTypeTabBar(
50-
builder: (VideoViewType viewType) =>
51-
_BumbleBeeRemoteVideo(viewType),
52-
),
53-
_ViewTypeTabBar(
54-
builder: (VideoViewType viewType) =>
55-
_BumbleBeeEncryptedLiveStream(viewType),
56-
),
57-
_ViewTypeTabBar(
58-
builder: (VideoViewType viewType) =>
59-
_ButterFlyAssetVideo(viewType),
60-
),
61-
]
62-
// Platform views are only supported on iOS as of now.
63-
: const <Widget>[
64-
_BumbleBeeRemoteVideo(VideoViewType.textureView),
65-
_BumbleBeeEncryptedLiveStream(VideoViewType.textureView),
66-
_ButterFlyAssetVideo(VideoViewType.textureView),
67-
],
45+
children: <Widget>[
46+
_ViewTypeTabBar(
47+
builder: (VideoViewType viewType) =>
48+
_BumbleBeeRemoteVideo(viewType),
49+
),
50+
_ViewTypeTabBar(
51+
builder: (VideoViewType viewType) =>
52+
_BumbleBeeEncryptedLiveStream(viewType),
53+
),
54+
_ViewTypeTabBar(
55+
builder: (VideoViewType viewType) =>
56+
_ButterFlyAssetVideo(viewType),
57+
),
58+
],
6859
),
6960
),
7061
);

packages/video_player/video_player_avfoundation/lib/src/avfoundation_video_player.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:async';
66

7-
import 'package:flutter/foundation.dart';
87
import 'package:flutter/services.dart';
98
import 'package:flutter/widgets.dart';
109
import 'package:video_player_platform_interface/video_player_platform_interface.dart';
@@ -73,10 +72,7 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform {
7372
@override
7473
Future<int?> createWithOptions(VideoCreationOptions options) async {
7574
final DataSource dataSource = options.dataSource;
76-
// Platform views are not supported on macOS yet. Use texture view instead.
77-
final VideoViewType viewType = defaultTargetPlatform == TargetPlatform.macOS
78-
? VideoViewType.textureView
79-
: options.viewType;
75+
final VideoViewType viewType = options.viewType;
8076

8177
String? asset;
8278
String? packageName;

0 commit comments

Comments
 (0)