Skip to content

Commit

Permalink
update encoder params to match discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Sep 17, 2024
1 parent 6beee77 commit 7111880
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/webrtc/opcodes/Identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export async function onIdentify(this: WebSocket, data: IdentifyPayload) {
transport: producerTransport,
producers: {},
consumers: [],
headerExtensions: [],
};

const clients = getClients(voiceState.channel_id)!;
Expand Down
23 changes: 12 additions & 11 deletions src/webrtc/opcodes/SelectProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import { Payload, Send, WebSocket } from "@spacebar/gateway";
import { SelectProtocolSchema, validateSchema } from "@spacebar/util";
import { types as MediaSoupTypes } from "mediasoup";
import * as sdpTransform from "sdp-transform";
import * as SemanticSDP from "semantic-sdp";
import { getRouter, VoiceOPCodes } from "../util";
import { getRouter, SUPPORTED_EXTENTIONS, VoiceOPCodes } from "../util";

// request:
// {
Expand Down Expand Up @@ -121,17 +122,17 @@ export async function onSelectProtocol(this: WebSocket, payload: Payload) {
// }

const offer = SemanticSDP.SDPInfo.parse("m=audio\n" + data.sdp);
const offer2 = sdpTransform.parse(data.sdp!);
this.client.sdpOffer = offer;
// this.client.codecs = data.codecs!;
// this.client.headerExtensions =
// sdp.ext
// ?.filter((x) => SUPPORTED_EXTENTIONS.includes(x.uri))
// .map((x) => ({
// uri: x.uri as MediaSoupTypes.RtpHeaderExtensionUri,
// id: x.value,
// parameters: x.config,
// encrypt: false,
// })) ?? [];
this.client.headerExtensions =
offer2.ext
?.filter((x) => SUPPORTED_EXTENTIONS.includes(x.uri))
.map((x) => ({
uri: x.uri as MediaSoupTypes.RtpHeaderExtensionUri,
id: x.value,
parameters: x.config,
encrypt: false,
})) ?? [];

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
Expand Down
20 changes: 11 additions & 9 deletions src/webrtc/opcodes/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export async function onVideo(this: WebSocket, payload: Payload) {
{ type: "nack" },
{ type: "transport-cc" },
],
parameters: {
minptime: 10,
usedtx: 1,
useinbandfec: 1,
},
},
],
encodings: [
Expand Down Expand Up @@ -130,6 +135,9 @@ export async function onVideo(this: WebSocket, payload: Payload) {
clockRate: 90000,
parameters: {
"level-asymmetry-allowed": 1,
"packetization-mode": 1,
"profile-level-id": "42e01f",
"x-google-start-bitrate": 2500,
},
rtcpFeedback: [
{ type: "nack" },
Expand All @@ -145,17 +153,11 @@ export async function onVideo(this: WebSocket, payload: Payload) {
ssrc: d.video_ssrc,
rtx: { ssrc: d.rtx_ssrc! },
scalabilityMode: "L1T1",
scaleResolutionDownBy: 1,
maxBitrate: 2500000,
},
],
// headerExtensions: this.client
// .sdpOffer2!.media[1].ext?.filter((x) =>
// SUPPORTED_EXTENTIONS.includes(x.uri),
// )
// .map((x) => ({
// uri: x.uri as NMediaSoupTypes.RtpHeaderExtensionUri,
// id: x.value,
// encrypt: false,
// })),
headerExtensions: this.client.headerExtensions,
},
});

Expand Down
83 changes: 46 additions & 37 deletions src/webrtc/util/MediaServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface Client {
video?: MediaSoupTypes.Producer;
};
consumers: MediaSoupTypes.Consumer[];
headerExtensions: MediaSoupTypes.RtpHeaderExtensionParameters[];
}

export function getClients(channel_id: string) {
Expand Down Expand Up @@ -211,46 +212,51 @@ export const MEDIA_CODECS: MediaSoupTypes.RtpCodecCapability[] = [
clockRate: 48000,
channels: 2,
rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
},
{
kind: "audio",
mimeType: "audio/multiopus",
clockRate: 48000,
channels: 4,
// Quad channel.
parameters: {
channel_mapping: "0,1,2,3",
num_streams: 2,
coupled_streams: 2,
minptime: 10,
usedtx: 1,
useinbandfec: 1,
},
rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
},
{
kind: "audio",
mimeType: "audio/multiopus",
clockRate: 48000,
channels: 6,
// 5.1.
parameters: {
channel_mapping: "0,4,1,2,3,5",
num_streams: 4,
coupled_streams: 2,
},
rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
},
{
kind: "audio",
mimeType: "audio/multiopus",
clockRate: 48000,
channels: 8,
// 7.1.
parameters: {
channel_mapping: "0,6,1,2,3,4,5,7",
num_streams: 5,
coupled_streams: 3,
},
rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
},
// {
// kind: "audio",
// mimeType: "audio/multiopus",
// clockRate: 48000,
// channels: 4,
// // Quad channel.
// parameters: {
// channel_mapping: "0,1,2,3",
// num_streams: 2,
// coupled_streams: 2,
// },
// rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
// },
// {
// kind: "audio",
// mimeType: "audio/multiopus",
// clockRate: 48000,
// channels: 6,
// // 5.1.
// parameters: {
// channel_mapping: "0,4,1,2,3,5",
// num_streams: 4,
// coupled_streams: 2,
// },
// rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
// },
// {
// kind: "audio",
// mimeType: "audio/multiopus",
// clockRate: 48000,
// channels: 8,
// // 7.1.
// parameters: {
// channel_mapping: "0,6,1,2,3,4,5,7",
// num_streams: 5,
// coupled_streams: 3,
// },
// rtcpFeedback: [{ type: "nack" }, { type: "transport-cc" }],
// },
{
kind: "video",
mimeType: "video/VP8",
Expand Down Expand Up @@ -281,6 +287,9 @@ export const MEDIA_CODECS: MediaSoupTypes.RtpCodecCapability[] = [
clockRate: 90000,
parameters: {
"level-asymmetry-allowed": 1,
"packetization-mode": 1,
"profile-level-id": "42e01f",
"x-google-start-bitrate": 2500,
},
rtcpFeedback: [
{ type: "nack" },
Expand Down

0 comments on commit 7111880

Please sign in to comment.