Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/selfish-numbers-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/rtc-node": patch
---

Add method to query rtcStats on room via `await room.getRtcStats()`
6 changes: 6 additions & 0 deletions packages/livekit-rtc/src/ffi_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class FfiClient extends (EventEmitter as new () => TypedEmitter<FfiClient
return globalThis._ffiClientInstance;
}

private _nextRequestAsyncId = BigInt(0);

constructor() {
super();
this.setMaxListeners(0);
Expand Down Expand Up @@ -70,6 +72,10 @@ export class FfiClient extends (EventEmitter as new () => TypedEmitter<FfiClient
return livekitRetrievePtr(data);
}

getNextRequestAsyncId() {
return ++this._nextRequestAsyncId;
}

async waitFor<T>(
predicate: (ev: FfiEvent) => boolean,
options?: { signal?: AbortSignal },
Expand Down
36 changes: 35 additions & 1 deletion packages/livekit-rtc/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// SPDX-License-Identifier: Apache-2.0
import { Mutex } from '@livekit/mutex';
import { EncryptionState, type EncryptionType } from '@livekit/rtc-ffi-bindings';
import type { FfiEvent } from '@livekit/rtc-ffi-bindings';
import type {
FfiEvent,
GetSessionStatsCallback,
GetSessionStatsResponse,
} from '@livekit/rtc-ffi-bindings';
import { DisconnectReason, type OwnedParticipant } from '@livekit/rtc-ffi-bindings';
import type {
DataStream_Trailer,
Expand Down Expand Up @@ -191,6 +195,36 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
return this.sidPromise;
}

async getRtcStats() {
if (!this.isConnected || !this.ffiHandle) {
throw new Error('getRtcStats requires a connected room');
}
// subscribe before issuing the request
const requestId = FfiClient.instance.getNextRequestAsyncId();
const cbPromise = FfiClient.instance.waitFor<GetSessionStatsCallback>(
(ev: FfiEvent) =>
ev.message.case === 'getSessionStats' && ev.message.value.asyncId === requestId,
{ signal: this.disconnectController.signal },
);
FfiClient.instance.request<GetSessionStatsResponse>({
message: {
case: 'getSessionStats',
value: {
roomHandle: this.ffiHandle.handle,
requestAsyncId: requestId,
},
},
});
const cb = await cbPromise;
if (cb.message.case === 'error') {
throw new Error(cb.message.value);
} else if (cb.message.case === 'result') {
return cb.message.value;
} else {
throw new Error('could not retrieve rtc stats');
}
}

get numParticipants(): number {
return this.info?.numParticipants ?? 0;
}
Expand Down
Loading