Skip to content

Commit

Permalink
BBMetalCamera adds setFrameRate(_:) method
Browse files Browse the repository at this point in the history
  • Loading branch information
Silence-GitHub committed May 21, 2019
1 parent 0184791 commit a9b7a40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
41 changes: 41 additions & 0 deletions BBMetalImage/BBMetalImage/BBMetalCamera.swift
Expand Up @@ -345,6 +345,47 @@ public class BBMetalCamera: NSObject {
return true
}

/// Sets camera frame rate
///
/// - Parameter frameRate: camera frame rate
@discardableResult
public func setFrameRate(_ frameRate: Float64) -> Bool {
var success = false
lock.wait()
do {
try camera.lockForConfiguration()
var targetFormat: AVCaptureDevice.Format?
let dimensions = CMVideoFormatDescriptionGetDimensions(camera.activeFormat.formatDescription)
for format in camera.formats {
let newDimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription)
if dimensions.width == newDimensions.width,
dimensions.height == newDimensions.height {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate >= frameRate,
range.minFrameRate <= frameRate {
targetFormat = format
break
}
}
if targetFormat != nil { break }
}
}
if let format = targetFormat {
camera.activeFormat = format
camera.activeVideoMaxFrameDuration = CMTime(value: 1, timescale: CMTimeScale(frameRate))
camera.activeVideoMinFrameDuration = CMTime(value: 1, timescale: CMTimeScale(frameRate))
success = true
} else {
print("Can not find valid format for camera frame rate \(frameRate)")
}
camera.unlockForConfiguration()
} catch {
print("Error for camera lockForConfiguration: \(error as NSError)")
}
lock.signal()
return success
}

/// Starts capturing
public func start() { session.startRunning() }

Expand Down
1 change: 0 additions & 1 deletion BBMetalImageDemo/BBMetalImageDemo/CameraFilterVC.swift
Expand Up @@ -68,7 +68,6 @@ class CameraFilterVC: UIViewController {

filePath = NSTemporaryDirectory() + "test.mp4"
let url = URL(fileURLWithPath: filePath)
try? FileManager.default.removeItem(at: url)
videoWriter = BBMetalVideoWriter(url: url, frameSize: BBMetalIntSize(width: 1080, height: 1920))

camera = BBMetalCamera(sessionPreset: .hd1920x1080)
Expand Down

0 comments on commit a9b7a40

Please sign in to comment.