Skip to content

Commit 9dfbb34

Browse files
Merge pull request flutter#4 from holykou/master
PR review changes
2 parents b182cc9 + ecdbee2 commit 9dfbb34

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

packages/camera/example/ios/Runner/Info.plist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
<string>Can I use the camera please? Only for demo purpose of the app</string>
2929
<key>NSMicrophoneUsageDescription</key>
3030
<string>Only for demo purpose of the app</string>
31-
<key>UIFileSharingEnabled</key>
32-
<true/>
3331
<key>UILaunchStoryboardName</key>
3432
<string>LaunchScreen</string>
3533
<key>UIMainStoryboardFile</key>

packages/camera/ios/Classes/CameraPlugin.m

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
8787
- (void)start;
8888
- (void)stop;
8989
- (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result;
90-
- (void)stopRecordingVideo;
90+
- (void)stopRecordingVideoWithResult:(FlutterResult)result;
9191
- (void)captureToFile:(NSString *)filename result:(FlutterResult)result;
9292
@end
9393

@@ -139,7 +139,6 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
139139
[_captureSession addConnection:connection];
140140
_capturePhotoOutput = [AVCapturePhotoOutput new];
141141
[_captureSession addOutput:_capturePhotoOutput];
142-
143142
return self;
144143
}
145144

@@ -176,7 +175,7 @@ - (void)captureOutput:(AVCaptureOutput *)output
176175
}
177176
}
178177
if (!CMSampleBufferDataIsReady(sampleBuffer)) {
179-
NSLog(@"sample buffer is not ready. Skipping sample");
178+
_eventSink(@{@"event" : @"error", @"errorDescription" : @"sample buffer is not ready. Skipping sample"});
180179
return;
181180
}
182181
if (_isRecording == YES) {
@@ -194,30 +193,25 @@ - (void)captureOutput:(AVCaptureOutput *)output
194193
}
195194

196195
- (void)newVideoSample:(CMSampleBufferRef)sampleBuffer {
197-
if (_isRecording) {
198196
if (_videoWriter.status > AVAssetWriterStatusWriting) {
199-
NSLog(@"Warning: writer status is %ld", (long)_videoWriter.status);
200-
if (_videoWriter.status == AVAssetWriterStatusFailed) NSLog(@"Error: %@", _videoWriter.error);
197+
if (_videoWriter.status == AVAssetWriterStatusFailed)
198+
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",_videoWriter.error]});
201199
return;
202200
}
203201
if (![_videoWriterInput appendSampleBuffer:sampleBuffer]) {
204-
NSLog(@"Unable to write to video input");
202+
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",@"Unable to write to video input"]});
205203
}
206-
}
207204
}
208205

209206
- (void)newAudioSample:(CMSampleBufferRef)sampleBuffer {
210-
if (_isRecording) {
211207
if (_videoWriter.status > AVAssetWriterStatusWriting) {
212-
NSLog(@"Warning: writer status is %ld", (long)_videoWriter.status);
213-
if (_videoWriter.status == AVAssetWriterStatusFailed) NSLog(@"Error: %@", _videoWriter.error);
208+
if (_videoWriter.status == AVAssetWriterStatusFailed)
209+
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",_videoWriter.error]});
214210
return;
215211
}
216-
217212
if (![_audioWriterInput appendSampleBuffer:sampleBuffer]) {
218-
NSLog(@"Unable to write to audio input");
213+
_eventSink(@{@"event" : @"error", @"errorDescription" : [NSString stringWithFormat:@"%@",@"Unable to write to audio input"]});
219214
}
220-
}
221215
}
222216

