-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Optimize VideoSource capture and add WebCameraSource. (#59)
- Loading branch information
1 parent
9f9bfdf
commit 47692c7
Showing
6 changed files
with
163 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using UnityEngine; | ||
using LiveKit.Proto; | ||
using UnityEngine.Rendering; | ||
using Unity.Collections; | ||
using UnityEngine.Experimental.Rendering; | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LiveKit | ||
{ | ||
// VideoSource for Unity WebCamTexture | ||
public class WebCameraSource : RtcVideoSource | ||
{ | ||
TextureFormat _textureFormat; | ||
|
||
public WebCamTexture Texture { get; } | ||
|
||
public override int GetWidth() | ||
{ | ||
return Texture.width; | ||
} | ||
|
||
public override int GetHeight() | ||
{ | ||
return Texture.height; | ||
} | ||
|
||
protected override VideoRotation GetVideoRotation() | ||
{ | ||
return VideoRotation._180; | ||
} | ||
|
||
public WebCameraSource(WebCamTexture texture, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Texture, bufferType) | ||
{ | ||
Texture = texture; | ||
base.Init(); | ||
} | ||
|
||
~WebCameraSource() | ||
{ | ||
Dispose(); | ||
} | ||
|
||
// Read the texture data into a native array asynchronously | ||
protected override bool ReadBuffer() | ||
{ | ||
if (_reading && !Texture.isPlaying) | ||
return false; | ||
_reading = true; | ||
var textureChanged = false; | ||
|
||
if (_dest == null || _dest.width != GetWidth() || _dest.height != GetHeight()) | ||
{ | ||
var compatibleFormat = SystemInfo.GetCompatibleFormat(Texture.graphicsFormat, FormatUsage.ReadPixels); | ||
_textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat); | ||
_bufferType = GetVideoBufferType(_textureFormat); | ||
_data = new NativeArray<byte>(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); | ||
_dest = new Texture2D(GetWidth(), GetHeight(), TextureFormat.BGRA32, false); | ||
textureChanged = true; | ||
} | ||
|
||
Color32[] pixels = new Color32[GetWidth() * GetHeight()]; | ||
Texture.GetPixels32(pixels); | ||
var bytes = MemoryMarshal.Cast<Color32, byte>(pixels); | ||
_data.CopyFrom(bytes.ToArray()); | ||
_requestPending = true; | ||
Graphics.CopyTexture(Texture, _dest); | ||
|
||
return textureChanged; | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.