From 9006bf47b4ed13da3a6b17b9ed6dac21af51cf48 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 6 Nov 2025 13:40:35 +0100 Subject: [PATCH 1/3] chore: Add LICENSE.txt meta file --- LICENSE.txt.meta | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 LICENSE.txt.meta diff --git a/LICENSE.txt.meta b/LICENSE.txt.meta new file mode 100644 index 00000000..7fd8f95c --- /dev/null +++ b/LICENSE.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 0d42073554a06654195ea41a4474dccf +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: From ceacbf48a9922f231198aab2cd86611acd4d9edf Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 11 Nov 2025 13:48:39 +0100 Subject: [PATCH 2/3] refactor: Graphics.Blit approach to reading video source buffer --- Runtime/Scripts/CameraVideoSource.cs | 25 ++++++++----------------- Runtime/Scripts/RtcVideoSource.cs | 4 ++-- Runtime/Scripts/ScreenVideoSource.cs | 23 +++++++---------------- Runtime/Scripts/TextureVideoSource.cs | 16 ++++++++++------ Runtime/Scripts/WebCameraSource.cs | 2 +- 5 files changed, 28 insertions(+), 42 deletions(-) diff --git a/Runtime/Scripts/CameraVideoSource.cs b/Runtime/Scripts/CameraVideoSource.cs index 05dad0a8..27752533 100644 --- a/Runtime/Scripts/CameraVideoSource.cs +++ b/Runtime/Scripts/CameraVideoSource.cs @@ -33,7 +33,7 @@ protected override VideoRotation GetVideoRotation() return VideoRotation._0; } - public CameraVideoSource(Camera camera, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Screen, bufferType) + public CameraVideoSource(Camera camera, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Screen, bufferType) { Camera = camera; base.Init(); @@ -64,7 +64,10 @@ private void ClearRenderTexture() protected override bool ReadBuffer() { if (_reading) + { return false; + } + _reading = true; var textureChanged = false; try @@ -78,26 +81,14 @@ protected override bool ReadBuffer() _renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); Camera.targetTexture = _renderTexture as RenderTexture; _captureBuffer = new NativeArray(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); - _previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); textureChanged = true; } - ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); -#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX - // Flip the texture for OSX - Graphics.CopyTexture(_renderTexture, _previewTexture); - var pixels = _previewTexture.GetPixels(); - var flippedPixels = new Color[pixels.Length]; - for (int i = 0; i < _previewTexture.height; i++) - { - Array.Copy(pixels, i * _previewTexture.width, flippedPixels, (_previewTexture.height - i - 1) * _previewTexture.width, _previewTexture.width); - } - _previewTexture.SetPixels(flippedPixels); -#else - Graphics.CopyTexture(_renderTexture, _previewTexture); -#endif + ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); - AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _renderTexture, 0, _textureFormat, OnReadback); + Graphics.Blit(_renderTexture, _previewTexture, new Vector2(1f, -1f), new Vector2(0f, 1f)); + AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback); } catch (Exception e) { diff --git a/Runtime/Scripts/RtcVideoSource.cs b/Runtime/Scripts/RtcVideoSource.cs index fc9b7677..68927557 100644 --- a/Runtime/Scripts/RtcVideoSource.cs +++ b/Runtime/Scripts/RtcVideoSource.cs @@ -26,11 +26,11 @@ public enum VideoStreamSource protected abstract VideoRotation GetVideoRotation(); - public delegate void TextureReceiveDelegate(Texture2D tex2d); + public delegate void TextureReceiveDelegate(RenderTexture tex2d); /// Called when we receive a new texture (first texture or the resolution changed) public event TextureReceiveDelegate TextureReceived; - protected Texture2D _previewTexture; + protected RenderTexture _previewTexture; protected NativeArray _captureBuffer; protected VideoStreamSource _sourceType; protected VideoBufferType _bufferType; diff --git a/Runtime/Scripts/ScreenVideoSource.cs b/Runtime/Scripts/ScreenVideoSource.cs index 2e3321e0..7ddf785f 100644 --- a/Runtime/Scripts/ScreenVideoSource.cs +++ b/Runtime/Scripts/ScreenVideoSource.cs @@ -58,7 +58,10 @@ private void ClearRenderTexture() protected override bool ReadBuffer() { if (_reading) + { return false; + } + _reading = true; var textureChanged = false; try @@ -71,26 +74,14 @@ protected override bool ReadBuffer() _bufferType = GetVideoBufferType(_textureFormat); _renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); _captureBuffer = new NativeArray(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); - _previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); textureChanged = true; } - ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); -#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX - // Flip the texture for OSX - Graphics.CopyTexture(_renderTexture, _previewTexture); - var pixels = _previewTexture.GetPixels(); - var flippedPixels = new Color[pixels.Length]; - for (int i = 0; i < _previewTexture.height; i++) - { - Array.Copy(pixels, i * _previewTexture.width, flippedPixels, (_previewTexture.height - i - 1) * _previewTexture.width, _previewTexture.width); - } - _previewTexture.SetPixels(flippedPixels); -#else - Graphics.CopyTexture(_renderTexture, _previewTexture); -#endif + ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); - AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _renderTexture, 0, _textureFormat, OnReadback); + Graphics.Blit(_renderTexture, _previewTexture, new Vector2(1f, -1f), new Vector2(0f, 1f)); + AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback); } catch (Exception e) { diff --git a/Runtime/Scripts/TextureVideoSource.cs b/Runtime/Scripts/TextureVideoSource.cs index 5b140c86..f23052c2 100644 --- a/Runtime/Scripts/TextureVideoSource.cs +++ b/Runtime/Scripts/TextureVideoSource.cs @@ -1,9 +1,8 @@ -using UnityEngine; using LiveKit.Proto; -using UnityEngine.Rendering; using Unity.Collections; +using UnityEngine; using UnityEngine.Experimental.Rendering; -using System; +using UnityEngine.Rendering; namespace LiveKit { @@ -43,19 +42,24 @@ public TextureVideoSource(Texture texture, VideoBufferType bufferType = VideoBuf protected override bool ReadBuffer() { if (_reading) + { return false; + } + _reading = true; var textureChanged = false; - if (_previewTexture == null || _previewTexture.width != GetWidth() || _previewTexture.height != GetHeight()) { + if (_previewTexture == null || _previewTexture.width != GetWidth() || _previewTexture.height != GetHeight()) + { var compatibleFormat = SystemInfo.GetCompatibleFormat(Texture.graphicsFormat, FormatUsage.ReadPixels); _textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat); _bufferType = GetVideoBufferType(_textureFormat); _captureBuffer = new NativeArray(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); - _previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); textureChanged = true; } - Graphics.CopyTexture(Texture, _previewTexture); + + Graphics.Blit(Texture, _previewTexture, new Vector2(1f, -1f), new Vector2(0f, 1f)); AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback); return textureChanged; } diff --git a/Runtime/Scripts/WebCameraSource.cs b/Runtime/Scripts/WebCameraSource.cs index efe2098a..b0080d65 100644 --- a/Runtime/Scripts/WebCameraSource.cs +++ b/Runtime/Scripts/WebCameraSource.cs @@ -71,7 +71,7 @@ protected override bool ReadBuffer() _bufferType = GetVideoBufferType(_textureFormat); _readBuffer = new Color32[width * height]; - _previewTexture = new Texture2D(width, height, TextureFormat.BGRA32, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); _captureBuffer = new NativeArray(width * height * GetStrideForBuffer(_bufferType), Allocator.Persistent); if (CamTexture.graphicsFormat != _previewTexture.graphicsFormat) From 907464ebc3ee080d724b68899bff96a72fdcad5f Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 11 Nov 2025 14:38:45 +0100 Subject: [PATCH 3/3] chore: Revert formatting changes --- Runtime/Scripts/CameraVideoSource.cs | 3 --- Runtime/Scripts/ScreenVideoSource.cs | 3 --- Runtime/Scripts/TextureVideoSource.cs | 3 --- 3 files changed, 9 deletions(-) diff --git a/Runtime/Scripts/CameraVideoSource.cs b/Runtime/Scripts/CameraVideoSource.cs index 27752533..b9cf7ff4 100644 --- a/Runtime/Scripts/CameraVideoSource.cs +++ b/Runtime/Scripts/CameraVideoSource.cs @@ -64,10 +64,7 @@ private void ClearRenderTexture() protected override bool ReadBuffer() { if (_reading) - { return false; - } - _reading = true; var textureChanged = false; try diff --git a/Runtime/Scripts/ScreenVideoSource.cs b/Runtime/Scripts/ScreenVideoSource.cs index 7ddf785f..1c3c411b 100644 --- a/Runtime/Scripts/ScreenVideoSource.cs +++ b/Runtime/Scripts/ScreenVideoSource.cs @@ -58,10 +58,7 @@ private void ClearRenderTexture() protected override bool ReadBuffer() { if (_reading) - { return false; - } - _reading = true; var textureChanged = false; try diff --git a/Runtime/Scripts/TextureVideoSource.cs b/Runtime/Scripts/TextureVideoSource.cs index f23052c2..6ca16386 100644 --- a/Runtime/Scripts/TextureVideoSource.cs +++ b/Runtime/Scripts/TextureVideoSource.cs @@ -42,10 +42,7 @@ public TextureVideoSource(Texture texture, VideoBufferType bufferType = VideoBuf protected override bool ReadBuffer() { if (_reading) - { return false; - } - _reading = true; var textureChanged = false;