99#import " SCRecorder.h"
1010#import " SCRecordSession_Internal.h"
1111#define dispatch_handler (x ) if (x != nil ) dispatch_async(dispatch_get_main_queue(), x)
12- #define SCRecorderFocusContext ((void *)0x1 )
13- #define SCRecorderVideoEnabledContext ((void *)0x2 )
14- #define SCRecorderAudioEnabledContext ((void *)0x3 )
15- #define SCRecorderPhotoOptionsContext ((void *)0x3 )
1612#define kSCRecorderRecordSessionQueueKey " SCRecorderRecordSessionQueue"
1713#define kMinTimeBetweenAppend 0.004
1814
@@ -49,6 +45,12 @@ @interface SCRecorder() {
4945
5046@implementation SCRecorder
5147
48+ static char * SCRecorderFocusContext = " FocusContext" ;
49+ static char * SCRecorderExposureContext = " ExposureContext" ;
50+ static char * SCRecorderVideoEnabledContext = " VideoEnabledContext" ;
51+ static char * SCRecorderAudioEnabledContext = " AudioEnabledContext" ;
52+ static char * SCRecorderPhotoOptionsContext = " PhotoOptionsContext" ;
53+
5254- (id )init {
5355 self = [super init ];
5456
@@ -333,7 +335,11 @@ - (void)stopRunning {
333335}
334336
335337- (void )_subjectAreaDidChange {
336- [self focusCenter ];
338+ id <SCRecorderDelegate> delegate = self.delegate ;
339+
340+ if (![delegate respondsToSelector: @selector (recorderShouldAutomaticallyRefocus: )] || [delegate recorderShouldAutomaticallyRefocus: self ]) {
341+ [self focusCenter ];
342+ }
337343}
338344
339345- (UIImage *)_imageFromSampleBufferHolder : (SCSampleBufferHolder *)sampleBufferHolder {
@@ -875,6 +881,20 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
875881 } else {
876882 [self _focusDidComplete ];
877883 }
884+ } else if (context == SCRecorderExposureContext) {
885+ BOOL isAdjustingExposure = [[change objectForKey: NSKeyValueChangeNewKey ] boolValue ];
886+
887+ [self setAdjustingExposure: isAdjustingExposure];
888+
889+ if (isAdjustingExposure) {
890+ if ([delegate respondsToSelector: @selector (recorderDidStartAdjustingExposure: )]) {
891+ [delegate recorderDidStartAdjustingExposure: self ];
892+ }
893+ } else {
894+ if ([delegate respondsToSelector: @selector (recorderDidEndAdjustingExposure: )]) {
895+ [delegate recorderDidEndAdjustingExposure: self ];
896+ }
897+ }
878898 } else if (context == SCRecorderAudioEnabledContext) {
879899 if ([NSThread isMainThread ]) {
880900 [self reconfigureVideoInput: NO audioInput: YES ];
@@ -898,10 +918,12 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
898918
899919- (void )addVideoObservers : (AVCaptureDevice*)videoDevice {
900920 [videoDevice addObserver: self forKeyPath: @" adjustingFocus" options: NSKeyValueObservingOptionNew context: SCRecorderFocusContext];
921+ [videoDevice addObserver: self forKeyPath: @" adjustingExposure" options: NSKeyValueObservingOptionNew context: SCRecorderExposureContext];
901922}
902923
903924- (void )removeVideoObservers : (AVCaptureDevice*)videoDevice {
904925 [videoDevice removeObserver: self forKeyPath: @" adjustingFocus" ];
926+ [videoDevice removeObserver: self forKeyPath: @" adjustingExposure" ];
905927}
906928
907929- (void )configureDevice : (AVCaptureDevice*)newDevice mediaType : (NSString *)mediaType error : (NSError **)error {
@@ -1070,6 +1092,7 @@ - (void)_applyPointOfInterest:(CGPoint)point continuousMode:(BOOL)continuousMode
10701092 NSError *error;
10711093 if ([device lockForConfiguration: &error]) {
10721094 BOOL focusing = NO ;
1095+ BOOL adjustingExposure = NO ;
10731096
10741097 if (device.isFocusPointOfInterestSupported ) {
10751098 device.focusPointOfInterest = point;
@@ -1085,6 +1108,7 @@ - (void)_applyPointOfInterest:(CGPoint)point continuousMode:(BOOL)continuousMode
10851108
10861109 if ([device isExposureModeSupported: exposureMode]) {
10871110 device.exposureMode = exposureMode;
1111+ adjustingExposure = YES ;
10881112 }
10891113
10901114 if ([device isWhiteBalanceModeSupported: whiteBalanceMode]) {
@@ -1093,14 +1117,22 @@ - (void)_applyPointOfInterest:(CGPoint)point continuousMode:(BOOL)continuousMode
10931117
10941118 [device unlockForConfiguration ];
10951119
1120+ id <SCRecorderDelegate> delegate = self.delegate ;
10961121 if (focusMode != AVCaptureFocusModeContinuousAutoFocus && focusing) {
1097- id <SCRecorderDelegate> delegate = self.delegate ;
10981122 if ([delegate respondsToSelector: @selector (recorderWillStartFocus: )]) {
10991123 [delegate recorderWillStartFocus: self ];
11001124 }
11011125
11021126 [self setAdjustingFocus: YES ];
11031127 }
1128+
1129+ if (exposureMode != AVCaptureExposureModeContinuousAutoExposure && adjustingExposure) {
1130+ [self setAdjustingExposure: YES ];
1131+
1132+ if ([delegate respondsToSelector: @selector (recorderWillStartAdjustingExposure: )]) {
1133+ [delegate recorderWillStartAdjustingExposure: self ];
1134+ }
1135+ }
11041136 }
11051137}
11061138
@@ -1124,6 +1156,14 @@ - (void)refocus {
11241156 [self autoFocusAtPoint: self .focusPointOfInterest];
11251157}
11261158
1159+ - (CGPoint)exposurePointOfInterest {
1160+ return [self .currentVideoDeviceInput device ].exposurePointOfInterest ;
1161+ }
1162+
1163+ - (BOOL )exposureSupported {
1164+ return [self .currentVideoDeviceInput device ].isExposurePointOfInterestSupported ;
1165+ }
1166+
11271167- (CGPoint)focusPointOfInterest {
11281168 return [self .currentVideoDeviceInput device ].focusPointOfInterest ;
11291169}
@@ -1320,6 +1360,16 @@ - (BOOL)isAdjustingFocus {
13201360 return _adjustingFocus;
13211361}
13221362
1363+ - (void )setAdjustingExposure : (BOOL )adjustingExposure {
1364+ if (_isAdjustingExposure != adjustingExposure) {
1365+ [self willChangeValueForKey: @" isAdjustingExposure" ];
1366+
1367+ _isAdjustingExposure = adjustingExposure;
1368+
1369+ [self didChangeValueForKey: @" isAdjustingExposure" ];
1370+ }
1371+ }
1372+
13231373- (void )setAdjustingFocus : (BOOL )adjustingFocus {
13241374 if (_adjustingFocus != adjustingFocus) {
13251375 [self willChangeValueForKey: @" isAdjustingFocus" ];
0 commit comments