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
10 changes: 9 additions & 1 deletion Runtime/Scripts/Participant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ internal void OnTrackUnpublished(RemoteTrackPublication publication)
{
TrackUnpublished?.Invoke(publication);
}

internal void DisposeHandles()
{
foreach (var pub in _tracks.Values)
pub.DisposeHandles();
_tracks.Clear();
Handle?.Dispose();
}
}

public sealed class LocalParticipant : Participant
Expand Down Expand Up @@ -616,7 +624,7 @@ internal void OnPublish(PublishTrackCallback e)

IsError = !string.IsNullOrEmpty(e.Error);
IsDone = true;
var publication = new LocalTrackPublication(e.Publication.Info);
var publication = new LocalTrackPublication(e.Publication.Info, FfiHandle.FromOwnedHandle(e.Publication.Handle));
publication.UpdateTrack(_localTrack as Track);
_localTrack.UpdateSid(publication.Sid);
_internalTracks.Add(e.Publication.Info.Sid, publication);
Expand Down
44 changes: 40 additions & 4 deletions Runtime/Scripts/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ public Proto.RoomOptions ToProto()
}
}

public class Room
public class Room : IDisposable
{
internal FfiHandle RoomHandle = null;
private bool _disposed = false;
private readonly Dictionary<string, RemoteParticipant> _participants = new();
private StreamHandlerRegistry _streamHandlers = new();

Expand Down Expand Up @@ -183,14 +184,46 @@ public ConnectInstruction Connect(string url, string token, RoomOptions options)

public void Disconnect()
{
if (this.RoomHandle == null)
if (_disposed || RoomHandle == null)
return;
var (response, _) = FFIBridge.Instance.SendDisconnectRequest(this);
using (response)
{
Utils.Debug($"Disconnect.... {RoomHandle}");
Utils.Debug($"Disconnect response.... {response}");
}
// Release the Rust-side room synchronously. Without this the FfiRoom
// (peer connection, signaling client, libwebrtc state) lingers in the
// FFI handle table until the SafeHandle finalizer runs.
Cleanup();
}

public void Dispose()
{
Disconnect();
GC.SuppressFinalize(this);
}

private void Cleanup()
{
if (_disposed)
return;
_disposed = true;

FfiClient.Instance.RoomEventReceived -= OnEventReceived;
FfiClient.Instance.RpcMethodInvocationReceived -= OnRpcMethodInvocationReceived;
FfiClient.Instance.DisconnectReceived -= OnDisconnectReceived;

// Participant + track + publication FFI handles are independent entries in the
// Rust handle table — dropping the room handle alone does not cascade to them, so
// they would otherwise linger until C# GC finalizes each SafeHandle.
LocalParticipant?.DisposeHandles();
foreach (var p in _participants.Values)
p.DisposeHandles();
_participants.Clear();

RoomHandle?.Dispose();
RoomHandle = null;
}

/// <summary>
Expand Down Expand Up @@ -266,6 +299,10 @@ internal void OnRpcMethodInvocationReceived(RpcMethodInvocationEvent e)

internal void OnEventReceived(RoomEvent e)
{
// After Cleanup() the handle is null but late events may still flow
// through the FfiClient before the unsubscribe fully takes effect.
if (RoomHandle == null)
return;
if (e.RoomHandle != (ulong)RoomHandle.DangerousGetHandle())
return;

Expand Down Expand Up @@ -564,8 +601,7 @@ private void OnDisconnectReceived(DisconnectCallback e)

private void OnDisconnect()
{
FfiClient.Instance.RoomEventReceived -= OnEventReceived;
FfiClient.Instance.RpcMethodInvocationReceived -= OnRpcMethodInvocationReceived;
Cleanup();
}

internal RemoteParticipant CreateRemoteParticipantWithTracks(ConnectCallback.Types.ParticipantWithTracks item)
Expand Down
1 change: 1 addition & 0 deletions Runtime/Scripts/RtcAudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ protected virtual void Dispose(bool disposing)
}
_pendingFrameData.Clear();
}
Handle?.Dispose();
_disposed = true;
Utils.Debug($"{DebugTag} disposed");
}
Expand Down
1 change: 1 addition & 0 deletions Runtime/Scripts/RtcVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ protected virtual void Dispose(bool disposing)
Debug.Log("Disposing capture buffer");
_captureBuffer.Dispose();
}
Handle?.Dispose();
_disposed = true;
}

Expand Down
5 changes: 5 additions & 0 deletions Runtime/Scripts/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ internal void UpdateMuted(bool muted)
{
_info.Muted = muted;
}

internal void DisposeHandles()
{
Handle?.Dispose();
}
}

public sealed class LocalAudioTrack : Track, ILocalTrack, IAudioTrack
Expand Down
22 changes: 21 additions & 1 deletion Runtime/Scripts/TrackPublication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ internal void UpdateMuted(bool muted)
_info.Muted = muted;
Track?.UpdateMuted(muted);
}

internal virtual void DisposeHandles()
{
Track?.DisposeHandles();
}
}

public sealed class RemoteTrackPublication : TrackPublication
Expand All @@ -54,6 +59,12 @@ internal RemoteTrackPublication(TrackPublicationInfo info, FfiHandle handle) : b
Handle = handle;
}

internal override void DisposeHandles()
{
base.DisposeHandles();
Handle?.Dispose();
}

public void SetSubscribed(bool subscribed)
{
Subscribed = subscribed;
Expand Down Expand Up @@ -85,8 +96,17 @@ public sealed class LocalTrackPublication : TrackPublication
{
public new ILocalTrack Track => base.Track as ILocalTrack;

internal LocalTrackPublication(TrackPublicationInfo info) : base(info)
private FfiHandle Handle;

internal LocalTrackPublication(TrackPublicationInfo info, FfiHandle handle) : base(info)
{
Handle = handle;
}

internal override void DisposeHandles()
{
base.DisposeHandles();
Handle?.Dispose();
}
}
}
1 change: 1 addition & 0 deletions Samples~/Meet/Assets/Runtime/MeetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private void Update()
private void OnDestroy()
{
_webCamTexture?.Stop();
_room?.Disconnect();
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion client-sdk-rust~
Loading