11using UnityEngine ;
22using LiveKit . Proto ;
3- using UnityEngine . Rendering ;
43using Unity . Collections ;
54using UnityEngine . Experimental . Rendering ;
6- using System ;
75using System . Runtime . InteropServices ;
86
97namespace LiveKit
@@ -14,21 +12,21 @@ public class WebCameraSource : RtcVideoSource
1412 TextureFormat _textureFormat ;
1513 private RenderTexture _tempTexture ;
1614
17- public WebCamTexture Texture { get ; }
15+ public WebCamTexture CamTexture { get ; }
1816
1917 public override int GetWidth ( )
2018 {
21- return Texture . width ;
19+ return CamTexture . width ;
2220 }
2321
2422 public override int GetHeight ( )
2523 {
26- return Texture . height ;
24+ return CamTexture . height ;
2725 }
2826
2927 protected override VideoRotation GetVideoRotation ( )
3028 {
31- switch ( Texture . videoRotationAngle )
29+ switch ( CamTexture . videoRotationAngle )
3230 {
3331 case 90 : return VideoRotation . _90 ;
3432 case 180 : return VideoRotation . _0 ;
@@ -38,52 +36,69 @@ protected override VideoRotation GetVideoRotation()
3836
3937 public WebCameraSource ( WebCamTexture texture , VideoBufferType bufferType = VideoBufferType . Rgba ) : base ( VideoStreamSource . Texture , bufferType )
4038 {
41- Texture = texture ;
39+ CamTexture = texture ;
4240 base . Init ( ) ;
4341 }
4442
4543 ~ WebCameraSource ( )
4644 {
47- Dispose ( ) ;
45+ Dispose ( false ) ;
4846 }
4947
48+ private Color32 [ ] _readBuffer ;
49+
5050 // Read the texture data into a native array asynchronously
5151 protected override bool ReadBuffer ( )
5252 {
53- if ( _reading && ! Texture . isPlaying )
53+ if ( _reading && ! CamTexture . isPlaying )
5454 return false ;
5555 _reading = true ;
5656 var textureChanged = false ;
5757
58- if ( _dest == null || _dest . width != GetWidth ( ) || _dest . height != GetHeight ( ) )
58+ int width = GetWidth ( ) ;
59+ int height = GetHeight ( ) ;
60+
61+ if ( _previewTexture == null ||
62+ _previewTexture . width != width ||
63+ _previewTexture . height != height )
5964 {
60- var compatibleFormat = SystemInfo . GetCompatibleFormat ( Texture . graphicsFormat , FormatUsage . ReadPixels ) ;
65+ Debug . Log ( "Creating new texture" ) ;
66+ // Required when using Allocator.Persistent
67+ if ( _captureBuffer . IsCreated )
68+ _captureBuffer . Dispose ( ) ;
69+
70+ var compatibleFormat = SystemInfo . GetCompatibleFormat ( CamTexture . graphicsFormat , FormatUsage . ReadPixels ) ;
6171 _textureFormat = GraphicsFormatUtility . GetTextureFormat ( compatibleFormat ) ;
6272 _bufferType = GetVideoBufferType ( _textureFormat ) ;
63- _data = new NativeArray < byte > ( GetWidth ( ) * GetHeight ( ) * GetStrideForBuffer ( _bufferType ) , Allocator . Persistent ) ;
64- _dest = new Texture2D ( GetWidth ( ) , GetHeight ( ) , TextureFormat . BGRA32 , false ) ;
65- if ( Texture . graphicsFormat != _dest . graphicsFormat ) _tempTexture = new RenderTexture ( GetWidth ( ) , GetHeight ( ) , 0 , _dest . graphicsFormat ) ;
73+
74+ _readBuffer = new Color32 [ width * height ] ;
75+ _previewTexture = new Texture2D ( width , height , TextureFormat . BGRA32 , false ) ;
76+ _captureBuffer = new NativeArray < byte > ( width * height * GetStrideForBuffer ( _bufferType ) , Allocator . Persistent ) ;
77+
78+ if ( CamTexture . graphicsFormat != _previewTexture . graphicsFormat )
79+ _tempTexture = new RenderTexture ( width , height , 0 , _previewTexture . graphicsFormat ) ;
80+
6681 textureChanged = true ;
6782 }
6883
69- Color32 [ ] pixels = new Color32 [ GetWidth ( ) * GetHeight ( ) ] ;
70- Texture . GetPixels32 ( pixels ) ;
71- var bytes = MemoryMarshal . Cast < Color32 , byte > ( pixels ) ;
72- _data . CopyFrom ( bytes . ToArray ( ) ) ;
84+ CamTexture . GetPixels32 ( _readBuffer ) ;
85+ MemoryMarshal . Cast < Color32 , byte > ( _readBuffer )
86+ . CopyTo ( _captureBuffer . AsSpan ( ) ) ;
87+
7388 _requestPending = true ;
74-
75- if ( Texture . graphicsFormat != _dest . graphicsFormat )
89+
90+ if ( CamTexture . graphicsFormat != _previewTexture . graphicsFormat )
7691 {
77- Graphics . Blit ( Texture , _tempTexture ) ;
78- Graphics . CopyTexture ( _tempTexture , _dest ) ;
92+ Graphics . Blit ( CamTexture , _tempTexture ) ;
93+ Graphics . CopyTexture ( _tempTexture , _previewTexture ) ;
7994 }
8095 else
8196 {
8297#if UNITY_6000_0_OR_NEWER && UNITY_IOS
83- _dest . SetPixels ( Texture . GetPixels ( ) ) ;
84- _dest . Apply ( ) ;
98+ _previewTexture . SetPixels ( CamTexture . GetPixels ( ) ) ;
99+ _previewTexture . Apply ( ) ;
85100#else
86- Graphics . CopyTexture ( Texture , _dest ) ;
101+ Graphics . CopyTexture ( CamTexture , _previewTexture ) ;
87102#endif
88103 }
89104
0 commit comments