Skip to content

Commit

Permalink
Added adjusting exposures message in delegate.
Browse files Browse the repository at this point in the history
  • Loading branch information
rFlex committed Jul 4, 2015
1 parent 829b1ce commit 79b318c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
10 changes: 10 additions & 0 deletions Library/Sources/SCRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@
*/
@property (assign, nonatomic) BOOL keepMirroringOnWrite;

/**
Whether adjusting exposure is supported on the current camera device
*/
@property (readonly, nonatomic) BOOL exposureSupported;

/**
The current exposure point of interest
*/
@property (readonly, nonatomic) CGPoint exposurePointOfInterest;

/**
Whether the focus is supported on the current camera device
*/
Expand Down
48 changes: 42 additions & 6 deletions Library/Sources/SCRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#import "SCRecorder.h"
#import "SCRecordSession_Internal.h"
#define dispatch_handler(x) if (x != nil) dispatch_async(dispatch_get_main_queue(), x)
#define SCRecorderFocusContext ((void*)0x1)
#define SCRecorderVideoEnabledContext ((void*)0x2)
#define SCRecorderAudioEnabledContext ((void*)0x3)
#define SCRecorderPhotoOptionsContext ((void*)0x3)
#define kSCRecorderRecordSessionQueueKey "SCRecorderRecordSessionQueue"
#define kMinTimeBetweenAppend 0.004

