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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(video): synchronize the decoded endpoint before playback ends
yhhcb80c0aff24c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d36c34124..acc5540e1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **WASM video progress reaches the decoded endpoint.** Playback ending after a seek or a large update no longer leaves the clock at its previous position. The decoder derives the final duration from the last frame, and the SDK refreshes it before notifying end-of-playback listeners. Looping and seeking back to the beginning retain their normal clocks.+- **Single-timestamp MPEG videos report duration and support seeking.** Videos whose frames fit in one PES packet no longer report a zero duration or seek only to the first frame. The decoder counts picture headers without decoding frames at startup and uses frame-based seeking when packet timestamps cannot describe the timeline. Timestamp-based seeking remains in use for other streams.- **Video component playback settings update live streams.** Changing volume, mute, loop, or playback rate updates the current stream without reopening the decoder. Unchanged settings avoid repeated backend calls across frames; source changes still create a stream with the current settings.diff --git a/sdk/src/video/WasmVideoBackend.ts b/sdk/src/video/WasmVideoBackend.tsindex f5b1d1a20..746026936 100644--- a/sdk/src/video/WasmVideoBackend.ts+++ b/sdk/src/video/WasmVideoBackend.ts@@ -310,6 +310,8 @@ class WasmVideoStreamHandle implements VideoStreamHandle {if (!this.loop_ && video._es_video_has_ended(this.decoder_)) {this.ended_ = true;this.playing_ = false;+ const duration = video._es_video_duration(this.decoder_);+ if (duration > 0) this.duration_ = duration;this.stopAudio_();this.onEnded?.();}diff --git a/sdk/tests/videodec-module.test.ts b/sdk/tests/videodec-module.test.tsindex e3357ec9e..e7e37f557 100644--- a/sdk/tests/videodec-module.test.ts+++ b/sdk/tests/videodec-module.test.ts@@ -82,6 +82,12 @@ describe.skipIf(!HAS_VIDEODEC)('videodec side module (pl_mpeg)', () => {} finally { mod._free(rgba); }expect(mod._es_video_seek(h, 0)).toBe(1);expect(mod._es_video_time(h)).toBe(0);+ expect(mod._es_video_seek(h, 0.5)).toBe(1);+ mod._es_video_advance(h, 999);+ expect(mod._es_video_has_ended(h)).toBe(1);+ expect(mod._es_video_time(h)).toBeCloseTo(mod._es_video_duration(h));+ expect(mod._es_video_seek(h, 0)).toBe(1);+ expect(mod._es_video_has_ended(h)).toBe(0);expect(mod._es_video_seek(h, 999)).toBe(1);expect(mod._es_video_time(h)).toBeLessThan(mod._es_video_duration(h));mod._es_video_set_loop(h, 1);@@ -131,6 +137,11 @@ describe.skipIf(!HAS_VIDEODEC)('videodec side module (pl_mpeg)', () => {const h = openFixture();mod._es_video_advance(h, 5.0);expect(mod._es_video_has_ended(h)).toBe(1);+ expect(mod._es_video_duration(h)).toBeCloseTo(3);+ expect(mod._es_video_time(h)).toBeCloseTo(3);+ expect(mod._es_video_seek(h, 0)).toBe(1);+ expect(mod._es_video_has_ended(h)).toBe(0);+ expect(mod._es_video_time(h)).toBe(0);mod._es_video_close(h);const h2 = openFixture();diff --git a/sdk/tests/wasm-video-backend.test.ts b/sdk/tests/wasm-video-backend.test.tsindex 32f27927e..b58a2bf74 100644--- a/sdk/tests/wasm-video-backend.test.ts+++ b/sdk/tests/wasm-video-backend.test.ts@@ -185,6 +185,20 @@ describe('WasmVideoBackend', () => {handle.stop();});+ it('refreshes a packet-estimated duration before notifying that playback ended', async () => {+ const { mod, raw, state } = mockVideoModule();+ const handle = new WasmVideoBackend({ sideModules: () => hostFor(mod) }).createStream('clip.esv', { loop: false });+ await flush();+ expect(handle.duration).toBe(2.5);+ raw._es_video_duration.mockReturnValue(3);+ state.time = 3; state.ended = 1;+ const ended = vi.fn(() => { expect(handle.duration).toBe(3); expect(handle.currentTime).toBe(3); });+ handle.onEnded = ended;+ handle.pump(engineModule);+ expect(ended).toHaveBeenCalledOnce();+ handle.stop();+ });+it('wires loop and playback rate into the decoder', async () => {const { mod, raw, state } = mockVideoModule();const backend = new WasmVideoBackend({ sideModules: () => hostFor(mod) });diff --git a/src/esengine/bindings/modules/video/VideoModuleEntry.cpp b/src/esengine/bindings/modules/video/VideoModuleEntry.cppindex debd0e2df..e309f2482 100644--- a/src/esengine/bindings/modules/video/VideoModuleEntry.cpp+++ b/src/esengine/bindings/modules/video/VideoModuleEntry.cpp@@ -141,7 +141,8 @@ double es_video_framerate(int handle) {/** Playhead in seconds (wraps on loop). */double es_video_time(int handle) {Instance* inst = get(handle);- return inst ? plm_get_time(inst->plm) : 0.0;+ if (!inst) return 0.0;+ return plm_has_ended(inst->plm) ? inst->duration : plm_get_time(inst->plm);}/** With looping on, the stream rewinds seamlessly and never reports ended. */@@ -164,6 +165,9 @@ int es_video_advance(int handle, double dt) {Instance* inst = get(handle);if (!inst) return 0;plm_decode(inst->plm, dt);+ if (plm_has_ended(inst->plm) && inst->frame) {+ inst->duration = inst->frame->time + 1.0 / plm_get_framerate(inst->plm);+ }return inst->newFrame ? 1 : 0;}diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 8c6bceda0..612c02f02 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): synchronize the decoded endpoint before playback ends':+ { note: 'WASM video progress reaches the decoded endpoint.' },'fix(video): support duration and seeking for single-timestamp streams':{ note: 'Single-timestamp MPEG videos report duration and support seeking.' },'fix(video): synchronize live component playback settings':