Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowLp174 committed Oct 4, 2022
1 parent 918f0a7 commit b4febca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
39 changes: 9 additions & 30 deletions Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ class Media {
this.socket = require("dgram").createSocket("udp4");
this.socket.bind(port);

const _this = this;
this.opusPackets = new require("stream").Readable({
read: async function() {
this.push(await _this.getRtpMessage());
}
});
this.opusPackets.on("data", (packet) => {
packetHandler(packet); // defined in the constructor params
});
this.socket.on("message", (packet) => {
packetHandler(packet); // defined in constructor params
})

this.port = port;
this.logs = logs;
Expand All @@ -44,14 +38,6 @@ class Media {
once(event, cb) {
return "Unimplemented";
}

getRtpMessage() {
return new Promise((res) => {
this.socket.once("message", (msg) => {
res(msg);
});
});
}
createFfmpegArgs(start="00:00:00") {
return ["-re", "-i", "-", "-ss", start, "-map", "0:a", "-b:a", "48k", "-maxrate", "48k", "-c:a", "libopus", "-f", "rtp", "rtp://127.0.0.1:" + this.port]
}
Expand All @@ -75,7 +61,6 @@ class Media {
return new Promise((res, rej) => {
this.track = null;
this.ffmpeg.kill();
this.opusPackets.destroy();
this.socket.close(res);
});
}
Expand All @@ -84,6 +69,10 @@ class Media {
class MediaPlayer extends Media {
constructor(logs=false, port=5030) {
super(logs, port, (packet) => {
if (!this.started) {
this.started = true;
this.emit("start");
}
if (this.paused) {
return this._save(packet);
}
Expand Down Expand Up @@ -163,10 +152,7 @@ class MediaPlayer extends Media {
}
this.packets = [];
this.intervals = [];
this.opusPackets.once("data", () => {
this.started = true;
this.emit("start");
});
this.started = false
if (f) this.#setupFmpeg();
}
destroy() {
Expand Down Expand Up @@ -209,10 +195,7 @@ class MediaPlayer extends Media {

this.packets = [];
this.intervals = [];
this.opusPackets.once("data", () => {
this.started = true;
this.emit("start");
});
this.started = false;
this.track = new MediaStreamTrack({ kind: "audio" });
this.emit("finish");
res();
Expand Down Expand Up @@ -245,10 +228,6 @@ class MediaPlayer extends Media {
this.originStream.on("end", () => {
this.streamFinished = true;
});
this.opusPackets.once("data", () => {
this.started = true;
this.emit("start");
});

// ffmpeg stuff
this.#setupFmpeg();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "revoice.js",
"version": "0.1.733",
"version": "0.1.734",
"description": "A voice module for Revolt",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit b4febca

Please sign in to comment.