web/libav: convert audio/frames to polyfill and back only if needed

This commit is contained in:
dumbmoron 2024-08-24 16:00:48 +00:00
parent 9f21a6eb46
commit d7b5b9e1e1
No known key found for this signature in database
2 changed files with 13 additions and 7 deletions

View file

@ -135,12 +135,13 @@ export default class EncodeLibAV extends LibAVWrapper {
// FIXME: figure out how to make typescript happy without this monstrosity // FIXME: figure out how to make typescript happy without this monstrosity
if (WebCodecsWrapper.isVideo(encoder)) { if (WebCodecsWrapper.isVideo(encoder)) {
WebCodecsWrapper.sendVideo( WebCodecsWrapper.encodeVideo(
value as VideoFrame, value as VideoFrame,
encoder as VideoEncoder encoder as VideoEncoder
); );
} else { } else {
WebCodecsWrapper.sendAudio( console.log(value);
WebCodecsWrapper.encodeAudio(
value as AudioData, value as AudioData,
encoder as AudioEncoder encoder as AudioEncoder
); );

View file

@ -99,7 +99,6 @@ export default class WebCodecsWrapper {
// FIXME: this is nasty, but whatever // FIXME: this is nasty, but whatever
async initAudioEncoder(config: AudioEncoderConfig, init: AudioEncoderInit) { async initAudioEncoder(config: AudioEncoderConfig, init: AudioEncoderInit) {
console.log(this);
const Encoder = await this.#getEncoder(config) as typeof AudioEncoder | null; const Encoder = await this.#getEncoder(config) as typeof AudioEncoder | null;
if (Encoder === null) return null; if (Encoder === null) return null;
@ -161,9 +160,12 @@ export default class WebCodecsWrapper {
|| obj instanceof LibAVPolyfill.EncodedAudioChunk; || 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 (destination instanceof LibAVPolyfill.AudioEncoder) {
if ('AudioData' in window && data instanceof AudioData) { if (hasAudioData && !isPolyfilled && data instanceof AudioData) {
const converted = LibAVPolyfill.AudioData.fromNative(data); const converted = LibAVPolyfill.AudioData.fromNative(data);
data.close(); data.close();
data = converted; data = converted;
@ -177,9 +179,12 @@ export default class WebCodecsWrapper {
return destination.encode(data); 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 (destination instanceof LibAVPolyfill.VideoEncoder) {
if ('VideoFrame' in window && data instanceof VideoFrame) { if (hasVideoFrame && !isPolyfilled && data instanceof VideoFrame) {
const converted = LibAVPolyfill.VideoFrame.fromNative(data); const converted = LibAVPolyfill.VideoFrame.fromNative(data);
data.close(); data.close();
data = converted; data = converted;