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: 23 additions & 0 deletions Runtime/Scripts/CameraVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public override void Stop()
ClearRenderTexture();
}

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

private void ClearRenderTexture()
{
if (_renderTexture)
Expand All @@ -71,6 +82,18 @@ protected override bool ReadBuffer()
{
if (_renderTexture == null || _renderTexture.width != GetWidth() || _renderTexture.height != GetHeight())
{
// Free previously allocated GPU/native resources before reallocating;
// otherwise the old textures and NativeArray leak on every resolution change.
if (_renderTexture != null)
{
_renderTexture.Release();
UnityEngine.Object.Destroy(_renderTexture);
}
if (_previewTexture != null)
UnityEngine.Object.Destroy(_previewTexture);
if (_captureBuffer.IsCreated)
_captureBuffer.Dispose();

var targetFormat = Utils.GetSupportedGraphicsFormat(SystemInfo.graphicsDeviceType);
var compatibleFormat = SystemInfo.GetCompatibleFormat(targetFormat, FormatUsage.ReadPixels);
_textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat);
Expand Down
23 changes: 23 additions & 0 deletions Runtime/Scripts/ScreenVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public override void Stop()
ClearRenderTexture();
}

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

private void ClearRenderTexture()
{
if (_renderTexture)
Expand All @@ -65,6 +76,18 @@ protected override bool ReadBuffer()
{
if (_renderTexture == null || _renderTexture.width != GetWidth() || _renderTexture.height != GetHeight())
{
// Free previously allocated GPU/native resources before reallocating;
// otherwise the old textures and NativeArray leak on every resolution change.
if (_renderTexture != null)
{
_renderTexture.Release();
UnityEngine.Object.Destroy(_renderTexture);
}
if (_previewTexture != null)
UnityEngine.Object.Destroy(_previewTexture);
if (_captureBuffer.IsCreated)
_captureBuffer.Dispose();

var targetFormat = Utils.GetSupportedGraphicsFormat(SystemInfo.graphicsDeviceType);
var compatibleFormat = SystemInfo.GetCompatibleFormat(targetFormat, FormatUsage.ReadPixels);
_textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat);
Expand Down
22 changes: 21 additions & 1 deletion Runtime/Scripts/WebCameraSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public WebCameraSource(WebCamTexture texture, VideoBufferType bufferType = Video
Dispose(false);
}

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

private Color32[] _readBuffer;

// Read the texture data into a native array asynchronously
Expand All @@ -64,7 +75,16 @@ protected override bool ReadBuffer()
_previewTexture.width != width ||
_previewTexture.height != height)
{
// Required when using Allocator.Persistent
// Free previously allocated GPU/native resources before reallocating;
// otherwise the old textures and NativeArray leak on every resolution change.
if (_previewTexture != null)
UnityEngine.Object.Destroy(_previewTexture);
if (_tempTexture != null)
{
_tempTexture.Release();
UnityEngine.Object.Destroy(_tempTexture);
_tempTexture = null;
}
if (_captureBuffer.IsCreated)
_captureBuffer.Dispose();

Expand Down
8 changes: 8 additions & 0 deletions Samples~/Meet/Assets/Runtime/MeetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ private void Update()

private void OnDestroy()
{
// Without this, scene change / app quit while connected leaves all tracks,
// streams, and their backing GPU/native resources allocated.
if (_room != null)
{
_room.Disconnect();
_room = null;
}
CleanUpAllTracks();
_webCamTexture?.Stop();
}

Expand Down
Loading