Skip to content

Commit

Permalink
feat(codec): move codec, max duration, and max file size settings to …
Browse files Browse the repository at this point in the history
…the session queue. (react-native-camera#2694)

This might prevent a race condition when changing presets/quality.

Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
  • Loading branch information
cristianoccazinsp and cristianocca committed Feb 5, 2020
1 parent 7806f84 commit 9b4af8e
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -1007,15 +1007,7 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
return;
}

if (options[@"maxDuration"]) {
Float64 maxDuration = [options[@"maxDuration"] floatValue];
self.movieFileOutput.maxRecordedDuration = CMTimeMakeWithSeconds(maxDuration, 30);
}

if (options[@"maxFileSize"]) {
self.movieFileOutput.maxRecordedFileSize = [options[@"maxFileSize"] integerValue];
}


// video preset will be cleanedup/restarted once capture is done
// with a camera cleanup call
if (options[@"quality"]) {
Expand All @@ -1032,6 +1024,7 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
}

AVCaptureConnection *connection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];

if (self.videoStabilizationMode != 0) {
if (connection.isVideoStabilizationSupported == NO) {
RCTLogWarn(@"%s: Video Stabilization is not supported on this device.", __func__);
Expand All @@ -1041,29 +1034,7 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
}
[connection setVideoOrientation:orientation];

if (options[@"codec"]) {
if (@available(iOS 10, *)) {
AVVideoCodecType videoCodecType = options[@"codec"];
if ([self.movieFileOutput.availableVideoCodecTypes containsObject:videoCodecType]) {
self.videoCodecType = videoCodecType;
if(options[@"videoBitrate"]) {
NSString *videoBitrate = options[@"videoBitrate"];
[self.movieFileOutput setOutputSettings:@{
AVVideoCodecKey:videoCodecType,
AVVideoCompressionPropertiesKey:
@{
AVVideoAverageBitRateKey:videoBitrate
}
} forConnection:connection];
} else {
[self.movieFileOutput setOutputSettings:@{AVVideoCodecKey:videoCodecType} forConnection:connection];
}
} else {
RCTLogWarn(@"%s: Setting videoCodec is only supported above iOS version 10.", __func__);
}
}
}



BOOL recordAudio = [options valueForKey:@"mute"] == nil || ([options valueForKey:@"mute"] != nil && ![options[@"mute"] boolValue]);

Expand Down Expand Up @@ -1097,7 +1068,46 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
}

dispatch_async(self.sessionQueue, ^{

// session preset might affect this, so we run this code
// also in the session queue

if (options[@"maxDuration"]) {
Float64 maxDuration = [options[@"maxDuration"] floatValue];
self.movieFileOutput.maxRecordedDuration = CMTimeMakeWithSeconds(maxDuration, 30);
}

if (options[@"maxFileSize"]) {
self.movieFileOutput.maxRecordedFileSize = [options[@"maxFileSize"] integerValue];
}

if (options[@"codec"]) {
if (@available(iOS 10, *)) {
AVVideoCodecType videoCodecType = options[@"codec"];
if ([self.movieFileOutput.availableVideoCodecTypes containsObject:videoCodecType]) {
self.videoCodecType = videoCodecType;
if(options[@"videoBitrate"]) {
NSString *videoBitrate = options[@"videoBitrate"];
[self.movieFileOutput setOutputSettings:@{
AVVideoCodecKey:videoCodecType,
AVVideoCompressionPropertiesKey:
@{
AVVideoAverageBitRateKey:videoBitrate
}
} forConnection:connection];
} else {
[self.movieFileOutput setOutputSettings:@{AVVideoCodecKey:videoCodecType} forConnection:connection];
}
} else {
RCTLogWarn(@"Video Codec %@ is not available.", videoCodecType);
}
}
else {
RCTLogWarn(@"%s: Setting videoCodec is only supported above iOS version 10.", __func__);
}
}


NSString *path = nil;
if (options[@"path"]) {
path = options[@"path"];
Expand Down

0 comments on commit 9b4af8e

Please sign in to comment.