From 4f62d9ae3803695c28851d749010c771e94e34f0 Mon Sep 17 00:00:00 2001 From: Yaroslav Popov Date: Tue, 13 Nov 2018 15:14:58 +0300 Subject: [PATCH] Added HTTP headers setting to video_player plugin --- .../plugins/videoplayer/VideoPlayerPlugin.java | 12 +++++++++++- .../video_player/ios/Classes/VideoPlayerPlugin.m | 15 ++++++++++----- packages/video_player/lib/video_player.dart | 6 +++++- packages/video_player/test/video_player_test.dart | 2 ++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java b/packages/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java index 2b730214068d..a6b07f10f6b3 100644 --- a/packages/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java +++ b/packages/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java @@ -70,6 +70,7 @@ private static class VideoPlayer { EventChannel eventChannel, TextureRegistry.SurfaceTextureEntry textureEntry, String dataSource, + Map httpHeaders, Result result) { this.eventChannel = eventChannel; this.textureEntry = textureEntry; @@ -83,13 +84,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); @@ -315,15 +321,19 @@ public void onMethodCall(MethodCall call, Result result) { eventChannel, handle, "asset:///" + assetLookupKey, + null, result); videoPlayers.put(handle.id(), player); } else { + @SuppressWarnings("unchecked") + Map httpHeaders = call.argument("httpHeaders"); player = new VideoPlayer( registrar.context(), eventChannel, handle, (String) call.argument("uri"), + httpHeaders, result); videoPlayers.put(handle.id(), player); } diff --git a/packages/video_player/ios/Classes/VideoPlayerPlugin.m b/packages/video_player/ios/Classes/VideoPlayerPlugin.m index aeec622ce7b6..2869ecf4d70a 100644 --- a/packages/video_player/ios/Classes/VideoPlayerPlugin.m +++ b/packages/video_player/ios/Classes/VideoPlayerPlugin.m @@ -36,7 +36,8 @@ @interface FLTVideoPlayer : NSObject @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*)headers; - (void)play; - (void)pause; - (void)setIsLooping:(bool)isLooping; @@ -52,17 +53,20 @@ - (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]; } -- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater { +- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater +httpHeaders:(NSDictionary*)headers{ self = [super init]; NSAssert(self, @"super init cannot be nil"); _isInitialized = false; _isPlaying = false; _disposed = false; + + AVURLAsset* urlAsset = [AVURLAsset URLAssetWithURL:url options:@{@"AVURLAssetHTTPHeaderFieldsKey": headers}]; - AVPlayerItem* item = [AVPlayerItem playerItemWithURL:url]; + AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:urlAsset]; [item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew @@ -340,8 +344,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { player = [[FLTVideoPlayer alloc] initWithAsset:assetPath frameUpdater:frameUpdater]; } else { dataSource = argsMap[@"uri"]; + NSDictionary* httpHeaders = argsMap[@"httpHeaders"]; player = [[FLTVideoPlayer alloc] initWithURL:[NSURL URLWithString:dataSource] - frameUpdater:frameUpdater]; + frameUpdater:frameUpdater httpHeaders: httpHeaders]; } int64_t textureId = [_registry registerTexture:player]; frameUpdater.textureId = textureId; diff --git a/packages/video_player/lib/video_player.dart b/packages/video_player/lib/video_player.dart index c297078bf8f9..cdc71226ded9 100644 --- a/packages/video_player/lib/video_player.dart +++ b/packages/video_player/lib/video_player.dart @@ -183,6 +183,7 @@ class VideoPlayerController extends ValueNotifier { Completer _creatingCompleter; StreamSubscription _eventSubscription; _VideoAppLifeCycleObserver _lifeCycleObserver; + Map httpHeaders; @visibleForTesting int get textureId => _textureId; @@ -200,7 +201,10 @@ class VideoPlayerController extends ValueNotifier { }; break; case DataSourceType.network: - dataSourceDescription = {'uri': dataSource}; + dataSourceDescription = { + 'uri': dataSource, + 'httpHeaders': httpHeaders + }; break; case DataSourceType.file: dataSourceDescription = {'uri': dataSource}; diff --git a/packages/video_player/test/video_player_test.dart b/packages/video_player/test/video_player_test.dart index 22dd9a2e3e67..e0422fdbbaa9 100644 --- a/packages/video_player/test/video_player_test.dart +++ b/packages/video_player/test/video_player_test.dart @@ -23,6 +23,8 @@ class FakeController extends ValueNotifier @override String get package => null; @override + Map httpHeaders; + @override Future get position async => value.position; @override