@@ -53,6 +53,7 @@ import type { PlayerAPI } from "../core.types";
5353import { swallow } from "./diagnostics" ;
5454import { shouldAttemptPeriodicTimelineBind } from "./timelineRebindPolicy" ;
5555import { installStudioCustomEase } from "./customEase" ;
56+ import { parseNumeric } from "./startExpression" ;
5657
5758const AUTHORED_DURATION_ATTR = "data-hf-authored-duration" ;
5859const AUTHORED_END_ATTR = "data-hf-authored-end" ;
@@ -606,37 +607,64 @@ export function initSandboxRuntimeModular(): void {
606607 return resolver . resolveDurationForElement ( element ) ;
607608 } ;
608609
609- const resolveMediaStartSeconds = ( element : Element , fallback = 0 ) : number => {
610- if ( ! element . hasAttribute ( "data-hf-auto-start" ) && element . hasAttribute ( "data-start" ) ) {
611- // `data-start` is authored relative to the media element's OWN sub-
612- // composition, not the root timeline — `fallback` carries the host
613- // composition's resolved absolute start (see syncMediaForCurrentState's
614- // inheritedStart), so it must be added, not discarded. Skipping it made
615- // a nested video play from root t=0 instead of holding until its
616- // parent scene began (issue #1838) — resolveStartForElement's own
617- // absolute-expression branch already adds this same host offset, this
618- // fast literal-value path just didn't.
619- const own = Math . max ( 0 , Number ( element . getAttribute ( "data-start" ) ?? 0 ) || 0 ) ;
620- return own + fallback ;
621- }
622- return resolveStartForElement ( element , fallback ) ;
610+ const resolveMediaCompositionContext = ( element : Element ) => {
611+ const compositionRoot = element . closest ( "[data-composition-id]" ) ;
612+ const inheritedStart = compositionRoot ? resolveStartForElement ( compositionRoot , 0 ) : null ;
613+ const inheritedDuration = compositionRoot
614+ ? resolveDurationForElement ( compositionRoot , { includeAuthoredTimingAttrs : true } )
615+ : null ;
616+ return { compositionRoot, inheritedStart, inheritedDuration } ;
623617 } ;
624618
619+ const resolveAbsoluteMediaStartSeconds = ( element : Element ) : number => {
620+ const context = resolveMediaCompositionContext ( element ) ;
621+ const inheritedStart = context . inheritedStart ?? 0 ;
622+ const authoredStart = parseNumeric ( element . getAttribute ( "data-start" ) ) ;
623+ if (
624+ element . hasAttribute ( "data-hf-auto-start" ) ||
625+ authoredStart == null ||
626+ inheritedStart <= 0
627+ ) {
628+ return resolveStartForElement ( element , inheritedStart ) ;
629+ }
630+
631+ // Both timing conventions exist in shipped projects:
632+ // - composition-local media, e.g. host@20 + video@0 => root@20
633+ 634+ // Preserve the global value when its authored window already intersects
635+ // the host's absolute window. Otherwise it is unambiguously local and
636+ // must inherit the recursively-resolved host start.
637+ const authoredDuration = parseNumeric ( element . getAttribute ( "data-duration" ) ) ;
638+ const hostDuration = context . inheritedDuration ;
639+ const hostEnd = hostDuration != null && hostDuration > 0 ? inheritedStart + hostDuration : null ;
640+ const authoredEnd =
641+ authoredDuration != null && authoredDuration > 0
642+ ? authoredStart + authoredDuration
643+ : authoredStart ;
644+ const overlapsHostWindow =
645+ hostEnd == null
646+ ? authoredStart >= inheritedStart
647+ : authoredStart < hostEnd &&
648+ ( authoredEnd > inheritedStart || authoredStart === inheritedStart ) ;
649+ return overlapsHostWindow ? authoredStart : inheritedStart + authoredStart ;
650+ } ;
651+
652+ window . __hfResolveMediaStartSeconds = resolveAbsoluteMediaStartSeconds ;
653+ runtimeCleanupCallbacks . push ( ( ) => {
654+ if ( window . __hfResolveMediaStartSeconds === resolveAbsoluteMediaStartSeconds ) {
655+ delete window . __hfResolveMediaStartSeconds ;
656+ }
657+ } ) ;
658+
625659 const isTimedElementVisibleAt = ( rawNode : HTMLElement , currentTime : number ) : boolean => {
626660 const tag = rawNode . tagName . toLowerCase ( ) ;
627661 if ( tag === "script" || tag === "style" || tag === "link" || tag === "meta" ) {
628662 return false ;
629663 }
630664
631665 const isMedia = tag === "video" || tag === "audio" ;
632- const mediaCompositionHost = isMedia
633- ? rawNode . closest ( "[data-composition-src], [data-composition-file]" )
634- : null ;
635- const mediaCompositionStart = mediaCompositionHost
636- ? resolveStartForElement ( mediaCompositionHost , 0 )
637- : 0 ;
638666 const start = isMedia
639- ? resolveMediaStartSeconds ( rawNode , mediaCompositionStart )
667+ ? resolveAbsoluteMediaStartSeconds ( rawNode )
640668 : resolveStartForElement ( rawNode , 0 ) ;
641669 let duration = resolveDurationForElement ( rawNode ) ;
642670 const compId = rawNode . getAttribute ( "data-composition-id" ) ;
@@ -736,7 +764,7 @@ export function initSandboxRuntimeModular(): void {
736764 if ( mediaNodes . length === 0 ) return null ;
737765 let maxWindowEndSeconds = 0 ;
738766 for ( const node of mediaNodes ) {
739- const start = resolveMediaStartSeconds ( node , 0 ) ;
767+ const start = resolveAbsoluteMediaStartSeconds ( node ) ;
740768 if ( ! Number . isFinite ( start ) ) continue ;
741769 const duration = resolveMediaElementDurationSeconds ( node ) ;
742770 if ( duration == null || duration <= MIN_VALID_TIMELINE_DURATION_SECONDS ) continue ;
@@ -1951,30 +1979,16 @@ export function initSandboxRuntimeModular(): void {
19511979 } ;
19521980
19531981 const syncMediaForCurrentState = ( ) => {
1954- const resolveMediaCompositionContext = ( element : HTMLVideoElement | HTMLAudioElement ) => {
1955- const compositionRoot = element . closest ( "[data-composition-id]" ) ;
1956- const inheritedStart = compositionRoot ? resolveStartForElement ( compositionRoot , 0 ) : null ;
1957- // Media sync follows the authored host window, matching visibility for
1958- // authored composition hosts. Live child timeline duration only fills in
1959- // when no authored timing exists, so seeks clamp against host clip timing.
1960- const inheritedDuration = compositionRoot
1961- ? resolveDurationForElement ( compositionRoot , { includeAuthoredTimingAttrs : true } )
1962- : null ;
1963- return { compositionRoot, inheritedStart, inheritedDuration } ;
1964- } ;
19651982 const cache = refreshRuntimeMediaCache ( {
19661983 shouldIncludeElement : ( element ) =>
19671984 element . hasAttribute ( "data-start" ) ||
19681985 Boolean ( resolveMediaCompositionContext ( element ) . compositionRoot ) ,
19691986 resolveStartSeconds : ( element ) => {
1970- const context = resolveMediaCompositionContext (
1971- element as HTMLVideoElement | HTMLAudioElement ,
1972- ) ;
1973- return resolveMediaStartSeconds ( element , context . inheritedStart ?? 0 ) ;
1987+ return resolveAbsoluteMediaStartSeconds ( element ) ;
19741988 } ,
19751989 resolveDurationSeconds : ( element ) => {
19761990 const context = resolveMediaCompositionContext ( element ) ;
1977- const start = resolveMediaStartSeconds ( element , context . inheritedStart ?? 0 ) ;
1991+ const start = resolveAbsoluteMediaStartSeconds ( element ) ;
19781992 const mediaStart =
19791993 Number . parseFloat ( element . dataset . playbackStart ?? element . dataset . mediaStart ?? "0" ) ||
19801994 0 ;
0 commit comments