diff --git a/examples/video-caching/README.md b/examples/video-caching/README.md new file mode 100644 index 00000000..50269f44 --- /dev/null +++ b/examples/video-caching/README.md @@ -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 diff --git a/examples/video-caching/ios/Podfile b/examples/video-caching/ios/Podfile index 3d42a6bc..4decf7e1 100644 --- a/examples/video-caching/ios/Podfile +++ b/examples/video-caching/ios/Podfile @@ -28,5 +28,5 @@ target 'VideoCaching' do '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 diff --git a/examples/video-caching/ios/Podfile.lock b/examples/video-caching/ios/Podfile.lock index 8cd11305..2689020a 100644 --- a/examples/video-caching/ios/Podfile.lock +++ b/examples/video-caching/ios/Podfile.lock @@ -1,7 +1,7 @@ PODS: - boost-for-react-native (1.63.0) - DoubleConversion (1.1.5) - - DVAssetLoaderDelegate (0.3.1) + - DVAssetLoaderDelegate (0.3.2) - Folly (2016.10.31.00): - boost-for-react-native - DoubleConversion @@ -9,10 +9,13 @@ PODS: - glog (0.3.4) - React (0.56.0): - React/Core (= 0.56.0) - - react-native-video (3.1.0): - - DVAssetLoaderDelegate (= 0.3.1) + - react-native-video/Video (3.1.0): - 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): - yoga (= 0.56.0.React) - React/CxxBridge (0.56.0): @@ -70,7 +73,7 @@ DEPENDENCIES: - DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`) - Folly (from `../node_modules/react-native/third-party-podspecs/Folly.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/CxxBridge (from `../node_modules/react-native`) - React/DevSupport (from `../node_modules/react-native`) @@ -112,14 +115,14 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c DoubleConversion: a9706f16e388b53ff12cca34473428ee29746a26 - DVAssetLoaderDelegate: 7d1e43ec1e98660b918a65fc50edf3d73e1f0ea6 + DVAssetLoaderDelegate: 38a24530292bf77900fdfdf635434f7f9b49486d Folly: c89ac2d5c6ab169cd7397ef27485c44f35f742c7 glog: b3b0330915eccea41c5cc9731a77cf564a9be5ea React: 1fe0eb13d90b625d94c3b117c274dcfd2e760e11 - react-native-video: 87e0a64ccdeca6ff26182a036650cc13e3817e3f + react-native-video: 44c6befbc1526283ca1919891fcebe4680feade4 SPTPersistentCache: df36ea46762d7cf026502bbb86a8b79d0080dff4 yoga: b1ce48b6cf950b98deae82838f5173ea7cf89e85 -PODFILE CHECKSUM: a72d15643ecf681eee8bfdb9918eaa0cba0620f7 +PODFILE CHECKSUM: f4123c35c77493d6ddbcb86898737abdf5e0fac8 COCOAPODS: 1.5.3 diff --git a/examples/video-caching/update.sh b/examples/video-caching/update.sh new file mode 100755 index 00000000..cfd09c31 --- /dev/null +++ b/examples/video-caching/update.sh @@ -0,0 +1,5 @@ +rm -rf node_modules +yarn +cd ios +rm -rf Pods/* Podfile.lock +pod install diff --git a/ios/RCTVideo.h b/ios/Video/RCTVideo.h similarity index 88% rename from ios/RCTVideo.h rename to ios/Video/RCTVideo.h index 1dcecc2a..0028c3f7 100644 --- a/ios/RCTVideo.h +++ b/ios/Video/RCTVideo.h @@ -4,12 +4,18 @@ #import "UIView+FindUIViewController.h" #import "RCTVideoPlayerViewController.h" #import "RCTVideoPlayerViewControllerDelegate.h" -#import "RCTVideoCache.h" + +#if __has_include() +#import #import "DVURLAsset.h" +#endif @class RCTEventDispatcher; - +#if __has_include() @interface RCTVideo : UIView +#else +@interface RCTVideo : UIView +#endif @property (nonatomic, copy) RCTBubblingEventBlock onVideoLoadStart; @property (nonatomic, copy) RCTBubblingEventBlock onVideoLoad; diff --git a/ios/RCTVideo.m b/ios/Video/RCTVideo.m similarity index 97% rename from ios/RCTVideo.m rename to ios/Video/RCTVideo.m index 712b83cb..3775528d 100644 --- a/ios/RCTVideo.m +++ b/ios/Video/RCTVideo.m @@ -1,5 +1,4 @@ #import -#import "RCTVideoCache.h" #import "RCTVideo.h" #import #import @@ -55,7 +54,9 @@ static NSString *const timedMetadata = @"timedMetadata"; NSString * _resizeMode; BOOL _fullscreenPlayerPresented; UIViewController * _presentingViewController; +#if __has_include() RCTVideoCache * _videoCache; +#endif } - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher @@ -78,8 +79,9 @@ static NSString *const timedMetadata = @"timedMetadata"; _allowsExternalPlayback = YES; _playWhenInactive = false; _ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey +#if __has_include() _videoCache = [RCTVideoCache sharedInstance]; - +#endif [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification @@ -432,17 +434,25 @@ static NSString *const timedMetadata = @"timedMetadata"; NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc] init]; if (isNetwork) { - [_videoCache getItemForUri:uri withCallback:^(AVAsset * _Nullable asset) { - if (asset) { - [self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler]; +#if __has_include() + [_videoCache getItemForUri:uri withCallback:^(AVAsset * _Nullable cachedAsset) { + if (cachedAsset) { + [self playerItemPrepareText:cachedAsset assetOptions:assetOptions withCallback:handler]; return; } +#endif NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; [assetOptions setObject:cookies forKey:AVURLAssetHTTPCookiesKey]; - DVURLAsset * dvAsset = [[DVURLAsset alloc] initWithURL:url options:assetOptions networkTimeout: 10000]; - dvAsset.loaderDelegate = self; - [self playerItemPrepareText:dvAsset assetOptions:assetOptions withCallback:handler]; +#if __has_include() + DVURLAsset *asset = [[DVURLAsset alloc] initWithURL:url options:assetOptions networkTimeout: 10000]; + asset.loaderDelegate = self; +#else + AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:assetOptions]; +#endif + [self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler]; +#if __has_include() }]; +#endif return; } else if (isAsset) { @@ -1045,8 +1055,8 @@ static NSString *const timedMetadata = @"timedMetadata"; _playerLayer = nil; } +#if __has_include() #pragma mark - DVAssetLoaderDelegate - - (void)dvAssetLoaderDelegate:(DVAssetLoaderDelegate *)loaderDelegate didLoadData:(NSData *)data forURL:(NSURL *)url { @@ -1056,6 +1066,7 @@ static NSString *const timedMetadata = @"timedMetadata"; #endif }]; } +#endif #pragma mark - RCTVideoPlayerViewControllerDelegate diff --git a/ios/RCTVideoManager.h b/ios/Video/RCTVideoManager.h similarity index 100% rename from ios/RCTVideoManager.h rename to ios/Video/RCTVideoManager.h diff --git a/ios/RCTVideoManager.m b/ios/Video/RCTVideoManager.m similarity index 100% rename from ios/RCTVideoManager.m rename to ios/Video/RCTVideoManager.m diff --git a/ios/RCTVideoPlayerViewController.h b/ios/Video/RCTVideoPlayerViewController.h similarity index 100% rename from ios/RCTVideoPlayerViewController.h rename to ios/Video/RCTVideoPlayerViewController.h diff --git a/ios/RCTVideoPlayerViewController.m b/ios/Video/RCTVideoPlayerViewController.m similarity index 100% rename from ios/RCTVideoPlayerViewController.m rename to ios/Video/RCTVideoPlayerViewController.m diff --git a/ios/RCTVideoPlayerViewControllerDelegate.h b/ios/Video/RCTVideoPlayerViewControllerDelegate.h similarity index 100% rename from ios/RCTVideoPlayerViewControllerDelegate.h rename to ios/Video/RCTVideoPlayerViewControllerDelegate.h diff --git a/ios/UIView+FindUIViewController.h b/ios/Video/UIView+FindUIViewController.h similarity index 100% rename from ios/UIView+FindUIViewController.h rename to ios/Video/UIView+FindUIViewController.h diff --git a/ios/UIView+FindUIViewController.m b/ios/Video/UIView+FindUIViewController.m similarity index 100% rename from ios/UIView+FindUIViewController.m rename to ios/Video/UIView+FindUIViewController.m diff --git a/ios/RCTVideoCache.h b/ios/VideoCaching/RCTVideoCache.h similarity index 100% rename from ios/RCTVideoCache.h rename to ios/VideoCaching/RCTVideoCache.h diff --git a/ios/RCTVideoCache.m b/ios/VideoCaching/RCTVideoCache.m similarity index 100% rename from ios/RCTVideoCache.m rename to ios/VideoCaching/RCTVideoCache.m diff --git a/react-native-video.podspec b/react-native-video.podspec index d6727356..c5e09c11 100644 --- a/react-native-video.podspec +++ b/react-native-video.podspec @@ -3,23 +3,33 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) Pod::Spec.new do |s| - s.name = "react-native-video" - s.version = package["version"] - s.summary = "A