Skip to content

fix: initialize preloaded media duration and buffered state - #2737

Open
wanxiankai wants to merge 1 commit into
streamich:masterfrom
wanxiankai:fix/2642-preloaded-media-duration
Open

wanxiankai wants to merge 1 commit into
streamich:masterfrom
wanxiankai:fix/2642-preloaded-media-duration

Conversation

@wanxiankai

Copy link
Copy Markdown

Description

Fixes #2642.

When audio metadata loads before React attaches its event handlers, useAudio misses the initial durationchange event and leaves state.duration at 0. This also clamps every controls.seek() call to the start of the recording. useVideo shares the same implementation.

Read duration and buffered alongside the existing media-state snapshot on mount and when src changes. Keep the initial duration at 0 while readyState is below HAVE_METADATA. This also clears the previous source's metadata when a replacement source has not loaded yet. No public API or dependency changes.

Add 16 regression tests covering audio/video, props/element inputs, preloaded metadata, later duration events, event forwarding, seeking, and source changes. Update both hook references. The new tests produce 12 failures and 4 passing controls against unchanged upstream code, and all 16 pass with this change.

Validation:

  • Full Jest suite: 77 suites / 508 tests passed (jest --runInBand --watchman=false).
  • Focused media tests: 17 passed, including the existing audio test.
  • Coverage: both outcomes of the added metadata guard are exercised (16/8 hits); the new synchronization statements are covered. Existing unrelated factory paths remain partially covered.
  • tsc --noEmit, CommonJS build (tsc), and ESM build (tsc -m esNext --outDir esm) passed.
  • Full ESLint run: 0 errors / 97 existing warnings. The new test file has no warnings; the factory retains its existing effect-dependency warning.
  • Real-browser comparison with Ego Lite: preload a two-second WAV in server-rendered <audio preload="metadata">, then hydrate after metadata is available. Upstream reports duration 0, buffered [], and seeking to 0.75 stays at 0; this change reports duration 2, buffered [{ start: 0, end: 2 }], and seeking reaches 0.75. No synthetic media metadata was used in this comparison.

AI assistance was used for investigation, implementation, and validation.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • Read the Contributing Guide
  • Perform a code self-review
  • Comment the code, particularly in hard-to-understand areas — no complex new logic requiring comments
  • Add documentation
  • Add hook's story at Storybook — existing audio/video stories cover the unchanged API
  • Cover changes with tests
  • Ensure the test suite passes (yarn test) — equivalent Jest invocation above, with Watchman disabled for this environment
  • Provide 100% tests coverage — added metadata synchronization statements and guard outcomes
  • Make sure code lints (yarn lint). Fix it with yarn lint:fix in case of failure.
  • Make sure types are fine (yarn lint:types).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[useAudio] state.duration is always 0 when preload audio in nextjs

1 participant