mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-02 05:13:50 +02:00
Make sure encoders are ensured only once
This commit is contained in:
+22
-3
@@ -14,7 +14,7 @@ import {
|
|||||||
VideoCodec,
|
VideoCodec,
|
||||||
} from './codec';
|
} from './codec';
|
||||||
import { OutputAudioTrack, OutputSubtitleTrack, OutputTrack, OutputVideoTrack } from './output';
|
import { OutputAudioTrack, OutputSubtitleTrack, OutputTrack, OutputVideoTrack } from './output';
|
||||||
import { assert, clamp, setInt24, setUint24 } from './misc';
|
import { assert, clamp, promiseWithResolvers, setInt24, setUint24 } from './misc';
|
||||||
import { Muxer } from './muxer';
|
import { Muxer } from './muxer';
|
||||||
import { SubtitleParser } from './subtitles';
|
import { SubtitleParser } from './subtitles';
|
||||||
import { EncodedAudioSample, EncodedVideoSample } from './sample';
|
import { EncodedAudioSample, EncodedVideoSample } from './sample';
|
||||||
@@ -161,6 +161,7 @@ const validateVideoEncodingConfig = (config: VideoEncodingConfig) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class VideoEncoderWrapper {
|
class VideoEncoderWrapper {
|
||||||
|
private ensureEncoderPromise: Promise<void> | null = null;
|
||||||
private encoder: VideoEncoder | null = null;
|
private encoder: VideoEncoder | null = null;
|
||||||
private muxer: Muxer | null = null;
|
private muxer: Muxer | null = null;
|
||||||
private lastMultipleOfKeyFrameInterval = -1;
|
private lastMultipleOfKeyFrameInterval = -1;
|
||||||
@@ -188,7 +189,11 @@ class VideoEncoderWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.encoder) {
|
if (!this.encoder) {
|
||||||
await this.ensureEncoder(videoFrame);
|
if (this.ensureEncoderPromise) {
|
||||||
|
await this.ensureEncoderPromise;
|
||||||
|
} else {
|
||||||
|
await this.ensureEncoder(videoFrame);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assert(this.encoder);
|
assert(this.encoder);
|
||||||
|
|
||||||
@@ -226,6 +231,9 @@ class VideoEncoderWrapper {
|
|||||||
throw new Error('VideoEncoder is not supported by this browser.');
|
throw new Error('VideoEncoder is not supported by this browser.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { promise, resolve } = promiseWithResolvers();
|
||||||
|
this.ensureEncoderPromise = promise;
|
||||||
|
|
||||||
const encoderConfig: VideoEncoderConfig = {
|
const encoderConfig: VideoEncoderConfig = {
|
||||||
codec: buildVideoCodecString(
|
codec: buildVideoCodecString(
|
||||||
this.encodingConfig.codec,
|
this.encodingConfig.codec,
|
||||||
@@ -261,6 +269,8 @@ class VideoEncoderWrapper {
|
|||||||
|
|
||||||
assert(this.source._connectedTrack);
|
assert(this.source._connectedTrack);
|
||||||
this.muxer = this.source._connectedTrack.output._muxer;
|
this.muxer = this.source._connectedTrack.output._muxer;
|
||||||
|
|
||||||
|
resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
async flush() {
|
async flush() {
|
||||||
@@ -458,6 +468,7 @@ const validateAudioEncodingConfig = (config: AudioEncodingConfig) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class AudioEncoderWrapper {
|
class AudioEncoderWrapper {
|
||||||
|
private ensureEncoderPromise: Promise<void> | null = null;
|
||||||
private encoderInitialized = false;
|
private encoderInitialized = false;
|
||||||
private encoder: AudioEncoder | null = null;
|
private encoder: AudioEncoder | null = null;
|
||||||
private muxer: Muxer | null = null;
|
private muxer: Muxer | null = null;
|
||||||
@@ -493,7 +504,11 @@ class AudioEncoderWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.encoderInitialized) {
|
if (!this.encoderInitialized) {
|
||||||
await this.ensureEncoder(audioData);
|
if (this.ensureEncoderPromise) {
|
||||||
|
await this.ensureEncoderPromise;
|
||||||
|
} else {
|
||||||
|
await this.ensureEncoder(audioData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assert(this.encoderInitialized);
|
assert(this.encoderInitialized);
|
||||||
|
|
||||||
@@ -592,6 +607,9 @@ class AudioEncoderWrapper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { promise, resolve } = promiseWithResolvers();
|
||||||
|
this.ensureEncoderPromise = promise;
|
||||||
|
|
||||||
if ((PCM_CODECS as readonly string[]).includes(this.encodingConfig.codec)) {
|
if ((PCM_CODECS as readonly string[]).includes(this.encodingConfig.codec)) {
|
||||||
this.initPcmEncoder();
|
this.initPcmEncoder();
|
||||||
} else {
|
} else {
|
||||||
@@ -634,6 +652,7 @@ class AudioEncoderWrapper {
|
|||||||
this.muxer = this.source._connectedTrack.output._muxer;
|
this.muxer = this.source._connectedTrack.output._muxer;
|
||||||
|
|
||||||
this.encoderInitialized = true;
|
this.encoderInitialized = true;
|
||||||
|
resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
private initPcmEncoder() {
|
private initPcmEncoder() {
|
||||||
|
|||||||
Reference in New Issue
Block a user