Skip to content

Commit 8789875

Browse files
authored
Add GetSessionStatsReceived functionality (#74)
1 parent 9d8f58e commit 8789875

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Runtime/Scripts/Internal/FFIClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal sealed class FfiClient : IFFIClient
3535
public event UnpublishTrackDelegate? UnpublishTrackReceived;
3636
public event ConnectReceivedDelegate? ConnectReceived;
3737
public event DisconnectReceivedDelegate? DisconnectReceived;
38+
public event GetSessionStatsDelegate? GetSessionStatsReceived;
3839
public event RoomEventReceivedDelegate? RoomEventReceived;
3940
public event TrackEventReceivedDelegate? TrackEventReceived;
4041
public event RpcMethodInvocationReceivedDelegate? RpcMethodInvocationReceived;
@@ -248,6 +249,9 @@ static unsafe void FFICallback(UIntPtr data, UIntPtr size)
248249
case FfiEvent.MessageOneofCase.Disconnect:
249250
Instance.DisconnectReceived?.Invoke(r.Disconnect!);
250251
break;
252+
case FfiEvent.MessageOneofCase.GetStats:
253+
Instance.GetSessionStatsReceived?.Invoke(r.GetStats);
254+
break;
251255
case FfiEvent.MessageOneofCase.PublishTranscription:
252256
break;
253257
case FfiEvent.MessageOneofCase.VideoStreamEvent:
@@ -261,7 +265,6 @@ static unsafe void FFICallback(UIntPtr data, UIntPtr size)
261265
case FfiEvent.MessageOneofCase.PerformRpc:
262266
Instance.PerformRpcReceived?.Invoke(r.PerformRpc!);
263267
break;
264-
case FfiEvent.MessageOneofCase.GetStats:
265268
case FfiEvent.MessageOneofCase.Panic:
266269
break;
267270
default:

Runtime/Scripts/Internal/FFIClients/FFIEvents.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace LiveKit.Internal
2020

2121
internal delegate void DisconnectReceivedDelegate(DisconnectCallback e);
2222

23-
23+
internal delegate void GetSessionStatsDelegate(GetStatsCallback e);
24+
2425
// Events
2526
internal delegate void RoomEventReceivedDelegate(RoomEvent e);
2627

Runtime/Scripts/Track.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ public interface ITrack
1515
WeakReference<Room> Room { get; }
1616
WeakReference<Participant> Participant { get; }
1717
FfiHandle TrackHandle { get; }
18+
19+
public GetSessionStatsInstruction GetStats()
20+
{
21+
using var request = FFIBridge.Instance.NewRequest<GetStatsRequest>();
22+
var getStats = request.request;
23+
getStats.TrackHandle = (ulong)TrackHandle.DangerousGetHandle();
24+
using var response = request.Send();
25+
FfiResponse res = response;
26+
return new GetSessionStatsInstruction(res.GetStats.AsyncId);
27+
}
28+
1829
}
1930

2031
public interface ILocalTrack : ITrack
@@ -157,4 +168,33 @@ public sealed class RemoteVideoTrack : Track, IRemoteTrack, IVideoTrack
157168
{
158169
internal RemoteVideoTrack(OwnedTrack track, Room room, RemoteParticipant participant) : base(track, room, participant) { }
159170
}
171+
172+
public sealed class GetSessionStatsInstruction : YieldInstruction
173+
{
174+
private readonly ulong _asyncId;
175+
public RtcStats[] Stats;
176+
public string Error;
177+
178+
internal GetSessionStatsInstruction(ulong asyncId)
179+
{
180+
_asyncId = asyncId;
181+
FfiClient.Instance.GetSessionStatsReceived += OnGetSessionStatsReceived;
182+
}
183+
184+
private void OnGetSessionStatsReceived(GetStatsCallback e)
185+
{
186+
if (e.AsyncId != _asyncId)
187+
return;
188+
189+
Error = e.Error;
190+
IsError = !string.IsNullOrEmpty(Error);
191+
IsDone = true;
192+
Stats = new RtcStats[e.Stats.Count];
193+
for (var i = 0; i < e.Stats.Count; i++)
194+
{
195+
Stats[i] = e.Stats[i];
196+
}
197+
FfiClient.Instance.GetSessionStatsReceived -= OnGetSessionStatsReceived;
198+
}
199+
}
160200
}

0 commit comments

Comments
 (0)