mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34:40 +00:00
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:
Binary file not shown.
@@ -48,3 +48,91 @@ export const sampleVideoAsset: AssetInfo = {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Metadata variant reproducing the real-world case where the container/format
|
||||
* duration is LONGER than the actual video stream (audio padding / container
|
||||
* overhead). This mirrors an observed asset whose ffprobe reported:
|
||||
* - video stream: nb_frames=268, duration=8.9333s
|
||||
* - format: duration=9.008s (~2 frames longer than the stream)
|
||||
*
|
||||
* Here the backing file is still the real 150-frame sample.mp4 (video stream =
|
||||
* 5.0s, 150 real decodable frames), but the stored metadata reports a container
|
||||
* duration of 5.075s — i.e. ~2.25 frames longer than the 150-frame stream.
|
||||
*
|
||||
* `metadata.totalFrames` stays at the accurate stream frame count (150, from
|
||||
* ffprobe `nb_frames`), exactly as the backend stores it.
|
||||
*/
|
||||
export const CONTAINER_LONGER_DURATION = 5.075
|
||||
|
||||
/**
|
||||
* The frame count the player's current heuristic derives from the inflated
|
||||
* container duration: round(5.075 * 30) = 152, i.e. 2 frames MORE than the 150
|
||||
* frames that actually exist in the stream. Frames 150 and 151 are phantom.
|
||||
*/
|
||||
export const CONTAINER_LONGER_COMPUTED_TOTAL_FRAMES = Math.round(
|
||||
CONTAINER_LONGER_DURATION * SAMPLE_FRAME_RATE,
|
||||
)
|
||||
|
||||
/** The last frame index that actually exists in the stream (0-based): 149. */
|
||||
export const REAL_LAST_FRAME = SAMPLE_TOTAL_FRAMES - 1
|
||||
|
||||
export const containerLongerVideoAsset: AssetInfo = {
|
||||
...sampleVideoAsset,
|
||||
id: 'e2e-container-longer-asset',
|
||||
media: {
|
||||
original: {
|
||||
downloadUrl: SAMPLE_VIDEO_URL,
|
||||
key: 'sample.mp4',
|
||||
},
|
||||
metadata: {
|
||||
duration: CONTAINER_LONGER_DURATION,
|
||||
originalWidth: SAMPLE_WIDTH,
|
||||
originalHeight: SAMPLE_HEIGHT,
|
||||
frameRate: SAMPLE_FRAME_RATE,
|
||||
totalFrames: SAMPLE_TOTAL_FRAMES,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Metadata variant for the LARGE-gap ("videoB") case: the audio track is much
|
||||
* longer than the video track, so the container tail is long enough for the
|
||||
* playhead to actually traverse it during playback.
|
||||
*
|
||||
* Backed by the real committed fixture `sample-long-audio.mp4` (verified via
|
||||
* ffprobe):
|
||||
* - video stream: nb_frames=150, duration=5.000s, 30fps
|
||||
* - audio stream: duration=6.500s
|
||||
* - format: duration=6.500s (1.5s / 45-frame tail — well above the
|
||||
* ~100ms playback stall threshold)
|
||||
*
|
||||
* Because the tail exceeds the stall threshold, both the current code and the
|
||||
* fix derive the frame count from the container duration
|
||||
* (round(6.5 * 30) = 195). The playhead advances through the audio tail to the
|
||||
* container end, so at natural end the readout reaches its maximum (194).
|
||||
*/
|
||||
export const LONG_AUDIO_VIDEO_URL = '/sample-long-audio.mp4'
|
||||
export const LONG_AUDIO_DURATION = 6.5
|
||||
|
||||
/** Container-derived frame count for the long-audio fixture: round(6.5 * 30). */
|
||||
export const LONG_AUDIO_COMPUTED_TOTAL_FRAMES = Math.round(LONG_AUDIO_DURATION * SAMPLE_FRAME_RATE)
|
||||
|
||||
export const longAudioVideoAsset: AssetInfo = {
|
||||
...sampleVideoAsset,
|
||||
id: 'e2e-long-audio-asset',
|
||||
name: 'sample-long-audio.mp4',
|
||||
media: {
|
||||
original: {
|
||||
downloadUrl: LONG_AUDIO_VIDEO_URL,
|
||||
key: 'sample-long-audio.mp4',
|
||||
},
|
||||
metadata: {
|
||||
duration: LONG_AUDIO_DURATION,
|
||||
originalWidth: SAMPLE_WIDTH,
|
||||
originalHeight: SAMPLE_HEIGHT,
|
||||
frameRate: SAMPLE_FRAME_RATE,
|
||||
totalFrames: SAMPLE_TOTAL_FRAMES,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -4,7 +4,12 @@ import type Player from 'video.js/dist/types/player'
|
||||
import VideoPlayer from '@/ui/components/viewers/video-player'
|
||||
import { calculateFrameCenterTime } from '@/ui/components/viewers/utils'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import { SAMPLE_FRAME_RATE, sampleVideoAsset } from './fixture'
|
||||
import {
|
||||
SAMPLE_FRAME_RATE,
|
||||
sampleVideoAsset,
|
||||
containerLongerVideoAsset,
|
||||
longAudioVideoAsset,
|
||||
} from './fixture'
|
||||
|
||||
/**
|
||||
* Backend-free harness page that mounts the *real* VideoPlayer component with a
|
||||
@@ -41,6 +46,23 @@ import { SAMPLE_FRAME_RATE, sampleVideoAsset } from './fixture'
|
||||
*/
|
||||
useUiStore.setState({ videoTimeDisplayMode: 'frames' })
|
||||
|
||||
/**
|
||||
* Select which fixture asset to mount based on the `?variant=` query param, so
|
||||
* specs can exercise different metadata shapes against the same real backing
|
||||
* video. Defaults to the canonical sample asset used by the other specs.
|
||||
*/
|
||||
function resolveAsset() {
|
||||
const variant =
|
||||
typeof window !== 'undefined'
|
||||
? new URLSearchParams(window.location.search).get('variant')
|
||||
: null
|
||||
if (variant === 'container-longer') return containerLongerVideoAsset
|
||||
if (variant === 'long-audio') return longAudioVideoAsset
|
||||
return sampleVideoAsset
|
||||
}
|
||||
|
||||
const harnessAsset = resolveAsset()
|
||||
|
||||
interface HarnessComment {
|
||||
id: string
|
||||
second: number
|
||||
@@ -110,7 +132,7 @@ function Harness(): ReactElement {
|
||||
<div className="flex flex-col flex-1 h-full overflow-hidden relative">
|
||||
<VideoPlayer
|
||||
key={playerKey}
|
||||
data={sampleVideoAsset}
|
||||
data={harnessAsset}
|
||||
playerRef={playerRef}
|
||||
onTimeUpdate={handleTimeUpdate}
|
||||
/>
|
||||
|
||||
@@ -26,6 +26,10 @@ const server = Bun.serve({
|
||||
new Response(Bun.file(join(fixturesDir, 'sample.mp4')), {
|
||||
headers: { 'Content-Type': 'video/mp4' },
|
||||
}),
|
||||
'/sample-long-audio.mp4': () =>
|
||||
new Response(Bun.file(join(fixturesDir, 'sample-long-audio.mp4')), {
|
||||
headers: { 'Content-Type': 'video/mp4' },
|
||||
}),
|
||||
'/': index,
|
||||
'/*': index,
|
||||
},
|
||||
|
||||
@@ -48,6 +48,16 @@ export class VideoPlayerPage {
|
||||
await this.waitUntilReady()
|
||||
}
|
||||
|
||||
/**
|
||||
* Navigate to the harness with a specific fixture variant (see the harness
|
||||
* `resolveAsset`), e.g. `'container-longer'` for the asset whose container
|
||||
* duration exceeds its real video stream.
|
||||
*/
|
||||
async gotoVariant(variant: string): Promise<void> {
|
||||
await this.page.goto(`/?variant=${encodeURIComponent(variant)}`)
|
||||
await this.waitUntilReady()
|
||||
}
|
||||
|
||||
/** Wait until the video.js engine has metadata and a usable frame readout. */
|
||||
async waitUntilReady(): Promise<void> {
|
||||
await this.video.waitFor({ state: 'attached' })
|
||||
@@ -92,6 +102,13 @@ export class VideoPlayerPage {
|
||||
return match ? Number.parseInt(match[1], 10) : Number.NaN
|
||||
}
|
||||
|
||||
/** The total frame index shown as "M" in the "N / M fr" readout. */
|
||||
async readoutTotalFrame(): Promise<number> {
|
||||
const text = (await this.timeReadout.textContent()) ?? ''
|
||||
const match = text.match(/\d+\s*\/\s*(\d+)\s*fr/)
|
||||
return match ? Number.parseInt(match[1], 10) : Number.NaN
|
||||
}
|
||||
|
||||
private async seekbarFraction(): Promise<number> {
|
||||
const track = await this.seekbar.boundingBox()
|
||||
const fill = await this.seekbarFill.boundingBox()
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user