Skip to content

Commit

Permalink
Revert "Enable strict type checking and use proto2 ffi syntax (#303)"
Browse files Browse the repository at this point in the history
This reverts commit f204721.
  • Loading branch information
lukasIO committed Oct 23, 2024
1 parent f204721 commit a10348c
Show file tree
Hide file tree
Showing 29 changed files with 9,415 additions and 4,671 deletions.
5 changes: 0 additions & 5 deletions .changeset/forty-bottles-battle.md

This file was deleted.

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
PATH=$PATH:$(pwd)/node_modules/.bin \
protoc \

protoc \
-I=$FFI_PROTOCOL \
--es_out $FFI_OUT_NODE \
--es_opt target=ts \
Expand Down
3 changes: 1 addition & 2 deletions packages/livekit-rtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
}
},
"dependencies": {
"@bufbuild/protobuf": "^2.2.0",
"@bufbuild/protobuf": "^1.4.2",
"@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,10 +1,9 @@
// 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 { type AudioFrameBufferInfo, AudioFrameBufferInfoSchema } from './proto/audio_frame_pb.js';
import { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';

export class AudioFrame {
data: Int16Array;
Expand All @@ -31,10 +30,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 @@ -45,7 +44,7 @@ export class AudioFrame {

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

if (buffer.length === 0) {
throw new Error('buffer is empty');
Expand Down
19 changes: 9 additions & 10 deletions packages/livekit-rtc/src/audio_resampler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 @@ -10,9 +9,9 @@ import type {
PushSoxResamplerResponse,
} from './proto/audio_frame_pb.js';
import {
FlushSoxResamplerRequestSchema,
NewSoxResamplerRequestSchema,
PushSoxResamplerRequestSchema,
FlushSoxResamplerRequest,
NewSoxResamplerRequest,
PushSoxResamplerRequest,
SoxQualityRecipe,
SoxResamplerDataType,
} from './proto/audio_frame_pb.js';
Expand Down Expand Up @@ -59,7 +58,7 @@ export class AudioResampler {
this.#outputRate = outputRate;
this.#channels = channels;

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

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

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

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

Expand Down
17 changes: 8 additions & 9 deletions packages/livekit-rtc/src/audio_source.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 @@ -14,9 +13,9 @@ import type {
} from './proto/audio_frame_pb.js';
import {
AudioSourceType,
CaptureAudioFrameRequestSchema,
ClearAudioBufferRequestSchema,
NewAudioSourceRequestSchema,
CaptureAudioFrameRequest,
ClearAudioBufferRequest,
NewAudioSourceRequest,
} from './proto/audio_frame_pb.js';

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

const req = create(NewAudioSourceRequestSchema, {
const req = new NewAudioSourceRequest({
type: AudioSourceType.AUDIO_SOURCE_NATIVE,
sampleRate: sampleRate,
numChannels: numChannels,
Expand All @@ -60,8 +59,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 @@ -72,7 +71,7 @@ export class AudioSource {
}

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

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

const req = create(CaptureAudioFrameRequestSchema, {
const req = new CaptureAudioFrameRequest({
sourceHandle: this.ffiHandle.handle,
buffer: frame.protoInfo(),
});
Expand Down
11 changes: 5 additions & 6 deletions packages/livekit-rtc/src/audio_stream.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// 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, NewAudioStreamRequestSchema } from './proto/audio_frame_pb.js';
import { AudioStreamType, NewAudioStreamRequest } from './proto/audio_frame_pb.js';
import type { Track } from './track.js';

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

const req = create(NewAudioStreamRequestSchema, {
const req = new NewAudioStreamRequest({
type: AudioStreamType.AUDIO_STREAM_NATIVE,
trackHandle: track.ffi_handle.handle,
sampleRate: sampleRate,
Expand All @@ -45,8 +44,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 @@ -62,7 +61,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

0 comments on commit a10348c

Please sign in to comment.