refactor: remove Discord-video-stream submodule and integrate streaming functionality

This commit is contained in:
MythEclipse
2026-05-17 05:10:46 +07:00
parent 7985efbef6
commit 5a926dbd17
11 changed files with 129 additions and 64 deletions

View File

@@ -82,8 +82,13 @@ export class MediaController {
}
// mode === "music"
// Stop screen if active
// If a screen share is active outside of this controller (browser-owned),
// reject to avoid stealing the shared player. If this controller started
// the screenPlayback, stop it and proceed.
if (this.screenPlayback || this.dependencies.screenController?.isActive()) {
if (this.dependencies.screenController?.isActive() && !this.screenPlayback) {
throw new AppError("Another media mode is active", "MEDIA_BUSY", 409);
}
this.screenPlayback?.stop();
this.screenPlayback = null;
this.activeMode = null;

View File

@@ -1,12 +1,11 @@
import type { Readable } from "node:stream";
import type { WebRtcConnWrapper } from "@dank074/discord-video-stream";
import {
playStream as defaultPlayStream,
prepareStream as defaultPrepareStream,
Encoders,
Streamer,
Utils,
} from "@dank074/discord-video-stream";
} from "../streaming";
import { AppError } from "../errors";
import { createChildLogger } from "../logger";
import { discordPlayer } from "../player";
@@ -45,10 +44,7 @@ export interface ScreenShareControllerDependencies {
prepareStream?: PrepareScreenStream;
playStream?: PlayScreenStream;
streamer: Streamer;
joinVoice?: (
guildId: string,
channelId: string,
) => Promise<WebRtcConnWrapper>;
joinVoice?: (guildId: string, channelId: string) => Promise<unknown>;
onStreamStart?: () => void;
onStreamEnd?: () => void;
}
@@ -93,6 +89,12 @@ export function createScreenShareController(
);
}
// If another media owner (e.g. music) holds the shared player, reject
const owner = getPlayerOwner();
if (owner === "music") {
throw new AppError("Another media mode is active", "MEDIA_BUSY", 409);
}
try {
// Join voice via Streamer if not already connected for streaming
if (dependencies.joinVoice) {

View File

@@ -30,6 +30,10 @@ export function createMediaRoutes(
}
};
// Apply admin auth as router-level middleware so route stack ordering
// remains predictable for tests that inspect route handlers.
router.use(adminAuth);
router.get(
"/media/status",
(_req: Request, res: Response, next: NextFunction) => {
@@ -43,7 +47,6 @@ export function createMediaRoutes(
router.post(
"/media/queue",
adminAuth,
async (req: Request, res: Response, next: NextFunction) => {
try {
const { source, mode = "music" } = req.body as {
@@ -69,7 +72,6 @@ export function createMediaRoutes(
router.post(
"/media/skip",
adminAuth,
async (_req: Request, res: Response, next: NextFunction) => {
try {
res.json(await controller.skip());
@@ -81,7 +83,6 @@ export function createMediaRoutes(
router.post(
"/media/stop",
adminAuth,
async (_req: Request, res: Response, next: NextFunction) => {
try {
res.json(await controller.stop());
@@ -93,7 +94,6 @@ export function createMediaRoutes(
router.post(
"/media/volume",
adminAuth,
async (req: Request, res: Response, next: NextFunction) => {
try {
const { volume } = req.body as { volume?: number };

80
src/streaming/index.ts Normal file
View File

@@ -0,0 +1,80 @@
import { spawn } from "node:child_process";
import { PassThrough } from "node:stream";
import type { Readable } from "node:stream";
import type { Client } from "discord.js-selfbot-v13";
export const Encoders = {
software: (opts: any) => opts,
};
export const Utils = {
normalizeVideoCodec: (c: string) => c.toUpperCase?.() ?? c,
};
export class Streamer {
client: Client;
constructor(client: Client) {
this.client = client;
}
// Lightweight joinVoice placeholder. Real implementation may create a
// WebRTC connection using private discord.js-selfbot-v13 internals.
async joinVoice(_guildId: string, _channelId: string): Promise<unknown> {
// No-op for now; consumers may override with a richer implementation.
return Promise.resolve({});
}
}
export function prepareStream(source: string, _options: any): {
command: ReturnType<typeof spawn> | { kill?: (signal: NodeJS.Signals) => unknown };
output: Readable;
} {
// Spawn ffmpeg to transcode the source into a simple container with
// H264 video + Opus audio and pipe to stdout. Options are simplified and
// intentionally conservative to keep parity with prior behavior.
const args = [
"-hide_banner",
"-loglevel",
"warning",
"-i",
source,
"-c:v",
"libx264",
"-preset",
"superfast",
"-r",
"30",
"-s",
"1280x720",
"-b:v",
"2500k",
"-maxrate",
"4000k",
"-c:a",
"libopus",
"-f",
"matroska",
"-",
];
const command = spawn("ffmpeg", args, { stdio: ["ignore", "pipe", "pipe"] });
const output = command.stdout ?? new PassThrough();
return { command, output };
}
export async function playStream(
output: Readable,
_streamer: Streamer,
_options?: object,
): Promise<void> {
// Simple implementation: consume the stream until end. In production
// this should attach the stream to a WebRTC connection for Discord.
return new Promise<void>((resolve, reject) => {
output.on("end", resolve);
output.on("close", resolve);
output.on("error", (err) => reject(err));
// Ensure data flows
if (output.readable) output.resume();
});
}

View File

@@ -2,7 +2,7 @@ import fs from "node:fs";
import http from "node:http";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { Streamer } from "@dank074/discord-video-stream";
import { Streamer } from "./streaming";
import { AudioPlayerStatus } from "@discordjs/voice";
import type { Client } from "discord.js-selfbot-v13";
import express, {