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/dry-buttons-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/rtc-node": patch
---

ensure userdata is passed through when resampling
5 changes: 5 additions & 0 deletions .changeset/eleven-timers-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/rtc-node": minor
---

Add typeguards for frame processors in order to avoid dual package hazard
18 changes: 15 additions & 3 deletions packages/livekit-rtc/src/audio_frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ export class AudioFrame {
this._userdata = userdata;
}

static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame {
static create(
sampleRate: number,
channels: number,
samplesPerChannel: number,
userdata?: Record<string, unknown>,
): AudioFrame {
const data = new Int16Array(channels * samplesPerChannel);
return new AudioFrame(data, sampleRate, channels, samplesPerChannel);
return new AudioFrame(data, sampleRate, channels, samplesPerChannel, userdata);
}

/** @internal */
Expand Down Expand Up @@ -103,5 +108,12 @@ export const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFram
}

const data = new Int16Array(buffer.map((x) => [...x.data]).flat());
return new AudioFrame(data, sampleRate, channels, totalSamplesPerChannel);

// Merge userdata from all frames
const mergedUserdata: Record<string, unknown> = {};
for (const frame of buffer) {
Object.assign(mergedUserdata, frame.userdata);
}

return new AudioFrame(data, sampleRate, channels, totalSamplesPerChannel, mergedUserdata);
};
2 changes: 2 additions & 0 deletions packages/livekit-rtc/src/audio_resampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ export class AudioResampler {
}

const outputData = FfiClient.instance.copyBuffer(res.outputPtr, res.size!);

return [
new AudioFrame(
new Int16Array(outputData.buffer),
this.#outputRate,
this.#channels,
Math.trunc(outputData.length / this.#channels / 2),
data.userdata,
),
];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/livekit-rtc/src/audio_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { UnderlyingSource } from 'node:stream/web';
import { AudioFrame } from './audio_frame.js';
import type { FfiEvent } from './ffi_client.js';
import { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';
import { FrameProcessor } from './frame_processor.js';
import { type FrameProcessor, isAudioFrameProcessor } from './frame_processor.js';
import { log } from './log.js';
import type { NewAudioStreamResponse } from './proto/audio_frame_pb.js';
import { AudioStreamType, NewAudioStreamRequest } from './proto/audio_frame_pb.js';
Expand Down Expand Up @@ -41,7 +41,7 @@ class AudioStreamSource implements UnderlyingSource<AudioFrame> {
if (sampleRateOrOptions !== undefined && typeof sampleRateOrOptions !== 'number') {
this.sampleRate = sampleRateOrOptions.sampleRate ?? 48000;
this.numChannels = sampleRateOrOptions.numChannels ?? 1;
if (sampleRateOrOptions.noiseCancellation instanceof FrameProcessor) {
if (isAudioFrameProcessor(sampleRateOrOptions.noiseCancellation)) {
this.frameProcessor = sampleRateOrOptions.noiseCancellation;
} else {
this.legacyNcOptions = sampleRateOrOptions.noiseCancellation;
Expand Down
35 changes: 35 additions & 0 deletions packages/livekit-rtc/src/frame_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,42 @@ export type FrameProcessorCredentials = {
url: string;
};

export const FrameProcessorSymbol = Symbol.for('lk.frame-processor');

export function isFrameProcessor<Type extends 'audio' | 'video'>(
maybeProcessor: unknown,
type?: Type,
): maybeProcessor is FrameProcessor<
Type extends 'audio' ? AudioFrame : Type extends 'video' ? VideoFrame : AudioFrame | VideoFrame
> {
return (
maybeProcessor !== null &&
typeof maybeProcessor === 'object' &&
'symbol' in maybeProcessor &&
maybeProcessor.symbol === FrameProcessorSymbol &&
(!type || ('type' in maybeProcessor && maybeProcessor.type === type))
);
}

export function isAudioFrameProcessor(
maybeProcessor: unknown,
): maybeProcessor is FrameProcessor<AudioFrame> {
return isFrameProcessor(maybeProcessor, 'audio');
}

export function isVideoFrameProcessor(
maybeProcessor: unknown,
): maybeProcessor is FrameProcessor<VideoFrame> {
return isFrameProcessor(maybeProcessor, 'video');
}

export abstract class FrameProcessor<Frame extends VideoFrame | AudioFrame> {
readonly symbol = FrameProcessorSymbol;
Copy link
Contributor

@1egoman 1egoman Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Since think this means that it won't work with frame processors defined with old @livekit/rtc-node versions, make sure you don't forget to update the peerDependency version in the aic package after you've tested all this locally and are ready to release your changes!

abstract readonly type: Frame extends VideoFrame
? 'video'
: Frame extends AudioFrame
? 'audio'
: never;
abstract isEnabled(): boolean;
abstract setEnabled(enabled: boolean): void;

Expand Down
Loading