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
29 changes: 7 additions & 22 deletions agents/src/voice/room_io/_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import {
type AudioFrame,
AudioStream,
FrameProcessor,
type FrameProcessor,
type NoiseCancellationOptions,
RemoteParticipant,
type RemoteTrack,
type RemoteTrackPublication,
type Room,
RoomEvent,
TrackSource,
isFrameProcessor,
} from '@livekit/rtc-node';
import type { ReadableStream } from 'node:stream/web';
import { log } from '../../log.js';
Expand Down Expand Up @@ -44,15 +45,14 @@ export class ParticipantAudioInputStream extends AudioInput {
this.room = room;
this.sampleRate = sampleRate;
this.numChannels = numChannels;
if (noiseCancellation instanceof FrameProcessor) {
if (isFrameProcessor<FrameProcessor<AudioFrame>>(noiseCancellation)) {
this.frameProcessor = noiseCancellation;
} else {
this.noiseCancellation = noiseCancellation;
}

this.room.on(RoomEvent.TrackSubscribed, this.onTrackSubscribed);
this.room.on(RoomEvent.TrackUnpublished, this.onTrackUnpublished);
this.room.on(RoomEvent.TokenRefreshed, this.onTokenRefreshed);
}

setParticipant(participant: RemoteParticipant | string | null) {
Expand Down Expand Up @@ -153,40 +153,25 @@ export class ParticipantAudioInputStream extends AudioInput {
outputRate: this.sampleRate,
}),
);
this.frameProcessor?.onStreamInfoUpdated({
participantIdentity: participant.identity,
roomName: this.room.name!,
publicationSid: publication.sid!,
});
this.frameProcessor?.onCredentialsUpdated({
token: this.room.token!,
url: this.room.serverUrl!,
});
return true;
};

private onTokenRefreshed = () => {
if (this.room.token && this.room.serverUrl) {
this.frameProcessor?.onCredentialsUpdated({
token: this.room.token,
url: this.room.serverUrl,
});
}
};

private createStream(track: RemoteTrack): ReadableStream<AudioFrame> {
return new AudioStream(track, {
sampleRate: this.sampleRate,
numChannels: this.numChannels,
noiseCancellation: this.frameProcessor || this.noiseCancellation,
// Don't let the AudioStream close the processor when the track switches —
// this input stream owns the processor across track changes and closes it
// itself in close().
autoCloseNoiseCancellation: false,
Comment thread
1egoman marked this conversation as resolved.
// TODO(AJS-269): resolve compatibility issue with node-sdk to remove the forced type casting
}) as unknown as ReadableStream<AudioFrame>;
Comment thread
1egoman marked this conversation as resolved.
}

override async close() {
this.room.off(RoomEvent.TrackSubscribed, this.onTrackSubscribed);
this.room.off(RoomEvent.TrackUnpublished, this.onTrackUnpublished);
this.room.off(RoomEvent.TokenRefreshed, this.onTokenRefreshed);
this.closeStream();
await super.close();

Expand Down
Loading
Loading