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

[video_player]Added HTTP headers setting to video_player plugin #897

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private static class VideoPlayer {
EventChannel eventChannel,
TextureRegistry.SurfaceTextureEntry textureEntry,
String dataSource,
Map<String, String> httpHeaders,
Result result) {
this.eventChannel = eventChannel;
this.textureEntry = textureEntry;
Expand All @@ -86,13 +87,18 @@ private static class VideoPlayer {
if (uri.getScheme().equals("asset") || uri.getScheme().equals("file")) {
dataSourceFactory = new DefaultDataSourceFactory(context, "ExoPlayer");
} else {
dataSourceFactory =
DefaultHttpDataSourceFactory httpDataSourceFactory =
new DefaultHttpDataSourceFactory(
"ExoPlayer",
null,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true);

dataSourceFactory = httpDataSourceFactory;
if (httpHeaders != null) {
httpDataSourceFactory.getDefaultRequestProperties().set(httpHeaders);
}
}

MediaSource mediaSource = buildMediaSource(uri, dataSourceFactory, context);
Expand Down Expand Up @@ -328,15 +334,19 @@ public void onMethodCall(MethodCall call, Result result) {
eventChannel,
handle,
"asset:///" + assetLookupKey,
null,
result);
videoPlayers.put(handle.id(), player);
} else {
@SuppressWarnings("unchecked")
Map<String, String> httpHeaders = call.argument("httpHeaders");
player =
new VideoPlayer(
registrar.context(),
eventChannel,
handle,
(String) call.argument("uri"),
httpHeaders,
result);
videoPlayers.put(handle.id(), player);
}
Expand Down
27 changes: 17 additions & 10 deletions packages/video_player/ios/Classes/VideoPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ @interface FLTVideoPlayer : NSObject <FlutterTexture, FlutterStreamHandler>
@property(nonatomic, readonly) bool isPlaying;
@property(nonatomic, readonly) bool isLooping;
@property(nonatomic, readonly) bool isInitialized;
- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater;
- (instancetype)initWithURL:(NSURL*)url
frameUpdater:(FLTFrameUpdater*)frameUpdater
httpHeaders:(NSDictionary<NSString*, NSString*>*)headers;
- (void)play;
- (void)pause;
- (void)setIsLooping:(bool)isLooping;
Expand All @@ -54,7 +56,7 @@ - (void)updatePlayingState;
@implementation FLTVideoPlayer
- (instancetype)initWithAsset:(NSString*)asset frameUpdater:(FLTFrameUpdater*)frameUpdater {
NSString* path = [[NSBundle mainBundle] pathForResource:asset ofType:nil];
return [self initWithURL:[NSURL fileURLWithPath:path] frameUpdater:frameUpdater];
return [self initWithURL:[NSURL fileURLWithPath:path] frameUpdater:frameUpdater httpHeaders:nil];
}

- (void)addObservers:(AVPlayerItem*)item {
Expand Down Expand Up @@ -140,8 +142,15 @@ - (void)createVideoOutputAndDisplayLink:(FLTFrameUpdater*)frameUpdater {
_displayLink.paused = YES;
}

- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater {
AVPlayerItem* item = [AVPlayerItem playerItemWithURL:url];
- (instancetype)initWithURL:(NSURL*)url
frameUpdater:(FLTFrameUpdater*)frameUpdater
httpHeaders:(NSDictionary<NSString*, NSString*>*)headers {
NSDictionary<NSString*, id>* options = nil;
if (headers != (NSDictionary<NSString*, NSString*>*)[NSNull null]) {
options = @{@"AVURLAssetHTTPHeaderFieldsKey" : headers};
}
AVURLAsset* urlAsset = [AVURLAsset URLAssetWithURL:url options:options];
AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:urlAsset];
return [self initWithPlayerItem:item frameUpdater:frameUpdater];
}

Expand All @@ -153,7 +162,7 @@ - (CGAffineTransform)fixTransform:(AVAssetTrack*)videoTrack {
// 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: %d. Natural width,height: %f, %f", rotationDegrees,
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");
Expand Down Expand Up @@ -185,8 +194,6 @@ - (instancetype)initWithPlayerItem:(AVPlayerItem*)item frameUpdater:(FLTFrameUpd
if (_disposed) return;
if ([videoTrack statusOfValueForKey:@"preferredTransform"
error:nil] == AVKeyValueStatusLoaded) {
CGSize size = videoTrack.naturalSize;

// Rotate the video by using a videoComposition and the preferredTransform
_preferredTransform = [self fixTransform:videoTrack];
// Note:
Expand All @@ -209,8 +216,6 @@ - (instancetype)initWithPlayerItem:(AVPlayerItem*)item frameUpdater:(FLTFrameUpd
_player = [AVPlayer playerWithPlayerItem:item];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

CGSize size = item.presentationSize;

[self createVideoOutputAndDisplayLink:frameUpdater];

[self addObservers:item];
Expand Down Expand Up @@ -462,8 +467,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
player = [[FLTVideoPlayer alloc] initWithAsset:assetPath frameUpdater:frameUpdater];
[self onPlayerSetup:player frameUpdater:frameUpdater result:result];
} else if (uriArg) {
NSDictionary* httpHeaders = argsMap[@"httpHeaders"];
player = [[FLTVideoPlayer alloc] initWithURL:[NSURL URLWithString:uriArg]
frameUpdater:frameUpdater];
frameUpdater:frameUpdater
httpHeaders:httpHeaders];
[self onPlayerSetup:player frameUpdater:frameUpdater result:result];
} else {
result(FlutterMethodNotImplemented);
Expand Down
10 changes: 7 additions & 3 deletions packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
/// the network.
///
/// The URI for the video is given by the [dataSource] argument and must not be
/// null.
VideoPlayerController.network(this.dataSource)
/// null. The [httpHeaders] for the request to the [dataSource] is optional and may be null.
VideoPlayerController.network(this.dataSource, {this.httpHeaders})
: dataSourceType = DataSourceType.network,
package = null,
super(VideoPlayerValue(duration: null));
Expand All @@ -175,6 +175,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {

int _textureId;
final String dataSource;
Map<String, String> httpHeaders;

/// Describes the type of data source this [VideoPlayerController]
/// is constructed with.
Expand Down Expand Up @@ -203,7 +204,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
};
break;
case DataSourceType.network:
dataSourceDescription = <String, dynamic>{'uri': dataSource};
dataSourceDescription = <String, dynamic>{
'uri': dataSource,
'httpHeaders': httpHeaders
};
break;
case DataSourceType.file:
dataSourceDescription = <String, dynamic>{'uri': dataSource};
Expand Down
2 changes: 2 additions & 0 deletions packages/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class FakeController extends ValueNotifier<VideoPlayerValue>
@override
String get package => null;
@override
Map<String, String> httpHeaders;
@override
Future<Duration> get position async => value.position;

@override
Expand Down