@@ -10,6 +10,7 @@ namespace LiveKit
1010 public class TextureVideoSource : RtcVideoSource
1111 {
1212 TextureFormat _textureFormat ;
13+ private RenderTexture _flippedRT ;
1314
1415 public Texture Texture { get ; }
1516
@@ -39,6 +40,17 @@ public TextureVideoSource(Texture texture, VideoBufferType bufferType = VideoBuf
3940 Dispose ( false ) ;
4041 }
4142
43+ protected override void Dispose ( bool disposing )
44+ {
45+ if ( disposing && _flippedRT != null )
46+ {
47+ _flippedRT . Release ( ) ;
48+ UnityEngine . Object . Destroy ( _flippedRT ) ;
49+ _flippedRT = null ;
50+ }
51+ base . Dispose ( disposing ) ;
52+ }
53+
4254 // Read the texture data into a native array asynchronously
4355 protected override bool ReadBuffer ( )
4456 {
@@ -53,10 +65,20 @@ protected override bool ReadBuffer()
5365 _bufferType = GetVideoBufferType ( _textureFormat ) ;
5466 _captureBuffer = new NativeArray < byte > ( GetWidth ( ) * GetHeight ( ) * GetStrideForBuffer ( _bufferType ) , Allocator . Persistent ) ;
5567 _previewTexture = new Texture2D ( GetWidth ( ) , GetHeight ( ) , _textureFormat , false ) ;
68+ if ( _flippedRT != null )
69+ {
70+ _flippedRT . Release ( ) ;
71+ UnityEngine . Object . Destroy ( _flippedRT ) ;
72+ }
73+ _flippedRT = new RenderTexture ( GetWidth ( ) , GetHeight ( ) , 0 , compatibleFormat ) ;
5674 textureChanged = true ;
5775 }
58- Graphics . CopyTexture ( Texture , _previewTexture ) ;
59- AsyncGPUReadback . RequestIntoNativeArray ( ref _captureBuffer , _previewTexture , 0 , _textureFormat , OnReadback ) ;
76+ // Vertically flip into an intermediate RT so the bytes AsyncGPUReadback produces are
77+ // top-down (WebRTC expects top-down; Unity render textures are bottom-up on macOS/Metal
78+ // and other GL-origin platforms).
79+ Graphics . Blit ( Texture , _flippedRT , new Vector2 ( 1f , - 1f ) , new Vector2 ( 0f , 1f ) ) ;
80+ Graphics . CopyTexture ( _flippedRT , _previewTexture ) ;
81+ AsyncGPUReadback . RequestIntoNativeArray ( ref _captureBuffer , _flippedRT , 0 , _textureFormat , OnReadback ) ;
6082 return textureChanged ;
6183 }
6284 }
0 commit comments