├── .gitignore ├── mod.ts ├── deps.ts ├── src ├── types.ts ├── player.ts ├── ffmpeg.ts ├── conn.ts ├── ws.ts └── udp.ts ├── README.md ├── test.ts └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /pcm.raw 3 | /run.bat 4 | /config.ts -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | export * from "./src/conn.ts"; 2 | export * from "./src/types.ts"; 3 | export * from "./src/udp.ts"; 4 | export * from "./src/ws.ts"; 5 | export * from "./src/ffmpeg.ts"; 6 | export * from "./src/player.ts"; 7 | -------------------------------------------------------------------------------- /deps.ts: -------------------------------------------------------------------------------- 1 | export { Encoder } from "https://unpkg.com/@evan/wasm@0.0.63/target/opus/deno.js"; 2 | export { secretbox } from "https://unpkg.com/@evan/wasm@0.0.63/target/nacl/deno.js"; 3 | export { readableStreamFromIterable } from "https://deno.land/std@0.98.0/io/streams.ts"; 4 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export enum OpCode { 2 | IDENTIFY = 0, 3 | SELECT_PROTOCOL = 1, 4 | READY = 2, 5 | HEARTBEAT = 3, 6 | SESSION_DESCRIPTION = 4, 7 | SPEAKING = 5, 8 | HEARTBEAT_ACK = 6, 9 | RESUME = 7, 10 | HELLO = 8, 11 | RESUMED = 9, 12 | CLIENT_DISCONNECT = 13, 13 | } 14 | 15 | export enum Speaking { 16 | MICROPHONE = 1 << 0, 17 | SOUNDSHARE = 1 << 1, 18 | PRIORITY = 1 << 2, 19 | } 20 | 21 | export type EncryptionMode = 22 | | "xsalsa20_poly1305" 23 | | "xsalsa20_poly1305_lite" 24 | | "xsalsa20_poly1305_suffix"; 25 | 26 | export const VOICE_VERSION = 4; 27 | export const CHANNELS = 2; 28 | export const SAMPLE_RATE = 48000; 29 | export const MAX_PACKET_SIZE = 28 + 1276 * 3; 30 | export const FRAME_DURATION = 20; 31 | export const FRAME_SIZE = SAMPLE_RATE * FRAME_DURATION / 1000; 32 | export const MAX_SEQ = 2 ** 16; 33 | export const MAX_TIMESTAMP = 2 ** 32; 34 | export const ENCRYPTION_MODE = "xsalsa20_poly1305"; // default 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # harmony_voice 2 | 3 | Discord Voice API implementation for Deno. 4 | 5 | ## Features 6 | 7 | - Built on modern Web Streams API. 8 | - Works with any Discord API library with some effort. 9 | - Experimental Voice Receive support. 10 | 11 | ## Usage 12 | 13 | ```ts 14 | const conn = new VoiceConnect(botUserID); 15 | // Obtained from VOICE_STATE_UPDATE Gateway Event 16 | conn.voiceStateUpdate({ channelID, guildID, sessionID }); 17 | // Obtained from VOICE_SERVER_UPDATE 18 | conn.voiceServerUpdate({ token, endpoint }); 19 | 20 | conn.connect(); 21 | 22 | // To play something 23 | const player = conn.player(); 24 | pcmStreamFromSomewhere.pipeTo(player.writable); 25 | 26 | // ytdl_core example 27 | const player = conn.player(); 28 | const info = await getInfo("id"); // from x/ytdl_core 29 | 30 | new PCMStream(stream.formats.find((e) => e.hasAudio && !e.hasVideo)!.url) 31 | .pipeTo(player.writable); 32 | ``` 33 | 34 | ## License 35 | 36 | Check [LICENSE](./LICENSE) for more info. 37 | 38 | Copyright 2022 © DjDeveloperr 39 | -------------------------------------------------------------------------------- /src/player.ts: -------------------------------------------------------------------------------- 1 | import type { VoiceConnection } from "./conn.ts"; 2 | import { 3 | CHANNELS, 4 | FRAME_DURATION, 5 | FRAME_SIZE, 6 | MAX_PACKET_SIZE, 7 | SAMPLE_RATE, 8 | } from "./types.ts"; 9 | import { Encoder, readableStreamFromIterable } from "../deps.ts"; 10 | 11 | const frame = new Uint8Array(MAX_PACKET_SIZE); 12 | frame.set([0x80, 0x78], 0); 13 | 14 | const encoder = new Encoder({ 15 | channels: CHANNELS, 16 | application: "audio", 17 | max_opus_size: undefined as any, 18 | sample_rate: SAMPLE_RATE, 19 | }); 20 | 21 | encoder.bitrate = 96000; 22 | encoder.complexity = 10; 23 | encoder.packet_loss = 2; 24 | encoder.signal = "music"; 25 | encoder.inband_fec = true; 26 | 27 | export class VoicePlayer { 28 | #startTime = Date.now(); 29 | #playTime = 0; 30 | #pausedTime = 0; 31 | #nextFrame?: number; 32 | playing = false; 33 | paused = false; 34 | #readable?: ReadableStream; 35 | #readableCtx?: ReadableStreamDefaultController; 36 | writable: WritableStream; 37 | 38 | constructor(public conn: VoiceConnection) { 39 | this.writable = new WritableStream({ 40 | start: () => { 41 | if (this.#nextFrame) { 42 | clearTimeout(this.#nextFrame); 43 | this.#nextFrame = undefined; 44 | } 45 | 46 | this.#startTime = Date.now(); 47 | this.#playTime = 0; 48 | this.#pausedTime = 0; 49 | this.playing = true; 50 | 51 | this.#readable = new ReadableStream({ 52 | start: (ctx) => { 53 | this.#readableCtx = ctx; 54 | }, 55 | }); 56 | 57 | const iter = encoder.encode_pcm_stream(FRAME_SIZE, this.#readable); 58 | const stream = readableStreamFromIterable( 59 | iter as AsyncIterable, 60 | ); 61 | const reader = stream.getReader(); 62 | 63 | const frame = async () => { 64 | if (!this.playing) return; 65 | 66 | if (this.paused) { 67 | this.#pausedTime += FRAME_DURATION; 68 | } else { 69 | const res = await reader.read(); 70 | 71 | if (res.done) { 72 | this.playing = false; 73 | this.conn.setSpeaking(); 74 | return; 75 | } else { 76 | const opus = res.value; 77 | try { 78 | await this.conn.udp!.sendVoice(opus); 79 | } catch (e) {} 80 | } 81 | 82 | this.#playTime += FRAME_DURATION; 83 | } 84 | 85 | this.#nextFrame = setTimeout( 86 | frame, 87 | this.#startTime + this.#playTime + this.#pausedTime - Date.now(), 88 | ); 89 | }; 90 | 91 | this.conn.setSpeaking("MICROPHONE"); 92 | frame().catch(() => { 93 | this.playing = false; 94 | }); 95 | }, 96 | write: (chunk) => { 97 | this.#readableCtx?.enqueue(chunk); 98 | }, 99 | close: () => { 100 | this.#readableCtx?.close(); 101 | }, 102 | }); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/ffmpeg.ts: -------------------------------------------------------------------------------- 1 | import { 2 | readableStreamFromReader, 3 | writableStreamFromWriter, 4 | } from "https://deno.land/std@0.98.0/io/streams.ts"; 5 | 6 | export interface FFmpegStreamOptions { 7 | path?: string; 8 | args: string[]; 9 | chunkSize?: number; 10 | stderr?: boolean; 11 | } 12 | 13 | export class FFmpegStream extends ReadableStream { 14 | #proc?: Deno.Process; 15 | #stderr?: ReadableStream; 16 | #stdin?: WritableStream; 17 | 18 | get proc() { 19 | if (!this.#proc) { 20 | this.#proc = Deno.run({ 21 | cmd: [(this.options.path || "ffmpeg"), ...this.options.args], 22 | stdout: "piped", 23 | stderr: this.options.stderr ? "piped" : "null", 24 | stdin: "piped", 25 | }); 26 | } 27 | 28 | if (this.#proc.stderr) { 29 | this.#stderr = readableStreamFromReader(this.#proc.stderr).pipeThrough( 30 | new TextDecoderStream(), 31 | ); 32 | } 33 | this.#stdin = writableStreamFromWriter(this.#proc.stdin!); 34 | return this.#proc; 35 | } 36 | 37 | get stderr() { 38 | if (!this.#stderr) { 39 | if (this.proc.stderr) { 40 | this.#stderr = readableStreamFromReader(this.proc.stderr).pipeThrough( 41 | new TextDecoderStream(), 42 | ); 43 | } 44 | } 45 | 46 | return this.#stderr; 47 | } 48 | 49 | get stdin() { 50 | if (!this.#stdin) { 51 | this.#stdin = writableStreamFromWriter(this.proc.stdin!); 52 | } 53 | 54 | return this.#stdin; 55 | } 56 | 57 | constructor(public options: FFmpegStreamOptions) { 58 | super({ 59 | pull: async (ctx) => { 60 | const proc = this.proc; 61 | 62 | for await ( 63 | const chunk of readableStreamFromReader(proc.stdout!, { 64 | chunkSize: options.chunkSize, 65 | }) 66 | ) { 67 | ctx.enqueue(chunk); 68 | } 69 | 70 | ctx.close(); 71 | proc.close(); 72 | }, 73 | }); 74 | } 75 | } 76 | 77 | export class PCMStream extends FFmpegStream { 78 | constructor(path: string) { 79 | super({ 80 | args: [ 81 | "-i", 82 | path, 83 | "-f", 84 | "s16le", 85 | "-acodec", 86 | "pcm_s16le", 87 | "-ac", 88 | "2", 89 | "-ar", 90 | "48000", 91 | "-", 92 | ], 93 | }); 94 | } 95 | } 96 | 97 | const I16_MIN = 2 ** 16 / 2 - 1; 98 | const I16_MAX = -1 * I16_MIN; 99 | 100 | export class VolumeTransformer extends TransformStream { 101 | constructor(public options: { volume: number }) { 102 | super({ 103 | transform(chunk, ctx) { 104 | chunk = chunk.slice(); // todo: do we have to copy to prevent mutating passed buffer? 105 | const view = new DataView(chunk.buffer); 106 | 107 | for (let i = 0; i < chunk.length; i += 2) { 108 | view.setInt16( 109 | i, 110 | Math.max( 111 | I16_MAX, 112 | Math.min(I16_MIN, options.volume * view.getInt16(i, true)), 113 | ), 114 | true, 115 | ); 116 | } 117 | 118 | ctx.enqueue(chunk); 119 | }, 120 | }); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/conn.ts: -------------------------------------------------------------------------------- 1 | import { Encoder, readableStreamFromIterable, secretbox } from "../deps.ts"; 2 | import { VoicePlayer } from "./player.ts"; 3 | import { EncryptionMode, Speaking } from "./types.ts"; 4 | import { VoiceUDP } from "./udp.ts"; 5 | import { VoiceWebSocket } from "./ws.ts"; 6 | 7 | const CHANNELS = 2; 8 | const SAMPLE_RATE = 48000; 9 | const MAX_PACKET_SIZE = 28 + 1276 * 3; 10 | const FRAME_DURATION = 20; 11 | const FRAME_SIZE = SAMPLE_RATE * FRAME_DURATION / 1000; 12 | 13 | const frame = new Uint8Array(MAX_PACKET_SIZE); 14 | frame.set([0x80, 0x78], 0); 15 | 16 | const encoder = new Encoder({ 17 | channels: CHANNELS, 18 | application: "audio", 19 | max_opus_size: undefined as any, 20 | sample_rate: SAMPLE_RATE, 21 | }); 22 | 23 | encoder.bitrate = 96000; 24 | encoder.complexity = 10; 25 | encoder.packet_loss = 2; 26 | encoder.signal = "music"; 27 | encoder.inband_fec = true; 28 | 29 | export interface VoiceConnectionConfig { 30 | mode?: EncryptionMode; 31 | receive?: "opus" | "pcm"; 32 | } 33 | 34 | export class VoiceConnection { 35 | guildID?: string; 36 | channelID?: string; 37 | sessionID?: string; 38 | 39 | token?: string; 40 | endpoint?: string; 41 | 42 | ws: VoiceWebSocket; 43 | 44 | udp: VoiceUDP; 45 | 46 | mode?: EncryptionMode; 47 | #key = new Uint8Array(secretbox.key_length); 48 | 49 | get key() { 50 | return this.#key; 51 | } 52 | 53 | set key(val: Uint8Array) { 54 | this.#key.set(val); 55 | } 56 | 57 | #startTime = Date.now(); 58 | #playTime = 0; 59 | #pausedTime = 0; 60 | #nextFrame?: number; 61 | paused = false; 62 | 63 | get ready() { 64 | return this.ws.ready; 65 | } 66 | 67 | constructor( 68 | public userID: string, 69 | public config: VoiceConnectionConfig = {}, 70 | ) { 71 | this.ws = new VoiceWebSocket(this); 72 | this.udp = new VoiceUDP(this); 73 | } 74 | 75 | voiceStateUpdate({ guildID, channelID, sessionID }: { 76 | guildID: string; 77 | channelID: string; 78 | sessionID: string; 79 | }) { 80 | this.guildID = guildID; 81 | this.channelID = channelID; 82 | this.sessionID = sessionID; 83 | } 84 | 85 | voiceServerUpdate({ token, endpoint }: { token: string; endpoint: string }) { 86 | this.token = token; 87 | this.endpoint = endpoint; 88 | } 89 | 90 | connect() { 91 | this.ws.connect(); 92 | } 93 | 94 | setSpeaking(...flags: (keyof typeof Speaking)[]) { 95 | return this.ws.sendSpeaking( 96 | flags.map((e) => Speaking[e]).reduce((a, b) => a | b, 0), 97 | ); 98 | } 99 | 100 | #closables: CallableFunction[] = []; 101 | 102 | player() { 103 | const player = new VoicePlayer(this); 104 | this.#closables.push(() => { 105 | player.playing = false; 106 | }); 107 | return player; 108 | } 109 | 110 | async playPCM(pcm: Iterable | AsyncIterable) { 111 | if (this.#nextFrame) { 112 | clearTimeout(this.#nextFrame); 113 | this.#nextFrame = undefined; 114 | } 115 | 116 | const iter = encoder.encode_pcm_stream(FRAME_SIZE, pcm); 117 | const stream = readableStreamFromIterable( 118 | iter as AsyncIterable, 119 | ); 120 | const reader = stream.getReader(); 121 | 122 | this.#startTime = Date.now(); 123 | this.#playTime = 0; 124 | this.#pausedTime = 0; 125 | 126 | const frame = async () => { 127 | if (this.paused) { 128 | this.#pausedTime += FRAME_DURATION; 129 | } else { 130 | const res = await reader.read(); 131 | 132 | if (res.done) { 133 | this.ws?.sendSpeaking(0); 134 | return; 135 | } else { 136 | const opus = res.value; 137 | await this.udp!.sendVoice(opus); 138 | } 139 | 140 | this.#playTime += FRAME_DURATION; 141 | } 142 | 143 | this.#nextFrame = setTimeout( 144 | frame, 145 | this.#startTime + this.#playTime + this.#pausedTime - Date.now(), 146 | ); 147 | }; 148 | 149 | this.ws?.sendSpeaking(Speaking.MICROPHONE); 150 | await frame(); 151 | } 152 | 153 | readable(userID: string) { 154 | return this.udp.readable(userID); 155 | } 156 | 157 | close() { 158 | if (this.#nextFrame) { 159 | clearTimeout(this.#nextFrame); 160 | this.#nextFrame = undefined; 161 | } 162 | this.ws?.close(); 163 | this.udp?.close(); 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /test.ts: -------------------------------------------------------------------------------- 1 | import * as discord from "https://deno.land/x/harmony@v2.0.0-rc2/mod.ts"; 2 | import * as voice from "./mod.ts"; 3 | import ytsr from "https://deno.land/x/youtube_sr@v4.0.1-deno/mod.ts"; 4 | import { getInfo } from "https://deno.land/x/ytdl_core@0.0.1/mod.ts"; 5 | import { TOKEN } from "./config.ts"; 6 | 7 | const client = new discord.CommandClient({ 8 | prefix: ".", 9 | token: TOKEN, 10 | intents: ["GUILDS", "GUILD_VOICE_STATES", "GUILD_MESSAGES"], 11 | }); 12 | 13 | const conns = new discord.Collection(); 14 | 15 | client.commands.add( 16 | class extends discord.Command { 17 | name = "join"; 18 | 19 | async execute(ctx: discord.CommandContext) { 20 | if (!ctx.guild) return; 21 | if (conns.has(client.user!.id)) { 22 | return ctx.message.reply("I've already joined VC."); 23 | } 24 | 25 | const vs = await ctx.guild.voiceStates.get(ctx.author.id); 26 | if (!vs || !vs.channel) { 27 | return ctx.message.reply("You're not in a Voice Channel."); 28 | } 29 | 30 | const data = await vs.channel.join({ deaf: true }); 31 | 32 | const conn = new voice.VoiceConnection(client.user!.id, { 33 | mode: "xsalsa20_poly1305", 34 | receive: "opus", 35 | }); 36 | 37 | conn.voiceStateUpdate({ 38 | guildID: data.guild.id, 39 | channelID: vs.channel.id, 40 | sessionID: data.sessionID, 41 | }); 42 | 43 | conn.voiceServerUpdate({ endpoint: data.endpoint, token: data.token }); 44 | 45 | conn.connect(); 46 | 47 | conns.set(ctx.guild.id, conn); 48 | 49 | ctx.message.reply("Joined Voice Channel!"); 50 | } 51 | }, 52 | ); 53 | 54 | client.commands.add( 55 | class extends discord.Command { 56 | name = "play"; 57 | 58 | async execute(ctx: discord.CommandContext) { 59 | if (!ctx.guild) return; 60 | if (!conns.has(ctx.guild.id)) { 61 | return ctx.message.reply( 62 | "I have not even joined a Voice Channel here.", 63 | ); 64 | } 65 | 66 | const conn = conns.get(ctx.guild.id)!; 67 | if (!conn.ready) return ctx.message.reply("Connection not ready."); 68 | 69 | if (!ctx.argString.length) { 70 | return ctx.message.reply("Give some query for search!"); 71 | } 72 | 73 | const search = await ytsr.searchOne(ctx.argString); 74 | if (!search || !search.id) return ctx.message.reply("Nothing found."); 75 | 76 | const info = await getInfo(search.id); 77 | const url = info.formats.find((e) => e.hasAudio && !e.hasVideo)!.url; 78 | 79 | const player = conn.player(); 80 | const stream = new voice.PCMStream(url); 81 | stream.pipeTo(player.writable); 82 | 83 | ctx.message.reply("Playing now - " + search.title + "!"); 84 | } 85 | }, 86 | ); 87 | 88 | client.commands.add( 89 | class extends discord.Command { 90 | name = "receive"; 91 | 92 | async execute(ctx: discord.CommandContext) { 93 | if (!ctx.guild) return; 94 | if (!conns.has(ctx.guild.id)) { 95 | return ctx.message.reply( 96 | "I have not even joined a Voice Channel here.", 97 | ); 98 | } 99 | 100 | const conn = conns.get(ctx.guild.id)!; 101 | 102 | const user = ctx.message.mentions.users.first(); 103 | 104 | if (!user) { 105 | return ctx.message.reply("Mention someone to receive audio for."); 106 | } 107 | 108 | ctx.message.reply("Receiving now."); 109 | 110 | for await (const frame of conn.readable(user.id)) { 111 | console.log(frame); 112 | } 113 | } 114 | }, 115 | ); 116 | 117 | client.commands.add( 118 | class extends discord.Command { 119 | name = "leave"; 120 | 121 | async execute(ctx: discord.CommandContext) { 122 | if (!ctx.guild) return; 123 | if (!conns.has(ctx.guild.id)) { 124 | return ctx.message.reply( 125 | "I have not even joined a Voice Channel here.", 126 | ); 127 | } 128 | 129 | const conn = conns.get(ctx.guild.id); 130 | conn?.close(); 131 | conns.delete(ctx.guild.id); 132 | 133 | const vs = await ctx.guild.voiceStates.get(client.user!.id); 134 | if (vs) { 135 | await vs.channel?.leave(); 136 | } 137 | 138 | ctx.message.reply("Left voice channel."); 139 | } 140 | }, 141 | ); 142 | 143 | client.on("commandError", console.error); 144 | 145 | client.connect().then(() => console.log("Connected!")); 146 | -------------------------------------------------------------------------------- /src/ws.ts: -------------------------------------------------------------------------------- 1 | import type { VoiceConnection } from "./conn.ts"; 2 | import { ENCRYPTION_MODE, OpCode, VOICE_VERSION } from "./types.ts"; 3 | 4 | export class VoiceWebSocket { 5 | ws?: WebSocket; 6 | 7 | ping = 0; 8 | #heartbeatInterval?: number; 9 | #heartbeatTimer?: number; 10 | #lastHeartbeatSent?: number; 11 | #lastHeartbeatACK?: number; 12 | 13 | ready = false; 14 | 15 | ssrc?: number; 16 | addr?: Deno.NetAddr; 17 | 18 | #ssrcMap: { [userID: string]: number } = {}; 19 | 20 | getUserFromSSRC(ssrc: number): string | undefined { 21 | for (const id in this.#ssrcMap) { 22 | if (this.#ssrcMap[id] === ssrc) return id; 23 | } 24 | } 25 | 26 | constructor(public conn: VoiceConnection) {} 27 | 28 | connect() { 29 | if (this.conn.guildID === undefined) { 30 | throw new Error("Provide a Guild ID before connecting to WebSocket"); 31 | } 32 | if (this.conn.userID === undefined) { 33 | throw new Error("Provide a User ID before connecting to WebSocket"); 34 | } 35 | if (this.conn.token === undefined) { 36 | throw new Error( 37 | "Provide a Token (voice server) before connecting to WebSocket", 38 | ); 39 | } 40 | if (this.conn.sessionID === undefined) { 41 | throw new Error("Provide a Session ID before connecting to WebSocket"); 42 | } 43 | 44 | this.ready = false; 45 | this.ws = new WebSocket(`wss://${this.conn.endpoint}/?v=${VOICE_VERSION}`); 46 | 47 | this.ws.onopen = () => { 48 | console.log(`[WSS] Open`); 49 | this.sendIdentify(); 50 | }; 51 | 52 | this.ws.onclose = ({ code, reason }) => { 53 | if (this.#heartbeatTimer) { 54 | clearInterval(this.#heartbeatTimer); 55 | this.#heartbeatTimer = undefined; 56 | } 57 | 58 | console.log(`[WSS] Close ${code} ${reason}`); 59 | switch (code) { 60 | case 1000: 61 | case 1005: 62 | case 4001: 63 | case 4002: 64 | case 4005: 65 | // case 4006: 66 | case 4009: 67 | case 4015: 68 | this.connect(); 69 | break; 70 | 71 | case 0: 72 | case 4014: 73 | break; 74 | 75 | default: 76 | throw new Error( 77 | `Voice WebSocket closed with code: ${code} (${reason || 78 | "no reason"})`, 79 | ); 80 | } 81 | }; 82 | 83 | this.ws.onmessage = (evt) => { 84 | const { op, d } = JSON.parse(evt.data); 85 | 86 | switch (op) { 87 | case OpCode.HELLO: 88 | this.#heartbeatInterval = d.heartbeat_interval as number; 89 | this.sendHeartbeat(); 90 | this.#heartbeatTimer = setInterval(() => { 91 | if ( 92 | this.#lastHeartbeatSent !== undefined && 93 | this.#lastHeartbeatACK !== undefined && 94 | this.#lastHeartbeatACK < this.#lastHeartbeatSent 95 | ) { 96 | clearInterval(this.#heartbeatTimer); 97 | this.#heartbeatTimer = undefined; 98 | this.ws!.close(1000, "Dead connection"); 99 | return; 100 | } 101 | this.sendHeartbeat(); 102 | }, this.#heartbeatInterval); 103 | break; 104 | 105 | case OpCode.READY: 106 | if (!d.modes.includes(ENCRYPTION_MODE)) { 107 | throw new Error("Encryption Mode not found"); 108 | } 109 | 110 | this.ssrc = d.ssrc as number; 111 | this.conn.udp.frameView.setUint32(8, this.ssrc, false); 112 | 113 | this.addr = { hostname: d.ip, port: d.port, transport: "udp" }; 114 | 115 | this.conn.udp.listen(); 116 | 117 | this.conn.udp.ipDiscovery().then((addr) => { 118 | if (this.conn.config.receive) this.conn.udp._startReceiver(); 119 | 120 | this.sendSelectProtocol({ 121 | address: addr.hostname, 122 | port: addr.port, 123 | }); 124 | }); 125 | break; 126 | 127 | case OpCode.SESSION_DESCRIPTION: 128 | this.conn.key = d.secret_key; 129 | this.conn.mode = d.mode; 130 | this.ready = true; 131 | break; 132 | 133 | case OpCode.SPEAKING: 134 | this.#ssrcMap[d.user_id] = d.ssrc; 135 | break; 136 | 137 | case OpCode.HEARTBEAT_ACK: 138 | this.#lastHeartbeatACK = Date.now(); 139 | 140 | if (this.#lastHeartbeatSent !== undefined) { 141 | this.ping = this.#lastHeartbeatACK - this.#lastHeartbeatSent; 142 | } 143 | break; 144 | 145 | case OpCode.CLIENT_DISCONNECT: 146 | delete this.#ssrcMap[d.user_id]; 147 | break; 148 | 149 | case OpCode.RESUMED: 150 | break; 151 | 152 | default: 153 | break; 154 | } 155 | }; 156 | 157 | this.ws.onerror = (evt) => { 158 | console.error(evt); 159 | }; 160 | } 161 | 162 | sendWS(op: OpCode, data: any) { 163 | if (this.ws === undefined || this.ws.readyState !== this.ws.OPEN) { 164 | return false; 165 | } 166 | this.ws!.send(JSON.stringify({ op, d: data })); 167 | return true; 168 | } 169 | 170 | sendIdentify() { 171 | return this.sendWS(OpCode.IDENTIFY, { 172 | server_id: this.conn.guildID, 173 | user_id: this.conn.userID, 174 | session_id: this.conn.sessionID, 175 | token: this.conn.token, 176 | }); 177 | } 178 | 179 | sendHeartbeat() { 180 | const sent = this.sendWS(OpCode.HEARTBEAT, Date.now()); 181 | if (sent) this.#lastHeartbeatSent = Date.now(); 182 | return sent; 183 | } 184 | 185 | sendSelectProtocol({ address, port }: { address: string; port: number }) { 186 | return this.sendWS(OpCode.SELECT_PROTOCOL, { 187 | protocol: "udp", 188 | data: { address, port, mode: this.conn.config.mode ?? ENCRYPTION_MODE }, 189 | }); 190 | } 191 | 192 | sendSpeaking(flags: number, delay = 0) { 193 | return this.sendWS(OpCode.SPEAKING, { 194 | speaking: flags, 195 | ssrc: this.ssrc, 196 | delay, 197 | }); 198 | } 199 | 200 | close(code = 1000, reason = "") { 201 | this.ws?.close(code, reason); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /src/udp.ts: -------------------------------------------------------------------------------- 1 | import type { VoiceConnection } from "./conn.ts"; 2 | import { secretbox } from "../deps.ts"; 3 | import { 4 | ENCRYPTION_MODE, 5 | FRAME_SIZE, 6 | MAX_PACKET_SIZE, 7 | MAX_SEQ, 8 | MAX_TIMESTAMP, 9 | } from "./types.ts"; 10 | 11 | const frame = new Uint8Array(MAX_PACKET_SIZE); 12 | frame.set([0x80, 0x78], 0); 13 | 14 | const textDecoder = new TextDecoder(); 15 | 16 | export class VoiceUDP { 17 | socket?: Deno.DatagramConn; 18 | 19 | #nonce = new Uint8Array(secretbox.nonce_length); 20 | #nonceView = new DataView(this.#nonce.buffer); 21 | 22 | #frame = frame.slice(); 23 | #frameView = new DataView(this.#frame.buffer); 24 | 25 | get frameView() { 26 | return this.#frameView; 27 | } 28 | 29 | #seq = 0; 30 | #timestamp = 0; 31 | #nonceIncr?: number; 32 | 33 | #receivers: { 34 | [user: string]: [ReadableStream, ReadableStreamDefaultController]; 35 | } = {}; 36 | 37 | constructor(public conn: VoiceConnection) {} 38 | 39 | listen() { 40 | if (this.socket) { 41 | this.socket.close(); 42 | } 43 | 44 | this.socket = Deno.listenDatagram({ 45 | hostname: "0.0.0.0", 46 | port: 0, 47 | transport: "udp", 48 | }); 49 | } 50 | 51 | #recvStarted = false; 52 | 53 | async _startReceiver() { 54 | if (this.#recvStarted) return false; 55 | this.#recvStarted = true; 56 | const receive = async () => { 57 | try { 58 | const [data] = await this.socket!.receive(); 59 | if (data[0] === 0x80 && data[1] === 0x78 && data.length >= 12) { 60 | const view = new DataView(data.buffer); 61 | const ssrc = view.getUint32(8, false); 62 | 63 | const userID = this.conn.ws.getUserFromSSRC(ssrc); 64 | if (userID) { 65 | const nonce = new Uint8Array(secretbox.nonce_length); 66 | let end; 67 | switch ( 68 | this.conn.mode ?? this.conn.config.mode ?? ENCRYPTION_MODE 69 | ) { 70 | case "xsalsa20_poly1305": 71 | nonce.set(data.subarray(0, 12)); 72 | end = data.length; 73 | break; 74 | 75 | case "xsalsa20_poly1305_lite": 76 | nonce.set(data.subarray(data.length - 4, data.length)); 77 | end = data.length - 4; 78 | break; 79 | 80 | case "xsalsa20_poly1305_suffix": 81 | nonce.set(data.subarray(data.length - 24, data.length)); 82 | end = data.length - 24; 83 | break; 84 | 85 | default: 86 | throw new Error( 87 | "Unsupported encryption mode " + this.conn.mode, 88 | ); 89 | } 90 | 91 | const audio = data.subarray(12, end); 92 | const opus = secretbox.open(audio, this.conn.key, nonce); 93 | 94 | this.readable(userID); // ensure stream exists 95 | const ctx = this.#receivers[userID][1]; 96 | 97 | if (this.conn.config.receive === "opus") { 98 | ctx.enqueue(opus); 99 | } else { 100 | ctx.enqueue(opus); 101 | } 102 | } 103 | } // ignore other packets 104 | await receive(); 105 | } catch (e) {} 106 | }; 107 | await receive(); 108 | } 109 | 110 | async ipDiscovery(): Promise { 111 | const buf = new Uint8Array(70); 112 | 113 | let view = new DataView(buf.buffer); 114 | view.setUint16(0, 0x1, false); 115 | view.setUint16(2, 70, false); 116 | view.setUint32(4, this.conn.ws?.ssrc!, false); 117 | 118 | await this.socket!.send(buf, this.conn.ws?.addr!); 119 | 120 | const [recv] = await this.socket!.receive(); 121 | view = new DataView(recv.buffer); 122 | const port = view.getUint16(recv.byteLength - 2, false); 123 | const hostname = textDecoder.decode( 124 | recv.subarray(1 + recv.indexOf(0, 3), recv.indexOf(0, 4)), 125 | ); 126 | 127 | return { port, hostname, transport: "udp" }; 128 | } 129 | 130 | readable(userID: string): ReadableStream { 131 | const has = this.#receivers[userID]?.[0]; 132 | if (has) return has; 133 | else { 134 | let ctx: ReadableStreamDefaultController | undefined; 135 | const stream = new ReadableStream({ 136 | start: (c) => { 137 | ctx = c; 138 | }, 139 | }); 140 | this.#receivers[userID] = [stream, ctx!]; 141 | return stream; 142 | } 143 | } 144 | 145 | async sendVoice(opus: Uint8Array) { 146 | this.#seq++; 147 | if (MAX_SEQ <= this.#seq) { 148 | this.#seq -= MAX_SEQ; 149 | } 150 | 151 | this.#timestamp += FRAME_SIZE; 152 | if (MAX_TIMESTAMP <= this.#timestamp) { 153 | this.#timestamp %= MAX_TIMESTAMP; 154 | } 155 | 156 | this.#frameView.setUint16(2, this.#seq, false); 157 | this.#frameView.setUint32(4, this.#timestamp, false); 158 | 159 | let audio; 160 | let end; 161 | switch (this.conn.mode) { 162 | case "xsalsa20_poly1305": 163 | // Nonce is 12 bytes copied from RTP header 164 | this.#nonce.set(this.#frame.subarray(0, 12)); 165 | 166 | audio = secretbox.seal(opus, this.conn.key, this.#nonce); 167 | this.#frame.set(audio, 12); 168 | end = audio.length + 12; 169 | break; 170 | 171 | case "xsalsa20_poly1305_suffix": 172 | // Nonce is random 24 bytes 173 | crypto.getRandomValues(this.#nonce); 174 | 175 | audio = secretbox.seal(opus, this.conn.key, this.#nonce); 176 | this.#frame.set(audio, 12); 177 | // Nonce is stored in last 24 bytes of frame 178 | this.#frame.set(this.#nonce, 12 + audio.length); 179 | end = audio.length + 12 + 24; 180 | break; 181 | 182 | case "xsalsa20_poly1305_lite": 183 | // Nonce is a incrementing u32 184 | this.#nonceIncr = this.#nonceIncr || -1; 185 | this.#nonceIncr++; 186 | if (MAX_TIMESTAMP <= this.#nonceIncr) this.#nonceIncr = 0; 187 | this.#nonceView.setUint32(0, this.#nonceIncr, false); 188 | 189 | audio = secretbox.seal(opus, this.conn.key, this.#nonce); 190 | this.#frame.set(audio, 12); 191 | // Nonce is stored in last 4 bytes of frame 192 | this.#frame.set(this.#nonce.subarray(0, 4), 12 + audio.length); 193 | end = audio.length + 12 + 4; 194 | break; 195 | 196 | default: 197 | throw new Error("Unsupported encryption mode"); 198 | } 199 | 200 | const buffer = this.#frame.subarray(0, end); 201 | return this.socket!.send( 202 | buffer, 203 | this.conn.ws?.addr!, 204 | ); 205 | } 206 | 207 | close() { 208 | this.socket!.close(); 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2021-2022 DjDeveloperr 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------