Skip to content

Commit

Permalink
Enabled flash during video capture.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreilob committed Jun 8, 2023
1 parent c96fcef commit 8777f54
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 15 deletions.
14 changes: 14 additions & 0 deletions Sources/CameraKage/AVFoundationsInternals/VideoCaptureDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class VideoCaptureDevice: NSObject {
return min(min(maxFactor, options.maximumZoomScale), videoDevice.activeFormat.videoMaxZoomFactor)
}

func configureFlash(_ flashMode: FlashMode) throws {
guard videoDevice.isTorchModeSupported(flashMode.avTorchModeOption),
videoDevice.torchMode != flashMode.avTorchModeOption else {
throw CameraError.cameraComponentError(reason: .torchModeNotSupported)
}
do {
try videoDevice.lockForConfiguration()
videoDevice.torchMode = flashMode.avTorchModeOption
videoDevice.unlockForConfiguration()
} catch {
throw CameraError.cameraComponentError(reason: .failedToLockDevice)
}
}

func configureVideoDevice(forSession session: CaptureSession,
andOptions options: CameraComponentParsedOptions,
isFlipped: Bool) -> Bool {
Expand Down
19 changes: 10 additions & 9 deletions Sources/CameraKage/CameraKage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ public class CameraKage: UIView {

/**
Starts a video recording for the camera. `CameraKageDelegate` sends a notification when the recording has started.

- parameter flashOption: Indicates what flash option should be used for the video recording. Default is `.off`.

- important: Front camera dosen't support video recordings with flash mode `.on`.
*/
public func startVideoRecording() {
cameraComposer.startVideoRecording()
public func startVideoRecording(_ flashOption: FlashMode) {
cameraComposer.startVideoRecording(flashOption)
}

/**
Expand Down Expand Up @@ -180,13 +184,10 @@ public class CameraKage: UIView {
}

private func setupComposer() {
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.cameraComposer = CameraComposer()
self.cameraComposer.delegate = self
self.addSubview(self.cameraComposer)
self.cameraComposer.layoutToFill(inView: self)
}
cameraComposer = CameraComposer()
cameraComposer.delegate = self
addSubview(cameraComposer)
cameraComposer.layoutToFill(inView: self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ extension FlashMode {
case .auto: return .auto
}
}

var avTorchModeOption: AVCaptureDevice.TorchMode {
switch self {
case .on: return .on
case .off: return .off
case .auto: return .auto
}
}
}
16 changes: 15 additions & 1 deletion Sources/CameraKage/General/Camera/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,26 @@ class Camera {
photoOutput.capturePhoto(flashMode, redEyeCorrection: redEyeCorrection)
}

func startMovieRecording() {
func startMovieRecording(_ flashMode: FlashMode) {
do {
try videoDevice.configureFlash(flashMode)
} catch let error as CameraError {
delegate?.camera(self, didFail: error)
} catch {
delegate?.camera(self, didFail: .cameraComponentError(reason: .torchModeNotSupported))
}
movieOutput.startMovieRecording()
}

func stopMovieRecording() {
movieOutput.stopMovieRecording()
do {
try videoDevice.configureFlash(.off)
} catch let error as CameraError {
delegate?.camera(self, didFail: error)
} catch {
delegate?.camera(self, didFail: .cameraComponentError(reason: .torchModeNotSupported))
}
}

func focus(with focusMode: FocusMode,
Expand Down
4 changes: 2 additions & 2 deletions Sources/CameraKage/General/Camera/CameraComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class CameraComponent: UIView {
camera.capturePhoto(flashMode, redEyeCorrection: redEyeCorrection)
}

func startMovieRecording() {
camera.startMovieRecording()
func startMovieRecording(_ flashMode: FlashMode) {
camera.startMovieRecording(flashMode)
}

func stopMovieRecording() {
Expand Down
4 changes: 2 additions & 2 deletions Sources/CameraKage/General/Camera/CameraComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ final class CameraComposer: UIView, CameraComposerProtocol {
}
}

func startVideoRecording() {
func startVideoRecording(_ flashOption: FlashMode) {
sessionQueue.async { [weak self] in
guard let self, !self.isRecording else { return }
self.cameraComponent.startMovieRecording()
self.cameraComponent.startMovieRecording(flashOption)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protocol CameraComposerProtocol: UIView {
func startCameraSession(with options: CameraComponentParsedOptions)
func stopCameraSession()
func capturePhoto(_ flashOption: FlashMode, redEyeCorrection: Bool)
func startVideoRecording()
func startVideoRecording(_ flashOption: FlashMode)
func stopVideoRecording()
func flipCamera()
func adjustFocusAndExposure(with focusMode: FocusMode,
Expand Down
3 changes: 3 additions & 0 deletions Sources/CameraKage/General/CameraError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public enum CameraError: Error {

/// Occurs when a camera initialization fails.
case failedToComposeCamera

/// Error thrown when a specified torch mode is not supported by the device.
case torchModeNotSupported
}

public enum CameraSessionErrorReason {
Expand Down

0 comments on commit 8777f54

Please sign in to comment.