Skip to content

Commit

Permalink
chore: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe committed Dec 23, 2024
1 parent 19ed4ac commit 444c764
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
38 changes: 28 additions & 10 deletions ts/Decoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions ts/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface AgoraEnvOptions {
* @ignore
*/
encodeAlpha: boolean;
/**
* @ignore
*/
maxDecodeRetryCount: number;
}

/**
Expand Down
1 change: 1 addition & 0 deletions ts/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,5 @@ export const AgoraEnv: AgoraEnvType = {
enableWebCodecsDecoder: false,
encodeAlpha: false,
videoFallbackStrategy: VideoFallbackStrategy.PerformancePriority,
maxDecodeRetryCount: 100,
};

0 comments on commit 444c764

Please sign in to comment.