This commit is contained in:
Madeline
2022-09-26 22:41:21 +10:00
parent fcc0884e36
commit 99ee7e9400
280 changed files with 6800 additions and 3908 deletions
+5 -2
View File
@@ -2,5 +2,8 @@ import { Payload, Send, WebSocket } from "@fosscord/gateway";
import { VoiceOPCodes } from "../util";
export async function onBackendVersion(this: WebSocket, data: Payload) {
await Send(this, { op: VoiceOPCodes.VOICE_BACKEND_VERSION, d: { voice: "0.8.43", rtc_worker: "0.3.26" } });
}
await Send(this, {
op: VoiceOPCodes.VOICE_BACKEND_VERSION,
d: { voice: "0.8.43", rtc_worker: "0.3.26" },
});
}
+8 -2
View File
@@ -1,4 +1,10 @@
import { CLOSECODES, Payload, Send, setHeartbeat, WebSocket } from "@fosscord/gateway";
import {
CLOSECODES,
Payload,
Send,
setHeartbeat,
WebSocket,
} from "@fosscord/gateway";
import { VoiceOPCodes } from "../util";
export async function onHeartbeat(this: WebSocket, data: Payload) {
@@ -6,4 +12,4 @@ export async function onHeartbeat(this: WebSocket, data: Payload) {
if (isNaN(data.d)) return this.close(CLOSECODES.Decode_error);
await Send(this, { op: VoiceOPCodes.HEARTBEAT_ACK, d: data.d });
}
}
+24 -11
View File
@@ -1,33 +1,46 @@
import { CLOSECODES, Payload, Send, WebSocket } from "@fosscord/gateway";
import { validateSchema, VoiceIdentifySchema, VoiceState } from "@fosscord/util";
import {
validateSchema,
VoiceIdentifySchema,
VoiceState,
} from "@fosscord/util";
import { endpoint, getClients, VoiceOPCodes, PublicIP } from "@fosscord/webrtc";
import SemanticSDP from "semantic-sdp";
const defaultSDP = require("./sdp.json");
export async function onIdentify(this: WebSocket, data: Payload) {
clearTimeout(this.readyTimeout);
const { server_id, user_id, session_id, token, streams, video } = validateSchema("VoiceIdentifySchema", data.d) as VoiceIdentifySchema;
const { server_id, user_id, session_id, token, streams, video } =
validateSchema("VoiceIdentifySchema", data.d) as VoiceIdentifySchema;
const voiceState = await VoiceState.findOne({ where: { guild_id: server_id, user_id, token, session_id } });
const voiceState = await VoiceState.findOne({
where: { guild_id: server_id, user_id, token, session_id },
});
if (!voiceState) return this.close(CLOSECODES.Authentication_failed);
this.user_id = user_id;
this.session_id = session_id;
const sdp = SemanticSDP.SDPInfo.expand(defaultSDP);
sdp.setDTLS(SemanticSDP.DTLSInfo.expand({ setup: "actpass", hash: "sha-256", fingerprint: endpoint.getDTLSFingerprint() }));
sdp.setDTLS(
SemanticSDP.DTLSInfo.expand({
setup: "actpass",
hash: "sha-256",
fingerprint: endpoint.getDTLSFingerprint(),
}),
);
this.client = {
websocket: this,
out: {
tracks: new Map()
tracks: new Map(),
},
in: {
audio_ssrc: 0,
video_ssrc: 0,
rtx_ssrc: 0
rtx_ssrc: 0,
},
sdp,
channel_id: voiceState.channel_id
channel_id: voiceState.channel_id,
};
const clients = getClients(voiceState.channel_id)!;
@@ -51,10 +64,10 @@ export async function onIdentify(this: WebSocket, data: Payload) {
"xsalsa20_poly1305_lite_rtpsize",
"xsalsa20_poly1305_lite",
"xsalsa20_poly1305_suffix",
"xsalsa20_poly1305"
"xsalsa20_poly1305",
],
ip: PublicIP,
experiments: []
}
experiments: [],
},
});
}
}
+15 -12
View File
@@ -6,7 +6,10 @@ import SemanticSDP, { MediaInfo, SDPInfo } from "semantic-sdp";
export async function onSelectProtocol(this: WebSocket, payload: Payload) {
if (!this.client) return;
const data = validateSchema("SelectProtocolSchema", payload.d) as SelectProtocolSchema;
const data = validateSchema(
"SelectProtocolSchema",
payload.d,
) as SelectProtocolSchema;
const offer = SemanticSDP.SDPInfo.parse("m=audio\n" + data.sdp!);
this.client.sdp!.setICE(offer.getICE());
@@ -25,14 +28,14 @@ export async function onSelectProtocol(this: WebSocket, payload: Payload) {
const candidate = candidates[0];
const answer =
`m=audio ${port} ICE/SDP`
+ `a=fingerprint:${fingerprint}`
+ `c=IN IP4 ${PublicIP}`
+ `a=rtcp:${port}`
+ `a=ice-ufrag:${ice.getUfrag()}`
+ `a=ice-pwd:${ice.getPwd()}`
+ `a=fingerprint:${fingerprint}`
+ `a=candidate:1 1 ${candidate.getTransport()} ${candidate.getFoundation()} ${candidate.getAddress()} ${candidate.getPort()} typ host`;
`m=audio ${port} ICE/SDP` +
`a=fingerprint:${fingerprint}` +
`c=IN IP4 ${PublicIP}` +
`a=rtcp:${port}` +
`a=ice-ufrag:${ice.getUfrag()}` +
`a=ice-pwd:${ice.getPwd()}` +
`a=fingerprint:${fingerprint}` +
`a=candidate:1 1 ${candidate.getTransport()} ${candidate.getFoundation()} ${candidate.getAddress()} ${candidate.getPort()} typ host`;
await Send(this, {
op: VoiceOPCodes.SELECT_PROTOCOL_ACK,
@@ -40,7 +43,7 @@ export async function onSelectProtocol(this: WebSocket, payload: Payload) {
video_codec: "H264",
sdp: answer,
media_session_id: this.session_id,
audio_codec: "opus"
}
audio_codec: "opus",
},
});
}
}
+3 -3
View File
@@ -15,8 +15,8 @@ export async function onSpeaking(this: WebSocket, data: Payload) {
d: {
user_id: client.websocket.user_id,
speaking: data.d.speaking,
ssrc: ssrc?.audio_ssrc || 0
}
ssrc: ssrc?.audio_ssrc || 0,
},
});
});
}
}
+27 -12
View File
@@ -21,8 +21,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
SemanticSDP.StreamInfo.expand({
id,
// @ts-ignore
tracks: []
})
tracks: [],
}),
);
this.client.in.stream = stream;
@@ -46,8 +46,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
SemanticSDP.StreamInfo.expand({
id: "out" + this.user_id,
// @ts-ignore
tracks: []
})
tracks: [],
}),
);
this.client.out.stream = out;
@@ -64,20 +64,35 @@ export async function onVideo(this: WebSocket, payload: Payload) {
}
if (d.audio_ssrc) {
handleSSRC.call(this, "audio", { media: d.audio_ssrc, rtx: d.audio_ssrc + 1 });
handleSSRC.call(this, "audio", {
media: d.audio_ssrc,
rtx: d.audio_ssrc + 1,
});
}
if (d.video_ssrc && d.rtx_ssrc) {
handleSSRC.call(this, "video", { media: d.video_ssrc, rtx: d.rtx_ssrc });
handleSSRC.call(this, "video", {
media: d.video_ssrc,
rtx: d.rtx_ssrc,
});
}
}
function attachTrack(this: WebSocket, track: IncomingStreamTrack, user_id: string) {
function attachTrack(
this: WebSocket,
track: IncomingStreamTrack,
user_id: string,
) {
if (!this.client) return;
const outTrack = this.client.transport!.createOutgoingStreamTrack(track.getMedia());
const outTrack = this.client.transport!.createOutgoingStreamTrack(
track.getMedia(),
);
outTrack.attachTo(track);
this.client.out.stream!.addTrack(outTrack);
var ssrcs = this.client.out.tracks.get(user_id)!;
if (!ssrcs) ssrcs = this.client.out.tracks.set(user_id, { audio_ssrc: 0, rtx_ssrc: 0, video_ssrc: 0 }).get(user_id)!;
if (!ssrcs)
ssrcs = this.client.out.tracks
.set(user_id, { audio_ssrc: 0, rtx_ssrc: 0, video_ssrc: 0 })
.get(user_id)!;
if (track.getMedia() === "audio") {
ssrcs.audio_ssrc = outTrack.getSSRCs().media!;
@@ -90,8 +105,8 @@ function attachTrack(this: WebSocket, track: IncomingStreamTrack, user_id: strin
op: VoiceOPCodes.VIDEO,
d: {
user_id: user_id,
...ssrcs
} as VoiceVideoSchema
...ssrcs,
} as VoiceVideoSchema,
});
}
@@ -115,4 +130,4 @@ function handleSSRC(this: WebSocket, type: "audio" | "video", ssrcs: SSRCs) {
attachTrack.call(this, track, client.websocket.user_id);
});
}
}
}
+2 -2
View File
@@ -15,5 +15,5 @@ export default {
[VoiceOPCodes.VOICE_BACKEND_VERSION]: onBackendVersion,
[VoiceOPCodes.VIDEO]: onVideo,
[VoiceOPCodes.SPEAKING]: onSpeaking,
[VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol
};
[VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol,
};
+1 -1
View File
@@ -417,4 +417,4 @@
}
],
"candidates": []
}
}