Skip to content

Commit

Permalink
Add GetSessionStatsReceived functionality (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
holofermes authored Dec 24, 2024
1 parent 9d8f58e commit 8789875
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Runtime/Scripts/Internal/FFIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal sealed class FfiClient : IFFIClient
public event UnpublishTrackDelegate? UnpublishTrackReceived;
public event ConnectReceivedDelegate? ConnectReceived;
public event DisconnectReceivedDelegate? DisconnectReceived;
public event GetSessionStatsDelegate? GetSessionStatsReceived;
public event RoomEventReceivedDelegate? RoomEventReceived;
public event TrackEventReceivedDelegate? TrackEventReceived;
public event RpcMethodInvocationReceivedDelegate? RpcMethodInvocationReceived;
Expand Down Expand Up @@ -248,6 +249,9 @@ static unsafe void FFICallback(UIntPtr data, UIntPtr size)
case FfiEvent.MessageOneofCase.Disconnect:
Instance.DisconnectReceived?.Invoke(r.Disconnect!);
break;
case FfiEvent.MessageOneofCase.GetStats:
Instance.GetSessionStatsReceived?.Invoke(r.GetStats);
break;
case FfiEvent.MessageOneofCase.PublishTranscription:
break;
case FfiEvent.MessageOneofCase.VideoStreamEvent:
Expand All @@ -261,7 +265,6 @@ static unsafe void FFICallback(UIntPtr data, UIntPtr size)
case FfiEvent.MessageOneofCase.PerformRpc:
Instance.PerformRpcReceived?.Invoke(r.PerformRpc!);
break;
case FfiEvent.MessageOneofCase.GetStats:
case FfiEvent.MessageOneofCase.Panic:
break;
default:
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Scripts/Internal/FFIClients/FFIEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace LiveKit.Internal

internal delegate void DisconnectReceivedDelegate(DisconnectCallback e);


internal delegate void GetSessionStatsDelegate(GetStatsCallback e);

// Events
internal delegate void RoomEventReceivedDelegate(RoomEvent e);

Expand Down
40 changes: 40 additions & 0 deletions Runtime/Scripts/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ public interface ITrack
WeakReference<Room> Room { get; }
WeakReference<Participant> Participant { get; }
FfiHandle TrackHandle { get; }

public GetSessionStatsInstruction GetStats()
{
using var request = FFIBridge.Instance.NewRequest<GetStatsRequest>();
var getStats = request.request;
getStats.TrackHandle = (ulong)TrackHandle.DangerousGetHandle();
using var response = request.Send();
FfiResponse res = response;
return new GetSessionStatsInstruction(res.GetStats.AsyncId);
}

}

public interface ILocalTrack : ITrack
Expand Down Expand Up @@ -157,4 +168,33 @@ public sealed class RemoteVideoTrack : Track, IRemoteTrack, IVideoTrack
{
internal RemoteVideoTrack(OwnedTrack track, Room room, RemoteParticipant participant) : base(track, room, participant) { }
}

public sealed class GetSessionStatsInstruction : YieldInstruction
{
private readonly ulong _asyncId;
public RtcStats[] Stats;
public string Error;

internal GetSessionStatsInstruction(ulong asyncId)
{
_asyncId = asyncId;
FfiClient.Instance.GetSessionStatsReceived += OnGetSessionStatsReceived;
}

private void OnGetSessionStatsReceived(GetStatsCallback e)
{
if (e.AsyncId != _asyncId)
return;

Error = e.Error;
IsError = !string.IsNullOrEmpty(Error);
IsDone = true;
Stats = new RtcStats[e.Stats.Count];
for (var i = 0; i < e.Stats.Count; i++)
{
Stats[i] = e.Stats[i];
}
FfiClient.Instance.GetSessionStatsReceived -= OnGetSessionStatsReceived;
}
}
}

0 comments on commit 8789875

Please sign in to comment.