99import CoreMedia
1010import Metal
1111
12- //TODO
13- #if false
14-
1512
1613#if os(iOS) && (!arch(i386) && !arch(x86_64))
1714 import MetalKit
@@ -22,6 +19,17 @@ protocol AgoraMetalRenderMirrorDataSource: NSObjectProtocol {
2219 func renderViewShouldMirror( renderView: AgoraMetalRender ) -> Bool
2320}
2421
22+ enum AgoraVideoRotation : Int {
23+ /** 0: No rotation */
24+ case rotationNone = 0
25+ /** 1: 90 degrees */
26+ case rotation90 = 1
27+ /** 2: 180 degrees */
28+ case rotation180 = 2
29+ /** 3: 270 degrees */
30+ case rotation270 = 3
31+ }
32+
2533class AgoraMetalRender : UIView {
2634 weak var mirrorDataSource : AgoraMetalRenderMirrorDataSource ?
2735
@@ -64,48 +72,39 @@ class AgoraMetalRender: UIView {
6472 }
6573}
6674
67- extension AgoraMetalRender : AgoraVideoSinkProtocol {
68- func shouldInitialize( ) -> Bool {
69- initializeRenderPipelineState ( )
70- return true
71- }
72-
73- func shouldStart( ) {
74- #if os(iOS) && (!arch(i386) && !arch(x86_64))
75- metalView. delegate = self
76- #endif
77- }
78-
79- func shouldStop( ) {
80- #if os(iOS) && (!arch(i386) && !arch(x86_64))
81- metalView. delegate = nil
82- #endif
83- }
84-
85- func shouldDispose( ) {
86- _ = semaphore. wait ( timeout: . distantFuture)
87- textures = nil
88- vertexBuffer = nil
89- #if os(macOS) || (os(iOS) && (!arch(i386) && !arch(x86_64)))
90- metalView. delegate = nil
91- textureCache = nil
92- #endif
93- commandQueue = nil
94- semaphore. signal ( )
95- }
96-
97- func bufferType( ) -> AgoraVideoBufferType {
98- return . pixelBuffer
75+ func getAgoraRotation( rotation: Int32 ) -> AgoraVideoRotation ? {
76+ switch rotation {
77+ case 0 :
78+ return . rotationNone
79+ case 90 :
80+ return . rotation90
81+ case 180 :
82+ return . rotation180
83+ case 270 :
84+ return . rotation270
85+ default :
86+ return nil
9987 }
100-
101- func pixelFormat( ) -> AgoraVideoPixelFormat {
102- return . NV12
88+ }
89+
90+ extension AgoraMetalRender : AgoraVideoFrameDelegate {
91+ func onCapture( _ videoFrame: AgoraOutputVideoFrame ) -> Bool {
92+
93+
94+ return true
10395 }
10496
105- func renderPixelBuffer( _ pixelBuffer: CVPixelBuffer , rotation: AgoraVideoRotation ) {
106- #if os(iOS) && (!arch(i386) && !arch(x86_64))
97+ func onRenderVideoFrame( _ videoFrame: AgoraOutputVideoFrame , uid: UInt , connectionId: UInt ) -> Bool {
98+
99+ guard let rotation = getAgoraRotation ( rotation: videoFrame. rotation) else {
100+ return false
101+ }
102+
103+ guard let pixelBuffer = videoFrame. pixelBuffer else {
104+ return false
105+ }
107106 guard CVPixelBufferLockBaseAddress ( pixelBuffer, . readOnly) == kCVReturnSuccess else {
108- return
107+ return false
109108 }
110109 defer {
111110 CVPixelBufferUnlockBaseAddress ( pixelBuffer, . readOnly)
@@ -128,7 +127,17 @@ extension AgoraMetalRender: AgoraVideoSinkProtocol {
128127 let uvTexture = texture ( pixelBuffer: pixelBuffer, textureCache: textureCache, planeIndex: 1 , pixelFormat: . rg8Unorm) {
129128 self . textures = [ yTexture, uvTexture]
130129 }
131- #endif
130+ return false
131+ }
132+
133+ func getVideoFrameProcessMode( ) -> AgoraVideoFrameProcessMode {
134+ initializeRenderPipelineState ( )
135+ metalView. delegate = self
136+ return . readOnly
137+ }
138+
139+ func getVideoPixelFormatPreference( ) -> AgoraVideoFormat {
140+ return . cvPixel
132141 }
133142}
134143
@@ -255,7 +264,7 @@ extension AgoraVideoRotation {
255264 guard viewSize. width > 0 , viewSize. height > 0 , videoSize. width > 0 , videoSize. height > 0 else {
256265 return nil
257266 }
258-
267+
259268 let widthAspito : Float
260269 let heightAspito : Float
261270 if self == . rotation90 || self == . rotation270 {
@@ -265,7 +274,7 @@ extension AgoraVideoRotation {
265274 widthAspito = Float ( videoSize. width / viewSize. width)
266275 heightAspito = Float ( videoSize. height / viewSize. height)
267276 }
268-
277+
269278 let x : Float
270279 let y : Float
271280 if widthAspito < heightAspito {
@@ -275,12 +284,12 @@ extension AgoraVideoRotation {
275284 x = widthAspito / heightAspito
276285 y = 1
277286 }
278-
287+
279288 let A = float4 ( x, - y, 0.0 , 1.0 )
280289 let B = float4 ( - x, - y, 0.0 , 1.0 )
281290 let C = float4 ( x, y, 0.0 , 1.0 )
282291 let D = float4 ( - x, y, 0.0 , 1.0 )
283-
292+
284293 switch self {
285294 case . rotationNone:
286295 if mirror {
@@ -309,5 +318,3 @@ extension AgoraVideoRotation {
309318 }
310319 }
311320}
312-
313- #endif
0 commit comments