Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when the source is a cameraroll #2639

Merged
merged 6 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Add Google's maven repository to avoid build error [#2552] (https://github.com/react-native-video/react-native-video/pull/2552)

- Fix crash when the source is a cameraroll [#2639] (https://github.com/react-native-video/react-native-video/pull/2639)
freeboub marked this conversation as resolved.
Show resolved Hide resolved

### Version 5.2.0

- Fix for tvOS native audio menu language selector
Expand Down
2 changes: 1 addition & 1 deletion Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export default class Video extends Component {
}

const isNetwork = !!(uri && uri.match(/^https?:/));
const isAsset = !!(uri && uri.match(/^(assets-library|ipod-library|file|content|ms-appx|ms-appdata):/));
const isAsset = !!(uri && uri.match(/^(assets-library|ph|ipod-library|file|content|ms-appx|ms-appdata):/));

let nativeResizeMode;
const RCTVideoInstance = this.getViewManagerConfig('RCTVideo');
Expand Down
64 changes: 41 additions & 23 deletions ios/Video/RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import <React/UIView+React.h>
#include <MediaAccessibility/MediaAccessibility.h>
#include <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>

static NSString *const statusKeyPath = @"status";
static NSString *const playbackLikelyToKeepUpKeyPath = @"playbackLikelyToKeepUp";
Expand Down Expand Up @@ -492,22 +493,22 @@ - (void)playerItemPrepareText:(AVAsset *)asset assetOptions:(NSDictionary * __nu
handler([AVPlayerItem playerItemWithAsset:mixComposition]);
}

- (void)playerItemForSource:(NSDictionary *)source withCallback:(void(^)(AVPlayerItem *))handler
{
- (void)loadAssetFromSource:(NSDictionary *)source withCallback:(void(^)(AVURLAsset *asset, NSMutableDictionary *assetOptions)) handler {
bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]];
bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]];
bool shouldCache = [RCTConvert BOOL:[source objectForKey:@"shouldCache"]];

NSString *uri = [source objectForKey:@"uri"];
NSString *type = [source objectForKey:@"type"];
AVURLAsset *asset;

if (!uri || [uri isEqualToString:@""]) {
DebugLog(@"Could not find video URL in source '%@'", source);
return;
}

NSURL *url = isNetwork || isAsset
? [NSURL URLWithString:uri]
: [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
? [NSURL URLWithString:uri]
: [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc] init];

if (isNetwork) {
Expand All @@ -529,26 +530,43 @@ - (void)playerItemForSource:(NSDictionary *)source withCallback:(void(^)(AVPlaye
return;
}
#endif

asset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
handler([AVURLAsset URLAssetWithURL:url options:assetOptions], assetOptions);
} else if (isAsset) {
asset = [AVURLAsset URLAssetWithURL:url options:nil];
if ([uri hasPrefix:@"ph://"]) {
NSString *assetId = [uri substringFromIndex:@"ph://".length];
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil] firstObject];
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.networkAccessAllowed = YES;

[[PHCachingImageManager new] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
handler((AVURLAsset *)asset, assetOptions);
}];
} else {
handler([AVURLAsset URLAssetWithURL:url options:nil], assetOptions);
}
} else {
asset = [AVURLAsset URLAssetWithURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]] options:nil];
}
// Reset _loadingRequest
if (_loadingRequest != nil) {
[_loadingRequest finishLoading];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]] options:nil];
handler(asset, assetOptions);
}
_requestingCertificate = NO;
_requestingCertificateErrored = NO;
// End Reset _loadingRequest
if (self->_drm != nil) {
dispatch_queue_t queue = dispatch_queue_create("assetQueue", nil);
[asset.resourceLoader setDelegate:self queue:queue];
}

[self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler];
}

- (void)playerItemForSource:(NSDictionary *)source withCallback:(void(^)(AVPlayerItem *))handler
{
[self loadAssetFromSource:source withCallback:^(AVURLAsset *asset, NSMutableDictionary *assetOptions) {
// Reset _loadingRequest
if (self->_loadingRequest != nil) {
[self->_loadingRequest finishLoading];
}
self->_requestingCertificate = NO;
self->_requestingCertificateErrored = NO;
// End Reset _loadingRequest
if (self->_drm != nil) {
dispatch_queue_t queue = dispatch_queue_create("assetQueue", nil);
[asset.resourceLoader setDelegate:self queue:queue];
}

[self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler];
}];
}

#if __has_include(<react-native-video/RCTVideoCache.h>)
Expand Down Expand Up @@ -2028,4 +2046,4 @@ - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPict
}
#endif

@end
@end