@@ -86,8 +86,8 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
86
86
error : (NSError **)error ;
87
87
- (void )start ;
88
88
- (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 ;
91
91
- (void )captureToFile : (NSString *)filename result : (FlutterResult)result ;
92
92
@end
93
93
@@ -211,12 +211,14 @@ - (void)newVideoSample:(CMSampleBufferRef)sampleBuffer {
211
211
});
212
212
return ;
213
213
}
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
+ }
220
222
}
221
223
222
224
- (void )newAudioSample : (CMSampleBufferRef)sampleBuffer {
@@ -228,12 +230,14 @@ - (void)newAudioSample:(CMSampleBufferRef)sampleBuffer {
228
230
});
229
231
return ;
230
232
}
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
+ }
237
241
}
238
242
239
243
- (void )close {
@@ -270,7 +274,7 @@ - (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
270
274
_eventSink = events;
271
275
return nil ;
272
276
}
273
- - (void )startRecordingVideoAtPath : (NSString *)path result : (FlutterResult)result {
277
+ - (void )startVideoRecordingAtPath : (NSString *)path result : (FlutterResult)result {
274
278
if (!_isRecording) {
275
279
if (![self setupWriterForPath: path]) {
276
280
_eventSink (@{@" event" : @" error" , @" errorDescription" : @" Setup Writer Failed" });
@@ -284,16 +288,26 @@ - (void)startRecordingVideoAtPath:(NSString *)path result:(FlutterResult)result
284
288
}
285
289
}
286
290
287
- - (void )stopRecordingVideoWithResult : (FlutterResult)result {
291
+ - (void )stopVideoRecordingWithResult : (FlutterResult)result {
288
292
if (_isRecording) {
289
293
_isRecording = NO ;
290
- __block NSString *path = _videoWriter.outputURL .absoluteString ;
291
- if (_videoWriter.status != 0 ) {
294
+ if (_videoWriter.status != AVAssetWriterStatusUnknown) {
292
295
[_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
+ }
294
303
}];
295
304
}
296
305
}
306
+ else
307
+ {
308
+ NSError *error = [NSError errorWithDomain: NSCocoaErrorDomain code: NSURLErrorResourceUnavailable userInfo: @{NSLocalizedDescriptionKey :@" Video is not recording!" }];
309
+ result ([error flutterError ]);
310
+ }
297
311
}
298
312
299
313
- (BOOL )setupWriterForPath : (NSString *)path {
@@ -355,6 +369,12 @@ - (void)setUpCaptureSessionForAudio {
355
369
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio];
356
370
AVCaptureDeviceInput *audioInput =
357
371
[AVCaptureDeviceInput deviceInputWithDevice: audioDevice error: &error];
372
+ if (error) {
373
+ _eventSink (@{
374
+ @" event" : @" error" ,
375
+ @" errorDescription" : error.description
376
+ });
377
+ }
358
378
// Setup the audio output.
359
379
_audioOutput = [[AVCaptureAudioDataOutput alloc ] init ];
360
380
@@ -378,7 +398,7 @@ - (void)setUpCaptureSessionForAudio {
378
398
@interface CameraPlugin ()
379
399
@property (readonly , nonatomic ) NSObject <FlutterTextureRegistry> *registry;
380
400
@property (readonly , nonatomic ) NSObject <FlutterBinaryMessenger> *messenger;
381
- @property (readonly , nonatomic ) NSMutableDictionary *cams ;
401
+ @property (readonly , nonatomic ) FLTCam *camera ;
382
402
@end
383
403
384
404
@implementation CameraPlugin
@@ -397,17 +417,11 @@ - (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry
397
417
NSAssert (self, @" super init cannot be nil" );
398
418
_registry = registry;
399
419
_messenger = messenger;
400
- _cams = [NSMutableDictionary dictionaryWithCapacity: 1 ];
401
420
return self;
402
421
}
403
422
404
423
- (void )handleMethodCall : (FlutterMethodCall *)call result : (FlutterResult)result {
405
424
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 ];
411
425
result (nil );
412
426
} else if ([@" availableCameras" isEqualToString: call.method]) {
413
427
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
@@ -447,7 +461,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
447
461
result ([error flutterError ]);
448
462
} else {
449
463
int64_t textureId = [_registry registerTexture: cam];
450
- _cams[@(textureId)] = cam;
464
+ _camera = cam;
451
465
cam.onFrameAvailable = ^{
452
466
[_registry textureFrameAvailable: textureId];
453
467
};
@@ -470,21 +484,18 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
470
484
} else {
471
485
NSDictionary *argsMap = call.arguments ;
472
486
NSUInteger textureId = ((NSNumber *)argsMap[@" textureId" ]).unsignedIntegerValue ;
473
- FLTCam *cam = _cams[@(textureId)];
487
+
474
488
475
489
if ([@" takePicture" isEqualToString: call.method]) {
476
- [cam captureToFile: call.arguments[@" path" ] result: result];
490
+ [_camera captureToFile: call.arguments[@" path" ] result: result];
477
491
} else if ([@" dispose" isEqualToString: call.method]) {
478
492
[_registry unregisterTexture: textureId];
479
- [cam close ];
480
- [_cams removeObjectForKey: @(textureId)];
493
+ [_camera close ];
481
494
result (nil );
482
495
} else if ([@" startVideoRecording" isEqualToString: call.method]) {
483
- [cam startRecordingVideoAtPath: call.arguments[@" filePath" ] result: result];
484
-
496
+ [_camera startVideoRecordingAtPath: call.arguments[@" filePath" ] result: result];
485
497
} else if ([@" stopVideoRecording" isEqualToString: call.method]) {
486
- [cam stopRecordingVideoWithResult: result];
487
- result (nil );
498
+ [_camera stopVideoRecordingWithResult: result];
488
499
} else {
489
500
result (FlutterMethodNotImplemented);
490
501
}
0 commit comments