Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 68 additions & 7 deletions src/composables/video/useVideoFilmstrip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,11 @@ describe('useVideoFilmstrip', () => {
expect(bitmap.close).toHaveBeenCalledTimes(1)
})

it('leaves the thumbnail empty when the capture fails', async () => {
vi.stubGlobal(
'createImageBitmap',
vi.fn(async () => {
throw new Error('decode failed')
})
)
it('leaves the thumbnail empty when every capture attempt fails', async () => {
const createImageBitmapMock = vi.fn(async () => {
throw new Error('decode failed')
})
vi.stubGlobal('createImageBitmap', createImageBitmapMock)
vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas)
installVideoMocks()

Expand All @@ -371,10 +369,73 @@ describe('useVideoFilmstrip', () => {
useVideoFilmstrip(videoUrl)
)

await vi.advanceTimersByTimeAsync(6000)
await vi.waitFor(() => expect(loading.value).toBe(false))

expect(createImageBitmapMock.mock.calls.length).toBeGreaterThan(1)
expect(thumbnail.value).toBe('')
expect(error.value).toBeNull()

const attemptsAtTimeout = createImageBitmapMock.mock.calls.length
await vi.advanceTimersByTimeAsync(5000)
expect(createImageBitmapMock.mock.calls.length).toBe(attemptsAtTimeout)
})

it('stops polling for a frame when the load is superseded', async () => {
const bitmap = { width: 171, height: 96, close: vi.fn() }
const videos: MockVideoElement[] = []
const createImageBitmapMock = vi.fn(async () => {
if (videos.length === 1) throw new Error('decode failed')
Comment thread
jtydhr88 marked this conversation as resolved.
Resolved
return bitmap
})
vi.stubGlobal('createImageBitmap', createImageBitmapMock)
vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas)
installVideoMocks({
onVideoCreated: (video) => {
videos.push(video)
}
})

const videoUrl = ref('https://example.com/first.mp4')
const { thumbnail, error, loading } = runWithScope(() =>
useVideoFilmstrip(videoUrl)
)
await vi.advanceTimersByTimeAsync(500)
const attemptsBeforeSwitch = createImageBitmapMock.mock.calls.length
expect(attemptsBeforeSwitch).toBeGreaterThan(1)

videoUrl.value = 'https://example.com/second.mp4'
await vi.waitFor(() => expect(loading.value).toBe(false))

expect(videos[0].src).toBe('')
expect(error.value).toBeNull()
expect(thumbnail.value).not.toBe('')

const attemptsAfterSettle = createImageBitmapMock.mock.calls.length
await vi.advanceTimersByTimeAsync(5000)
expect(createImageBitmapMock.mock.calls.length).toBe(attemptsAfterSettle)
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

it('retries the capture when the frame is not yet decodable', async () => {
const bitmap = { width: 171, height: 96, close: vi.fn() }
const createImageBitmapMock = vi
.fn(async () => bitmap)
.mockRejectedValueOnce(new Error('The image source is not usable.'))
.mockRejectedValueOnce(new Error('The image source is not usable.'))
vi.stubGlobal('createImageBitmap', createImageBitmapMock)
vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas)
installVideoMocks()

const videoUrl = ref('https://example.com/video.mp4')
const { thumbnail, loading } = runWithScope(() =>
useVideoFilmstrip(videoUrl)
)

await vi.advanceTimersByTimeAsync(3000)
await vi.waitFor(() => expect(loading.value).toBe(false))

expect(createImageBitmapMock).toHaveBeenCalledTimes(3)
expect(thumbnail.value).not.toBe('')
})

it('downscales the captured thumbnail to the filmstrip height', async () => {
Expand Down
25 changes: 24 additions & 1 deletion src/composables/video/useVideoFilmstrip.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { delay } from 'es-toolkit'
import { onScopeDispose, ref, watch } from 'vue'
import type { Ref } from 'vue'

Expand All @@ -9,6 +10,8 @@ export const FILMSTRIP_THUMBNAIL_MAX_WIDTH = 384

const METADATA_EVENT_TIMEOUT_MS = 15000
const SEEK_EVENT_TIMEOUT_MS = 5000
const FRAME_DECODE_TIMEOUT_MS = 5000
const FRAME_DECODE_RETRY_INTERVAL_MS = 250

export type FilmstripError = 'canvas-unavailable' | 'load-failed'

Expand Down Expand Up @@ -152,6 +155,25 @@ function representativeFrameTime(duration: number): number {
: 0
}

async function waitForDecodableFrame(
video: HTMLVideoElement,
canvas: HTMLCanvasElement,
context: CanvasRenderingContext2D,
signal: AbortSignal
): Promise<string> {
const deadlineMs = Date.now() + FRAME_DECODE_TIMEOUT_MS
while (Date.now() < deadlineMs) {
try {
await delay(FRAME_DECODE_RETRY_INTERVAL_MS, { signal })
} catch {
throw new LoadAbortedError('Aborted waiting for a decodable frame')
}
const frame = await captureFrame(video, canvas, context)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): because captureFrame collapses every failure into '' (captureFrameViaBitmap(...).catch(() => '')), this loop cannot tell a transient InvalidStateError from a permanent one — a tainted-canvas SecurityError, an unsupported codec, or a video that never reports non-zero videoWidth. All of them get the full 5s / ~20 attempts.

The cost is small per your measurements, so this is not blocking. But if captureFrame surfaced the rejection reason (or just a transient boolean) rather than an empty string, the poll could bail immediately on the permanent cases and give those videos back their 5s. That would also make the retry intent self-documenting at the call site.

if (frame) return frame
}
return ''

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit-pick (non-blocking): the PR description says the failure left "the trim timeline permanently blank with no error or skeleton". Worth being precise that this fixes the transient case only — on exhaustion the outcome is still a blank thumbnail with error left null, just 5s later, which the reworked test asserts directly:

expect(thumbnail.value).toBe('')
expect(error.value).toBeNull()

That is a reasonable scope and I am not asking to widen it. The one thing that genuinely did improve for the permanent case is the skeleton: loading now stays true for the duration of the poll, so users get a spinner instead of an instant blank. Might be worth saying so in the description, since a reader could otherwise expect an error state that this deliberately does not add.

}

async function captureRepresentativeFrame(
video: HTMLVideoElement,
canvas: HTMLCanvasElement,
Expand All @@ -168,7 +190,8 @@ async function captureRepresentativeFrame(
if (!(waitError instanceof EventTimeoutError)) throw waitError
}
}
return captureFrame(video, canvas, context)
const frame = await captureFrame(video, canvas, context)
return frame || waitForDecodableFrame(video, canvas, context, signal)
}

export function useVideoFilmstrip(
Expand Down
Loading