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

View File

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