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')
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)
})

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)
if (frame) return frame
}
return ''
}

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