Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Runtime/Scripts/TextureVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace LiveKit
public class TextureVideoSource : RtcVideoSource
{
TextureFormat _textureFormat;
private RenderTexture _flippedRT;

public Texture Texture { get; }

Expand Down Expand Up @@ -39,6 +40,17 @@ public TextureVideoSource(Texture texture, VideoBufferType bufferType = VideoBuf
Dispose(false);
}

protected override void Dispose(bool disposing)
{
if (disposing && _flippedRT != null)
{
_flippedRT.Release();
UnityEngine.Object.Destroy(_flippedRT);
_flippedRT = null;
}
base.Dispose(disposing);
}

// Read the texture data into a native array asynchronously
protected override bool ReadBuffer()
{
Expand All @@ -53,10 +65,20 @@ protected override bool ReadBuffer()
_bufferType = GetVideoBufferType(_textureFormat);
_captureBuffer = new NativeArray<byte>(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent);
_previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false);
if (_flippedRT != null)
{
_flippedRT.Release();
UnityEngine.Object.Destroy(_flippedRT);
}
_flippedRT = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat);
textureChanged = true;
}
Graphics.CopyTexture(Texture, _previewTexture);
AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback);
// Vertically flip into an intermediate RT so the bytes AsyncGPUReadback produces are
// top-down (WebRTC expects top-down; Unity render textures are bottom-up on macOS/Metal
// and other GL-origin platforms).
Graphics.Blit(Texture, _flippedRT, new Vector2(1f, -1f), new Vector2(0f, 1f));
Graphics.CopyTexture(_flippedRT, _previewTexture);
AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _flippedRT, 0, _textureFormat, OnReadback);
return textureChanged;
}
}
Expand Down
Loading