Skip to content

Instantly share code, notes, and snippets.

@standinga
Created December 14, 2020 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save standinga/f8f0beddb19779280dc3b9a793f5c2a9 to your computer and use it in GitHub Desktop.
Save standinga/f8f0beddb19779280dc3b9a793f5c2a9 to your computer and use it in GitHub Desktop.
Updated ViewController
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