223217
- (void)close {
@@ -255,50 +249,47 @@ - (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
255249
return nil;
256250
}
257251
- (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result {
258-
if (!_isAudioSetup) {
259-
[self setUpCaptureSessionForAudio];
260-
}
261252

262253
if (!_isRecording) {
263-
NSLog(@"start video recording...");
264254
if (![self setupWriterForPath:path]) {
265-
NSLog(@"Setup Writer Failed");
255+
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Setup Writer Failed"});
266256
return;
267257
}
268-
[_captureSession stopRunning];
258+
[_captureSession stopRunning];
269259
_isRecording = YES;
270-
[_captureSession startRunning];
260+
[_captureSession startRunning];
271261
}
272-
// NSLog(@"Saved: %@ ", path );
273262
}
274-
- (void)stopRecordingVideo {
275-
if (_isRecording)
276263

264+
- (void)stopRecordingVideoWithResult:(FlutterResult)result{
265+
if (_isRecording)
277266
{
278267
_isRecording = NO;
279-
[_captureSession stopRunning];
268+
__block NSString *path = _videoWriter.outputURL.absoluteString;
280269
if (_videoWriter.status != 0) {
281270
[_videoWriter finishWritingWithCompletionHandler:^{
282-
[_captureSession startRunning];
271+
result(@{@"outputURL":path});
283272
}];
284273
}
285-
NSLog(@"video recording stopped");
286274
}
287275
}
276+
288277
- (BOOL)setupWriterForPath:(NSString *)path {
289278
NSError *error = nil;
290-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
291-
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
292-
time_t unixTime = (time_t)[[NSDate date] timeIntervalSince1970];
293-
NSString *timestamp = [NSString stringWithFormat:@"%ld", unixTime];
294-
NSString *filename = [NSString stringWithFormat:@"iPhoneVideo_%@.mp4", timestamp];
295-
NSURL *outputURL =
296-
[NSURL fileURLWithPath:[documentsDirectoryPath stringByAppendingPathComponent:filename]];
297-
279+
NSURL *outputURL;
298280
if (path != nil) {
299281
outputURL = [NSURL fileURLWithPath:path];
300282
}
301-
283+
else
284+
{
285+
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
286+
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
287+
time_t unixTime = (time_t)[[NSDate date] timeIntervalSince1970];
288+
NSString *timestamp = [NSString stringWithFormat:@"%ld", unixTime];
289+
NSString *filename = [NSString stringWithFormat:@"iPhoneVideo_%@.mp4", timestamp];
290+
outputURL =
291+
[NSURL fileURLWithPath:[documentsDirectoryPath stringByAppendingPathComponent:filename]];
292+
}
302293
_videoWriter =
303294
[[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:&error];
304295
NSParameterAssert(_videoWriter);
@@ -308,18 +299,15 @@ - (BOOL)setupWriterForPath:(NSString *)path {
308299
[NSNumber numberWithInt:_previewSize.height], AVVideoWidthKey,
309300
[NSNumber numberWithInt:_previewSize.width], AVVideoHeightKey,
310301
nil];
311-
312302
_videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
313303
outputSettings:videoSettings];
314-
315304
NSParameterAssert(_videoWriterInput);
316305
_videoWriterInput.expectsMediaDataInRealTime = YES;
317306

318307
// Add the audio input
319308
AudioChannelLayout acl;
320309
bzero(&acl, sizeof(acl));
321310
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
322-
323311
NSDictionary *audioOutputSettings = nil;
324312
// Both type of audio inputs causes output video file to be corrupted.
325313
audioOutputSettings = [NSDictionary
@@ -328,14 +316,11 @@ - (BOOL)setupWriterForPath:(NSString *)path {
328316
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
329317
[NSData dataWithBytes:&acl length:sizeof(acl)],
330318
AVChannelLayoutKey, nil];
331-
332319
_audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
333320
outputSettings:audioOutputSettings];
334321
_audioWriterInput.expectsMediaDataInRealTime = YES;
335-
336322
[_videoWriter addInput:_videoWriterInput];
337-
[_videoWriter addInput:_audioWriterInput];
338-
323+
[_videoWriter addInput:_audioWriterInput];
339324
dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
340325
[_captureVideoOutput setSampleBufferDelegate:self queue:queue];
341326
[_audioOutput setSampleBufferDelegate:self queue:queue];
@@ -359,7 +344,7 @@ - (void)setUpCaptureSessionForAudio {
359344
[_captureSession addOutput:_audioOutput];
360345
_isAudioSetup = YES;
361346
} else {
362-
NSLog(@"Error: Unable to add Audio input/output to session capture");
347+
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Unable to add Audio input/output to session capture"});
363348
_isAudioSetup = NO;
364349
}
365350
}
@@ -457,8 +442,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
457442
@"captureHeight" : @(cam.captureSize.height),
458443
});
459444
// starting the choosen cam
460-
FLTCam *cam2 = _cams[@(textureId)];
461-
[cam2 start];
445+
[cam start];
462446
}
463447
} else {
464448
NSDictionary *argsMap = call.arguments;
@@ -476,7 +460,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
476460
[cam startRecordingVideoAtPath:call.arguments[@"filePath"] result:result];
477461

478462
} else if ([@"stopVideoRecording" isEqualToString:call.method]) {
479-
[cam stopRecordingVideo];
463+
[cam stopRecordingVideoWithResult:result];
480464
result(nil);
481465
} else {
482466
result(FlutterMethodNotImplemented);

0 commit comments

Comments
 (0)