Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/components/canvas/players/video-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,17 @@ export class VideoPlayer extends Player {
const desyncThreshold = 0.3;

if (shouldClipPlay) {
this.applyPlaybackVolume();
if (!this.isPlaying) {
this.isPlaying = true;
this.activeSyncTimer = 0;
this.texture.source.resource.volume = this.getVolume();
// Preserve the prepared frame unless playback has moved substantially.
if (Math.abs(this.texture.source.resource.currentTime - sourceTime) > desyncThreshold) {
this.texture.source.resource.currentTime = sourceTime;
}
this.texture.source.resource.play().catch(console.error);
}

if (this.texture.source.resource.volume !== this.getVolume()) {
this.texture.source.resource.volume = this.getVolume();
}

if (this.texture.source.resource.playbackRate !== speed) {
this.texture.source.resource.playbackRate = speed;
}
Expand Down Expand Up @@ -257,7 +253,7 @@ export class VideoPlayer extends Player {
}

const loadUrl = toLoadUrl(src);
const loadOptions: pixi.UnresolvedAsset = { src: loadUrl, data: { autoPlay: false, muted: false } };
const loadOptions: pixi.UnresolvedAsset = { src: loadUrl, data: { autoPlay: false, muted: videoAsset.volume === 0 } };

// Use unique loader to create independent video element per player
// This prevents conflicts when multiple clips use the same video source
Expand Down Expand Up @@ -298,10 +294,8 @@ export class VideoPlayer extends Player {
this.sprite = new pixi.Sprite(this.texture);
this.contentContainer.addChild(this.sprite);

// Set initial volume immediately so the element never sits at the browser default of 1.0
this.texture.source.resource.volume = this.getVolume();

this.texture.source.resource.preservesPitch = false;
this.applyPlaybackVolume();
video.preservesPitch = false;
}

private disposeVideo(): void {
Expand Down Expand Up @@ -337,6 +331,19 @@ export class VideoPlayer extends Player {
return this.volumeKeyframeBuilder.getValue(this.getPlaybackTime());
}

private applyPlaybackVolume(): void {
const video = this.texture?.source.resource;
if (!video) return;

// iOS can ignore volume writes, so a clip configured at zero needs an explicit mute.
const muted = (this.clipConfiguration.asset as VideoAsset).volume === 0;
if (!muted) {
const volume = this.getVolume();
if (video.volume !== volume) video.volume = volume;
}
if (video.muted !== muted) video.muted = muted;
}

public override getAssetSpeed(): number {
return this.animatedSpeed ? this.animatedSpeed.curve.getValue(this.getPlaybackTime()) : super.getAssetSpeed();
}
Expand Down
34 changes: 34 additions & 0 deletions tests/media-player-fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,40 @@ function createVideoTexture(width: number, height: number) {
}

describe("media player fallbacks", () => {
it("mutes zero-volume clips even when the browser ignores volume writes", async () => {
const edit = createEdit();
const video = document.createElement("video");
const setVolume = jest.fn();
const play = jest.fn(() => Promise.resolve(video.muted));
Object.defineProperties(video, {
readyState: { value: 4 },
volume: { get: () => 1, set: setVolume },
play: { value: play }
});
const texture = new pixi.Texture({ source: new pixi.VideoSource({ resource: video }) });
edit.assetLoader.loadVideoUnique.mockResolvedValueOnce(texture);
const asset = { type: "video" as const, src: "https://example.com/video.mp4", volume: 0 };
const player = new VideoPlayer(edit as never, { ...createVideoClip(), asset });
await player.load();

expect(edit.assetLoader.loadVideoUnique).toHaveBeenCalledWith(expect.any(String), {
src: expect.any(String),
data: { autoPlay: false, muted: true }
});
expect(video.muted).toBe(true);
edit.isPlaying = true;
player.update(0, 16);
expect(play).toHaveBeenCalledTimes(1);
await expect(play.mock.results[0].value).resolves.toBe(true);
expect(setVolume).not.toHaveBeenCalled();

asset.volume = 0.35;
player.reconfigureAfterRestore();
player.update(0, 16);
expect(video.muted).toBe(false);
expect(setVolume).toHaveBeenLastCalledWith(0.35);
});

it.each([1, 1.5])("prepares trimmed frames before display and after rewind at speed %s", async speed => {
const edit = createEdit();
edit.playbackTime = 12.47;
Expand Down
Loading