Skip to content

Commit

Permalink
v0.1.0 strict mode on voice
Browse files Browse the repository at this point in the history
  • Loading branch information
itzTheMeow committed Sep 16, 2023
1 parent a4c4967 commit 934bc64
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 42 deletions.
2 changes: 1 addition & 1 deletion voice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Voice clients for browser and node.

[![npm](https://img.shields.io/npm/dt/@revkit/voice?label=Downloads&style=flat-square&color=ff4654)](https://www.npmjs.com/package/@revkit/voice)

Unfinished voice module.
Almost finished voice module.
4 changes: 2 additions & 2 deletions voice/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@revkit/voice",
"version": "0.0.8",
"description": "Voice support for browser/node using revkit-based clients.",
"version": "0.1.0",
"description": "Voice library for Revolt that works in the browser and node.js!",
"main": "dist/cjs/index.js",
"module": "dist/es6/index.js",
"types": "dist/es6/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion voice/src/Signaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class Signaling<Platform extends MSCPlatform> extends EventEmitte

/** Disconnects the websocket. */
disconnect() {
if (this.connected) this.ws.close(1000);
if (this.connected) this.ws?.close(1000);
}

private parseData(event: WebSocket.MessageEvent) {
Expand Down
40 changes: 12 additions & 28 deletions voice/src/VoiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class VoiceClient<
public client = new Client();
public channelID: string | null = null;
public get channel() {
return this.client.channels.get(this.channelID);
return this.channelID ? this.client.channels.get(this.channelID) : undefined;
}

/**
Expand Down Expand Up @@ -117,17 +117,8 @@ export class VoiceClient<
}
case WSEvents.UserStartProduce: {
const participant = this.participants.get(data.id);
if (!participant || participant.user.id == this.client.user.id) return;
if (<any>data.type in participant) {
participant[data.type] = true;
} else {
return this.emit(
"error",
new Error(
`Invalid produce type ${data.type} for ${participant.user.username} (${participant.user.id})`
)
);
}
if (!participant || participant.user.id == this.client.user.id || !data.type) return;
if (data.type in participant) participant[data.type] = true;
this.participants.fireUpdate([participant]);

if (this.recvTransport) await this.startConsume(data.id, data.type);
Expand All @@ -136,17 +127,8 @@ export class VoiceClient<
}
case WSEvents.UserStopProduce: {
const participant = this.participants.get(data.id);
if (!participant) return;
if (<any>data.type in participant) {
participant[data.type] = false;
} else {
return this.emit(
"error",
new Error(
`Invalid produce type ${data.type} for ${participant.user.username} (${participant.user.id})`
)
);
}
if (!participant || !data.type) return;
if (data.type in participant) participant[data.type] = false;
this.participants.fireUpdate([participant]);

if (this.recvTransport) this.stopConsume(data.id, data.type);
Expand Down Expand Up @@ -210,8 +192,8 @@ export class VoiceClient<

if (this.sendTransport) this.sendTransport.close();
if (this.recvTransport) this.recvTransport.close();
this.sendTransport = undefined;
this.recvTransport = undefined;
this.sendTransport = null;
this.recvTransport = null;

this.emit("close", error);
}
Expand Down Expand Up @@ -354,7 +336,7 @@ export class VoiceClient<
}

public async stopProduce(type: ProduceType) {
let producer: MSC["Producer"];
let producer: MSC["Producer"] | undefined;
switch (type) {
case "audio":
producer = this.audioProducer;
Expand All @@ -376,7 +358,7 @@ export class VoiceClient<

try {
await this.signaling.stopProduce(type);
} catch (error) {
} catch (error: any) {
if (error.error === "ProducerNotFound") return;
this.emit("error", error);
}
Expand Down Expand Up @@ -428,7 +410,7 @@ export class VoiceClient<
this.setStatus(VoiceStatus.RTC_CONNECTING);
await this.initializeTransports();
} catch (err) {
this.emit("error", err);
this.emit("error", err instanceof Error ? err : new Error(String(err)));
this.setStatus(VoiceStatus.READY);
return this;
}
Expand All @@ -452,6 +434,7 @@ export class VoiceClient<
}
}

/** Deafens yourself and stops consuming audio from participants. */
public async startDeafen() {
if (this.deafened) return;
this._deafened = true;
Expand All @@ -460,6 +443,7 @@ export class VoiceClient<
});
this.emit("selfDeafenUpdate", true);
}
/** Undeafens yourself and restarts consuming audio from participants. */
public async stopDeafen() {
if (!this.deafened) return;
this._deafened = false;
Expand Down
1 change: 1 addition & 0 deletions voice/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const DEFAULT_CONSUMER: VoiceClientConsumer<"browser"> = (type, track) =>
audio.play();
return () => audio.pause();
}
return () => {};
};

