2015-04-08 13:02:42 -06:00
|
|
|
#import "RCTConvert.h"
|
2015-03-30 23:07:55 -06:00
|
|
|
#import "RCTVideo.h"
|
|
|
|
#import "RCTLog.h"
|
2015-04-06 13:17:32 -06:00
|
|
|
#import "RCTBridgeModule.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "UIView+React.h"
|
2015-04-04 18:55:37 -06:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2015-03-30 23:07:55 -06:00
|
|
|
|
2015-04-08 02:58:00 -06:00
|
|
|
NSString *const RNVideoEventLoaded = @"videoLoaded";
|
|
|
|
NSString *const RNVideoEventLoading = @"videoLoading";
|
|
|
|
NSString *const RNVideoEventProgress = @"videoProgress";
|
2015-04-08 16:15:57 -06:00
|
|
|
NSString *const RNVideoEventSeek = @"videoSeek";
|
2015-04-08 02:58:00 -06:00
|
|
|
NSString *const RNVideoEventLoadingError = @"videoLoadError";
|
2015-04-08 00:49:14 -06:00
|
|
|
|
|
|
|
static NSString *const statusKeyPath = @"status";
|
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
@implementation RCTVideo
|
|
|
|
{
|
2015-04-07 16:24:49 -06:00
|
|
|
AVPlayer *_player;
|
2015-04-08 00:49:14 -06:00
|
|
|
AVPlayerItem *_playerItem;
|
2015-04-07 16:24:49 -06:00
|
|
|
AVPlayerLayer *_playerLayer;
|
|
|
|
NSURL *_videoURL;
|
2015-04-06 14:05:05 -06:00
|
|
|
|
2015-04-07 16:24:49 -06:00
|
|
|
/* Required to publish events */
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
2015-04-06 14:05:05 -06:00
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
bool _pendingSeek;
|
|
|
|
float _pendingSeekTime;
|
|
|
|
float _lastSeekTime;
|
|
|
|
|
2015-04-07 16:24:49 -06:00
|
|
|
/* For sending videoProgress events */
|
|
|
|
id _progressUpdateTimer;
|
|
|
|
int _progressUpdateInterval;
|
|
|
|
NSDate *_prevProgressUpdateTime;
|
2015-04-07 21:38:16 -06:00
|
|
|
|
|
|
|
/* Keep track of any modifiers, need to be applied after each play */
|
|
|
|
float _volume;
|
|
|
|
float _rate;
|
|
|
|
BOOL _muted;
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher {
|
2015-03-30 23:07:55 -06:00
|
|
|
if ((self = [super init])) {
|
2015-04-06 13:17:32 -06:00
|
|
|
_eventDispatcher = eventDispatcher;
|
2015-04-08 11:34:27 -06:00
|
|
|
_rate = 1.0;
|
|
|
|
_volume = 1.0;
|
2015-04-08 16:15:57 -06:00
|
|
|
|
|
|
|
_pendingSeek = false;
|
|
|
|
_pendingSeekTime = 0.0f;
|
|
|
|
_lastSeekTime = 0.0f;
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
#pragma mark - Progress
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)sendProgressUpdate {
|
2015-04-06 14:05:05 -06:00
|
|
|
AVPlayerItem *video = [_player currentItem];
|
2015-04-08 00:49:14 -06:00
|
|
|
if (video == nil || video.status != AVPlayerItemStatusReadyToPlay) {
|
2015-04-06 14:05:05 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
if (_prevProgressUpdateTime == nil ||
|
|
|
|
(([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) {
|
2015-04-08 02:58:00 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:RNVideoEventProgress body:@{
|
2015-04-06 14:05:05 -06:00
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(video.currentTime)],
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
2015-04-08 11:34:27 -06:00
|
|
|
|
2015-04-06 14:05:05 -06:00
|
|
|
_prevProgressUpdateTime = [NSDate date];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
- (void)stopProgressTimer {
|
|
|
|
[_progressUpdateTimer invalidate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startProgressTimer {
|
|
|
|
_progressUpdateInterval = 250;
|
|
|
|
_prevProgressUpdateTime = nil;
|
2015-04-08 11:34:27 -06:00
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
[self stopProgressTimer];
|
2015-04-08 11:34:27 -06:00
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
_progressUpdateTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(sendProgressUpdate)];
|
|
|
|
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Player and source
|
|
|
|
|
|
|
|
- (void)setSrc:(NSDictionary *)source {
|
2015-04-08 00:49:14 -06:00
|
|
|
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
|
2015-04-08 09:46:13 -06:00
|
|
|
_playerItem = [self playerItemForSource:source];
|
2015-04-08 00:49:14 -06:00
|
|
|
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
|
|
|
|
|
|
|
|
[_player pause];
|
|
|
|
[_playerLayer removeFromSuperlayer];
|
|
|
|
|
|
|
|
_player = [AVPlayer playerWithPlayerItem:_playerItem];
|
2015-04-04 18:55:37 -06:00
|
|
|
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
|
2015-04-08 00:49:14 -06:00
|
|
|
|
2015-04-04 18:55:37 -06:00
|
|
|
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
|
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
_playerLayer.needsDisplayOnBoundsChange = YES;
|
2015-04-06 13:17:32 -06:00
|
|
|
|
2015-04-04 18:55:37 -06:00
|
|
|
[self.layer addSublayer:_playerLayer];
|
|
|
|
self.layer.needsDisplayOnBoundsChange = YES;
|
2015-04-06 13:17:32 -06:00
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:RNVideoEventLoading body:@{
|
2015-04-08 00:49:14 -06:00
|
|
|
@"src": @{
|
2015-04-08 09:46:13 -06:00
|
|
|
@"uri": [source objectForKey:@"uri"],
|
|
|
|
@"type": [source objectForKey:@"type"],
|
|
|
|
@"isNetwork":[NSNumber numberWithBool:(bool)[source objectForKey:@"isNetwork"]]
|
2015-04-08 00:49:14 -06:00
|
|
|
},
|
2015-04-06 13:17:32 -06:00
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
2015-04-08 00:49:14 -06:00
|
|
|
}
|
2015-04-07 09:31:40 -06:00
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source {
|
|
|
|
bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]];
|
|
|
|
bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]];
|
|
|
|
NSString *uri = [source objectForKey:@"uri"];
|
|
|
|
NSString *type = [source objectForKey:@"type"];
|
|
|
|
|
|
|
|
NSURL *url = (isNetwork || isAsset) ?
|
|
|
|
[NSURL URLWithString:uri] :
|
|
|
|
[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
|
|
|
|
|
|
|
|
if (isAsset) {
|
|
|
|
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
|
|
|
|
return [AVPlayerItem playerItemWithAsset:asset];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [AVPlayerItem playerItemWithURL:url];
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
|
if (object == _playerItem) {
|
|
|
|
if (_playerItem.status == AVPlayerItemStatusReadyToPlay) {
|
2015-04-09 08:19:09 -06:00
|
|
|
// NSLog(@"duration: %f",CMTimeGetSeconds(_playerItem.asset.duration));
|
2015-04-08 02:58:00 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:RNVideoEventLoaded body:@{
|
2015-04-08 00:49:14 -06:00
|
|
|
@"duration": [NSNumber numberWithFloat:CMTimeGetSeconds(_playerItem.duration)],
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(_playerItem.currentTime)],
|
|
|
|
@"canPlayReverse": [NSNumber numberWithBool:_playerItem.canPlayReverse],
|
|
|
|
@"canPlayFastForward": [NSNumber numberWithBool:_playerItem.canPlayFastForward],
|
|
|
|
@"canPlaySlowForward": [NSNumber numberWithBool:_playerItem.canPlaySlowForward],
|
|
|
|
@"canPlaySlowReverse": [NSNumber numberWithBool:_playerItem.canPlaySlowReverse],
|
|
|
|
@"canStepBackward": [NSNumber numberWithBool:_playerItem.canStepBackward],
|
|
|
|
@"canStepForward": [NSNumber numberWithBool:_playerItem.canStepForward],
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
[self startProgressTimer];
|
2015-04-08 00:49:14 -06:00
|
|
|
[_player play];
|
|
|
|
[self applyModifiers];
|
|
|
|
} else if(_playerItem.status == AVPlayerItemStatusFailed) {
|
2015-04-08 09:46:13 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:RNVideoEventLoadingError body:@{
|
2015-04-08 00:49:14 -06:00
|
|
|
@"error": @{
|
|
|
|
@"code": [NSNumber numberWithInt:_playerItem.error.code],
|
|
|
|
@"domain": _playerItem.error.domain
|
|
|
|
},
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
|
|
}
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
- (void)playerItemDidReachEnd:(NSNotification *)notification {
|
2015-04-08 11:34:27 -06:00
|
|
|
AVPlayerItem *item = [notification object];
|
|
|
|
[item seekToTime:kCMTimeZero];
|
|
|
|
[_player play];
|
|
|
|
[self applyModifiers];
|
2015-04-08 09:46:13 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
#pragma mark - Prop setters
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)setResizeMode:(NSString*)mode {
|
2015-04-04 18:55:37 -06:00
|
|
|
_playerLayer.videoGravity = mode;
|
|
|
|
}
|
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
- (void)setPaused:(BOOL)paused {
|
2015-04-07 21:38:16 -06:00
|
|
|
if (paused) {
|
2015-04-08 09:46:13 -06:00
|
|
|
[self stopProgressTimer];
|
2015-04-07 09:31:40 -06:00
|
|
|
[_player pause];
|
|
|
|
} else {
|
2015-04-08 09:46:13 -06:00
|
|
|
[self startProgressTimer];
|
2015-04-07 09:31:40 -06:00
|
|
|
[_player play];
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-04-07 09:31:40 -06:00
|
|
|
}
|
2015-04-05 11:17:03 -06:00
|
|
|
}
|
|
|
|
|
2015-04-09 07:50:20 -06:00
|
|
|
- (void)setSeek:(float)seekTime {
|
2015-04-08 16:15:57 -06:00
|
|
|
int timeScale = 10000;
|
|
|
|
|
|
|
|
AVPlayerItem *item = _player.currentItem;
|
|
|
|
if (item && item.status == AVPlayerItemStatusReadyToPlay) {
|
|
|
|
// TODO check loadedTimeRanges
|
|
|
|
|
|
|
|
CMTime cmSeekTime = CMTimeMakeWithSeconds(seekTime, timeScale);
|
|
|
|
CMTime current = item.currentTime;
|
|
|
|
// TODO figure out a good tolerance level
|
|
|
|
CMTime tolerance = CMTimeMake(1000, timeScale);
|
2015-04-09 07:50:20 -06:00
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
if (CMTimeCompare(current, cmSeekTime) != 0) {
|
2015-04-09 08:19:09 -06:00
|
|
|
[_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance completionHandler:^(BOOL finished) {
|
|
|
|
[_eventDispatcher sendInputEventWithName:RNVideoEventSeek body:@{
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
|
|
|
|
@"seekTime": [NSNumber numberWithFloat:seekTime],
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
2015-04-08 16:15:57 -06:00
|
|
|
}];
|
2015-04-09 08:19:09 -06:00
|
|
|
|
|
|
|
_pendingSeek = false;
|
2015-04-08 16:15:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// TODO see if this makes sense and if so,
|
|
|
|
// actually implement it
|
|
|
|
_pendingSeek = true;
|
|
|
|
_pendingSeekTime = seekTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRate:(float)rate {
|
2015-04-07 21:38:16 -06:00
|
|
|
_rate = rate;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
- (void)setMuted:(BOOL)muted {
|
2015-04-07 21:38:16 -06:00
|
|
|
_muted = muted;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVolume:(float)volume {
|
|
|
|
_volume = volume;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
- (void)applyModifiers {
|
2015-04-08 11:34:27 -06:00
|
|
|
/* volume must be set to 0 if muted is YES, or the video freezes playback */
|
2015-04-07 21:38:16 -06:00
|
|
|
if (_muted) {
|
|
|
|
[_player setVolume:0];
|
|
|
|
[_player setMuted:YES];
|
|
|
|
} else {
|
|
|
|
[_player setVolume:_volume];
|
|
|
|
[_player setMuted:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_player setRate:_rate];
|
|
|
|
}
|
2015-04-04 18:55:37 -06:00
|
|
|
|
|
|
|
- (void)setRepeatEnabled {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(playerItemDidReachEnd:)
|
|
|
|
name:AVPlayerItemDidPlayToEndTimeNotification
|
|
|
|
object:[_player currentItem]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setRepeatDisabled {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)setRepeat:(BOOL)repeat {
|
2015-03-31 00:29:15 -06:00
|
|
|
if (repeat) {
|
2015-04-04 18:55:37 -06:00
|
|
|
[self setRepeatEnabled];
|
2015-03-31 00:29:15 -06:00
|
|
|
} else {
|
2015-04-04 18:55:37 -06:00
|
|
|
[self setRepeatDisabled];
|
2015-03-31 00:29:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
#pragma mark - React View Management
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex {
|
2015-03-30 23:07:55 -06:00
|
|
|
RCTLogError(@"video cannot have any subviews");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)removeReactSubview:(UIView *)subview {
|
2015-03-30 23:07:55 -06:00
|
|
|
RCTLogError(@"video cannot have any subviews");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)layoutSubviews {
|
2015-03-30 23:07:55 -06:00
|
|
|
[super layoutSubviews];
|
2015-04-04 18:55:37 -06:00
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
}
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
#pragma mark - Lifecycle
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-04-07 11:41:34 -06:00
|
|
|
- (void)removeFromSuperview
|
2015-04-04 18:55:37 -06:00
|
|
|
{
|
2015-04-07 11:41:34 -06:00
|
|
|
[_player pause];
|
2015-04-06 14:05:05 -06:00
|
|
|
[_progressUpdateTimer invalidate];
|
2015-04-07 11:41:34 -06:00
|
|
|
[_playerLayer removeFromSuperlayer];
|
|
|
|
_player = nil;
|
|
|
|
_prevProgressUpdateTime = nil;
|
|
|
|
_eventDispatcher = nil;
|
2015-04-08 00:49:14 -06:00
|
|
|
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
|
2015-04-04 18:55:37 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|