Skip to content

Commit

Permalink
fix the mess, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nbsp committed Dec 29, 2024
1 parent 0115aae commit 0dd8462
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 131 deletions.
16 changes: 8 additions & 8 deletions packages/livekit-rtc/src/audio_frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export class AudioFrame {

/** @internal */
static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame {
const info = owned.info;
const len = info.numChannels * info.samplesPerChannel * 2; // c_int16
const data = FfiClient.instance.copyBuffer(info.dataPtr, len);
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();
return new AudioFrame(
new Int16Array(data.buffer),
info.sampleRate,
info.numChannels,
info.samplesPerChannel,
info.sampleRate!,
info.numChannels!,
info.samplesPerChannel!,
);
}

Expand All @@ -63,8 +63,8 @@ export class AudioFrame {
* @param buffer - a single AudioFrame or list thereof
*/
export const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFrame => {
if (!buffer['length']) {
return buffer as AudioFrame;
if (!Array.isArray(buffer)) {
return buffer;
}
buffer = buffer as AudioFrame[];

Expand Down
16 changes: 10 additions & 6 deletions packages/livekit-rtc/src/audio_resampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ export class AudioResampler {
},
});

if (res.error) {
throw new Error(res.error);
}

this.#ffiHandle = new FfiHandle(res.resampler.handle.id);
switch (res.message.case) {
case 'resampler':
this.#ffiHandle = new FfiHandle(res.message.value.handle.id);
break;
case 'error':
default:
throw new Error(res.message.value);
}
}

/**
Expand Down Expand Up @@ -115,7 +119,7 @@ export class AudioResampler {
return [];
}

const outputData = FfiClient.instance.copyBuffer(res.outputPtr, res.size);
const outputData = FfiClient.instance.copyBuffer(res.outputPtr, res.size!);
return [
new AudioFrame(
new Int16Array(outputData.buffer),
Expand Down Expand Up @@ -153,7 +157,7 @@ export class AudioResampler {
return [];
}

const outputData = FfiClient.instance.copyBuffer(res.outputPtr, res.size);
const outputData = FfiClient.instance.copyBuffer(res.outputPtr, res.size!);
return [
new AudioFrame(
new Int16Array(outputData.buffer),
Expand Down
6 changes: 3 additions & 3 deletions packages/livekit-rtc/src/audio_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

export class AudioSource {
/** @internal */
info: AudioSourceInfo;
info?: AudioSourceInfo;
/** @internal */
ffiHandle: FfiHandle;
/** @internal */
Expand Down Expand Up @@ -59,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 Down
6 changes: 3 additions & 3 deletions packages/livekit-rtc/src/audio_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Track } from './track.js';

export class AudioStream implements AsyncIterableIterator<AudioFrame> {
/** @internal */
info: AudioStreamInfo;
info?: AudioStreamInfo;
/** @internal */
ffiHandle: FfiHandle;
/** @internal */
Expand Down Expand Up @@ -44,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 Down
23 changes: 11 additions & 12 deletions packages/livekit-rtc/src/e2ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class KeyProvider {
},
});

return (res.message.value as GetSharedKeyResponse).key;
return (res.message.value as GetSharedKeyResponse).key!;
}

ratchetSharedKey(keyIndex: number): Uint8Array {
Expand All @@ -120,7 +120,7 @@ export class KeyProvider {
},
});

return (res.message.value as RatchetSharedKeyResponse).newKey;
return (res.message.value as RatchetSharedKeyResponse).newKey!;
}

setKey(participantIdentity: string, keyIndex: number) {
Expand Down Expand Up @@ -162,7 +162,7 @@ export class KeyProvider {
},
});

return (res.message.value as GetKeyResponse).key;
return (res.message.value as GetKeyResponse).key!;
}

ratchetKey(participantIdentity: string, keyIndex: number): Uint8Array {
Expand All @@ -184,7 +184,7 @@ export class KeyProvider {
},
});

return (res.message.value as RatchetKeyResponse).newKey;
return (res.message.value as RatchetKeyResponse).newKey!;
}
}

Expand Down Expand Up @@ -255,12 +255,11 @@ export class E2EEManager {
this.roomHandle = roomHandle;
this.enabled = opts !== undefined;

if (opts !== undefined) {
const options = { ...defaultE2EEOptions, ...opts };
opts ??= defaultE2EEOptions;
const options = { ...defaultE2EEOptions, ...opts };

this.options = options;
this.keyProvider = new KeyProvider(roomHandle, options.keyProviderOptions);
}
this.options = options;
this.keyProvider = new KeyProvider(roomHandle, options.keyProviderOptions);
}

