custom compositor
import AVFoundation
final class CustomVideoCompositor: NSObject, AVVideoCompositing {
enum CustomVideoCompositorError: Int, Error, LocalizedError { case sourcePixelBuffer }
// HDR ?
var sourcePixelBufferAttributes: [String: Any]? = [String(kCVPixelBufferPixelFormatTypeKey): [kCVPixelFormatType_32BGRA]]
// HDR ?
var requiredPixelBufferAttributesForRenderContext: [String: Any] = [String(kCVPixelBufferPixelFormatTypeKey): [kCVPixelFormatType_32BGRA]]
// var supportsHDRSourceFrames: Bool = true
func renderContextChanged(_ newRenderContext: AVVideoCompositionRenderContext) {
print(#function)
}
func startRequest(_ request: AVAsynchronousVideoCompositionRequest) {
guard let sourcePixelBuffer = request.sourcePixelBuffer
else {
print("request.sourcePixelBuffer is nil")
request.finish(with: CustomVideoCompositorError.sourcePixelBuffer)
return
}
guard let instruction = (request.videoCompositionInstruction as? CustomVideoCompositionInstruction)
else {
print("CustomVideoCompositionInstruction is nil")
request.finish(withComposedVideoFrame: sourcePixelBuffer)
return
}
guard let sourceTexture = sourcePixelBuffer.createTexture2(dependency: instruction.dependency)
else {
print("sourceTexture is nil")
request.finish(withComposedVideoFrame: sourcePixelBuffer)
return
}
guard let outputPixelBuffer = request.renderContext.newPixelBuffer()
else {
print("request.renderContext.newPixelBuffer()")
request.finish(withComposedVideoFrame: sourcePixelBuffer)
return
}
guard let outputTexture = outputPixelBuffer.createTexture2(dependency: instruction.dependency)
else {
print("request.renderContext.newPixelBuffer()")
request.finish(withComposedVideoFrame: sourcePixelBuffer)
return
}
instruction.offscreenRenderer.render(
background: OffscreenRenderer.Background(
model: instruction.textureModel,
texture: sourceTexture,
colorStrength: instruction.videoColorStrength
),
outputTexture: outputTexture
)
request.finish(withComposedVideoFrame: outputPixelBuffer)
instruction.dependency.clearCache() // CVMetalTextureCacheFlush
}
}
Topic:
Graphics & Games
SubTopic:
General
Tags: