Skip to content

Commit da21284

Browse files
authored
feat: support rendering callback for rtc video source. (#43)
* feat: support rendering callback for rtc video source. * update. * bump version. * update. * texture dispose.
1 parent dc1d8c7 commit da21284

File tree

6 files changed

+62
-15
lines changed

6 files changed

+62
-15
lines changed

Runtime/Scripts/CameraVideoSource.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ private void ClearRenderTexture()
5252
}
5353

5454
// Read the texture data into a native array asynchronously
55-
protected override void ReadBuffer()
55+
protected override bool ReadBuffer()
5656
{
5757
if (_reading)
58-
return;
58+
return false;
5959
_reading = true;
60-
60+
var textureChanged = false;
6161
try
6262
{
6363
if (_dest == null || _dest.width != GetWidth() || _dest.height != GetHeight())
@@ -69,6 +69,7 @@ protected override void ReadBuffer()
6969
_dest = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat);
7070
Camera.targetTexture = _dest as RenderTexture;
7171
_data = new NativeArray<byte>(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent);
72+
textureChanged = true;
7273
}
7374
ScreenCapture.CaptureScreenshotIntoRenderTexture(_dest as RenderTexture);
7475
AsyncGPUReadback.RequestIntoNativeArray(ref _data, _dest, 0, _textureFormat, OnReadback);
@@ -77,7 +78,7 @@ protected override void ReadBuffer()
7778
{
7879
Utils.Error(e);
7980
}
80-
81+
return textureChanged;
8182
}
8283
}
8384
}

Runtime/Scripts/RtcVideoSource.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public enum VideoStreamSource
2626
public abstract int GetWidth();
2727
public abstract int GetHeight();
2828

29+
public delegate void TextureReceiveDelegate(Texture2D tex2d);
30+
/// Called when we receive a new texture (first texture or the resolution changed)
31+
public event TextureReceiveDelegate TextureReceived;
32+
2933
protected Texture _dest;
3034
protected NativeArray<byte> _data;
3135
protected VideoStreamSource _sourceType;
@@ -35,6 +39,7 @@ public enum VideoStreamSource
3539
protected bool _requestPending = false;
3640
protected bool isDisposed = true;
3741
protected bool _playing = false;
42+
private Texture2D _texture2D = null;
3843

3944
internal RtcVideoSource(VideoStreamSource sourceType, VideoBufferType bufferType)
4045
{
@@ -121,12 +126,41 @@ public virtual void Stop()
121126
_playing = false;
122127
}
123128

129+
private void LoadToTexture2D(Texture2D tex, RenderTexture rTex)
130+
{
131+
var old_rt = RenderTexture.active;
132+
RenderTexture.active = rTex;
133+
134+
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
135+
tex.Apply();
136+
137+
RenderTexture.active = old_rt;
138+
}
139+
124140
public IEnumerator Update()
125141
{
126142
while (_playing)
127143
{
128144
yield return null;
129-
ReadBuffer();
145+
var textureChanged = ReadBuffer();
146+
147+
if(textureChanged)
148+
{
149+
if (_texture2D == null)
150+
{
151+
_texture2D = new Texture2D(_dest.width, _dest.height, TextureFormat.RGB24, false);
152+
} else
153+
{
154+
_texture2D.Reinitialize(_dest.width, _dest.height);
155+
}
156+
TextureReceived?.Invoke(_texture2D);
157+
}
158+
159+
if(TextureReceived.GetInvocationList().Length > 0)
160+
{
161+
LoadToTexture2D(_texture2D, _dest as RenderTexture);
162+
}
163+
130164
SendFrame();
131165
}
132166

@@ -136,13 +170,14 @@ public IEnumerator Update()
136170
public virtual void Dispose()
137171
{
138172
if (!isDisposed)
139-
{
140-
_data.Dispose();
173+
{
174+
if (_data != null) _data.Dispose();
175+
if (_texture2D != null) UnityEngine.Object.Destroy(_texture2D);
141176
isDisposed = true;
142177
}
143178
}
144179

145-
protected abstract void ReadBuffer();
180+
protected abstract bool ReadBuffer();
146181

147182
protected virtual bool SendFrame()
148183
{

Runtime/Scripts/ScreenVideoSource.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ private void ClearRenderTexture()
4949
}
5050

5151
// Read the texture data into a native array asynchronously
52-
protected override void ReadBuffer()
52+
protected override bool ReadBuffer()
5353
{
5454
if (_reading)
55-
return;
55+
return false;
5656
_reading = true;
57+
var textureChanged = false;
5758
try
5859
{
5960
if (_dest == null || _dest.width != GetWidth() || _dest.height != GetHeight())
@@ -64,6 +65,7 @@ protected override void ReadBuffer()
6465
_bufferType = GetVideoBufferType(_textureFormat);
6566
_dest = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat);
6667
_data = new NativeArray<byte>(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent);
68+
textureChanged = true;
6769
}
6870
ScreenCapture.CaptureScreenshotIntoRenderTexture(_dest as RenderTexture);
6971
AsyncGPUReadback.RequestIntoNativeArray(ref _data, _dest, 0, _textureFormat, OnReadback);
@@ -72,6 +74,7 @@ protected override void ReadBuffer()
7274
{
7375
Utils.Error(e);
7476
}
77+
return textureChanged;
7578
}
7679

7780
protected override bool SendFrame()

Runtime/Scripts/TextureVideoSource.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ public TextureVideoSource(Texture texture, VideoBufferType bufferType = VideoBuf
3434
}
3535

3636
// Read the texture data into a native array asynchronously
37-
protected override void ReadBuffer()
37+
protected override bool ReadBuffer()
3838
{
3939
if (_reading)
40-
return;
40+
return false;
4141
_reading = true;
42+
var textureChanged = false;
4243
if (!SystemInfo.IsFormatSupported(Texture.graphicsFormat, FormatUsage.ReadPixels))
4344
{
4445
if (_dest == null || _dest.width != GetWidth() || _dest.height != GetHeight())
@@ -48,17 +49,24 @@ protected override void ReadBuffer()
4849
_bufferType = GetVideoBufferType(_textureFormat);
4950
_data = new NativeArray<byte>(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent);
5051
_dest = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false);
52+
textureChanged = true;
5153
}
5254
Graphics.CopyTexture(Texture, _dest);
5355
}
5456
else
5557
{
58+
if(_dest == null || _dest != Texture)
59+
{
60+
textureChanged = true;
61+
}
62+
5663
_dest = Texture;
5764
_textureFormat = GraphicsFormatUtility.GetTextureFormat(Texture.graphicsFormat);
5865
_bufferType = GetVideoBufferType(_textureFormat);
5966
}
6067

6168
AsyncGPUReadback.RequestIntoNativeArray(ref _data, _dest, 0, _textureFormat, OnReadback);
69+
return textureChanged;
6270
}
6371
}
6472
}

Runtime/Scripts/VideoStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void Dispose(bool disposing)
7171
{
7272
if (disposing)
7373
VideoBuffer?.Dispose();
74-
74+
if (Texture != null) UnityEngine.Object.Destroy(Texture);
7575
_disposed = true;
7676
}
7777
}
@@ -106,7 +106,7 @@ public IEnumerator Update()
106106
var textureChanged = false;
107107
if (Texture == null || Texture.width != rWidth || Texture.height != rHeight)
108108
{
109-
if (Texture != null) UnityEngine.Object.Destroy(Texture);
109+
if (Texture != null) UnityEngine.Object.Destroy(Texture);
110110
Texture = new Texture2D((int)rWidth, (int)rHeight, TextureFormat.RGBA32, false);
111111
Texture.ignoreMipmapLimit = false;
112112
textureChanged = true;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io.livekit.livekit-sdk",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"displayName": "LiveKit SDK",
55
"description": "LiveKit",
66
"unity": "2021.3",

0 commit comments

Comments
 (0)