Skip to content

Commit

Permalink
Merge pull request flutter#4 from holykou/master
Browse files Browse the repository at this point in the history
PR review changes
  • Loading branch information
quentinleguennec authored Apr 12, 2018
2 parents b182cc9 + ecdbee2 commit 9dfbb34
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
2 changes: 0 additions & 2 deletions packages/camera/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
<string>Can I use the camera please? Only for demo purpose of the app</string>
<key>NSMicrophoneUsageDescription</key>
<string>Only for demo purpose of the app</string>
<key>UIFileSharingEnabled</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
78 changes: 31 additions & 47 deletions packages/camera/ios/Classes/CameraPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
- (void)start;
- (void)stop;
- (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result;
- (void)stopRecordingVideo;
- (void)stopRecordingVideoWithResult:(FlutterResult)result;
- (void)captureToFile:(NSString *)filename result:(FlutterResult)result;
@end

Expand Down Expand Up @@ -139,7 +139,6 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
[_captureSession addConnection:connection];
_capturePhotoOutput = [AVCapturePhotoOutput new];
[_captureSession addOutput:_capturePhotoOutput];

return self;
}

Expand Down Expand Up @@ -176,7 +175,7 @@ - (void)captureOutput:(AVCaptureOutput *)output
}
}
if (!CMSampleBufferDataIsReady(sampleBuffer)) {
NSLog(@"sample buffer is not ready. Skipping sample");
_eventSink(@{@"event" : @"error", @"errorDescription" : @"sample buffer is not ready. Skipping sample"});
return;
}
if (_isRecording == YES) {
Expand All @@ -194,30 +193,25 @@ - (void)captureOutput:(AVCaptureOutput *)output
}

- (void)newVideoSample:(CMSampleBufferRef)sampleBuffer {
if (_isRecording) {
if (_videoWriter.status > AVAssetWriterStatusWriting) {
NSLog(@"Warning: writer status is %ld", (long)_videoWriter.status);
if (_videoWriter.status == AVAssetWriterStatusFailed) NSLog(@"Error: %@", _videoWriter.error);
if (_videoWriter.status == AVAssetWriterStatusFailed)
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",_videoWriter.error]});
return;
}
if (![_videoWriterInput appendSampleBuffer:sampleBuffer]) {
NSLog(@"Unable to write to video input");
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",@"Unable to write to video input"]});
}
}
}

- (void)newAudioSample:(CMSampleBufferRef)sampleBuffer {
if (_isRecording) {
if (_videoWriter.status > AVAssetWriterStatusWriting) {
NSLog(@"Warning: writer status is %ld", (long)_videoWriter.status);
if (_videoWriter.status == AVAssetWriterStatusFailed) NSLog(@"Error: %@", _videoWriter.error);
if (_videoWriter.status == AVAssetWriterStatusFailed)
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",_videoWriter.error]});
return;
}

if (![_audioWriterInput appendSampleBuffer:sampleBuffer]) {
NSLog(@"Unable to write to audio input");
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",@"Unable to write to audio input"]});
}
}
}

- (void)close {
Expand Down Expand Up @@ -255,50 +249,47 @@ - (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
return nil;
}
- (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result {
if (!_isAudioSetup) {
[self setUpCaptureSessionForAudio];
}

if (!_isRecording) {
NSLog(@"start video recording...");
if (![self setupWriterForPath:path]) {
NSLog(@"Setup Writer Failed");
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Setup Writer Failed"});
return;
}
[_captureSession stopRunning];
[_captureSession stopRunning];
_isRecording = YES;
[_captureSession startRunning];
[_captureSession startRunning];
}
// NSLog(@"Saved: %@ ", path );
}
- (void)stopRecordingVideo {
if (_isRecording)

- (void)stopRecordingVideoWithResult:(FlutterResult)result{
if (_isRecording)
{
_isRecording = NO;
[_captureSession stopRunning];
__block NSString *path = _videoWriter.outputURL.absoluteString;
if (_videoWriter.status != 0) {
[_videoWriter finishWritingWithCompletionHandler:^{
[_captureSession startRunning];
result(@{@"outputURL":path});
}];
}
NSLog(@"video recording stopped");
}
}

- (BOOL)setupWriterForPath:(NSString *)path {
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
time_t unixTime = (time_t)[[NSDate date] timeIntervalSince1970];
NSString *timestamp = [NSString stringWithFormat:@"%ld", unixTime];
NSString *filename = [NSString stringWithFormat:@"iPhoneVideo_%@.mp4", timestamp];
NSURL *outputURL =
[NSURL fileURLWithPath:[documentsDirectoryPath stringByAppendingPathComponent:filename]];

NSURL *outputURL;
if (path != nil) {
outputURL = [NSURL fileURLWithPath:path];
}

else
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
time_t unixTime = (time_t)[[NSDate date] timeIntervalSince1970];
NSString *timestamp = [NSString stringWithFormat:@"%ld", unixTime];
NSString *filename = [NSString stringWithFormat:@"iPhoneVideo_%@.mp4", timestamp];
outputURL =
[NSURL fileURLWithPath:[documentsDirectoryPath stringByAppendingPathComponent:filename]];
}
_videoWriter =
[[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(_videoWriter);
Expand All @@ -308,18 +299,15 @@ - (BOOL)setupWriterForPath:(NSString *)path {
[NSNumber numberWithInt:_previewSize.height], AVVideoWidthKey,
[NSNumber numberWithInt:_previewSize.width], AVVideoHeightKey,
nil];

_videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];

NSParameterAssert(_videoWriterInput);
_videoWriterInput.expectsMediaDataInRealTime = YES;

// Add the audio input
AudioChannelLayout acl;
bzero(&acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;

NSDictionary *audioOutputSettings = nil;
// Both type of audio inputs causes output video file to be corrupted.
audioOutputSettings = [NSDictionary
Expand All @@ -328,14 +316,11 @@ - (BOOL)setupWriterForPath:(NSString *)path {
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[NSData dataWithBytes:&acl length:sizeof(acl)],
AVChannelLayoutKey, nil];

_audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:audioOutputSettings];
_audioWriterInput.expectsMediaDataInRealTime = YES;

[_videoWriter addInput:_videoWriterInput];
[_videoWriter addInput:_audioWriterInput];

[_videoWriter addInput:_audioWriterInput];
dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
[_captureVideoOutput setSampleBufferDelegate:self queue:queue];
[_audioOutput setSampleBufferDelegate:self queue:queue];
Expand All @@ -359,7 +344,7 @@ - (void)setUpCaptureSessionForAudio {
[_captureSession addOutput:_audioOutput];
_isAudioSetup = YES;
} else {
NSLog(@"Error: Unable to add Audio input/output to session capture");
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Unable to add Audio input/output to session capture"});
_isAudioSetup = NO;
}
}
Expand Down Expand Up @@ -457,8 +442,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
@"captureHeight" : @(cam.captureSize.height),
});
// starting the choosen cam
FLTCam *cam2 = _cams[@(textureId)];
[cam2 start];
[cam start];
}
} else {
NSDictionary *argsMap = call.arguments;
Expand All @@ -476,7 +460,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[cam startRecordingVideoAtPath:call.arguments[@"filePath"] result:result];

} else if ([@"stopVideoRecording" isEqualToString:call.method]) {
[cam stopRecordingVideo];
[cam stopRecordingVideoWithResult:result];
result(nil);
} else {
result(FlutterMethodNotImplemented);
Expand Down

0 comments on commit 9dfbb34

Please sign in to comment.