From d7b5b9e1e167f741e69b3cefae5d64952eb34af3 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 24 Aug 2024 16:00:48 +0000 Subject: [PATCH] web/libav: convert audio/frames to polyfill and back only if needed --- web/src/lib/libav/encode.ts | 5 +++-- web/src/lib/libav/webcodecs.ts | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/web/src/lib/libav/encode.ts b/web/src/lib/libav/encode.ts index 1731cb56..78e5b9f1 100644 --- a/web/src/lib/libav/encode.ts +++ b/web/src/lib/libav/encode.ts @@ -135,12 +135,13 @@ export default class EncodeLibAV extends LibAVWrapper { // FIXME: figure out how to make typescript happy without this monstrosity if (WebCodecsWrapper.isVideo(encoder)) { - WebCodecsWrapper.sendVideo( + WebCodecsWrapper.encodeVideo( value as VideoFrame, encoder as VideoEncoder ); } else { - WebCodecsWrapper.sendAudio( + console.log(value); + WebCodecsWrapper.encodeAudio( value as AudioData, encoder as AudioEncoder ); diff --git a/web/src/lib/libav/webcodecs.ts b/web/src/lib/libav/webcodecs.ts index b1c712dc..ddfac5e7 100644 --- a/web/src/lib/libav/webcodecs.ts +++ b/web/src/lib/libav/webcodecs.ts @@ -99,7 +99,6 @@ export default class WebCodecsWrapper { // FIXME: this is nasty, but whatever async initAudioEncoder(config: AudioEncoderConfig, init: AudioEncoderInit) { - console.log(this); const Encoder = await this.#getEncoder(config) as typeof AudioEncoder | null; if (Encoder === null) return null; @@ -161,9 +160,12 @@ export default class WebCodecsWrapper { || obj instanceof LibAVPolyfill.EncodedAudioChunk; } - static sendAudio(data: AudioData | LibAVPolyfill.AudioData, destination: AudioEncoder) { + static encodeAudio(data: AudioData | LibAVPolyfill.AudioData, destination: AudioEncoder) { + const hasAudioData = 'AudioData' in window; + const isPolyfilled = hasAudioData && window.AudioData === LibAVPolyfill.AudioData; + if (destination instanceof LibAVPolyfill.AudioEncoder) { - if ('AudioData' in window && data instanceof AudioData) { + if (hasAudioData && !isPolyfilled && data instanceof AudioData) { const converted = LibAVPolyfill.AudioData.fromNative(data); data.close(); data = converted; @@ -177,9 +179,12 @@ export default class WebCodecsWrapper { return destination.encode(data); } - static sendVideo(data: VideoFrame | LibAVPolyfill.VideoFrame, destination: VideoEncoder) { + static encodeVideo(data: VideoFrame | LibAVPolyfill.VideoFrame, destination: VideoEncoder) { + const hasVideoFrame = 'VideoFrame' in window; + const isPolyfilled = hasVideoFrame && VideoFrame === LibAVPolyfill.VideoFrame; + if (destination instanceof LibAVPolyfill.VideoEncoder) { - if ('VideoFrame' in window && data instanceof VideoFrame) { + if (hasVideoFrame && !isPolyfilled && data instanceof VideoFrame) { const converted = LibAVPolyfill.VideoFrame.fromNative(data); data.close(); data = converted;