这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@ladvoc
Copy link
Contributor

@ladvoc ladvoc commented May 21, 2025

This PR addresses two memory issues related to video capture, the first of which was identified by the Unity Profiler as a key contributor to broader performance problems:

  1. Extraneous allocations in WebCameraSource: the read buffer method was performing two unnecessary heap allocations per call, leading to large GC events and severe performance impacts in other parts of the SDK (namely in audio capture):
// This results in 2 * width * heigh * 32 bytes being allocated on each invocation
Color32[] pixels = new Color32[GetWidth() * GetHeight()]; // first allocation
Texture.GetPixels32(pixels);
var bytes = MemoryMarshal.Cast<Color32, byte>(pixels);
_data.CopyFrom(bytes.ToArray()); // second allocation*

*Span<T>.ToArray also performs heap allocation as per the docs.

The solution is to only allocate the temporary buffer once until the configuration is invalidated, and use MemoryMarshal and Span methods to avoid the additional ToArray call:

CamTexture.GetPixels32(pixels);
MemoryMarshal.Cast<Color32, byte>(pixels)
    .CopyTo(_captureBuffer.AsSpan());
  1. Native array used by RtcVideoSource and its subclasses was not disposed of, causing a memory leak; when a native array is created using Allocator.Persistent, a manual call to dispose is required. RtcVideoSource now implements IDispose to ensure the native array is disposed of even if Dispose() is not manually called.

CLT-1613

ladvoc added 2 commits May 21, 2025 22:04
Native array wasn't disposed when using Allocator.Persistent. Also implement IDispose for RtcVideoSource
@ladvoc ladvoc requested a review from theomonnom May 21, 2025 12:51
@ladvoc ladvoc merged commit 55c765d into main May 23, 2025
14 checks passed
@ladvoc ladvoc deleted the ladvoc/video-allocation branch May 23, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants