Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 5 additions & 18 deletions Runtime/Scripts/CameraVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -78,26 +78,14 @@ protected override bool ReadBuffer()
_renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat);
Camera.targetTexture = _renderTexture as RenderTexture;
_captureBuffer = new NativeArray<byte>(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)
{
Expand All @@ -107,4 +95,3 @@ protected override bool ReadBuffer()
}
}
}

5 changes: 2 additions & 3 deletions Runtime/Scripts/RtcVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> _captureBuffer;
protected VideoStreamSource _sourceType;
protected VideoBufferType _bufferType;
Expand Down Expand Up @@ -220,4 +220,3 @@ protected virtual void Dispose(bool disposing)
}
}
}

21 changes: 4 additions & 17 deletions Runtime/Scripts/ScreenVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,14 @@ protected override bool ReadBuffer()
_bufferType = GetVideoBufferType(_textureFormat);
_renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat);
_captureBuffer = new NativeArray<byte>(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)
{
Expand All @@ -110,4 +98,3 @@ protected override bool SendFrame()
}
}
}

14 changes: 7 additions & 7 deletions Runtime/Scripts/TextureVideoSource.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -47,18 +46,19 @@ protected override bool ReadBuffer()
_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<byte>(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;
}
}
}

3 changes: 1 addition & 2 deletions Runtime/Scripts/WebCameraSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(width * height * GetStrideForBuffer(_bufferType), Allocator.Persistent);

if (CamTexture.graphicsFormat != _previewTexture.graphicsFormat)
Expand Down Expand Up @@ -105,4 +105,3 @@ protected override bool ReadBuffer()
}
}
}

Loading