@@ -24,16 +24,29 @@ public final class UpscalingCompositor: NSObject, AVVideoCompositing {
2424 public func renderContextChanged( _: AVVideoCompositionRenderContext ) { }
2525
2626 public func startRequest( _ asyncVideoCompositionRequest: AVAsynchronousVideoCompositionRequest ) {
27- let sourceFrame = asyncVideoCompositionRequest. sourceFrame (
28- byTrackID: CMPersistentTrackID ( truncating: asyncVideoCompositionRequest. sourceTrackIDs [ 0 ] )
29- ) !
27+ guard let trackID = asyncVideoCompositionRequest. sourceTrackIDs. first else {
28+ asyncVideoCompositionRequest. finish ( with: Error . couldNotGetSourceTrackID)
29+ return
30+ }
31+ guard let sourceFrame = asyncVideoCompositionRequest. sourceFrame (
32+ byTrackID: CMPersistentTrackID ( truncating: trackID)
33+ ) else {
34+ asyncVideoCompositionRequest. finish ( with: Error . couldNotGetSourceFrame)
35+ return
36+ }
3037 #if canImport(MetalFX)
31- let destinationFrame = asyncVideoCompositionRequest. renderContext. newPixelBuffer ( ) !
32-
33- let commandBuffer = commandQueue. makeCommandBuffer ( ) !
34-
35- var colorTexture : CVMetalTexture ?
36- CVMetalTextureCacheCreateTextureFromImage (
38+ guard let destinationFrame = asyncVideoCompositionRequest. renderContext. newPixelBuffer ( ) else {
39+ asyncVideoCompositionRequest. finish ( with: Error . couldNotCreateDestinationPixelBuffer)
40+ return
41+ }
42+
43+ guard let commandBuffer = commandQueue. makeCommandBuffer ( ) else {
44+ asyncVideoCompositionRequest. finish ( with: Error . couldNotMakeCommandBuffer)
45+ return
46+ }
47+
48+ var colorTexture : CVMetalTexture !
49+ var status = CVMetalTextureCacheCreateTextureFromImage (
3750 nil ,
3851 cvTextureCache,
3952 sourceFrame,
@@ -44,9 +57,13 @@ public final class UpscalingCompositor: NSObject, AVVideoCompositing {
4457 0 ,
4558 & colorTexture
4659 )
60+ guard status == kCVReturnSuccess else {
61+ asyncVideoCompositionRequest. finish ( with: Error . couldNotCreateMetalTextureFromSourceFrame ( status) )
62+ return
63+ }
4764
48- var outputTexture : CVMetalTexture ?
49- CVMetalTextureCacheCreateTextureFromImage (
65+ var outputTexture : CVMetalTexture !
66+ status = CVMetalTextureCacheCreateTextureFromImage (
5067 nil ,
5168 cvTextureCache,
5269 destinationFrame,
@@ -57,18 +74,32 @@ public final class UpscalingCompositor: NSObject, AVVideoCompositing {
5774 0 ,
5875 & outputTexture
5976 )
60-
61- let intermediateOutputTexture = device. makeTexture ( descriptor: intermediateOutputTextureDescriptor) !
62- spatialScaler. colorTexture = CVMetalTextureGetTexture ( colorTexture!)
77+ guard status == kCVReturnSuccess else {
78+ asyncVideoCompositionRequest. finish ( with: Error . couldNotCreateMetalTextureFromDestinationFrame ( status) )
79+ return
80+ }
81+
82+ guard let intermediateOutputTexture = device. makeTexture ( descriptor: intermediateOutputTextureDescriptor) else {
83+ asyncVideoCompositionRequest. finish ( with: Error . couldNotMakeIntermediateOutputTexture)
84+ return
85+ }
86+ spatialScaler. colorTexture = CVMetalTextureGetTexture ( colorTexture)
6387 spatialScaler. outputTexture = intermediateOutputTexture
6488
6589 spatialScaler. encode ( commandBuffer: commandBuffer)
66- let blitCommandEncoder = commandBuffer. makeBlitCommandEncoder ( ) !
67- blitCommandEncoder. copy ( from: intermediateOutputTexture, to: CVMetalTextureGetTexture ( outputTexture!) !)
90+ guard let blitCommandEncoder = commandBuffer. makeBlitCommandEncoder ( ) else {
91+ asyncVideoCompositionRequest. finish ( with: Error . couldNotMakeBlitCommandEncoder)
92+ return
93+ }
94+ blitCommandEncoder. copy ( from: intermediateOutputTexture, to: CVMetalTextureGetTexture ( outputTexture) !)
6895 blitCommandEncoder. endEncoding ( )
6996
7097 commandBuffer. commit ( )
7198 commandBuffer. waitUntilCompleted ( )
99+ if let error = commandBuffer. error {
100+ asyncVideoCompositionRequest. finish ( with: error)
101+ return
102+ }
72103
73104 asyncVideoCompositionRequest. finish ( withComposedVideoFrame: destinationFrame)
74105 #else
@@ -110,3 +141,18 @@ public final class UpscalingCompositor: NSObject, AVVideoCompositing {
110141 return textureDescriptor
111142 } ( )
112143}
144+
145+ // MARK: UpscalingCompositor.Error
146+
147+ extension UpscalingCompositor {
148+ enum Error : Swift . Error {
149+ case couldNotGetSourceTrackID
150+ case couldNotGetSourceFrame
151+ case couldNotCreateDestinationPixelBuffer
152+ case couldNotMakeCommandBuffer
153+ case couldNotCreateMetalTextureFromSourceFrame( CVReturn )
154+ case couldNotCreateMetalTextureFromDestinationFrame( CVReturn )
155+ case couldNotMakeIntermediateOutputTexture
156+ case couldNotMakeBlitCommandEncoder
157+ }
158+ }
0 commit comments