fix(webui): resolve video player total frames mismatch

Avoid deriving total frames from container duration when the audio tail mismatch is below the dynamic playback stall threshold. This prevents the rendering of phantom trailing frames that the playhead cannot reach during playback.

Also adds comprehensive unit and E2E tests covering both small and large audio tail gaps.
This commit is contained in:
yiling
2026-07-04 16:44:09 +08:00
parent f32f2d43aa
commit 175b5ac171
13 changed files with 489 additions and 18 deletions
@@ -0,0 +1,87 @@
import { expect, test } from '@playwright/test'
import { VideoPlayerPage } from '../pages/video-player.page'
import { REAL_LAST_FRAME, SAMPLE_TOTAL_FRAMES } from '../harness/fixture'
/**
* Regression reproduction for the frame-count inflation bug.
*
* When an asset's container/format duration is slightly LONGER than its actual
* video stream (common: audio padding / container overhead), the player derived
* `totalFrames` from the container duration (`round(duration * fps)`) instead of
* the accurate stream frame count (`metadata.totalFrames` == ffprobe nb_frames).
* This invents phantom trailing frames that do not exist in the stream, so:
*
* - normal playback stops N frames SHORT of the displayed maximum (it can only
* reach the last real frame), and
* - frame-stepping can walk PAST the last real frame onto phantom frames.
*
* The `container-longer` fixture reports duration 5.075s while the real backing
* video is exactly 150 frames @ 30fps (5.0s stream). The buggy heuristic derives
* round(5.075*30) = 152 frames, so the readout shows "/ 151 fr" even though the
* last real (and last reachable) frame is 149.
*
* Correct behaviour (asserted below): playing to the natural end must land on
* the last displayed frame — i.e. the readout's current frame equals the
* readout's total frame, and that total equals the real last frame of the
* stream. This spec FAILS against the buggy build and PASSES once totalFrames is
* derived from the accurate stream frame count.
*/
// Land a few frames from the real end so playing out the tail (and firing the
// native `ended` event) is fast and deterministic.
const NEAR_END_FRAME = SAMPLE_TOTAL_FRAMES - 3
test.beforeEach(async ({ page }) => {
// Mock every backend call so this stays a pure frontend test (mirrors the
// other player specs). Patterns are anchored to the origin root so they only
// match real backend requests, not Vite-served source modules.
await page.route(/^https?:\/\/[^/]+\/(api|files)\//, (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: '{}' }),
)
})
test.describe('video player container-duration mismatch', () => {
test('plays to the last real frame instead of stopping short on phantom frames', async ({
page,
}) => {
const vp = new VideoPlayerPage(page)
await vp.gotoVariant('container-longer')
await vp.muteNativeVideo()
// Drive the real player to its natural end.
await vp.seekToFrame(NEAR_END_FRAME)
await vp.clickPlayToggle()
await vp.waitForEnded()
await expect.poll(() => vp.isPaused(), { timeout: 5_000 }).toBe(true)
// Let the end-of-playback frame settle (currentTime stops moving).
await expect
.poll(
async () => {
const a = await vp.currentTime()
await vp.page.waitForTimeout(80)
const b = await vp.currentTime()
return Math.abs(b - a) < 1e-3
},
{ timeout: 5_000 },
)
.toBe(true)
const totalFrame = await vp.readoutTotalFrame()
// The displayed total must reflect the real stream (last real frame = 149),
// not the inflated container-derived count (151). This fails under the bug.
expect(totalFrame).toBe(REAL_LAST_FRAME)
// Core user-facing invariant: reaching the natural end lands on the last
// displayed frame — the playhead is not stranded short of the maximum.
await expect
.poll(async () => (await vp.snapshot()).readoutFrame, { timeout: 5_000 })
.toBe(totalFrame)
// The seekbar is visually full at the end (within tolerance).
const snap = await vp.snapshot()
expect(snap.seekbarFraction).toBeGreaterThan(0.98)
})
})
@@ -0,0 +1,77 @@
import { expect, test } from '@playwright/test'
import { VideoPlayerPage } from '../pages/video-player.page'
import { REAL_LAST_FRAME, SAMPLE_TOTAL_FRAMES } from '../harness/fixture'
/**
* Companion to container-duration-mismatch.spec.ts, covering the LARGE-gap
* ("videoB") case: the audio track is much longer than the video track (here a
* 150-frame / 5.0s video with a 6.5s audio tail — a 1.5s tail, far above the
* ~100ms playback stall threshold).
*
* Because the tail is long enough for the RAF fallback to activate and drive the
* playhead through the audio-only region, the timeline is (correctly) derived
* from the container duration: round(6.5 * 30) = 195 frames, readout max 194.
* Playing to the natural end must advance the playhead all the way to that
* container-based maximum, and the readout must show "194 / 194".
*
* This behaviour is expected to hold BOTH before and after the frame-count fix
* (the fix only changes the small-gap case), so this spec passes on the current
* code and guards against regressing the large-gap case.
*/
// Land a couple frames before the VIDEO track ends, so playback streams through
// the video tail and then the long audio-only tail to the natural end.
const NEAR_VIDEO_END_FRAME = SAMPLE_TOTAL_FRAMES - 3
test.beforeEach(async ({ page }) => {
await page.route(/^https?:\/\/[^/]+\/(api|files)\//, (route) =>
route.fulfill({ status: 200, contentType: 'application/json', body: '{}' }),
)
})
test.describe('video player long audio tail (large gap)', () => {
test('playhead traverses the audio tail to the container-based maximum', async ({ page }) => {
const vp = new VideoPlayerPage(page)
await vp.gotoVariant('long-audio')
await vp.muteNativeVideo()
// The displayed total reflects the container (audio-extended) duration, not
// the shorter video track: it is well beyond the last real video frame.
const totalFrame = await vp.readoutTotalFrame()
expect(totalFrame).toBeGreaterThan(REAL_LAST_FRAME)
// Drive the player from just before the video track ends, out through the
// audio-only tail, to the natural end of the media.
await vp.seekToFrame(NEAR_VIDEO_END_FRAME)
await vp.clickPlayToggle()
await vp.waitForEnded()
await expect.poll(() => vp.isPaused(), { timeout: 5_000 }).toBe(true)
// Let the end-of-playback frame settle.
await expect
.poll(
async () => {
const a = await vp.currentTime()
await vp.page.waitForTimeout(80)
const b = await vp.currentTime()
return Math.abs(b - a) < 1e-3
},
{ timeout: 5_000 },
)
.toBe(true)
// The playhead advanced past the last real video frame into the audio tail...
const snap = await vp.snapshot()
expect(snap.readoutFrame).toBeGreaterThan(REAL_LAST_FRAME)
// ...and reached the container-based maximum at the natural end.
await expect
.poll(async () => (await vp.snapshot()).readoutFrame, { timeout: 5_000 })
.toBe(totalFrame)
// The seekbar is visually full at the end.
const finalSnap = await vp.snapshot()
expect(finalSnap.seekbarFraction).toBeGreaterThan(0.98)
})
})