Skip to content

Commit 2bf1cbc

Browse files
Merge pull request flutter#7 from holykou/master
PR changes for iOS
2 parents 08b5884 + c9de84c commit 2bf1cbc

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

packages/camera/ios/Classes/CameraPlugin.m

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
8686
error:(NSError **)error;
8787
- (void)start;
8888
- (void)stop;
89-
- (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result;
90-
- (void)stopRecordingVideoWithResult:(FlutterResult)result;
89+
- (void)startVideoRecordingAtPath:(NSString *)path result:(FlutterResult)result;
90+
- (void)stopVideoRecordingWithResult:(FlutterResult)result;
9191
- (void)captureToFile:(NSString *)filename result:(FlutterResult)result;
9292
@end
9393

@@ -211,12 +211,14 @@ - (void)newVideoSample:(CMSampleBufferRef)sampleBuffer {
211211
});
212212
return;
213213
}
214-
if (![_videoWriterInput appendSampleBuffer:sampleBuffer]) {
215-
_eventSink(@{
216-
@"event" : @"error",
217-
@"errorDescription" : [NSString stringWithFormat:@"%@", @"Unable to write to video input"]
218-
});
219-
}
214+
if (_videoWriterInput.readyForMoreMediaData) {
215+
if (![_videoWriterInput appendSampleBuffer:sampleBuffer]) {
216+
_eventSink(@{
217+
@"event" : @"error",
218+
@"errorDescription" : [NSString stringWithFormat:@"%@", @"Unable to write to video input"]
219+
});
220+
}
221+
}
220222
}
221223

222224
- (void)newAudioSample:(CMSampleBufferRef)sampleBuffer {
@@ -228,12 +230,14 @@ - (void)newAudioSample:(CMSampleBufferRef)sampleBuffer {
228230
});
229231
return;
230232
}
231-
if (![_audioWriterInput appendSampleBuffer:sampleBuffer]) {
232-
_eventSink(@{
233-
@"event" : @"error",
234-
@"errorDescription" : [NSString stringWithFormat:@"%@", @"Unable to write to audio input"]
235-
});
236-
}
233+
if (_audioWriterInput.readyForMoreMediaData) {
234+
if (![_audioWriterInput appendSampleBuffer:sampleBuffer]) {
235+
_eventSink(@{
236+
@"event" : @"error",
237+
@"errorDescription" : [NSString stringWithFormat:@"%@", @"Unable to write to audio input"]
238+
});
239+
}
240+
}
237241
}
238242

239243
- (void)close {
@@ -270,7 +274,7 @@ - (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
270274
_eventSink = events;
271275
return nil;
272276
}
273-
- (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result {
277+
- (void)startVideoRecordingAtPath:(NSString *)path result:(FlutterResult)result {
274278
if (!_isRecording) {
275279
if (![self setupWriterForPath:path]) {
276280
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Setup Writer Failed"});
@@ -284,16 +288,26 @@ - (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result
284288
}
285289
}
286290

287-
- (void)stopRecordingVideoWithResult:(FlutterResult)result {
291+
- (void)stopVideoRecordingWithResult:(FlutterResult)result {
288292
if (_isRecording) {
289293
_isRecording = NO;
290-
__block NSString *path = _videoWriter.outputURL.absoluteString;
291-
if (_videoWriter.status != 0) {
294+
if (_videoWriter.status != AVAssetWriterStatusUnknown) {
292295
[_videoWriter finishWritingWithCompletionHandler:^{
293-
result(@{@"outputURL" : path});
296+
if (self->_videoWriter.status == AVAssetWriterStatusCompleted) {
297+
result(nil);
298+
}
299+
else
300+
{
301+
self->_eventSink(@{@"event" : @"error", @"errorDescription" : @"AVAssetWriter could not finish writing!"});
302+
}
294303
}];
295304
}
296305
}
306+
else
307+
{
308+
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSURLErrorResourceUnavailable userInfo:@{NSLocalizedDescriptionKey:@"Video is not recording!"}];
309+
result([error flutterError]);
310+
}
297311
}
298312

299313
- (BOOL)setupWriterForPath:(NSString *)path {
@@ -355,6 +369,12 @@ - (void)setUpCaptureSessionForAudio {
355369
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
356370
AVCaptureDeviceInput *audioInput =
357371
[AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
372+
if (error) {
373+
_eventSink(@{
374+
@"event" : @"error",
375+
@"errorDescription" : error.description
376+
});
377+
}
358378
// Setup the audio output.
359379
_audioOutput = [[AVCaptureAudioDataOutput alloc] init];
360380

@@ -378,7 +398,7 @@ - (void)setUpCaptureSessionForAudio {
378398
@interface CameraPlugin ()
379399
@property(readonly, nonatomic) NSObject<FlutterTextureRegistry> *registry;
380400
@property(readonly, nonatomic) NSObject<FlutterBinaryMessenger> *messenger;
381-
@property(readonly, nonatomic) NSMutableDictionary *cams;
401+
@property(readonly, nonatomic) FLTCam *camera;
382402
@end
383403

384404
@implementation CameraPlugin
@@ -397,17 +417,11 @@ - (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry
397417
NSAssert(self, @"super init cannot be nil");
398418
_registry = registry;
399419
_messenger = messenger;
400-
_cams = [NSMutableDictionary dictionaryWithCapacity:1];
401420
return self;
402421
}
403422

404423
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
405424
if ([@"init" isEqualToString:call.method]) {
406-
for (NSNumber *textureId in _cams) {
407-
[_registry unregisterTexture:[textureId longLongValue]];
408-
[[_cams objectForKey:textureId] close];
409-
}
410-
[_cams removeAllObjects];
411425
result(nil);
412426
} else if ([@"availableCameras" isEqualToString:call.method]) {
413427
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
@@ -447,7 +461,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
447461
result([error flutterError]);
448462
} else {
449463
int64_t textureId = [_registry registerTexture:cam];
450-
_cams[@(textureId)] = cam;
464+
_camera = cam;
451465
cam.onFrameAvailable = ^{
452466
[_registry textureFrameAvailable:textureId];
453467
};
@@ -470,21 +484,18 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
470484
} else {
471485
NSDictionary *argsMap = call.arguments;
472486
NSUInteger textureId = ((NSNumber *)argsMap[@"textureId"]).unsignedIntegerValue;
473-
FLTCam *cam = _cams[@(textureId)];
487+
474488

475489
if ([@"takePicture" isEqualToString:call.method]) {
476-
[cam captureToFile:call.arguments[@"path"] result:result];
490+
[_camera captureToFile:call.arguments[@"path"] result:result];
477491
} else if ([@"dispose" isEqualToString:call.method]) {
478492
[_registry unregisterTexture:textureId];
479-
[cam close];
480-
[_cams removeObjectForKey:@(textureId)];
493+
[_camera close];
481494
result(nil);
482495
} else if ([@"startVideoRecording" isEqualToString:call.method]) {
483-
[cam startRecordingVideoAtPath:call.arguments[@"filePath"] result:result];
484-
496+
[_camera startVideoRecordingAtPath:call.arguments[@"filePath"] result:result];
485497
} else if ([@"stopVideoRecording" isEqualToString:call.method]) {
486-
[cam stopRecordingVideoWithResult:result];
487-
result(nil);
498+
[_camera stopVideoRecordingWithResult:result];
488499
} else {
489500
result(FlutterMethodNotImplemented);
490501
}

0 commit comments

Comments
 (0)