Expand Down Expand Up @@ -49,6 +45,12 @@ @interface SCRecorder() {

@implementation SCRecorder

static char* SCRecorderFocusContext = "FocusContext";
static char* SCRecorderExposureContext = "ExposureContext";
static char* SCRecorderVideoEnabledContext = "VideoEnabledContext";
static char* SCRecorderAudioEnabledContext = "AudioEnabledContext";
static char* SCRecorderPhotoOptionsContext = "PhotoOptionsContext";

- (id)init {
self = [super init];

Expand Down Expand Up @@ -333,7 +335,11 @@ - (void)stopRunning {
}

- (void)_subjectAreaDidChange {
[self focusCenter];
id<SCRecorderDelegate> delegate = self.delegate;

if (![delegate respondsToSelector:@selector(recorderShouldAutomaticallyRefocus:)] || [delegate recorderShouldAutomaticallyRefocus:self]) {
[self focusCenter];
}
}

- (UIImage *)_imageFromSampleBufferHolder:(SCSampleBufferHolder *)sampleBufferHolder {
Expand Down Expand Up @@ -875,6 +881,18 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
} else {
[self _focusDidComplete];
}
} else if (context == SCRecorderExposureContext) {
BOOL isAdjustingExposure = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];

if (isAdjustingExposure) {
if ([delegate respondsToSelector:@selector(recorderDidStartAdjustingExposure:)]) {
[delegate recorderDidStartAdjustingExposure:self];
}
} else {
if ([delegate respondsToSelector:@selector(recorderDidEndAdjustingExposure:)]) {
[delegate recorderDidEndAdjustingExposure:self];
}
}
} else if (context == SCRecorderAudioEnabledContext) {
if ([NSThread isMainThread]) {
[self reconfigureVideoInput:NO audioInput:YES];
Expand All @@ -898,10 +916,12 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)addVideoObservers:(AVCaptureDevice*)videoDevice {
[videoDevice addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:SCRecorderFocusContext];
[videoDevice addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:SCRecorderExposureContext];
}

- (void)removeVideoObservers:(AVCaptureDevice*)videoDevice {
[videoDevice removeObserver:self forKeyPath:@"adjustingFocus"];
[videoDevice removeObserver:self forKeyPath:@"adjustingExposure"];
}

- (void)configureDevice:(AVCaptureDevice*)newDevice mediaType:(NSString*)mediaType error:(NSError**)error {
Expand Down Expand Up @@ -1070,6 +1090,7 @@ - (void)_applyPointOfInterest:(CGPoint)point continuousMode:(BOOL)continuousMode
NSError *error;
if ([device lockForConfiguration:&error]) {
BOOL focusing = NO;
BOOL adjustingExposure = NO;

if (device.isFocusPointOfInterestSupported) {
device.focusPointOfInterest = point;
Expand All @@ -1085,6 +1106,7 @@ - (void)_applyPointOfInterest:(CGPoint)point continuousMode:(BOOL)continuousMode

if ([device isExposureModeSupported:exposureMode]) {
device.exposureMode = exposureMode;
adjustingExposure = YES;
}

if ([device isWhiteBalanceModeSupported:whiteBalanceMode]) {
Expand All @@ -1093,14 +1115,20 @@ - (void)_applyPointOfInterest:(CGPoint)point continuousMode:(BOOL)continuousMode

[device unlockForConfiguration];

id<SCRecorderDelegate> delegate = self.delegate;
if (focusMode != AVCaptureFocusModeContinuousAutoFocus && focusing) {
id<SCRecorderDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(recorderWillStartFocus:)]) {
[delegate recorderWillStartFocus:self];
}

[self setAdjustingFocus:YES];
}

if (exposureMode != AVCaptureExposureModeContinuousAutoExposure && adjustingExposure) {
if ([delegate respondsToSelector:@selector(recorderWillStartAdjustingExposure:)]) {
[delegate recorderWillStartAdjustingExposure:self];
}
}
}
}

Expand All @@ -1124,6 +1152,14 @@ - (void)refocus {
[self autoFocusAtPoint:self.focusPointOfInterest];
}

- (CGPoint)exposurePointOfInterest {
return [self.currentVideoDeviceInput device].exposurePointOfInterest;
}

- (BOOL)exposureSupported {
return [self.currentVideoDeviceInput device].isExposurePointOfInterestSupported;
}

- (CGPoint)focusPointOfInterest {
return [self.currentVideoDeviceInput device].focusPointOfInterest;
}
Expand Down
21 changes: 21 additions & 0 deletions Library/Sources/SCRecorderDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ typedef NS_ENUM(NSInteger, SCFlashMode) {
*/
- (void)recorder:(SCRecorder *__nonnull)recorder didChangeFlashMode:(SCFlashMode)flashMode error:(NSError *__nullable)error;

/**
Called when the recorder has lost the focus. Returning true will make the recorder
automatically refocus at the center.
*/
- (BOOL)recorderShouldAutomaticallyRefocus:(SCRecorder *__nonnull)recorder;

/**
Called before the recorder will start focusing
*/
Expand All @@ -53,6 +59,21 @@ typedef NS_ENUM(NSInteger, SCFlashMode) {
*/
- (void)recorderDidEndFocus:(SCRecorder *__nonnull)recorder;

/**
Called before the recorder will start adjusting exposure
*/
- (void)recorderWillStartAdjustingExposure:(SCRecorder *__nonnull)recorder;

/**
Called when the recorder has started adjusting exposure
*/
- (void)recorderDidStartAdjustingExposure:(SCRecorder *__nonnull)recorder;

/**
Called when the recorder has finished adjusting exposure
*/
- (void)recorderDidEndAdjustingExposure:(SCRecorder *__nonnull)recorder;

/**
Called when the recorder has initialized the audio in a session
*/
Expand Down
10 changes: 9 additions & 1 deletion Library/Sources/SCRecorderToolsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ - (void)layoutSubviews {
}

- (void)adjustFocusView {
CGPoint currentFocusPoint = [self.recorder convertPointOfInterestToViewCoordinates:self.recorder.focusPointOfInterest];
CGPoint currentFocusPoint = CGPointMake(0.5, 0.5);

if (self.recorder.focusSupported) {
currentFocusPoint = self.recorder.focusPointOfInterest;
} else if (self.recorder.exposureSupported) {
currentFocusPoint = self.recorder.exposurePointOfInterest;
}

[self.recorder convertPointOfInterestToViewCoordinates:currentFocusPoint];
currentFocusPoint = [self convertPoint:currentFocusPoint fromView:self.recorder.previewView];
self.cameraFocusTargetView.center = currentFocusPoint;
}
Expand Down

0 comments on commit 79b318c

Please sign in to comment.