Skip to content

Commit

Permalink
use buffers for hasFrame (#5264)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpkane authored Dec 12, 2024
1 parent c4df037 commit 73da3ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
19 changes: 19 additions & 0 deletions app/packages/looker/src/lookers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import type { Buffers } from "../state";
import { hasFrame } from "./utils";

describe("looker utilities", () => {
it("determines frame availability given a buffer list", () => {
const BUFFERS: Buffers = [
[1, 3],
[5, 25],
];
for (const frameNumber of [1, 10, 25]) {
expect(hasFrame(BUFFERS, frameNumber)).toBe(true);
}

for (const frameNumber of [0, 4, 26]) {
expect(hasFrame(BUFFERS, frameNumber)).toBe(false);
}
});
});
7 changes: 7 additions & 0 deletions app/packages/looker/src/lookers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Buffers } from "../state";

export const hasFrame = (buffers: Buffers, frameNumber: number) => {
return buffers.some(
([start, end]) => start <= frameNumber && frameNumber <= end
);
};
9 changes: 2 additions & 7 deletions app/packages/looker/src/lookers/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { addToBuffers, removeFromBuffers } from "../util";
import { AbstractLooker } from "./abstract";
import { type Frame, acquireReader, clearReader } from "./frame-reader";
import { LookerUtils, withFrames } from "./shared";
import { hasFrame } from "./utils";

let LOOKER_WITH_READER: VideoLooker | null = null;

Expand Down Expand Up @@ -394,13 +395,7 @@ export class VideoLooker extends AbstractLooker<VideoState, VideoSample> {
}

private hasFrame(frameNumber: number) {
if (frameNumber === this.firstFrameNumber) {
return this.firstFrame;
}
return (
this.frames.has(frameNumber) &&
this.frames.get(frameNumber)?.deref() !== undefined
);
return hasFrame(this.state.buffers, frameNumber);
}

private getFrame(frameNumber: number) {
Expand Down

0 comments on commit 73da3ae

Please sign in to comment.