Skip to content

Conversation

@ijinfeng
Copy link

@ijinfeng ijinfeng commented Aug 4, 2025

Fix: Prevent disposal of sourceImpl when multiple receivers are connected

Problem

When using Broadcast mode with multiple WebRTC receivers connected, refreshing one of the receiver pages can trigger a teardown process on the Unity side.
During this process, the internal sourceImpl (e.g., RenderTextureSourceImpl) of VideoStreamSender is disposed, even though other receivers are still actively connected and using the same videoSender.

As a result, subsequent receivers continue streaming, but videoSender lacks a valid source, leading to fallback behavior (e.g., screen capture via CameraVideoSource) or broken stream output.

Root Cause

The default logic disposes sourceImpl when a transceiver is removed, without checking whether other active transceivers still exist for the same connection.

Solution

This fix adds a check before disposing sourceImpl:

✅ Only dispose sourceImpl when no active transceivers remain (i.e., when all consumers are gone).

This ensures that the video source (e.g., RenderTexture) stays alive as long as there is at least one receiver using it.

Impact

  • Prevents unintended fallback behavior when a receiver refreshes while others are still connected.
  • Fixes stream reliability in multi-receiver setups.
  • Keeps sourceImpl lifecycle tied to actual usage instead of per-teardown events.

Reproduction Steps (before fix)

  1. Open two browser clients connected to the same Unity stream (RenderTexture).
  2. Refresh the second page.
  3. Observe that both clients now receive screen-mode fallback or broken stream.

Result (after fix)

Refreshing one receiver no longer disrupts video stream for others.
The sourceImpl is only released once all transceivers are properly disposed.

@unity-cla-assistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


jinfeng seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

2 participants