Race conditions initializing flashMode (#436)

* Fix race condition initializing Android camera

* Fix race condition initializing flashMode iOS
This commit is contained in:
Ron Heft 2016-12-08 21:05:35 -05:00 committed by Nicolas Charpentier
parent 33bd0c4737
commit b308d08e9a
2 changed files with 30 additions and 23 deletions

View File

@ -35,7 +35,7 @@ public class RCTCamera {
} }
public Camera acquireCameraInstance(int type) { public synchronized Camera acquireCameraInstance(int type) {
if (null == _cameras.get(type) && null != _cameraTypeToIndex.get(type)) { if (null == _cameras.get(type) && null != _cameraTypeToIndex.get(type)) {
try { try {
Camera camera = Camera.open(_cameraTypeToIndex.get(type)); Camera camera = Camera.open(_cameraTypeToIndex.get(type));
@ -206,7 +206,7 @@ public class RCTCamera {
} }
public void setCaptureQuality(int cameraType, String captureQuality) { public void setCaptureQuality(int cameraType, String captureQuality) {
Camera camera = _cameras.get(cameraType); Camera camera = this.acquireCameraInstance(cameraType);
if (camera == null) { if (camera == null) {
return; return;
} }
@ -246,7 +246,7 @@ public class RCTCamera {
} }
public CamcorderProfile setCaptureVideoQuality(int cameraType, String captureQuality) { public CamcorderProfile setCaptureVideoQuality(int cameraType, String captureQuality) {
Camera camera = _cameras.get(cameraType); Camera camera = this.acquireCameraInstance(cameraType);
if (camera == null) { if (camera == null) {
return null; return null;
} }
@ -294,7 +294,7 @@ public class RCTCamera {
} }
public void setTorchMode(int cameraType, int torchMode) { public void setTorchMode(int cameraType, int torchMode) {
Camera camera = _cameras.get(cameraType); Camera camera = this.acquireCameraInstance(cameraType);
if (null == camera) { if (null == camera) {
return; return;
} }
@ -318,7 +318,7 @@ public class RCTCamera {
} }
public void setFlashMode(int cameraType, int flashMode) { public void setFlashMode(int cameraType, int flashMode) {
Camera camera = _cameras.get(cameraType); Camera camera = this.acquireCameraInstance(cameraType);
if (null == camera) { if (null == camera) {
return; return;
} }

View File

@ -14,6 +14,7 @@
@interface RCTCameraManager () @interface RCTCameraManager ()
@property (strong, nonatomic) RCTSensorOrientationChecker * sensorOrientationChecker; @property (strong, nonatomic) RCTSensorOrientationChecker * sensorOrientationChecker;
@property (assign, nonatomic) NSInteger* flashMode;
@end @end
@ -212,6 +213,7 @@ RCT_CUSTOM_VIEW_PROPERTY(type, NSInteger, RCTCamera) {
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice];
self.videoCaptureDeviceInput = captureDeviceInput; self.videoCaptureDeviceInput = captureDeviceInput;
[self setFlashMode];
} }
else else
{ {
@ -225,21 +227,25 @@ RCT_CUSTOM_VIEW_PROPERTY(type, NSInteger, RCTCamera) {
} }
RCT_CUSTOM_VIEW_PROPERTY(flashMode, NSInteger, RCTCamera) { RCT_CUSTOM_VIEW_PROPERTY(flashMode, NSInteger, RCTCamera) {
self.flashMode = [RCTConvert NSInteger:json];
[self setFlashMode];
}
- (void)setFlashMode {
AVCaptureDevice *device = [self.videoCaptureDeviceInput device]; AVCaptureDevice *device = [self.videoCaptureDeviceInput device];
NSError *error = nil; NSError *error = nil;
NSInteger *flashMode = [RCTConvert NSInteger:json];
if (![device hasFlash]) return; if (![device hasFlash]) return;
if (![device lockForConfiguration:&error]) { if (![device lockForConfiguration:&error]) {
NSLog(@"%@", error); NSLog(@"%@", error);
return; return;
} }
if (device.hasFlash && [device isFlashModeSupported:flashMode]) if (device.hasFlash && [device isFlashModeSupported:self.flashMode])
{ {
NSError *error = nil; NSError *error = nil;
if ([device lockForConfiguration:&error]) if ([device lockForConfiguration:&error])
{ {
[device setFlashMode:flashMode]; [device setFlashMode:self.flashMode];
[device unlockForConfiguration]; [device unlockForConfiguration];
} }
else else
@ -513,6 +519,7 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej
} }
else if (type == AVMediaTypeVideo) { else if (type == AVMediaTypeVideo) {
self.videoCaptureDeviceInput = captureDeviceInput; self.videoCaptureDeviceInput = captureDeviceInput;
[self setFlashMode];
} }
[self.metadataOutput setMetadataObjectTypes:self.metadataOutput.availableMetadataObjectTypes]; [self.metadataOutput setMetadataObjectTypes:self.metadataOutput.availableMetadataObjectTypes];
} }