Skip to content

Commit

Permalink
Bug showcase: decode video frames in reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Oct 16, 2023
1 parent e843097 commit e415089
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/video-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const decodeQueueHwm = 30;

const DEBUG = false;

// Bug showcases
const BUG_DECODE_VIDEO_IN_REVERSE = false;

export class BabyVideoElement extends HTMLElement {
readonly #canvas: HTMLCanvasElement;
readonly #canvasContext: CanvasRenderingContext2D;
Expand Down Expand Up @@ -89,6 +92,7 @@ export class BabyVideoElement extends HTMLElement {
#nextDecodedVideoFramePromise: Deferred<void> | undefined = undefined;
#lastRenderedFrame: number | undefined = undefined;
#nextRenderFrame: number = 0;
#needKeyFrame: boolean = true;

readonly #audioDecoder: AudioDecoder;
#lastAudioDecoderConfig: AudioDecoderConfig | undefined = undefined;
Expand Down Expand Up @@ -711,17 +715,31 @@ export class BabyVideoElement extends HTMLElement {
) {
this.#videoDecoder.configure(codecConfig);
this.#lastVideoDecoderConfig = codecConfig;
this.#needKeyFrame = true;
}
if (BUG_DECODE_VIDEO_IN_REVERSE) {
frames.reverse();
}
if (this.#needKeyFrame) {
// Remove non-keyframes
const firstKeyFrame = frames.findIndex((x) => x.type === "key");
if (firstKeyFrame > 0) {
frames.splice(0, firstKeyFrame);
}
}
for (const frame of frames) {
if (this.#needKeyFrame && frame.type === "key") {
this.#needKeyFrame = false;
}
this.#videoDecoder.decode(frame);
this.#decodingVideoFrames.push(frame);
}
// The "furthest decoded frame" depends on the rendering order,
// since we must decode the frames in their original order.
if (direction == Direction.FORWARD) {
this.#furthestDecodingVideoFrame = frames[frames.length - 1];
} else {
if (direction === Direction.BACKWARD && !BUG_DECODE_VIDEO_IN_REVERSE) {
this.#furthestDecodingVideoFrame = frames[0];
} else {
this.#furthestDecodingVideoFrame = frames[frames.length - 1];
}
}

Expand Down Expand Up @@ -847,6 +865,7 @@ export class BabyVideoElement extends HTMLElement {
this.#lastRenderedFrame = undefined;
this.#decodingVideoFrames.length = 0;
this.#decodedVideoFrames.length = 0;
this.#needKeyFrame = true;
this.#videoDecoder.reset();
}

Expand Down

0 comments on commit e415089

Please sign in to comment.