setEnabled(enabled: boolean) {
Expand Down Expand Up @@ -305,9 +304,9 @@ export class E2EEManager {
(cryptor) =>
new FrameCryptor(
this.roomHandle,
cryptor.participantIdentity,
cryptor.keyIndex,
cryptor.enabled,
cryptor.participantIdentity!,
cryptor.keyIndex!,
cryptor.enabled!,
),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/livekit-rtc/src/ffi_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { FfiEvent, FfiRequest, FfiResponse } from './proto/ffi_pb.js';
import { SDK_VERSION } from './version.js';

export { FfiHandle, type FfiEvent, type FfiResponse, type FfiRequest, livekitDispose as dispose };
export { FfiHandle, type FfiEvent, type FfiResponse, FfiRequest, livekitDispose as dispose };

export type FfiClientCallbacks = {
ffi_event: (event: FfiEvent) => void;
Expand Down
94 changes: 61 additions & 33 deletions packages/livekit-rtc/src/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,18 @@ import {
SetLocalNameRequest,
UnpublishTrackRequest,
} from './proto/room_pb.js';
import { PerformRpcCallback, PerformRpcRequest, PerformRpcResponse, RegisterRpcMethodRequest, RegisterRpcMethodResponse, RpcMethodInvocationResponseRequest, RpcMethodInvocationResponseResponse, UnregisterRpcMethodRequest, UnregisterRpcMethodResponse } from './proto/rpc_pb.js';
import { RpcError, type PerformRpcParams, type RpcInvocationData } from './rpc.js';
import {
PerformRpcCallback,
PerformRpcRequest,
PerformRpcResponse,
RegisterRpcMethodRequest,
RegisterRpcMethodResponse,
RpcMethodInvocationResponseRequest,
RpcMethodInvocationResponseResponse,
UnregisterRpcMethodRequest,
UnregisterRpcMethodResponse,
} from './proto/rpc_pb.js';
import { type PerformRpcParams, RpcError, type RpcInvocationData } from './rpc.js';
import type { LocalTrack } from './track.js';
import type { RemoteTrackPublication, TrackPublication } from './track_publication.js';
import { LocalTrackPublication } from './track_publication.js';
Expand All @@ -48,7 +58,7 @@ import type { ChatMessage } from './types.js';

export abstract class Participant {
/** @internal */
info: ParticipantInfo;
info?: ParticipantInfo;

/** @internal */
ffi_handle: FfiHandle;
Expand All @@ -60,28 +70,28 @@ export abstract class Participant {
this.ffi_handle = new FfiHandle(owned_info.handle.id);
}

get sid(): string {
return this.info.sid;
get sid(): string | undefined {
return this.info?.sid;
}

get name(): string {
return this.info.name;
get name(): string | undefined {
return this.info?.name;
}

get identity(): string {
return this.info.identity;
get identity(): string | undefined {
return this.info?.identity;
}

get metadata(): string {
return this.info.metadata;
get metadata(): string | undefined {
return this.info?.metadata;
}

get attributes(): Record<string, string> {
return this.info.attributes;
get attributes(): Record<string, string> | undefined {
return this.info?.attributes;
}

get kind(): ParticipantKind {
return this.info.kind;
get kind(): ParticipantKind | undefined {
return this.info?.kind;
}
}

Expand Down Expand Up @@ -223,11 +233,19 @@ export class LocalParticipant extends Participant {
return ev.message.case == 'chatMessage' && ev.message.value.asyncId == res.asyncId;
});

if (cb.error) {
throw new Error(cb.error);
switch (cb.message.case) {
case 'chatMessage':
const { id, timestamp, editTimestamp, message } = cb.message.value!;
return {
id: id!,
timestamp: Number(timestamp),
editTimestamp: Number(editTimestamp),
message: message!,
};
case 'error':
default:
throw new Error(cb.message.value);
}
const { id, timestamp, editTimestamp, message } = cb.chatMessage!;
return { id, timestamp: Number(timestamp), editTimestamp: Number(editTimestamp), message };
}

/**
Expand Down Expand Up @@ -261,11 +279,19 @@ export class LocalParticipant extends Participant {
return ev.message.case == 'chatMessage' && ev.message.value.asyncId == res.asyncId;
});

if (cb.error) {
throw new Error(cb.error);
switch (cb.message.case) {
case 'chatMessage':
const { id, timestamp, editTimestamp, message } = cb.message.value!;
return {
id: id!,
timestamp: Number(timestamp),
editTimestamp: Number(editTimestamp),
message: message!,
};
case 'error':
default:
throw new Error(cb.message.value);
}
const { id, timestamp, editTimestamp, message } = cb.chatMessage!;
return { id, timestamp: Number(timestamp), editTimestamp: Number(editTimestamp), message };
}

async updateName(name: string) {
Expand All @@ -286,7 +312,7 @@ export class LocalParticipant extends Participant {
async setAttributes(attributes: Record<string, string>) {
const req = new SetLocalAttributesRequest({
localParticipantHandle: this.ffi_handle.handle,
attributes: attributes,
attributes: Object.entries(attributes).map(([key, value]) => ({ key, value })),
});

const res = FfiClient.instance.request<SetLocalAttributesResponse>({
Expand Down Expand Up @@ -316,15 +342,17 @@ export class LocalParticipant extends Participant {
return ev.message.case == 'publishTrack' && ev.message.value.asyncId == res.asyncId;
});

if (cb.error) {
throw new Error(cb.error);
}
switch (cb.message.case) {
case 'publication':
const track_publication = new LocalTrackPublication(cb.message.value!);
track_publication.track = track;
this.trackPublications.set(track_publication.sid!, track_publication);

const track_publication = new LocalTrackPublication(cb.publication!);
track_publication.track = track;
this.trackPublications.set(track_publication.sid, track_publication);

return track_publication;
return track_publication;
case 'error':
default:
throw new Error(cb.message.value);
}
}

async unpublishTrack(trackSid: string) {
Expand Down Expand Up @@ -384,7 +412,7 @@ export class LocalParticipant extends Participant {
throw RpcError.fromProto(cb.error);
}

return cb.payload;
return cb.payload!;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/livekit-rtc/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class RpcError extends Error {
}

static fromProto(proto: RpcError_Proto) {
return new RpcError(proto.code, proto.message, proto.data);
return new RpcError(proto.code!, proto.message!, proto.data);
}

toProto() {
Expand Down
Loading

0 comments on commit 0dd8462

Please sign in to comment.