Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(video): replace live streams when component sources change

yhh61e4285e59f2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd17f6d7d..256697e2b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Video components switch streams when their source changes.** Changing a live component's source releases its previous stream and opens the new source. Unchanged sources keep their stream across frames; clearing the source or removing the entity still releases the current stream.
+
- **WASM video exports fail when required transcoding fails.** An unavailable or failed video converter no longer stages an original MP4 that the target's WASM decoder cannot play. The cook reports the failed asset, and mini-game exports propagate asset cooking failures as build errors instead of reporting a usable package.
- **Repeated exports remove obsolete cooked assets and packs.** Web and mini-game exports compare the previous and current asset manifests, deleting old texture hashes, removed assets, and packs no longer referenced by the build. Shared current packs, project sources, and unlisted output files are preserved.
diff --git a/sdk/src/video/VideoPlugin.ts b/sdk/src/video/VideoPlugin.ts
index b96a52fef..aca903f59 100644
--- a/sdk/src/video/VideoPlugin.ts
+++ b/sdk/src/video/VideoPlugin.ts
@@ -80,6 +80,7 @@ export class VideoPlugin implements Plugin {
app.insertResource(VideoPlayer, video);
const handles = new Map<number, VideoStreamHandle>();
+ const sources = new WeakMap<VideoStreamHandle, string>();
this.handles_ = handles;
const warnedNoSprite = new Set<number>();
const live = new Set<number>();
@@ -113,14 +114,18 @@ export class VideoPlugin implements Plugin {
if (!v.enabled || !v.source) continue;
const id = entity as number;
live.add(id);
- if (!handles.has(id)) {
- handles.set(id, videoAPI.play(v.source, {
+ const previous = handles.get(id);
+ if (!previous || sources.get(previous) !== v.source) {
+ if (previous) videoAPI.stop(previous);
+ const handle = videoAPI.play(v.source, {
autoplay: v.autoplay,
loop: v.loop,
muted: v.muted,
volume: v.volume,
playbackRate: v.playbackRate,
- }));
+ });
+ handles.set(id, handle);
+ sources.set(handle, v.source);
}
}
diff --git a/sdk/tests/video-source-switch.test.ts b/sdk/tests/video-source-switch.test.ts
new file mode 100644
index 000000000..4a9d4f93a
--- /dev/null
+++ b/sdk/tests/video-source-switch.test.ts
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { afterEach, expect, it, vi } from 'vitest';
+import { App } from '../src/app/app';
+import { VideoPlugin } from '../src/video/VideoPlugin';
+import { Video } from '../src/video/VideoComponents';
+import { NullVideoBackend } from '../src/video/NullVideoBackend';
+
+const { backend } = vi.hoisted(() => ({ backend: { createStream: vi.fn(), dispose: vi.fn(), name: 'probe' } }));
+vi.mock('../src/platform/base', async original => ({
+ ...await original<typeof import('../src/platform/base')>(),
+ getPlatform: () => ({ createVideoBackend: () => backend }),
+}));
+const apps: App[] = [];
+afterEach(() => { for (const app of apps.splice(0)) app.quit(); vi.clearAllMocks(); });
+
+it('switches a live source without restarting unchanged streams and releases retired handles', async () => {
+ const handles: ReturnType<NullVideoBackend['createStream']>[] = [];
+ backend.createStream.mockImplementation((_url, options) => {
+ const handle = new NullVideoBackend().createStream('', options);
+ vi.spyOn(handle, 'stop'); handles.push(handle); return handle;
+ });
+ const app = App.new(); apps.push(app); app.addPlugin(new VideoPlugin());
+ const entity = app.world.spawn();
+ app.world.insert(entity, Video, { source: 'first.mp4' });
+ await app.tick(1 / 60);
+ for (let i = 0; i < 10; i++) await app.tick(1 / 60);
+ expect(backend.createStream).toHaveBeenCalledTimes(1);
+ app.world.update(entity, Video, value => { value.source = 'second.mp4'; });
+ await app.tick(1 / 60);
+ expect(backend.createStream).toHaveBeenCalledTimes(2);
+ expect(backend.createStream.mock.calls[1][0]).toBe('second.mp4');
+ expect(handles[0].stop).toHaveBeenCalledOnce();
+ expect(handles[1].stop).not.toHaveBeenCalled();
+ for (let i = 0; i < 100; i++) {
+ app.world.update(entity, Video, value => { value.source = `clip-${i}.mp4`; });
+ await app.tick(1 / 60);
+ }
+ app.world.update(entity, Video, value => { value.source = ''; });
+ await app.tick(1 / 60);
+ expect(handles).toHaveLength(102);
+ for (const handle of handles) expect(handle.stop).toHaveBeenCalledOnce();
+ app.world.update(entity, Video, value => { value.source = 'second.mp4'; });
+ await app.tick(1 / 60);
+ expect(handles).toHaveLength(103);
+ app.world.despawn(entity);
+ expect(handles[102].stop).toHaveBeenCalledOnce();
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 87c54a532..935bf0018 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(video): replace live streams when component sources change':
+ { note: 'Video components switch streams when their source changes.' },
'fix(export): reject failed video transcoding for wasm targets':
{ note: 'WASM video exports fail when required transcoding fails.' },
'fix(export): prune obsolete cooked assets and packs':