/**
Expand Down
20 changes: 12 additions & 8 deletions voice/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export default class VoiceClient extends BaseVoiceClient<"node"> {
this.port = await MSC.findPort(PORT_MIN, Math.floor(PORT_MAX / 2), "udp4");
}

private transcoder: FFmpeg;
private volumeTransformer: VolumeTransformer;
private encoder: FFmpeg;
private transcoder?: FFmpeg;
private volumeTransformer?: VolumeTransformer;
private encoder?: FFmpeg;

/** Current volume of the player. */
public get volume() {
Expand Down Expand Up @@ -135,7 +135,7 @@ export default class VoiceClient extends BaseVoiceClient<"node"> {
if ((<any>err).code == "EPIPE") return;
this.emit("error", err);
});
this.transcoder.process.stderr.on("data", () => {});
this.transcoder.process.stderr?.on("data", () => {});
} else delete this.transcoder;
if (this.volumeTransformer) this.volumeTransformer.destroy();
if (respawn) {
Expand Down Expand Up @@ -173,13 +173,17 @@ export default class VoiceClient extends BaseVoiceClient<"node"> {
this.emit("error", err);
});
// fixes stream stopping after 4min
this.encoder.process.stderr.on("data", () => {});
this.encoder.process.stderr?.on("data", () => {});
} else delete this.encoder;
}

public async play(type: ProduceType, stream: Readable) {
await this.resetPort();
this.reset();

if (!this.transcoder || !this.volumeTransformer || !this.encoder)
throw new Error("Transcoders failed to start.");

switch (type) {
case "audio": {
// create track sent to vortex
Expand Down Expand Up @@ -255,17 +259,17 @@ a=rtpmap:${RTP_PAYLOAD_TYPE} opus/${this.options.sampleRate}/${this.options.audi
this.emit("error", err);
});

const track = producer[type].consumer.track;
const track = producer[type]?.consumer.track;
if (!track) return null;
if (track.onReceiveRtp.ended) return; // if the user unmuted and muted too quickly
if (track.onReceiveRtp.ended) return null; // if the user unmuted and muted too quickly

track.onReceiveRtp.subscribe((packet) => {
// if the decoder is destroyed, stop sending packets to it
if (decoder.destroyed) return cancel(participant, type);
try {
socket.send(packet.serialize(), port, "127.0.0.1");
} catch (err) {
this.emit("error", err);
this.emit("error", err instanceof Error ? err : new Error(String(err)));
}
});

Expand Down
3 changes: 2 additions & 1 deletion voice/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"outDir": "dist/cjs",
"esModuleInterop": true,
"declaration": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
},
"include": ["src"]
}
3 changes: 2 additions & 1 deletion voice/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"esModuleInterop": true,
"declaration": true,
"skipLibCheck": true,
"moduleResolution": "Node"
"moduleResolution": "Node",
"strict": true
},
"include": ["src"]
}

0 comments on commit 934bc64

Please sign in to comment.