Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ESM, enable TypeScript strict mode #92

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "@dank074/discord-video-stream",
"version": "3.2.0",
"description": "Experiment for making video streaming work for discord selfbots",
"main": "dist/index.js",
"exports": "./dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"files": [
"dist",
"src"
Expand Down
49 changes: 31 additions & 18 deletions src/client/Streamer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { VoiceConnection } from "./voice/VoiceConnection";
import { VoiceConnection } from "./voice/VoiceConnection.js";
import { Client } from 'discord.js-selfbot-v13';
import { MediaUdp } from "./voice/MediaUdp";
import { StreamConnection } from "./voice/StreamConnection";
import { GatewayOpCodes } from "./GatewayOpCodes";
import { StreamOptions } from "./voice";
import { MediaUdp } from "./voice/MediaUdp.js";
import { StreamConnection } from "./voice/StreamConnection.js";
import { GatewayOpCodes } from "./GatewayOpCodes.js";
import { StreamOptions } from "./voice/index.js";

export class Streamer {
private _voiceConnection?: VoiceConnection;
Expand Down Expand Up @@ -34,8 +34,13 @@ export class Streamer {
});
}

public joinVoice(guild_id: string, channel_id: string, options?: StreamOptions): Promise<MediaUdp> {
public joinVoice(guild_id: string, channel_id: string, options?: Partial<StreamOptions>): Promise<MediaUdp> {
return new Promise<MediaUdp>((resolve, reject) => {
if (!this.client.user)
{
reject("Client not logged in");
return;
}
this._voiceConnection = new VoiceConnection(
guild_id,
this.client.user.id,
Expand All @@ -49,11 +54,19 @@ export class Streamer {
});
}

public createStream(options?: StreamOptions): Promise<MediaUdp> {
public createStream(options?: Partial<StreamOptions>): Promise<MediaUdp> {
return new Promise<MediaUdp>((resolve, reject) => {
if (!this.client.user)
{
reject("Client not logged in");
return;
}
if (!this.voiceConnection)
{
reject("cannot start stream without first joining voice channel");

return;
}

this.signalStream(
this.voiceConnection.guildId,
this.voiceConnection.channelId
Expand Down Expand Up @@ -110,14 +123,14 @@ export class Streamer {
});

this.sendOpcode(GatewayOpCodes.STREAM_SET_PAUSED, {
stream_key: `guild:${guild_id}:${channel_id}:${this.client.user.id}`,
stream_key: `guild:${guild_id}:${channel_id}:${this.client.user!.id}`,
paused: false,
});
}

public signalStopStream(guild_id: string, channel_id: string): void {
this.sendOpcode(GatewayOpCodes.STREAM_DELETE, {
stream_key: `guild:${guild_id}:${channel_id}:${this.client.user.id}`
stream_key: `guild:${guild_id}:${channel_id}:${this.client.user!.id}`
});
}

Expand All @@ -134,7 +147,7 @@ export class Streamer {
private handleGatewayEvent(event: string, data: any): void {
switch(event) {
case "VOICE_STATE_UPDATE": {
if (data.user_id === this.client.user.id) {
if (data.user_id === this.client.user!.id) {
// transfer session data to voice connection
this.voiceConnection?.setSession(data.session_id);
}
Expand All @@ -152,12 +165,12 @@ export class Streamer {

if (this.voiceConnection?.guildId != guildId) return;

if (userId === this.client.user.id) {
this.voiceConnection.streamConnection.serverId = data.rtc_server_id;
if (userId === this.client.user!.id) {
this.voiceConnection!.streamConnection!.serverId = data.rtc_server_id;

this.voiceConnection.streamConnection.streamKey = data.stream_key;
this.voiceConnection.streamConnection.setSession(
this.voiceConnection.session_id
this.voiceConnection!.streamConnection!.streamKey = data.stream_key;
this.voiceConnection!.streamConnection!.setSession(
this.voiceConnection!.session_id!
);
}
break;
Expand All @@ -167,8 +180,8 @@ export class Streamer {

if (this.voiceConnection?.guildId != guildId) return;

if (userId === this.client.user.id) {
this.voiceConnection.streamConnection.setTokens(
if (userId === this.client.user!.id) {
this.voiceConnection!.streamConnection!.setTokens(
data.endpoint,
data.token
);
Expand Down
8 changes: 4 additions & 4 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './packet';
export * from './voice';
export * from "./GatewayOpCodes";
export * from "./Streamer";
export * from './packet/index.js';
export * from './voice/index.js';
export * from "./GatewayOpCodes.js";
export * from "./Streamer.js";
4 changes: 2 additions & 2 deletions src/client/packet/AudioPacketizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MediaUdp } from "../voice/MediaUdp";
import { BaseMediaPacketizer } from "./BaseMediaPacketizer";
import { MediaUdp } from "../voice/MediaUdp.js";
import { BaseMediaPacketizer } from "./BaseMediaPacketizer.js";

const frame_size = (48000 / 100) * 2;

Expand Down
13 changes: 9 additions & 4 deletions src/client/packet/BaseMediaPacketizer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { crypto_secretbox_easy } from "libsodium-wrappers";
import { MediaUdp } from "../voice/MediaUdp";
import _sodium from "libsodium-wrappers";
import { MediaUdp } from "../voice/MediaUdp.js";

export const max_int16bit = 2 ** 16;
export const max_int32bit = 2 ** 32;

const ntpEpoch = new Date("Jan 01 1900 GMT").getTime();

await _sodium.ready;
const crypto_secretbox_easy = _sodium.crypto_secretbox_easy;

export class BaseMediaPacketizer {
private _ssrc: number;
private _payloadType: number;
Expand All @@ -28,7 +31,9 @@ export class BaseMediaPacketizer {
this._sequence = 0;
this._timestamp = 0;
this._totalBytes = 0;
this._totalPackets = 0;
this._prevTotalPackets = 0;
this._lastPacketTime = 0;
this._mtu = 1200;
this._extensionEnabled = extensionEnabled;

Expand Down Expand Up @@ -154,7 +159,7 @@ export class BaseMediaPacketizer {
const nonceBuffer = this._mediaUdp.getNewNonceBuffer();
return Buffer.concat([
packetHeader,
crypto_secretbox_easy(senderReport, nonceBuffer, this._mediaUdp.mediaConnection.secretkey),
crypto_secretbox_easy(senderReport, nonceBuffer, this._mediaUdp.mediaConnection.secretkey!),
nonceBuffer.subarray(0, 4)
]);
}
Expand Down Expand Up @@ -214,7 +219,7 @@ export class BaseMediaPacketizer {
// encrypts all data that is not in rtp header.
// rtp header extensions and payload headers are also encrypted
public encryptData(message: string | Uint8Array, nonceBuffer: Buffer) : Uint8Array {
return crypto_secretbox_easy(message, nonceBuffer, this._mediaUdp.mediaConnection.secretkey);
return crypto_secretbox_easy(message, nonceBuffer, this._mediaUdp.mediaConnection.secretkey!);
}

public get mediaUdp(): MediaUdp {
Expand Down
17 changes: 8 additions & 9 deletions src/client/packet/VideoPacketizerAnnexB.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MediaUdp } from "../voice/MediaUdp";
import { BaseMediaPacketizer } from "./BaseMediaPacketizer";
import { MediaUdp } from "../voice/MediaUdp.js";
import { BaseMediaPacketizer } from "./BaseMediaPacketizer.js";
import {
H264Helpers,
H265Helpers,
type AnnexBHelpers
} from "../processing/AnnexBHelper";
} from "../processing/AnnexBHelper.js";

/**
* Annex B format
Expand Down Expand Up @@ -56,11 +56,12 @@ import {
...
*/
class VideoPacketizerAnnexB extends BaseMediaPacketizer {
protected _nalFunctions: AnnexBHelpers;
private _nalFunctions: AnnexBHelpers;

constructor(connection: MediaUdp) {
constructor(connection: MediaUdp, nalFunctions: AnnexBHelpers) {
super(connection, 0x65, true);
this.srInterval = 5 * connection.mediaConnection.streamOptions.fps * 3; // ~5 seconds, assuming ~3 packets per frame
this._nalFunctions = nalFunctions;
}

/**
Expand Down Expand Up @@ -158,8 +159,7 @@ class VideoPacketizerAnnexB extends BaseMediaPacketizer {

export class VideoPacketizerH264 extends VideoPacketizerAnnexB {
constructor(connection: MediaUdp) {
super(connection);
this._nalFunctions = H264Helpers;
super(connection, H264Helpers);
}
/**
* The FU indicator octet has the following format:
Expand Down Expand Up @@ -215,8 +215,7 @@ export class VideoPacketizerH264 extends VideoPacketizerAnnexB {

export class VideoPacketizerH265 extends VideoPacketizerAnnexB {
constructor(connection: MediaUdp) {
super(connection);
this._nalFunctions = H265Helpers;
super(connection, H265Helpers);
}
/**
* The FU indicator octet has the following format:
Expand Down
4 changes: 2 additions & 2 deletions src/client/packet/VideoPacketizerVP8.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MediaUdp } from "../voice/MediaUdp";
import { BaseMediaPacketizer, max_int16bit } from "./BaseMediaPacketizer";
import { MediaUdp } from "../voice/MediaUdp.js";
import { BaseMediaPacketizer, max_int16bit } from "./BaseMediaPacketizer.js";

/**
* VP8 payload format
Expand Down
8 changes: 4 additions & 4 deletions src/client/packet/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './AudioPacketizer';
export * from './BaseMediaPacketizer';
export * from './VideoPacketizerVP8';
export * from './VideoPacketizerAnnexB';
export * from './AudioPacketizer.js';
export * from './BaseMediaPacketizer.js';
export * from './VideoPacketizerVP8.js';
export * from './VideoPacketizerAnnexB.js';
18 changes: 11 additions & 7 deletions src/client/processing/AnnexBNalSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
H264NalUnitTypes,
H265NalUnitTypes,
type AnnexBHelpers
} from "./AnnexBHelper";
} from "./AnnexBHelper.js";

const emptyBuffer = Buffer.allocUnsafe(0);
const epbPrefix = Buffer.from([0x00, 0x00, 0x03]);
Expand All @@ -20,9 +20,15 @@ const nalSuffix = Buffer.from([0x00, 0x00, 0x01]);
* unit is composed of 1 to n Nal units
*/
class AnnexBNalSplitter extends Transform {
private _buffer: Buffer;
private _buffer: Buffer | null = null;
private _accessUnit: Buffer[] = [];
protected _nalFunctions: AnnexBHelpers;
private _nalFunctions: AnnexBHelpers;

constructor(nalFunctions: AnnexBHelpers)
{
super();
this._nalFunctions = nalFunctions;
}

/**
* Removes emulation prevention bytes from a nalu frame
Expand Down Expand Up @@ -126,8 +132,7 @@ class AnnexBNalSplitter extends Transform {
export class H264NalSplitter extends AnnexBNalSplitter {
constructor()
{
super();
this._nalFunctions = H264Helpers;
super(H264Helpers);
}
removeEpbs(frame: Buffer, unitType: number): Buffer {
if (unitType === H264NalUnitTypes.SPS || unitType === H264NalUnitTypes.SEI)
Expand All @@ -139,8 +144,7 @@ export class H264NalSplitter extends AnnexBNalSplitter {
export class H265NalSplitter extends AnnexBNalSplitter {
constructor()
{
super();
this._nalFunctions = H265Helpers;
super(H265Helpers);
}
removeEpbs(frame: Buffer, unitType: number): Buffer {
// We do not remove the EPBS, since the encoder expects it to be there
Expand Down
12 changes: 6 additions & 6 deletions src/client/processing/IvfSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type IvfHeader = {
class IvfTransformer extends Transform {
public headerSize: number;
public frameHeaderSize: number;
public header: IvfHeader;
public buf: Buffer;
public header: IvfHeader | null;
public buf: Buffer | null;
public retFullFrame: boolean;

constructor(options?: any) {
Expand Down Expand Up @@ -77,8 +77,8 @@ class IvfTransformer extends Transform {
}

_updateBufLen(size: number) {
if (this.buf.length > size)
this.buf = this.buf.subarray(size, this.buf.length);
if (this.buf!.length > size)
this.buf = this.buf!.subarray(size, this.buf!.length);
else
this.buf = null;
}
Expand All @@ -88,8 +88,8 @@ class IvfTransformer extends Transform {

// parse header
if (!this.header) {
if (this.buf.length >= this.headerSize) {
this._parseHeader(this.buf.subarray(0, this.headerSize));
if (this.buf!.length >= this.headerSize) {
this._parseHeader(this.buf!.subarray(0, this.headerSize));
this._updateBufLen(this.headerSize);
}
else {
Expand Down
Loading