make video cache feature optional

This commit is contained in:
Laurin Quast 2018-07-19 02:06:09 +02:00
parent 4c7676396a
commit 4f386d2c93
16 changed files with 91 additions and 32 deletions

View File

@ -0,0 +1,24 @@
# react-native-video caching example (currently only working on iOS)
# How to verify that caching is working (iOS)
1. run `./update.sh`
2. open `ios/VideoCaching.xcworkspace`
3. build and run project in simulator
4. after the video is loaded -> disconnect from the internet
5. kill the application
6. start the application again -> the video is there despite being offline :)
# How to verify that you can build the project without the caching feature (iOS)
1. In `ios/Podfile` apply the following changes
```diff
- pod 'react-native-video/VideoCaching', :path => '../node_modules/react-native-video/react-native-video.podspec'
+ pod 'react-native-video', :path => '../node_modules/react-native-video/react-native-video.podspec'
```
2. run `./update.sh`
3. open `ios/VideoCaching.xcworkspace`
4. build and run project in simulator
5. after the video is loaded -> disconnect from the internet
6. kill the application
7. start the application again -> the video should not load

View File

@ -28,5 +28,5 @@ target 'VideoCaching' do
'DevSupport' 'DevSupport'
] ]
pod 'react-native-video', :path => '../node_modules/react-native-video/react-native-video.podspec' pod 'react-native-video/VideoCaching', :path => '../node_modules/react-native-video/react-native-video.podspec'
end end

View File

@ -1,7 +1,7 @@
PODS: PODS:
- boost-for-react-native (1.63.0) - boost-for-react-native (1.63.0)
- DoubleConversion (1.1.5) - DoubleConversion (1.1.5)
- DVAssetLoaderDelegate (0.3.1) - DVAssetLoaderDelegate (0.3.2)
- Folly (2016.10.31.00): - Folly (2016.10.31.00):
- boost-for-react-native - boost-for-react-native
- DoubleConversion - DoubleConversion
@ -9,10 +9,13 @@ PODS:
- glog (0.3.4) - glog (0.3.4)
- React (0.56.0): - React (0.56.0):
- React/Core (= 0.56.0) - React/Core (= 0.56.0)
- react-native-video (3.1.0): - react-native-video/Video (3.1.0):
- DVAssetLoaderDelegate (= 0.3.1)
- React - React
- SPTPersistentCache (= 1.1.0) - react-native-video/VideoCaching (3.1.0):
- DVAssetLoaderDelegate (~> 0.3.1)
- React
- react-native-video/Video
- SPTPersistentCache (~> 1.1.0)
- React/Core (0.56.0): - React/Core (0.56.0):
- yoga (= 0.56.0.React) - yoga (= 0.56.0.React)
- React/CxxBridge (0.56.0): - React/CxxBridge (0.56.0):
@ -70,7 +73,7 @@ DEPENDENCIES:
- DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`) - DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`)
- Folly (from `../node_modules/react-native/third-party-podspecs/Folly.podspec`) - Folly (from `../node_modules/react-native/third-party-podspecs/Folly.podspec`)
- glog (from `../node_modules/react-native/third-party-podspecs/GLog.podspec`) - glog (from `../node_modules/react-native/third-party-podspecs/GLog.podspec`)
- react-native-video (from `../node_modules/react-native-video/react-native-video.podspec`) - react-native-video/VideoCaching (from `../node_modules/react-native-video/react-native-video.podspec`)
- React/Core (from `../node_modules/react-native`) - React/Core (from `../node_modules/react-native`)
- React/CxxBridge (from `../node_modules/react-native`) - React/CxxBridge (from `../node_modules/react-native`)
- React/DevSupport (from `../node_modules/react-native`) - React/DevSupport (from `../node_modules/react-native`)
@ -112,14 +115,14 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS: SPEC CHECKSUMS:
boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c
DoubleConversion: a9706f16e388b53ff12cca34473428ee29746a26 DoubleConversion: a9706f16e388b53ff12cca34473428ee29746a26
DVAssetLoaderDelegate: 7d1e43ec1e98660b918a65fc50edf3d73e1f0ea6 DVAssetLoaderDelegate: 38a24530292bf77900fdfdf635434f7f9b49486d
Folly: c89ac2d5c6ab169cd7397ef27485c44f35f742c7 Folly: c89ac2d5c6ab169cd7397ef27485c44f35f742c7
glog: b3b0330915eccea41c5cc9731a77cf564a9be5ea glog: b3b0330915eccea41c5cc9731a77cf564a9be5ea
React: 1fe0eb13d90b625d94c3b117c274dcfd2e760e11 React: 1fe0eb13d90b625d94c3b117c274dcfd2e760e11
react-native-video: 87e0a64ccdeca6ff26182a036650cc13e3817e3f react-native-video: 44c6befbc1526283ca1919891fcebe4680feade4
SPTPersistentCache: df36ea46762d7cf026502bbb86a8b79d0080dff4 SPTPersistentCache: df36ea46762d7cf026502bbb86a8b79d0080dff4
yoga: b1ce48b6cf950b98deae82838f5173ea7cf89e85 yoga: b1ce48b6cf950b98deae82838f5173ea7cf89e85
PODFILE CHECKSUM: a72d15643ecf681eee8bfdb9918eaa0cba0620f7 PODFILE CHECKSUM: f4123c35c77493d6ddbcb86898737abdf5e0fac8
COCOAPODS: 1.5.3 COCOAPODS: 1.5.3

