From 1072cf1e05ec8de8090591be3e457165a8184e3d Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Mon, 19 Aug 2024 07:49:20 +0000 Subject: [PATCH] web: move libav.ts to its own directory --- web/src/lib/{ => libav}/libav.ts | 37 +++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) rename web/src/lib/{ => libav}/libav.ts (95%) diff --git a/web/src/lib/libav.ts b/web/src/lib/libav/libav.ts similarity index 95% rename from web/src/lib/libav.ts rename to web/src/lib/libav/libav.ts index 343d7e01..a94c584d 100644 --- a/web/src/lib/libav.ts +++ b/web/src/lib/libav/libav.ts @@ -1,9 +1,11 @@ import mime from "mime"; import LibAV, { type LibAV as LibAVInstance, type Packet, type Stream } from "@imput/libav.js-encode-cli"; -import type { Chunk, ChunkMetadata, Decoder, FFmpegProgressCallback, FFmpegProgressEvent, FFmpegProgressStatus, FileInfo, OutputStream, RenderingPipeline, RenderParams } from "./types/libav"; +import type { Chunk, ChunkMetadata, Decoder, FFmpegProgressCallback, FFmpegProgressEvent, FFmpegProgressStatus, FileInfo, OutputStream, RenderingPipeline, RenderParams } from "../types/libav"; import type { FfprobeData } from "fluent-ffmpeg"; import * as LibAVWebCodecs from "libavjs-webcodecs-bridge"; import { BufferStream } from "./buffer-stream"; +import { BufferStream } from "../buffer-stream"; +import WebCodecsWrapper from "./webcodecs"; import { browser } from "$app/environment"; const QUEUE_THRESHOLD_MIN = 16; @@ -11,11 +13,13 @@ const QUEUE_THRESHOLD_MAX = 128; export default class LibAVWrapper { libav: Promise | null; + webcodecs: WebCodecsWrapper | null; concurrency: number; onProgress?: FFmpegProgressCallback; constructor(onProgress?: FFmpegProgressCallback) { this.libav = null; + this.webcodecs = null; this.concurrency = Math.min(4, browser ? navigator.hardwareConcurrency : 0); this.onProgress = onProgress; } @@ -26,6 +30,8 @@ export default class LibAVWrapper { yesthreads: true, base: '/_libav' }); + + this.webcodecs = new WebCodecsWrapper(await this.libav); } } @@ -38,11 +44,16 @@ export default class LibAVWrapper { async #get() { if (!this.libav) throw new Error("LibAV wasn't initialized"); - return await this.libav; + if (!this.webcodecs) throw new Error("unreachable"); + + return { + libav: await this.libav, + webcodecs: this.webcodecs + }; } async probe(blob: Blob) { - const libav = await this.#get(); + const { libav } = await this.#get(); await libav.mkreadaheadfile('input', blob); @@ -81,7 +92,7 @@ export default class LibAVWrapper { } async remux({ blob, output, args }: RenderParams) { - const libav = await this.#get(); + const { libav } = await this.#get(); const inputKind = blob.type.split("/")[0]; const inputExtension = LibAVWrapper.getExtensionFromType(blob); @@ -194,7 +205,7 @@ export default class LibAVWrapper { } async transcode(blob: Blob) { - const libav = await this.#get(); + const { libav } = await this.#get(); let fmtctx; await libav.mkreadaheadfile('input', blob); @@ -310,7 +321,7 @@ export default class LibAVWrapper { } async #processChunk({ chunk, metadata }: { chunk: Chunk, metadata: ChunkMetadata }, ostream: OutputStream, index: number) { - const libav = await this.#get(); + const { libav } = await this.#get(); let convertToPacket; if (chunk instanceof EncodedVideoChunk) { @@ -323,7 +334,7 @@ export default class LibAVWrapper { } async #mux(pipes: RenderingPipeline[], ostreams: OutputStream[]) { - const libav = await this.#get(); + const { libav } = await this.#get(); const write_pkt = await libav.av_packet_alloc(); let writer_ctx = 0, output_ctx = 0; @@ -414,7 +425,7 @@ export default class LibAVWrapper { } async* #demux(fmt_ctx: number) { - const libav = await this.#get(); + const { libav } = await this.#get(); const read_pkt = await libav.av_packet_alloc(); try { @@ -442,7 +453,7 @@ export default class LibAVWrapper { } async #createEncoder(stream: Stream, codec: string) { - const libav = await this.#get(); + const { libav } = await this.#get(); let streamToConfig, configToStream, Encoder; @@ -502,16 +513,16 @@ export default class LibAVWrapper { } async #createDecoder(stream: Stream) { - const libav = await this.#get(); + const { libav } = await this.#get(); - let streamToConfig, Decoder; + let streamToConfig, initDecoder; if (stream.codec_type === libav.AVMEDIA_TYPE_VIDEO) { streamToConfig = LibAVWebCodecs.videoStreamToConfig; - Decoder = VideoDecoder; + initDecoder = this.webcodecs.initVideoDecoder; } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) { streamToConfig = LibAVWebCodecs.audioStreamToConfig; - Decoder = AudioDecoder; + initDecoder = this.webcodecs.initAudioDecoder; } else throw "Unknown type: " + stream.codec_type; const config = await streamToConfig(libav, stream);