Skip to content

Commit

Permalink
Add an option to show video output on a drawable without video effects
Browse files Browse the repository at this point in the history
  • Loading branch information
levs42 committed Feb 13, 2024
1 parent 31cd4f8 commit eff0f29
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Sources/IO/IOStreamDrawable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public protocol IOStreamDrawable: AnyObject {
@available(tvOS 17.0, *)
var isCaptureVideoPreviewEnabled: Bool { get set }
#endif

/// Specifies if the video effects should be rendered or not.
var drawWithEffects: Bool { get set }

/// Attaches a drawable to a new NetStream object.
func attachStream(_ stream: IOStream?)
Expand Down
4 changes: 3 additions & 1 deletion Sources/IO/IOVideoMixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CoreMedia
import Foundation

protocol IOVideoMixerDelegate: AnyObject {
func videoMixer(_ videoMixer: IOVideoMixer<Self>, willProcess sampleBuffer: CMSampleBuffer)
func videoMixer(_ videoMixer: IOVideoMixer<Self>, didOutput imageBuffer: CVImageBuffer, presentationTimeStamp: CMTime)
func videoMixer(_ videoMixer: IOVideoMixer<Self>, didOutput sampleBUffer: CMSampleBuffer)
}
Expand Down Expand Up @@ -73,6 +74,7 @@ final class IOVideoMixer<T: IOVideoMixerDelegate> {

func append(_ sampleBuffer: CMSampleBuffer, channel: UInt8, isVideoMirrored: Bool) {
if channel == settings.channel {
delegate?.videoMixer(self, willProcess: sampleBuffer)
var imageBuffer: CVImageBuffer?
guard let buffer = sampleBuffer.imageBuffer else {
return
Expand Down Expand Up @@ -107,7 +109,7 @@ final class IOVideoMixer<T: IOVideoMixerDelegate> {
#if os(macOS)
pixelBufferPool?.createPixelBuffer(&imageBuffer)
#else
if buffer.width != Int(extent.width) || buffer.height != Int(extent.height) {
if settings.effectsBuffer || buffer.width != Int(extent.width) || buffer.height != Int(extent.height) {
pixelBufferPool?.createPixelBuffer(&imageBuffer)
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/IO/IOVideoMixerSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public struct IOVideoMixerSettings: Codable {
public let direction: ImageTransform
/// Specifies the main channel number.
public var channel: UInt8 = 0
/// Specifies if effects are always rendered to a new buffer.
public var effectsBuffer: Bool = false

/// Create a new IOVideoMixerSettings.
public init(mode: Mode, cornerRadius: CGFloat, regionOfInterest: CGRect, direction: ImageTransform) {
Expand Down
10 changes: 9 additions & 1 deletion Sources/IO/IOVideoUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,16 @@ extension IOVideoUnit {

extension IOVideoUnit: IOVideoMixerDelegate {
// MARK: IOVideoMixerDelegate
func videoMixer(_ videoMixer: IOVideoMixer<IOVideoUnit>, willProcess sampleBuffer: CMSampleBuffer) {
if let drawable, drawable.drawWithEffects == false {
drawable.enqueue(sampleBuffer)
}
}

func videoMixer(_ videoMixer: IOVideoMixer<IOVideoUnit>, didOutput sampleBuffer: CMSampleBuffer) {
drawable?.enqueue(sampleBuffer)
if let drawable, drawable.drawWithEffects {
drawable.enqueue(sampleBuffer)
}
mixer?.videoUnit(self, didOutput: sampleBuffer)
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/IO/MTHKView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class MTHKView: MTKView {
}
}

/// Specifies if the video effects should be rendered or not.
public var drawWithEffects: Bool = true

private var currentSampleBuffer: CMSampleBuffer?

private let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
Expand Down
3 changes: 3 additions & 0 deletions Sources/IO/PiPHKView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public class PiPHKView: UIView {
}
#endif

/// Specifies if the video effects should be rendered or not.
public var drawWithEffects: Bool = true

private weak var currentStream: IOStream? {
didSet {
currentStream?.drawable = self
Expand Down

0 comments on commit eff0f29

Please sign in to comment.