Skip to content

Commit b6819d2

Browse files
committed
fix(rendiv): bound open-ended Audio/Video durations by the composition
An <Audio>, <Video> or <OffthreadVideo> at the composition root, or inside a <Sequence> without durationInFrames, registered durationInFrames: Infinity. The stitcher turned that into atrim=duration=Infinity and FFmpeg failed after every frame had been rendered. Clamp each registered clip to compositionDuration - startAtFrame.
1 parent 6d6aca2 commit b6819d2

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎packages/rendiv/src/__tests__/audio.test.tsx‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { describe, it, expect } from 'vitest';
1+
import { describe, it, expect, beforeEach } from 'vitest';
22
import React from 'react';
33
import { render } from '@testing-library/react';
44
import { Audio } from '../components/Audio';
5+
import { Sequence } from '../components/Sequence';
56
import { TimelineContext } from '../context/TimelineContext';
67
import { CompositionContext, type CompositionConfig } from '../context/CompositionContext';
78
import { RendivEnvironmentContext } from '../context/RendivEnvironmentContext';
@@ -74,4 +75,42 @@ describe('Audio', () => {
7475
const audio = container.querySelector('audio')!;
7576
expect(audio.getAttribute('preload')).toBe('auto');
7677
});
78+
79+
describe('registered audio sources', () => {
80+
const getSources = () =>
81+
Array.from(((window as any).__RENDIV_AUDIO_SOURCES__ as Map<string, any>)?.values() ?? []);
82+
83+
beforeEach(() => {
84+
delete (window as any).__RENDIV_AUDIO_SOURCES__;
85+
});
86+
87+
it('bounds audio at the composition root by the composition duration', () => {
88+
renderAudioInContext(<Audio src="/test.mp3" />, { environment: 'rendering' });
89+
const [source] = getSources();
90+
expect(source.startAtFrame).toBe(0);
91+
expect(source.durationInFrames).toBe(150);
92+
});
93+
94+
it('bounds audio in a Sequence without durationInFrames by the rest of the composition', () => {
95+
renderAudioInContext(
96+
<Sequence from={30}>
97+
<Audio src="/test.mp3" />
98+
</Sequence>,
99+
{ frame: 30, environment: 'rendering' },
100+
);
101+
const [source] = getSources();
102+
expect(source.startAtFrame).toBe(30);
103+
expect(source.durationInFrames).toBe(120);
104+
});
105+
106+
it('keeps an explicit Sequence duration', () => {
107+
renderAudioInContext(
108+
<Sequence from={30} durationInFrames={60}>
109+
<Audio src="/test.mp3" />
110+
</Sequence>,
111+
{ frame: 30, environment: 'rendering' },
112+
);
113+
expect(getSources()[0].durationInFrames).toBe(60);
114+
});
115+
});
77116
});

‎packages/rendiv/src/components/Audio.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,15 @@ export function Audio({
146146
const sources = w.__RENDIV_AUDIO_SOURCES__ as Map<string, unknown>;
147147
const startFrames = computeLoopStartFrames(sequence.accumulatedOffset, sequence.loopStack, compositionDuration);
148148
for (const startFrame of startFrames) {
149-
const key = `audio|${src}|${startFrame}|${effectiveDuration}|${startFrom}|${volume}|${effectiveRate}`;
149+
// An open-ended Sequence (or none) has an infinite duration; bound the
150+
// clip by what is left of the composition so the stitcher gets a number.
151+
const durationInFrames = Math.min(effectiveDuration, compositionDuration - startFrame);
152+
const key = `audio|${src}|${startFrame}|${durationInFrames}|${startFrom}|${volume}|${effectiveRate}`;
150153
sources.set(key, {
151154
type: 'audio' as const,
152155
src,
153156
startAtFrame: startFrame,
154-
durationInFrames: effectiveDuration,
157+
durationInFrames,
155158
startFrom,
156159
volume,
157160
playbackRate: effectiveRate,

‎packages/rendiv/src/components/Video.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,15 @@ export function Video({
283283
const sources = w.__RENDIV_AUDIO_SOURCES__ as Map<string, unknown>;
284284
const startFrames = computeLoopStartFrames(sequence.accumulatedOffset, sequence.loopStack, compositionDuration);
285285
for (const startFrame of startFrames) {
286-
const key = `video|${src}|${startFrame}|${effectiveDuration}|${startFrom}|${volume}|${effectiveRate}`;
286+
// An open-ended Sequence (or none) has an infinite duration; bound the
287+
// clip by what is left of the composition so the stitcher gets a number.
288+
const durationInFrames = Math.min(effectiveDuration, compositionDuration - startFrame);
289+
const key = `video|${src}|${startFrame}|${durationInFrames}|${startFrom}|${volume}|${effectiveRate}`;
287290
sources.set(key, {
288291
type: 'video' as const,
289292
src,
290293
startAtFrame: startFrame,
291-
durationInFrames: effectiveDuration,
294+
durationInFrames,
292295
startFrom,
293296
volume,
294297
playbackRate: effectiveRate,

‎packages/rendiv/src/components/offthread-video.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,15 @@ export function OffthreadVideo({
7878
const sources = w.__RENDIV_AUDIO_SOURCES__ as Map<string, unknown>;
7979
const startFrames = computeLoopStartFrames(sequence.accumulatedOffset, sequence.loopStack, compositionDuration);
8080
for (const startFrame of startFrames) {
81-
const key = `video|${src}|${startFrame}|${effectiveDuration}|${startFrom}|${volume}|${effectiveRate}`;
81+
// An open-ended Sequence (or none) has an infinite duration; bound the
82+
// clip by what is left of the composition so the stitcher gets a number.
83+
const durationInFrames = Math.min(effectiveDuration, compositionDuration - startFrame);
84+
const key = `video|${src}|${startFrame}|${durationInFrames}|${startFrom}|${volume}|${effectiveRate}`;
8285
sources.set(key, {
8386
type: 'video' as const,
8487
src,
8588
startAtFrame: startFrame,
86-
durationInFrames: effectiveDuration,
89+
durationInFrames,
8790
startFrom,
8891
volume,
8992
playbackRate: effectiveRate,

0 commit comments

Comments
 (0)