1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-25 11:43:07 +01:00

fixing some stuff

This commit is contained in:
Puyodead1 2024-09-16 20:25:56 -04:00
parent b743062cb9
commit 6beee77a4c
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
3 changed files with 55 additions and 49 deletions

View File

@ -39,6 +39,8 @@ import { check } from "./instanceOf";
export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema;
const isNew = body.channel_id === null && body.guild_id === null;
let isChanged = false;
let voiceState: VoiceState;
try {
@ -54,6 +56,8 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
return;
}
if (voiceState.channel_id !== body.channel_id) isChanged = true;
//If a user change voice channel between guild we should send a left event first
if (
voiceState.guild_id !== body.guild_id &&
@ -112,7 +116,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
]);
//If it's null it means that we are leaving the channel and this event is not needed
if (voiceState.channel_id !== null) {
if ((isNew || isChanged) && voiceState.channel_id !== null) {
const guild = await Guild.findOne({
where: { id: voiceState.guild_id },
});

View File

@ -81,25 +81,27 @@ export async function onIdentify(this: WebSocket, data: IdentifyPayload) {
const producerTransport = await router.router.createWebRtcTransport({
listenIps: [{ ip: process.env.LISTEN_IP || getLocalIp() }],
enableUdp: true,
initialAvailableOutgoingBitrate: 150000000,
});
maxIncomingBitrate: 1500000,
initialAvailableOutgoingBitrate: 1000000,
} as any);
producerTransport.enableTraceEvent(["bwe", "probation"]);
// listen to any events
for (const event of producerTransport.eventNames()) {
if (typeof event !== "string") continue;
producerTransport.on(event as any, (...args) => {
console.debug(`producerTransport(${event}):`, args);
});
}
// listen to any events
for (const event of producerTransport.observer.eventNames()) {
if (typeof event !== "string") continue;
producerTransport.observer.on(event as any, (...args) => {
console.debug(`producerTransport observer(${event}):`, args);
});
}
producerTransport.on("trace", (trace) => {
console.log(`transport trace`, trace);
});
// setInterval(async () => {
// if (producerTransport.closed) return;
// console.log(
// `transport stats`,
// JSON.stringify(await producerTransport.getStats(), null, 4),
// );
// console.log(
// `transport dump`,
// JSON.stringify(await producerTransport.dump(), null, 4),
// );
// }, 10 * 1000);
const offer = SemanticSDP.SDPInfo.expand(defaultSDP);
offer.setDTLS(
@ -143,7 +145,6 @@ export async function onIdentify(this: WebSocket, data: IdentifyPayload) {
ssrc: ++this.client!.in.video_ssrc, // first stream should be 2
rtx_ssrc: ++this.client!.in.video_ssrc, // first stream should be 3
})),
ip: producerTransport.iceCandidates[0].ip,
port: producerTransport.iceCandidates[0].port,
modes: [

View File

@ -53,6 +53,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
await Send(this, { op: VoiceOPCodes.MEDIA_SINK_WANTS, d: { any: 100 } });
if (d.audio_ssrc === 0 && d.video_ssrc === 0) return;
const router = getRouter(channel_id);
if (!router) {
console.error(`router not found`);
@ -71,7 +73,7 @@ export async function onVideo(this: WebSocket, payload: Payload) {
rtpParameters: {
codecs: [
{
payloadType: 109,
payloadType: 111,
mimeType: "audio/opus",
clockRate: 48000,
channels: 2,
@ -84,6 +86,13 @@ export async function onVideo(this: WebSocket, payload: Payload) {
encodings: [
{
ssrc: d.audio_ssrc,
maxBitrate: 64000,
},
],
headerExtensions: [
{
id: 1,
uri: "urn:ietf:params:rtp-hdrext:ssrc-audio-level",
},
],
// headerExtensions: this.client
@ -100,15 +109,9 @@ export async function onVideo(this: WebSocket, payload: Payload) {
await audioProducer.enableTraceEvent(["rtp"]);
// producer.on("score", (score) => {
// console.debug(`audio producer score:`, score);
// });
// producer.on("trace", (trace) => {
// console.debug(`audio producer trace:`, trace);
// });
// this.client.producers.push(producer);
audioProducer.on("score", (score) => {
console.debug(`audio producer score:`, score);
});
this.client.producers.audio = audioProducer;
}
}
@ -122,19 +125,7 @@ export async function onVideo(this: WebSocket, payload: Payload) {
rtpParameters: {
codecs: [
{
payloadType: 120,
mimeType: "video/VP8",
clockRate: 90000,
rtcpFeedback: [
{ type: "nack" },
{ type: "nack", parameter: "pli" },
{ type: "ccm", parameter: "fir" },
{ type: "goog-remb" },
{ type: "transport-cc" },
],
},
{
payloadType: 126,
payloadType: 102,
mimeType: "video/H264",
clockRate: 90000,
parameters: {
@ -153,6 +144,7 @@ export async function onVideo(this: WebSocket, payload: Payload) {
{
ssrc: d.video_ssrc,
rtx: { ssrc: d.rtx_ssrc! },
scalabilityMode: "L1T1",
},
],
// headerExtensions: this.client
@ -172,10 +164,6 @@ export async function onVideo(this: WebSocket, payload: Payload) {
videoProducer.on("score", (score) => {
console.debug(`video producer score:`, score);
});
videoProducer.on("trace", (trace) => {
console.debug(`video producer trace:`, trace);
});
}
// loop the clients and add a consumer for each one
@ -186,8 +174,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
if (d.audio_ssrc !== 0) {
// close the existing consumer if it exists
const a = client.consumers.find((x) => x.kind === "audio");
await a?.close();
const a = client.consumers.filter((x) => x.kind === "audio");
await a.forEach(async (x) => await x.close());
const consumer = await client.transport.consume({
producerId: audioProducer?.id!,
rtpCapabilities: router.router.rtpCapabilities,
@ -198,8 +186,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
if (d.video_ssrc !== 0) {
// close the existing consumer if it exists
const a = client.consumers.find((x) => x.kind === "video");
await a?.close();
const a = client.consumers.filter((x) => x.kind === "video");
await a.forEach(async (x) => await x.close());
const consumer = await client.transport.consume({
producerId: videoProducer?.id!,
rtpCapabilities: router.router.rtpCapabilities,
@ -207,5 +195,18 @@ export async function onVideo(this: WebSocket, payload: Payload) {
});
client.consumers.push(consumer);
}
Send(client.websocket, {
op: VoiceOPCodes.VIDEO,
d: {
user_id: this.user_id,
audio_ssrc: d.audio_ssrc || 0,
video_ssrc: d.video_ssrc || 0,
streams: d.streams?.map((x) => ({
...x,
active: true,
})),
},
});
}
}