Skip to content

Commit 788a6cd

Browse files
committed
Added adjusting exposures message in delegate.
1 parent 829b1ce commit 788a6cd

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

Library/Sources/SCRecorder.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
*/
9393
@property (readonly, nonatomic) BOOL isAdjustingFocus;
9494

95+
/**
96+
Will be true if the camera is adjusting exposure.
97+
This property is KVO observable.
98+
*/
99+
@property (readonly, nonatomic) BOOL isAdjustingExposure;
100+
95101
/**
96102
The session preset used for the AVCaptureSession
97103
*/
@@ -189,6 +195,16 @@
189195
*/
190196
@property (assign, nonatomic) BOOL keepMirroringOnWrite;
191197

198+
/**
199+
Whether adjusting exposure is supported on the current camera device
200+
*/
201+
@property (readonly, nonatomic) BOOL exposureSupported;
202+
203+
/**
204+
The current exposure point of interest
205+
*/
206+
@property (readonly, nonatomic) CGPoint exposurePointOfInterest;
207+
192208
/**
193209
Whether the focus is supported on the current camera device
194210
*/

Library/Sources/SCRecorder.m

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
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"];

Library/Sources/SCRecorderDelegate.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ typedef NS_ENUM(NSInteger, SCFlashMode) {
3838
*/
3939
- (void)recorder:(SCRecorder *__nonnull)recorder didChangeFlashMode:(SCFlashMode)flashMode error:(NSError *__nullable)error;
4040

41+
/**
42+
Called when the recorder has lost the focus. Returning true will make the recorder
43+
automatically refocus at the center.
44+
*/
45+
- (BOOL)recorderShouldAutomaticallyRefocus:(SCRecorder *__nonnull)recorder;
46+
4147
/**
4248
Called before the recorder will start focusing
4349
*/
@@ -53,6 +59,21 @@ typedef NS_ENUM(NSInteger, SCFlashMode) {
5359
*/
5460
- (void)recorderDidEndFocus:(SCRecorder *__nonnull)recorder;
5561

62+
/**
63+
Called before the recorder will start adjusting exposure
64+
*/
65+
- (void)recorderWillStartAdjustingExposure:(SCRecorder *__nonnull)recorder;
66+
67+
/**
68+
Called when the recorder has started adjusting exposure
69+
*/
70+
- (void)recorderDidStartAdjustingExposure:(SCRecorder *__nonnull)recorder;
71+
72+
/**
73+
Called when the recorder has finished adjusting exposure
74+
*/
75+
- (void)recorderDidEndAdjustingExposure:(SCRecorder *__nonnull)recorder;
76+
5677
/**
5778
Called when the recorder has initialized the audio in a session
5879
*/

Library/Sources/SCRecorderToolsView.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ - (void)layoutSubviews {
9696
}
9797

9898
- (void)adjustFocusView {
99-
CGPoint currentFocusPoint = [self.recorder convertPointOfInterestToViewCoordinates:self.recorder.focusPointOfInterest];
99+
CGPoint currentFocusPoint = CGPointMake(0.5, 0.5);
100+
101+
if (self.recorder.focusSupported) {
102+
currentFocusPoint = self.recorder.focusPointOfInterest;
103+
} else if (self.recorder.exposureSupported) {
104+
currentFocusPoint = self.recorder.exposurePointOfInterest;
105+
}
106+
107+
[self.recorder convertPointOfInterestToViewCoordinates:currentFocusPoint];
100108
currentFocusPoint = [self convertPoint:currentFocusPoint fromView:self.recorder.previewView];
101109
self.cameraFocusTargetView.center = currentFocusPoint;
102110
}

0 commit comments

Comments
 (0)