Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functionalities added #136

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions LLSimpleCamera/LLSimpleCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,53 @@ typedef enum : NSUInteger {
*/
@property (nonatomic) BOOL tapToFocus;

/**
* If user lock focus by calling lockFocus. Disabled by default. Tap to focus will not work if this is YES.
*/
@property (nonatomic, readonly) BOOL isFocusLockedByUser;

/**
* Use this method to lock/unlock auto focus. This will set isFocusLockedByUser to YES/NO.
*/
- (void)lockFocus:(BOOL)shouldLockFocus;

/**
* If user lock exposure by calling lockExposure. Disabled by default.
*/
@property (nonatomic, readonly) BOOL isExpouserLockedByUser;

/**
* Use this method to lock/unlock exposure. This will set isExpouserLockedByUser to YES/NO.
*/
- (void)lockExposure:(BOOL)shouldLockExposure;

/**
* Max frame rate for videoSupportedFrameRateRanges
*/
@property (nonatomic, readonly) float maxFrameRate;

/**
* Min frame rate for videoSupportedFrameRateRanges
*/
@property (nonatomic, readonly) float minFrameRate;

/**
* Use this method to update frame rate within videoSupportedFrameRateRanges
*/
- (void)changeFrameRate:(float)frameRate;

/**
* Use this method to update sound input level if available from device (6S mic don't have permission,
* using headphone will work though
* soundLavel range is 0 to 1. 0 is the minimum, 1 is the maximum
*/
- (void)changeInputSoundLavel:(float)soundLavel;

/**
* Use this method to get current power level within value 0(min) to 1(max).
*/
- (float)getChannelSoundPowerLevel;

/**
* Set YES if you your view controller does not allow autorotation,
* however you want to take the device rotation into account no matter what. Disabled by default.
Expand Down
129 changes: 128 additions & 1 deletion LLSimpleCamera/LLSimpleCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ @interface LLSimpleCamera () <AVCaptureFileOutputRecordingDelegate, UIGestureRec
@property (strong, nonatomic) AVCaptureDevice *audioCaptureDevice;
@property (strong, nonatomic) AVCaptureDeviceInput *videoDeviceInput;
@property (strong, nonatomic) AVCaptureDeviceInput *audioDeviceInput;
@property (strong, nonatomic) AVCaptureAudioDataOutput *captureAudioDataOutput; //For sound input
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
@property (strong, nonatomic) CALayer *focusBoxLayer;
Expand Down Expand Up @@ -264,6 +265,14 @@ - (void)initialize
if([self.session canAddOutput:_movieFileOutput]) {
[self.session addOutput:_movieFileOutput];
}

// Add audio data input for getting sound level
_captureAudioDataOutput = [AVCaptureAudioDataOutput new];
if (_captureAudioDataOutput) {
if ([self.session canAddOutput:_captureAudioDataOutput]) {
[self.session addOutput:_captureAudioDataOutput];
}
}
}

// continiously adjust white balance
Expand Down Expand Up @@ -483,11 +492,17 @@ - (void)setVideoCaptureDevice:(AVCaptureDevice *)videoCaptureDevice

_effectiveScale = 1.0f;

// Getting max and min frame rate for video input
AVFrameRateRange *frameRateRange = [_videoCaptureDevice.activeFormat.videoSupportedFrameRateRanges firstObject];
_maxFrameRate = frameRateRange.maxFrameRate;
_minFrameRate = frameRateRange.minFrameRate;

// trigger block
if(self.onDeviceChange) {
__weak typeof(self) weakSelf = self;
self.onDeviceChange(weakSelf, videoCaptureDevice);
}

}

- (BOOL)isFlashAvailable
Expand Down Expand Up @@ -677,7 +692,7 @@ - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position

- (void)previewTapped:(UIGestureRecognizer *)gestureRecognizer
{
if(!self.tapToFocus) {
if(!self.tapToFocus || _isFocusLockedByUser) {
return;
}

Expand Down Expand Up @@ -749,6 +764,118 @@ - (void)showFocusBox:(CGPoint)point
}
}

- (void)lockFocus:(BOOL)shouldLockFocus {
AVCaptureDevice *device = _videoCaptureDevice;
if(shouldLockFocus) {
if([device isLockingFocusWithCustomLensPositionSupported] && [device isFocusModeSupported:AVCaptureFocusModeLocked]) {
NSError* error = nil;
if ([device lockForConfiguration:&error]) {
device.focusMode = AVCaptureFocusModeLocked;
_isFocusLockedByUser = YES;
[device unlockForConfiguration];
} else {
[self passError:error];
}
}
} else {

if([device isLockingFocusWithCustomLensPositionSupported] && [device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
NSError* error = nil;
if ([device lockForConfiguration:&error]) {
_isFocusLockedByUser = NO;
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
[device unlockForConfiguration];
} else {
[self passError:error];
}
}
}
}

#pragma mark - Exposure

- (void)lockExposure:(BOOL)shouldLockExposure {

AVCaptureDevice *device = _videoCaptureDevice;
if([device isExposureModeSupported:AVCaptureExposureModeLocked]) {
if(shouldLockExposure) {
NSError* error = nil;
if ([device lockForConfiguration:&error]) {
device.exposureMode = AVCaptureExposureModeLocked;
_isExpouserLockedByUser = YES;
[device unlockForConfiguration];
} else {
[self passError:error];
}
} else {
NSError* error = nil;
if ([device lockForConfiguration:&error]) {
_isExpouserLockedByUser = NO;
device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
[device unlockForConfiguration];
} else {
[self passError:error];
}
}
}
}

#pragma mark - Framerate

- (void)changeFrameRate:(float)frameRate {
NSError* error = nil;
AVCaptureDevice *device = _videoCaptureDevice;
AVFrameRateRange *frameRateRange = [device.activeFormat.videoSupportedFrameRateRanges firstObject];
if(frameRate<=frameRateRange.maxFrameRate && frameRate>=frameRateRange.minFrameRate) {
if([device lockForConfiguration:&error]) {
[device setActiveVideoMinFrameDuration:CMTimeMake(1, frameRate)];
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, frameRate)];
[device unlockForConfiguration];
} else {
[self passError:error];
}
}
}

#pragma mark - Audio

- (void)changeInputSoundLavel:(float)soundLavel {
if([[AVAudioSession sharedInstance] isInputGainSettable] && soundLavel>=0.0 && soundLavel<=1.0) {
NSError *error;
NSError *sessionError;
if(![AVAudioSession sharedInstance].category)
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&sessionError];
[[AVAudioSession sharedInstance] setInputGain:soundLavel error:&error];

if(error) {
[self passError:error];
}

if(sessionError) {
[self passError:sessionError];
}
}
}

- (float)getChannelSoundPowerLevel {
if(_captureAudioDataOutput) {
NSArray *connections = _captureAudioDataOutput.connections;
if ([connections count] > 0) {
AVCaptureConnection *connection = [connections objectAtIndex:0];
NSArray *audioChannels = connection.audioChannels;
float soundLevel = 0.0;
for (AVCaptureAudioChannel *channel in audioChannels) {
float avg = channel.averagePowerLevel;// value Range: 0:max -160:min
soundLevel = soundLevel+pow (10, avg / 20);// change to readable value Range: 0:min 1:max
}
return soundLevel/audioChannels.count;
}
}

return 0.0;
}


#pragma mark - UIViewController

- (void)viewWillAppear:(BOOL)animated
Expand Down
Loading