Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

fix(editor): refresh finalized companion audio

wiiiii123e12390845c2d
diff --git a/src/components/video-editor/VideoEditor.tsx b/src/components/video-editor/VideoEditor.tsx
index 0b23fb69..fdedb197 100644
--- a/src/components/video-editor/VideoEditor.tsx
+++ b/src/components/video-editor/VideoEditor.tsx
@@ -6862,6 +6862,7 @@ export default function VideoEditor() {
selectedAnnotationId={selectedAnnotationId}
onSelectAnnotation={handleSelectAnnotation}
showSourceAudioTrack={clipRegions.some((c) => c.showSourceAudio)}
+ sourceAudioResourceVersion={sourceAudioFallbackRefreshKey}
sourceAudioTrackSettings={audio.activeSourceAudioTrackSettings}
getSourceAudioTrackSettingsForClip={
audio.getSourceAudioTrackSettingsForClip
diff --git a/src/components/video-editor/audio/audioResourceVersion.test.ts b/src/components/video-editor/audio/audioResourceVersion.test.ts
new file mode 100644
index 00000000..f6a6198e
--- /dev/null
+++ b/src/components/video-editor/audio/audioResourceVersion.test.ts
@@ -0,0 +1,50 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ getAudioResourceVersionKey,
+ getVersionedAudioResourceUrl,
+ isAudioResourceLoadCurrent,
+} from "./audioResourceVersion";
+
+describe("getAudioResourceVersionKey", () => {
+ it("changes identity when a finalized companion replaces a partial file at the same path", () => {
+ const audioPath = "C:\\Recordly\\recording.mic.wav";
+
+ expect(getAudioResourceVersionKey(audioPath, 0)).not.toBe(
+ getAudioResourceVersionKey(audioPath, 1),
+ );
+ });
+
+ it("is stable for rerenders within the same finalization version", () => {
+ const audioPath = "C:\\Recordly\\recording.mic.wav";
+
+ expect(getAudioResourceVersionKey(audioPath, 3)).toBe(
+ getAudioResourceVersionKey(audioPath, 3),
+ );
+ });
+
+ it("keeps an in-flight load valid across rerenders and rejects an older version", () => {
+ const audioPath = "C:\\Recordly\\recording.mic.wav";
+ const versionOne = getAudioResourceVersionKey(audioPath, 1);
+ const versionTwo = getAudioResourceVersionKey(audioPath, 2);
+ const resources = new Map([[audioPath, versionOne]]);
+
+ expect(isAudioResourceLoadCurrent(resources, audioPath, versionOne)).toBe(true);
+ resources.set(audioPath, versionTwo);
+ expect(isAudioResourceLoadCurrent(resources, audioPath, versionOne)).toBe(false);
+ });
+
+ it("cache-busts only the trusted loopback media URL after finalization", () => {
+ const localUrl =
+ "http://127.0.0.1:43123/video?path=C%3A%5CRecordly%5Crecording.mic.wav";
+ const versionedUrl = new URL(getVersionedAudioResourceUrl(localUrl, 2));
+
+ expect(versionedUrl.searchParams.get("path")).toBe(
+ "C:\\Recordly\\recording.mic.wav",
+ );
+ expect(versionedUrl.searchParams.get("recordlyAudioVersion")).toBe("2");
+ expect(getVersionedAudioResourceUrl("https://cdn.example/audio.wav?sig=abc", 2)).toBe(
+ "https://cdn.example/audio.wav?sig=abc",
+ );
+ });
+});
diff --git a/src/components/video-editor/audio/audioResourceVersion.ts b/src/components/video-editor/audio/audioResourceVersion.ts
new file mode 100644
index 00000000..30021054
--- /dev/null
+++ b/src/components/video-editor/audio/audioResourceVersion.ts
@@ -0,0 +1,35 @@
+export function getAudioResourceVersionKey(resource: string, version = 0): string {
+ const safeVersion = Number.isFinite(version) ? Math.max(0, Math.trunc(version)) : 0;
+ return `${resource}::recordly-audio-v${safeVersion}`;
+}
+
+export function getVersionedAudioResourceUrl(resourceUrl: string, version = 0): string {
+ const safeVersion = Number.isFinite(version) ? Math.max(0, Math.trunc(version)) : 0;
+ if (safeVersion === 0) {
+ return resourceUrl;
+ }
+
+ try {
+ const url = new URL(resourceUrl);
+ const isLoopbackMediaServer =
+ (url.protocol === "http:" || url.protocol === "https:") &&
+ (url.hostname === "127.0.0.1" || url.hostname === "localhost") &&
+ url.pathname === "/video";
+ if (!isLoopbackMediaServer) {
+ return resourceUrl;
+ }
+
+ url.searchParams.set("recordlyAudioVersion", String(safeVersion));
+ return url.href;
+ } catch {
+ return resourceUrl;
+ }
+}
+
+export function isAudioResourceLoadCurrent(
+ resources: ReadonlyMap<string, string>,
+ resource: string,
+ expectedKey: string,
+): boolean {
+ return resources.get(resource) === expectedKey;
+}
diff --git a/src/components/video-editor/audio/useAudioPreviewSync.ts b/src/components/video-editor/audio/useAudioPreviewSync.ts
index 1ede8b3b..2d91e839 100644
--- a/src/components/video-editor/audio/useAudioPreviewSync.ts
+++ b/src/components/video-editor/audio/useAudioPreviewSync.ts
@@ -9,6 +9,11 @@ import {
resolvePreviewMediaDuration,
} from "@/lib/mediaTiming";
import type { AudioRegion, SpeedRegion } from "../types";
+import {
+ getAudioResourceVersionKey,
+ getVersionedAudioResourceUrl,
+ isAudioResourceLoadCurrent,
+} from "./audioResourceVersion";
const SOURCE_AUDIO_PREVIEW_PLAYING_SEEK_DRIFT_SECONDS = 0.18;
const SOURCE_AUDIO_PREVIEW_PAUSED_SEEK_DRIFT_SECONDS = 0.01;
@@ -23,6 +28,7 @@ interface UseAudioPreviewSyncParams {
effectiveSpeedRegions: SpeedRegion[];
previewSourceAudioFallbackPaths: string[];
sourceAudioFallbackStartDelayMsByPath: Record<string, number>;
+ sourceAudioResourceVersion: number;
isCurrentClipMuted: boolean;
getSourceTrackPreviewGain: (audioPath: string) => number;
onSourceFallbackLoadError: (error: unknown) => void;
@@ -38,6 +44,7 @@ export function useAudioPreviewSync({
effectiveSpeedRegions,
previewSourceAudioFallbackPaths,
sourceAudioFallbackStartDelayMsByPath,
+ sourceAudioResourceVersion,
isCurrentClipMuted,
getSourceTrackPreviewGain,
onSourceFallbackLoadError,
@@ -167,7 +174,6 @@ export function useAudioPreviewSync({
}, [previewVolume, resolvedUserTracks]);
useEffect(() => {
- let cancelled = false;
const existing = sourceAudioElementsRef.current;
const currentIds = new Set(resolvedSourceTracks.map((track) => track.sourceRef.path));
@@ -188,6 +194,7 @@ export function useAudioPreviewSync({
for (const track of resolvedSourceTracks) {
const audioPath = track.sourceRef.path;
+ const resourceKey = getAudioResourceVersionKey(audioPath, sourceAudioResourceVersion);
let audio = existing.get(audioPath);
if (!audio) {
audio = new Audio();
@@ -202,42 +209,57 @@ export function useAudioPreviewSync({
// We route directly through the HTMLAudioElement to ensure pitch preservation works
// during speed changes. Note: this limits maximum preview volume to 1.0 (100%).
- if (sourceAudioElementResourcesRef.current.get(audioPath) !== audioPath) {
+ if (sourceAudioElementResourcesRef.current.get(audioPath) !== resourceKey) {
audio.pause();
audio.src = "";
sourceAudioElementRevokersRef.current.get(audioPath)?.();
sourceAudioElementRevokersRef.current.delete(audioPath);
- sourceAudioElementResourcesRef.current.set(audioPath, audioPath);
+ sourceAudioElementResourcesRef.current.set(audioPath, resourceKey);
+ // Resource identity, not effect lifetime, decides staleness. Playback and fallback
+ // rerenders must not cancel the only load for the current finalized sidecar version.
void (async () => {
try {
const resolved = await resolveMediaElementSource(audioPath);
const latestAudio = existing.get(audioPath);
if (
- cancelled ||
latestAudio !== audio ||
- sourceAudioElementResourcesRef.current.get(audioPath) !== audioPath
+ !isAudioResourceLoadCurrent(
+ sourceAudioElementResourcesRef.current,
+ audioPath,
+ resourceKey,
+ )
) {
resolved.revoke();
return;
}
sourceAudioElementRevokersRef.current.set(audioPath, resolved.revoke);
- latestAudio.src = resolved.src;
+ latestAudio.src = getVersionedAudioResourceUrl(
+ resolved.src,
+ sourceAudioResourceVersion,
+ );
latestAudio.load();
if (isPlaying) {
playSourceAudioPreview();
}
} catch (error) {
- if (cancelled) {
+ const latestAudio = existing.get(audioPath);
+ if (
+ latestAudio !== audio ||
+ !isAudioResourceLoadCurrent(
+ sourceAudioElementResourcesRef.current,
+ audioPath,
+ resourceKey,
+ )
+ ) {
return;
}
sourceAudioElementRevokersRef.current.get(audioPath)?.();
sourceAudioElementRevokersRef.current.delete(audioPath);
sourceAudioElementResourcesRef.current.delete(audioPath);
- const latestAudio = existing.get(audioPath);
if (latestAudio === audio) {
latestAudio.pause();
latestAudio.src = "";
@@ -259,16 +281,13 @@ export function useAudioPreviewSync({
if (resolvedSourceTracks.length === 0) {
lastSourceAudioSyncTimeRef.current = null;
}
-
- return () => {
- cancelled = true;
- };
}, [
getSourceTrackPreviewGain,
isPlaying,
isCurrentClipMuted,
onSourceFallbackLoadError,
resolvedSourceTracks,
+ sourceAudioResourceVersion,
previewVolume,
playSourceAudioPreview,
]);
diff --git a/src/components/video-editor/audio/useVideoEditorAudio.ts b/src/components/video-editor/audio/useVideoEditorAudio.ts
index 9d61f850..34d2d5b2 100644
--- a/src/components/video-editor/audio/useVideoEditorAudio.ts
+++ b/src/components/video-editor/audio/useVideoEditorAudio.ts
@@ -126,6 +126,7 @@ export function useVideoEditorAudio({
effectiveSpeedRegions,
previewSourceAudioFallbackPaths,
sourceAudioFallbackStartDelayMsByPath,
+ sourceAudioResourceVersion: sourceAudioFallbackRefreshKey,
isCurrentClipMuted,
getSourceTrackPreviewGain,
onSourceFallbackLoadError,
diff --git a/src/components/video-editor/audio/waveform/WaveformGenerator.ts b/src/components/video-editor/audio/waveform/WaveformGenerator.ts
index 9dd6c69f..79ca0937 100644
--- a/src/components/video-editor/audio/waveform/WaveformGenerator.ts
+++ b/src/components/video-editor/audio/waveform/WaveformGenerator.ts
@@ -1,4 +1,5 @@
import WorkerConstructor from "./waveform.worker?worker";
+import { getAudioResourceVersionKey } from "../audioResourceVersion";
import type { AudioPeaksData } from "../../timeline/core/timelineTypes";
import { WAVEFORM_DEFAULT_PEAK_COUNT } from "../../timeline/core/constants";
@@ -60,8 +61,12 @@ export class WaveformGenerator {
});
}
- public async generate(url: string, peakCount = WAVEFORM_DEFAULT_PEAK_COUNT): Promise<AudioPeaksData> {
- const cacheKey = `${url}::${peakCount}`;
+ public async generate(
+ url: string,
+ peakCount = WAVEFORM_DEFAULT_PEAK_COUNT,
+ resourceVersion = 0,
+ ): Promise<AudioPeaksData> {
+ const cacheKey = `${getAudioResourceVersionKey(url, resourceVersion)}::${peakCount}`;
const cached = this.peaksCache.get(cacheKey);
if (cached) return cached;
diff --git a/src/components/video-editor/timeline/TimelineEditor.tsx b/src/components/video-editor/timeline/TimelineEditor.tsx
index 7b90ec62..46a610f2 100644
--- a/src/components/video-editor/timeline/TimelineEditor.tsx
+++ b/src/components/video-editor/timeline/TimelineEditor.tsx
@@ -81,6 +81,7 @@ export interface TimelineEditorProps {
videoSourcePath?: string | null;
cursorTelemetrySourcePath?: string | null;
showSourceAudioTrack?: boolean;
+ sourceAudioResourceVersion?: number;
onSourceAudioAvailabilityChange?: (available: boolean) => void;
sourceAudioTrackSettings?: SourceAudioTrackSettings;
getSourceAudioTrackSettingsForClip?: (clipId: string | null) => SourceAudioTrackSettings;
@@ -163,6 +164,7 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
videoSourcePath,
cursorTelemetrySourcePath,
showSourceAudioTrack = false,
+ sourceAudioResourceVersion = 0,
onSourceAudioAvailabilityChange,
sourceAudioTrackSettings = {},
getSourceAudioTrackSettingsForClip,
@@ -245,8 +247,10 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
return { previewSpans, hiddenZoomIds };
}, [clipRegions, liveSpanPreviewById, zoomRegions]);
const { shortcuts: keyShortcuts, isMac } = useShortcuts();
- const { peaks: sourceAudioPeaks, loading: sourceAudioLoading } =
- useTimelineAudioPeaks(videoPath);
+ const { peaks: sourceAudioPeaks, loading: sourceAudioLoading } = useTimelineAudioPeaks(
+ videoPath,
+ { resourceVersion: sourceAudioResourceVersion },
+ );
const localSourcePath = useMemo(() => {
if (!videoPath) return null;
return (
@@ -270,12 +274,16 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
);
const { peaks: micSidecarPeaks, loading: micSidecarLoading } = useTimelineAudioPeaks(
micSidecarPaths[0] ?? null,
- { fallbackResources: micSidecarFallbackPaths },
+ {
+ fallbackResources: micSidecarFallbackPaths,
+ resourceVersion: sourceAudioResourceVersion,
+ },
);
const { peaks: systemSidecarPeaks, loading: systemSidecarLoading } = useTimelineAudioPeaks(
systemSidecarPaths[0] ?? null,
{
fallbackResources: systemSidecarFallbackPaths,
+ resourceVersion: sourceAudioResourceVersion,
},
);
const sourceAudioTracks = useMemo(
diff --git a/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts b/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts
index cf5ca393..4d56cf09 100644
--- a/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts
+++ b/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from "react";
+import { getVersionedAudioResourceUrl } from "@/components/video-editor/audio/audioResourceVersion";
import { resolveMediaResourceUrl } from "@/lib/exporter/localMediaSource";
import { waveformGenerator } from "../../audio/waveform/WaveformGenerator";
import { fromFileUrl } from "../../projectPersistence";
@@ -43,6 +44,7 @@ interface TimelineAudioPeaksOptions {
enableSourceSidecarFallback?: boolean;
fallbackResources?: string[];
peakCount?: number;
+ resourceVersion?: number;
}
export interface TimelineAudioPeaksResult {
@@ -60,6 +62,7 @@ export function useTimelineAudioPeaks(
const enableSourceSidecarFallback = options.enableSourceSidecarFallback ?? false;
const fallbackResources = options.fallbackResources ?? EMPTY_FALLBACK_RESOURCES;
const peakCount = options.peakCount ?? WAVEFORM_DEFAULT_PEAK_COUNT;
+ const resourceVersion = options.resourceVersion ?? 0;
useEffect(() => {
sourceRef.current = mediaResource;
@@ -75,7 +78,8 @@ export function useTimelineAudioPeaks(
const run = async () => {
const tryGenerate = async (resource: string): Promise<AudioPeaksData> => {
const resolvedUrl = await resolveMediaResourceUrl(resource);
- return waveformGenerator.generate(resolvedUrl, peakCount);
+ const versionedUrl = getVersionedAudioResourceUrl(resolvedUrl, resourceVersion);
+ return waveformGenerator.generate(versionedUrl, peakCount, resourceVersion);
};
try {
@@ -135,7 +139,7 @@ export function useTimelineAudioPeaks(
return () => {
cancelled = true;
};
- }, [mediaResource, enableSourceSidecarFallback, fallbackResources, peakCount]);
+ }, [mediaResource, enableSourceSidecarFallback, fallbackResources, peakCount, resourceVersion]);
return { peaks, loading };
}