Skip to content

Commit

Permalink
Add some logging, add disclaimer about Deno and Bun
Browse files Browse the repository at this point in the history
  • Loading branch information
longnguyen2004 committed Mar 1, 2025
1 parent 8f57a09 commit 9327bcf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/media/LibavDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ import LibAV from "@lng2004/libav.js-variant-webcodecs-avf-with-decoders";

let libavInstance: Promise<LibAV.LibAV>;

// @ts-expect-error
const isDeno = typeof Deno !== "undefined";
// @ts-expect-error
const isBun = typeof Bun !== "undefined";

export async function createDecoder(id: number, codecpar: LibAV.CodecParameters)
{
if (isDeno || isBun)
{
console.error(
"The decoder currently doesn't work with Deno and Bun, due to " +
"various issues with Emscripten's pthread support leading to " +
"crashes. The decoder will not be initialized"
);
return null;
}
libavInstance ??= LibAV.LibAV({ yesthreads: true });
let freed = false;
let serializer: Promise<unknown> | null = null
Expand Down
14 changes: 14 additions & 0 deletions src/media/newApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ffmpeg from 'fluent-ffmpeg';
import pDebounce from 'p-debounce';
import sharp from 'sharp';
import Log from 'debug-level';
import { demux } from './LibavDemuxer.js';
import { setTimeout as delay } from 'node:timers/promises';
import { PassThrough, type Readable } from "node:stream";
Expand Down Expand Up @@ -357,10 +358,12 @@ export async function playStream(
cancelSignal?: AbortSignal
)
{
const logger = new Log("playStream");
cancelSignal?.throwIfAborted();
if (!streamer.voiceConnection)
throw new Error("Bot is not connected to a voice channel");

logger.debug("Initializing demuxer");
const { video, audio } = await demux(input);
cancelSignal?.throwIfAborted();

Expand Down Expand Up @@ -417,6 +420,7 @@ export async function playStream(
}

const mergedOptions = mergeOptions(options);
logger.debug({ options: mergedOptions }, "Merged options");

let udp: MediaUdp;
let stopStream: () => unknown;
Expand Down Expand Up @@ -466,14 +470,24 @@ export async function playStream(
if (mergedOptions.streamPreview && mergedOptions.type === "go-live")
{
(async () => {
const logger = new Log("playStream:preview");
logger.debug("Initializing decoder for stream preview");
const decoder = await createDecoder(video.codec, video.codecpar);
if (!decoder)
{
logger.warn("Failed to initialize decoder. Stream preview will be disabled");
return;
}
cleanupFuncs.push(() => decoder.free());
const updatePreview = pDebounce.promise(async (packet: LibAV.Packet) => {
if (!(packet.flags !== undefined && packet.flags & LibAV.AV_PKT_FLAG_KEY))
return;
const decodeStart = performance.now();
const [frame] = await decoder.decode([packet]).catch(() => []);
if (!frame)
return;
const decodeEnd = performance.now();
logger.debug(`Decoding a frame took ${decodeEnd - decodeStart}ms`);

return Promise.all([
delay(5000),
Expand Down

0 comments on commit 9327bcf

Please sign in to comment.