Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable strict type checking #303

Merged
merged 10 commits into from
Oct 21, 2024
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/forty-bottles-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/rtc-node": patch
---

Enable strict type checking and update ffi to use proto2 syntax
4 changes: 2 additions & 2 deletions packages/livekit-rtc/generate_proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ FFI_PROTOCOL=./rust-sdks/livekit-ffi/protocol
FFI_OUT_NODE=./src/proto

# ffi

protoc \
PATH=$PATH:$(pwd)/node_modules/.bin \
protoc \
-I=$FFI_PROTOCOL \
--es_out $FFI_OUT_NODE \
--es_opt target=ts \
Expand Down
3 changes: 2 additions & 1 deletion packages/livekit-rtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
}
},
"dependencies": {
"@bufbuild/protobuf": "^1.4.2",
"@bufbuild/protobuf": "^2.2.0",
"@livekit/mutex": "^1.0.0",
"@livekit/typed-emitter": "^3.0.0"
},
"devDependencies": {
"@bufbuild/protoc-gen-es": "^2.2.0",
"@napi-rs/cli": "^2.18.0",
"@types/node": "^20.9.2",
"prettier": "^3.0.3",
Expand Down
12 changes: 6 additions & 6 deletions packages/livekit-rtc/src/audio_frame.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { create } from '@bufbuild/protobuf';
import { FfiClient, FfiHandle } from './ffi_client.js';
import type { OwnedAudioFrameBuffer } from './proto/audio_frame_pb.js';
import { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';
import { type AudioFrameBufferInfo, AudioFrameBufferInfoSchema } from './proto/audio_frame_pb.js';

export class AudioFrame {
data: Int16Array;
Expand All @@ -30,10 +31,10 @@ export class AudioFrame {

/** @internal */
static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame {
const info = owned.info;
const info = owned.info!;
const len = info.numChannels * info.samplesPerChannel * 2; // c_int16
const data = FfiClient.instance.copyBuffer(info.dataPtr, len);
new FfiHandle(owned.handle.id).dispose();
new FfiHandle(owned.handle!.id).dispose();
return new AudioFrame(
new Int16Array(data.buffer),
info.sampleRate,
Expand All @@ -44,7 +45,7 @@ export class AudioFrame {

/** @internal */
protoInfo(): AudioFrameBufferInfo {
return new AudioFrameBufferInfo({
return create(AudioFrameBufferInfoSchema, {
dataPtr: FfiClient.instance.retrievePtr(new Uint8Array(this.data.buffer)),
sampleRate: this.sampleRate,
numChannels: this.channels,
Expand All @@ -63,10 +64,9 @@ export class AudioFrame {
* @param buffer - a single AudioFrame or list thereof
*/
export const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFrame => {
if (!buffer['length']) {
if (!Array.isArray(buffer)) {
return buffer as AudioFrame;
}
buffer = buffer as AudioFrame[];

if (buffer.length === 0) {
throw new Error('buffer is empty');
Expand Down
19 changes: 10 additions & 9 deletions packages/livekit-rtc/src/audio_resampler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { create } from '@bufbuild/protobuf';
import { AudioFrame } from './audio_frame.js';
import { FfiClient, FfiHandle } from './ffi_client.js';
import type {
Expand All @@ -9,9 +10,9 @@ import type {
PushSoxResamplerResponse,
} from './proto/audio_frame_pb.js';
import {
FlushSoxResamplerRequest,
NewSoxResamplerRequest,
PushSoxResamplerRequest,
FlushSoxResamplerRequestSchema,
NewSoxResamplerRequestSchema,
PushSoxResamplerRequestSchema,
SoxQualityRecipe,
SoxResamplerDataType,
} from './proto/audio_frame_pb.js';
Expand Down Expand Up @@ -58,7 +59,7 @@ export class AudioResampler {
this.#outputRate = outputRate;
this.#channels = channels;

const req = new NewSoxResamplerRequest({
const req = create(NewSoxResamplerRequestSchema, {
inputRate,
outputRate,
numChannels: channels,
Expand All @@ -75,11 +76,11 @@ export class AudioResampler {
},
});

if (res.error) {
throw new Error(res.error);
if (res.message.case !== 'resampler') {
throw new Error(res.message.value ?? 'Unknown Error');
}

this.#ffiHandle = new FfiHandle(res.resampler.handle.id);
this.#ffiHandle = new FfiHandle(res.message.value.handle!.id);
}

/**
Expand All @@ -94,7 +95,7 @@ export class AudioResampler {
* be empty if no output data is available yet.
*/
push(data: AudioFrame): AudioFrame[] {
const req = new PushSoxResamplerRequest({
const req = create(PushSoxResamplerRequestSchema, {
resamplerHandle: this.#ffiHandle.handle,
dataPtr: data.protoInfo().dataPtr,
size: data.data.byteLength,
Expand Down Expand Up @@ -134,7 +135,7 @@ export class AudioResampler {
* internal buffers are processed and all resampled data is output.
*/
flush(): AudioFrame[] {
const req = new FlushSoxResamplerRequest({
const req = create(FlushSoxResamplerRequestSchema, {
resamplerHandle: this.#ffiHandle.handle,
});

Expand Down
17 changes: 9 additions & 8 deletions packages/livekit-rtc/src/audio_source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { create } from '@bufbuild/protobuf';
import type { AudioFrame } from './audio_frame.js';
import { FfiClient } from './ffi_client.js';
import { FfiHandle } from './napi/native.js';
Expand All @@ -13,9 +14,9 @@ import type {
} from './proto/audio_frame_pb.js';
import {
AudioSourceType,
CaptureAudioFrameRequest,
ClearAudioBufferRequest,
NewAudioSourceRequest,
CaptureAudioFrameRequestSchema,
ClearAudioBufferRequestSchema,
NewAudioSourceRequestSchema,
} from './proto/audio_frame_pb.js';

export class AudioSource {
Expand Down Expand Up @@ -45,7 +46,7 @@ export class AudioSource {
this.lastCapture = 0;
this.currentQueueSize = 0;

const req = new NewAudioSourceRequest({
const req = create(NewAudioSourceRequestSchema, {
type: AudioSourceType.AUDIO_SOURCE_NATIVE,
sampleRate: sampleRate,
numChannels: numChannels,
Expand All @@ -59,8 +60,8 @@ export class AudioSource {
},
});

this.info = res.source.info;
this.ffiHandle = new FfiHandle(res.source.handle.id);
this.info = res.source!.info!;
this.ffiHandle = new FfiHandle(res.source!.handle!.id);
}

get queuedDuration(): number {
Expand All @@ -71,7 +72,7 @@ export class AudioSource {
}

clearQueue() {
const req = new ClearAudioBufferRequest({
const req = create(ClearAudioBufferRequestSchema, {
sourceHandle: this.ffiHandle.handle,
});

Expand Down Expand Up @@ -116,7 +117,7 @@ export class AudioSource {
// (e.g. using wait_for_playout for very small chunks)
this.timeout = setTimeout(this.release, this.currentQueueSize - 50);

const req = new CaptureAudioFrameRequest({
const req = create(CaptureAudioFrameRequestSchema, {
sourceHandle: this.ffiHandle.handle,
buffer: frame.protoInfo(),
});
Expand Down
11 changes: 6 additions & 5 deletions packages/livekit-rtc/src/audio_stream.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { create } from '@bufbuild/protobuf';
import { Mutex } from '@livekit/mutex';
import { AudioFrame } from './audio_frame.js';
import type { FfiEvent } from './ffi_client.js';
import { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';
import type { AudioStreamInfo, NewAudioStreamResponse } from './proto/audio_frame_pb.js';
import { AudioStreamType, NewAudioStreamRequest } from './proto/audio_frame_pb.js';
import { AudioStreamType, NewAudioStreamRequestSchema } from './proto/audio_frame_pb.js';
import type { Track } from './track.js';

export class AudioStream implements AsyncIterableIterator<AudioFrame> {
Expand All @@ -30,7 +31,7 @@ export class AudioStream implements AsyncIterableIterator<AudioFrame> {
this.sampleRate = sampleRate;
this.numChannels = numChannels;

const req = new NewAudioStreamRequest({
const req = create(NewAudioStreamRequestSchema, {
type: AudioStreamType.AUDIO_STREAM_NATIVE,
trackHandle: track.ffi_handle.handle,
sampleRate: sampleRate,
Expand All @@ -44,8 +45,8 @@ export class AudioStream implements AsyncIterableIterator<AudioFrame> {
},
});

this.info = res.stream.info;
this.ffiHandle = new FfiHandle(res.stream.handle.id);
this.info = res.stream!.info!;
this.ffiHandle = new FfiHandle(res.stream!.handle!.id);

FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onEvent);
}
Expand All @@ -61,7 +62,7 @@ export class AudioStream implements AsyncIterableIterator<AudioFrame> {
const streamEvent = ev.message.value.message;
switch (streamEvent.case) {
case 'frameReceived':
const frame = AudioFrame.fromOwnedInfo(streamEvent.value.frame);
const frame = AudioFrame.fromOwnedInfo(streamEvent.value.frame!);
if (this.queueResolve) {
this.queueResolve({ done: false, value: frame });
} else {
Expand Down
Loading