From 444c76439fab8eebbe018acd9c6dd4ca8242c36a Mon Sep 17 00:00:00 2001 From: gxz Date: Mon, 23 Dec 2024 14:50:33 +0800 Subject: [PATCH] chore: optimize --- ts/Decoder/index.ts | 38 ++++++++++++++++++++++++++++---------- ts/Types.ts | 4 ++++ ts/Utils.ts | 1 + 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/ts/Decoder/index.ts b/ts/Decoder/index.ts index 25881531d..79f71bffb 100644 --- a/ts/Decoder/index.ts +++ b/ts/Decoder/index.ts @@ -20,6 +20,7 @@ export class WebCodecsDecoder { private _base_ts = 0; private _base_ts_ntp = 1; private _last_ts_ntp = 1; + private _decode_retry_count = 0; constructor( renders: WebCodecsRenderer[], @@ -154,16 +155,33 @@ export class WebCodecsDecoder { this.updateTimestamps(ts); - this._decoder.decode( - new EncodedVideoChunk({ - data: imageBuffer, - timestamp: this._last_ts_ntp, - // @ts-ignore - type: frameType, - // @ts-ignore - transfer: [imageBuffer.buffer], - }) - ); + try { + this._decoder.decode( + new EncodedVideoChunk({ + data: imageBuffer, + timestamp: this._last_ts_ntp, + // @ts-ignore + type: frameType, + // @ts-ignore + transfer: [imageBuffer.buffer], + }) + ); + this._decode_retry_count = 0; + } catch (e) { + /// There are some cases that the decoder failed to decode the frame, we will retry for a few times + /// If the decoder still failed to decode the frame, we will fallback to native decoder + /// The retry count is defined in AgoraEnv.maxDecodeRetryCount + /// The retry count will be reset to 0 when the decoder successfully decode a frame + if (this._decode_retry_count >= AgoraEnv.maxDecodeRetryCount) { + AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback( + this._cacheContext + ); + throw new Error( + `failed to decode frame over ${this._decode_retry_count} times, fallback to native decoder` + ); + } + this._decode_retry_count++; + } } reset() { diff --git a/ts/Types.ts b/ts/Types.ts index 2556440d7..d3cf8953a 100644 --- a/ts/Types.ts +++ b/ts/Types.ts @@ -49,6 +49,10 @@ export interface AgoraEnvOptions { * @ignore */ encodeAlpha: boolean; + /** + * @ignore + */ + maxDecodeRetryCount: number; } /** diff --git a/ts/Utils.ts b/ts/Utils.ts index 200d77219..19830efec 100644 --- a/ts/Utils.ts +++ b/ts/Utils.ts @@ -193,4 +193,5 @@ export const AgoraEnv: AgoraEnvType = { enableWebCodecsDecoder: false, encodeAlpha: false, videoFallbackStrategy: VideoFallbackStrategy.PerformancePriority, + maxDecodeRetryCount: 100, };