View File

@ -0,0 +1,5 @@
rm -rf node_modules
yarn
cd ios
rm -rf Pods/* Podfile.lock
pod install

View File

@ -4,12 +4,18 @@
#import "UIView+FindUIViewController.h" #import "UIView+FindUIViewController.h"
#import "RCTVideoPlayerViewController.h" #import "RCTVideoPlayerViewController.h"
#import "RCTVideoPlayerViewControllerDelegate.h" #import "RCTVideoPlayerViewControllerDelegate.h"
#import "RCTVideoCache.h"
#if __has_include(<react-native-video/RCTVideoCache.h>)
#import <react-native-video/RCTVideoCache.h>
#import "DVURLAsset.h" #import "DVURLAsset.h"
#endif
@class RCTEventDispatcher; @class RCTEventDispatcher;
#if __has_include(<react-native-video/RCTVideoCache.h>)
@interface RCTVideo : UIView <RCTVideoPlayerViewControllerDelegate, DVAssetLoaderDelegatesDelegate> @interface RCTVideo : UIView <RCTVideoPlayerViewControllerDelegate, DVAssetLoaderDelegatesDelegate>
#else
@interface RCTVideo : UIView <RCTVideoPlayerViewControllerDelegate>
#endif
@property (nonatomic, copy) RCTBubblingEventBlock onVideoLoadStart; @property (nonatomic, copy) RCTBubblingEventBlock onVideoLoadStart;
@property (nonatomic, copy) RCTBubblingEventBlock onVideoLoad; @property (nonatomic, copy) RCTBubblingEventBlock onVideoLoad;

View File

@ -1,5 +1,4 @@
#import <React/RCTConvert.h> #import <React/RCTConvert.h>
#import "RCTVideoCache.h"
#import "RCTVideo.h" #import "RCTVideo.h"
#import <React/RCTBridgeModule.h> #import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h> #import <React/RCTEventDispatcher.h>
@ -55,7 +54,9 @@ static NSString *const timedMetadata = @"timedMetadata";
NSString * _resizeMode; NSString * _resizeMode;
BOOL _fullscreenPlayerPresented; BOOL _fullscreenPlayerPresented;
UIViewController * _presentingViewController; UIViewController * _presentingViewController;
#if __has_include(<react-native-video/RCTVideoCache.h>)
RCTVideoCache * _videoCache; RCTVideoCache * _videoCache;
#endif
} }
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
@ -78,8 +79,9 @@ static NSString *const timedMetadata = @"timedMetadata";
_allowsExternalPlayback = YES; _allowsExternalPlayback = YES;
_playWhenInactive = false; _playWhenInactive = false;
_ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey _ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey
#if __has_include(<react-native-video/RCTVideoCache.h>)
_videoCache = [RCTVideoCache sharedInstance]; _videoCache = [RCTVideoCache sharedInstance];
#endif
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:) selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification name:UIApplicationWillResignActiveNotification
@ -432,17 +434,25 @@ static NSString *const timedMetadata = @"timedMetadata";
NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc] init]; NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc] init];
if (isNetwork) { if (isNetwork) {
[_videoCache getItemForUri:uri withCallback:^(AVAsset * _Nullable asset) { #if __has_include(<react-native-video/RCTVideoCache.h>)
if (asset) { [_videoCache getItemForUri:uri withCallback:^(AVAsset * _Nullable cachedAsset) {
[self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler]; if (cachedAsset) {
[self playerItemPrepareText:cachedAsset assetOptions:assetOptions withCallback:handler];
return; return;
} }
#endif
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
[assetOptions setObject:cookies forKey:AVURLAssetHTTPCookiesKey]; [assetOptions setObject:cookies forKey:AVURLAssetHTTPCookiesKey];
DVURLAsset * dvAsset = [[DVURLAsset alloc] initWithURL:url options:assetOptions networkTimeout: 10000]; #if __has_include(<react-native-video/RCTVideoCache.h>)
dvAsset.loaderDelegate = self; DVURLAsset *asset = [[DVURLAsset alloc] initWithURL:url options:assetOptions networkTimeout: 10000];
[self playerItemPrepareText:dvAsset assetOptions:assetOptions withCallback:handler]; asset.loaderDelegate = self;
#else
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
#endif
[self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler];
#if __has_include(<react-native-video/RCTVideoCache.h>)
}]; }];
#endif
return; return;
} }
else if (isAsset) { else if (isAsset) {
@ -1045,8 +1055,8 @@ static NSString *const timedMetadata = @"timedMetadata";
_playerLayer = nil; _playerLayer = nil;
} }
#if __has_include(<react-native-video/RCTVideoCache.h>)
#pragma mark - DVAssetLoaderDelegate #pragma mark - DVAssetLoaderDelegate
- (void)dvAssetLoaderDelegate:(DVAssetLoaderDelegate *)loaderDelegate - (void)dvAssetLoaderDelegate:(DVAssetLoaderDelegate *)loaderDelegate
didLoadData:(NSData *)data didLoadData:(NSData *)data
forURL:(NSURL *)url { forURL:(NSURL *)url {
@ -1056,6 +1066,7 @@ static NSString *const timedMetadata = @"timedMetadata";
#endif #endif
}]; }];
} }
#endif
#pragma mark - RCTVideoPlayerViewControllerDelegate #pragma mark - RCTVideoPlayerViewControllerDelegate

View File

@ -3,23 +3,33 @@ require "json"
package = JSON.parse(File.read(File.join(__dir__, "package.json"))) package = JSON.parse(File.read(File.join(__dir__, "package.json")))
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "react-native-video" s.name = 'react-native-video'
s.version = package["version"] s.version = package['version']
s.summary = "A <Video /> element for react-native" s.summary = package['description']
s.author = "Brent Vatne <brentvatne@gmail.com> (https://github.com/brentvatne)" s.description = package['description']
s.license = package['license']
s.homepage = "https://github.com/brentvatne/react-native-video" s.author = package['author']
s.homepage = 'https://github.com/brentvatne/react-native-video'
s.license = "MIT" s.source = { :git => "https://github.com/brentvatne/react-native-video.git", :tag => "#{s.version}" }
s.ios.deployment_target = "8.0" s.ios.deployment_target = "8.0"
s.tvos.deployment_target = "9.0" s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/brentvatne/react-native-video.git", :tag => "#{s.version}" } s.subspec "Video" do |ss|
ss.source_files = "ios/Video/*.{h,m}"
s.static_framework = true
end
s.source_files = "ios/*.{h,m}" s.subspec "VideoCaching" do |ss|
ss.dependency "react-native-video/Video"
ss.dependency "SPTPersistentCache", "~> 1.1.0"
ss.dependency "DVAssetLoaderDelegate", "~> 0.3.1"
ss.source_files = "ios/VideoCaching/**/*.{h,m}"
s.static_framework = true
end
s.dependency "React" s.dependency "React"
s.dependency "SPTPersistentCache", "1.1.0"
s.dependency "DVAssetLoaderDelegate", "0.3.1" s.default_subspec = "Video"
end end