Skip to content

Commit c22fbd1

Browse files
committed
Initial Support for Spacebar WebRTC
Signed-off-by: Christopher Lentocha <[email protected]>
1 parent dd52fbf commit c22fbd1

18 files changed

+358
-126
lines changed

package-lock.json

+172
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,19 @@
116116
"@spacebar/api": "dist/api",
117117
"@spacebar/cdn": "dist/cdn",
118118
"@spacebar/gateway": "dist/gateway",
119-
"@spacebar/util": "dist/util"
119+
"@spacebar/util": "dist/util",
120+
"@spacebar/webrtc": "dist/webrtc"
120121
},
121122
"optionalDependencies": {
122123
"@yukikaze-bot/erlpack": "^1.0.1",
123124
"jimp": "^1.6.0",
124125
"mysql": "^2.18.1",
126+
"medooze-media-server": "0.129.9",
125127
"nodemailer-mailgun-transport": "^2.1.5",
126128
"nodemailer-mailjet-transport": "github:n0script22/nodemailer-mailjet-transport",
127129
"nodemailer-sendgrid-transport": "github:Maria-Golomb/nodemailer-sendgrid-transport",
128130
"pg": "^8.13.1",
131+
"semantic-sdp": "3.26.0",
129132
"sqlite3": "^5.1.7"
130133
}
131134
}

src/bundle/Server.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import http from "http";
2323
import * as Api from "@spacebar/api";
2424
import * as Gateway from "@spacebar/gateway";
2525
import { CDNServer } from "@spacebar/cdn";
26+
import * as Webrtc from "@spacebar/webrtc";
2627
import express from "express";
2728
import { green, bold } from "picocolors";
2829
import { Config, initDatabase, Sentry } from "@spacebar/util";
@@ -36,12 +37,14 @@ server.on("request", app);
3637
const api = new Api.SpacebarServer({ server, port, production, app });
3738
const cdn = new CDNServer({ server, port, production, app });
3839
const gateway = new Gateway.Server({ server, port, production });
40+
const webrtc = new Webrtc.Server({ server, port, production });
3941

4042
process.on("SIGTERM", async () => {
4143
console.log("Shutting down due to SIGTERM");
4244
await gateway.stop();
4345
await cdn.stop();
4446
await api.stop();
47+
await webrtc.stop();
4548
server.close();
4649
Sentry.close();
4750
});
@@ -54,7 +57,12 @@ async function main() {
5457
await new Promise((resolve) =>
5558
server.listen({ port }, () => resolve(undefined)),
5659
);
57-
await Promise.all([api.start(), cdn.start(), gateway.start()]);
60+
await Promise.all([
61+
api.start(),
62+
cdn.start(),
63+
gateway.start(),
64+
webrtc.start(),
65+
]);
5866

5967
Sentry.errorHandler(app);
6068

src/gateway/Server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class Server {
5656
}
5757

5858
this.server.on("upgrade", (request, socket, head) => {
59+
if (request.url?.includes("voice")) return;
5960
this.ws.handleUpgrade(request, socket, head, (socket) => {
6061
this.ws.emit("connection", socket, request);
6162
});

src/gateway/opcodes/VoiceStateUpdate.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
4040
check.call(this, VoiceStateUpdateSchema, data.d);
4141
const body = data.d as VoiceStateUpdateSchema;
42+
const isNew = body.channel_id === null && body.guild_id === null;
43+
let isChanged = false;
4244

4345
let voiceState: VoiceState;
4446
try {
@@ -54,6 +56,8 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
5456
return;
5557
}
5658

59+
if (voiceState.channel_id !== body.channel_id) isChanged = true;
60+
5761
//If a user change voice channel between guild we should send a left event first
5862
if (
5963
voiceState.guild_id !== body.guild_id &&
@@ -111,7 +115,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
111115
]);
112116

113117
//If it's null it means that we are leaving the channel and this event is not needed
114-
if (voiceState.channel_id !== null) {
118+
if ((isNew || isChanged) && voiceState.channel_id !== null) {
115119
const guild = await Guild.findOne({
116120
where: { id: voiceState.guild_id },
117121
});
@@ -134,7 +138,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
134138
guild_id: voiceState.guild_id,
135139
endpoint: guildRegion.endpoint,
136140
},
137-
guild_id: voiceState.guild_id,
141+
user_id: this.user_id,
138142
} as VoiceServerUpdateEvent);
139143
}
140144
}

src/gateway/util/Constants.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19-
// import { VoiceOPCodes } from "@spacebar/webrtc";
19+
import { VoiceOPCodes } from "@spacebar/webrtc";
2020

2121
export enum OPCODES {
2222
Dispatch = 0,
@@ -63,7 +63,7 @@ export enum CLOSECODES {
6363
}
6464

6565
export interface Payload {
66-
op: OPCODES /* | VoiceOPCodes */;
66+
op: OPCODES | VoiceOPCodes;
6767
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6868
d?: any;
6969
s?: number;

0 commit comments

Comments
 (0)