Created
December 14, 2020 23:41
-
-
Save standinga/f8f0beddb19779280dc3b9a793f5c2a9 to your computer and use it in GitHub Desktop.
Updated ViewController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVFoundation | |
import UIKit | |
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { | |
private let avManager = AVManager() | |
private let cameraView = AVSampleBufferView() | |
private let encodedView = AVSampleBufferView() | |
private let decodedView = AVSampleBufferView() | |
private let stackView: UIStackView = { | |
let view = UIStackView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.axis = .vertical | |
view.distribution = .fillEqually | |
return view | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(stackView) | |
[cameraView, encodedView, decodedView].forEach { | |
stackView.addArrangedSubview($0) | |
$0.bufferLayer.videoGravity = .resizeAspect | |
} | |
cameraView.layer.backgroundColor = UIColor.red.cgColor | |
encodedView.layer.backgroundColor = UIColor.green.cgColor | |
decodedView.layer.backgroundColor = UIColor.blue.cgColor | |
NSLayoutConstraint.activate([ | |
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
stackView.topAnchor.constraint(equalTo: view.topAnchor), | |
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor) | |
]) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
avManager.start(delegate: self) | |
} | |
// MARK: - AVCaptureVideoDataOutputSampleBufferDelegate | |
func captureOutput(_ output: AVCaptureOutput, | |
didOutput sampleBuffer: CMSampleBuffer, | |
from connection: AVCaptureConnection) { | |
DispatchQueue.main.async { | |
self.cameraView.bufferLayer.enqueue(sampleBuffer) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment