From 8148ad6700cc035e7adefbfd3f166faebad24907 Mon Sep 17 00:00:00 2001 From: Haik Aslanyan Date: Sat, 7 Mar 2020 15:17:20 +0400 Subject: [PATCH] added fps label on RCCameraViewController --- Sources/Public/RCCameraViewController.swift | 21 ++++++++++++++------- Sources/Public/RCCoder.swift | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Sources/Public/RCCameraViewController.swift b/Sources/Public/RCCameraViewController.swift index 170cf3e..e9b65f7 100644 --- a/Sources/Public/RCCameraViewController.swift +++ b/Sources/Public/RCCameraViewController.swift @@ -52,22 +52,31 @@ public final class RCCameraViewController: UIViewController, UIImagePickerContro cameraView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true return cameraView }() + lazy var timeLabel: UILabel = { + let label = UILabel.init(frame: CGRect.init(x: 20, y: 20, width: 100, height: 30)) + label.textColor = .white + self.view.addSubview(label) + return label + }() //MARK: Inits public required init?(coder: NSCoder) { super.init(coder: coder) modalPresentationStyle = .fullScreen + view.backgroundColor = .black } public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) modalPresentationStyle = .fullScreen + view.backgroundColor = .black } public convenience init() { self.init(nibName: nil, bundle: nil) modalPresentationStyle = .fullScreen + view.backgroundColor = .black } deinit { @@ -145,12 +154,9 @@ extension RCCameraViewController { do { captureSession.sessionPreset = .hd1280x720 let input = try AVCaptureDeviceInput(device: captureDevice) + input.device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 30) captureSession.addInput(input) let videoOutput = AVCaptureVideoDataOutput() - if let con = videoOutput.connection(with: .video) { - con.videoOrientation = .portrait - print(123) - } videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: .background)) videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)] captureSession.addOutput(videoOutput) @@ -185,8 +191,7 @@ extension RCCameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate { let lumaBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)?.advanced(by: bytesPerRow * origin) let lumaCopy = UnsafeMutableRawPointer.allocate(byteCount: bytesPerRow * size, alignment: MemoryLayout.alignment) lumaCopy.copyMemory(from: lumaBaseAddress!, byteCount: bytesPerRow * size) - let bufferData = UnsafeMutableBufferPointer(start: lumaCopy.bindMemory(to: UInt8.self, capacity: size * bytesPerRow), count: size * bytesPerRow) - if let message = try? coder.decode(buffer: bufferData, size: size) { + if let message = try? coder.decode(buffer: lumaCopy.assumingMemoryBound(to: UInt8.self), size: size) { DispatchQueue.main.async {[weak self] in guard let weakSelf = self else { return } weakSelf.dismiss(animated: true) { @@ -194,7 +199,9 @@ extension RCCameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate { } } } - print(Date().timeIntervalSince(date)) + DispatchQueue.main.async { + self.timeLabel.text = "\((1 / Date().timeIntervalSince(date)).rounded())" + } lumaCopy.deallocate() CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly) } diff --git a/Sources/Public/RCCoder.swift b/Sources/Public/RCCoder.swift index 0022fb2..9271679 100644 --- a/Sources/Public/RCCoder.swift +++ b/Sources/Public/RCCoder.swift @@ -55,8 +55,8 @@ public extension RCCoder { extension RCCoder { - func decode(buffer: UnsafeMutableBufferPointer, size: Int) throws -> String { - let bits = try imageDecoder.process(buffer: buffer, size: size) + func decode(buffer: UnsafeMutablePointer, size: Int) throws -> String { + let bits = try imageDecoder.process(pointer: buffer, size: size) let message = try bitCoder.decode(bits) return message }