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): support duration and seeking for single-timestamp streams

yhh6ff482561acd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7b49b5d9a..d36c34124 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **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.
- **Video plugin cleanup releases only the selected App.** Shared plugin instances retain separate video backends and stream maps for each App, so closing a preview cannot stop another App's video or leave its own backend behind. Cleanup detaches the world subscription and disables its old update system. Stopping component streams also retires them from the video service instead of pumping stopped handles.
diff --git a/sdk/tests/videodec-module.test.ts b/sdk/tests/videodec-module.test.ts
index 3a0c60b04..e3357ec9e 100644
--- a/sdk/tests/videodec-module.test.ts
+++ b/sdk/tests/videodec-module.test.ts
@@ -60,6 +60,36 @@ describe.skipIf(!HAS_VIDEODEC)('videodec side module (pl_mpeg)', () => {
mod._es_video_close(h);
});
+ it('reports a duration and seeks within a single video PES packet', () => {
+ const bytes = readFileSync(FIXTURE);
+ const start = bytes.indexOf(Buffer.from([0, 0, 1, 0xe0]));
+ expect(start).toBeGreaterThanOrEqual(0);
+ const end = start + 6 + bytes.readUInt16BE(start + 4);
+ const single = bytes.subarray(0, end);
+ const ptr = mod._malloc(single.length);
+ let h = 0;
+ try {
+ mod.HEAPU8.set(single, ptr); h = mod._es_video_open(ptr, single.length);
+ expect(h).toBeGreaterThan(0);
+ expect(mod._es_video_duration(h)).toBeGreaterThan(0.5);
+ expect(mod._es_video_seek(h, 0.5)).toBe(1);
+ expect(mod._es_video_time(h)).toBeGreaterThanOrEqual(0.5);
+ expect(mod._es_video_time(h)).toBeLessThan(0.55);
+ const rgba = mod._malloc(W * H * 4);
+ try {
+ expect(mod._es_video_frame_rgba(h, rgba, W * H * 4)).toBe(1);
+ expectColor(Array.from(mod.HEAPU8.subarray(rgba, rgba + 4)), 255, 0, 0);
+ } 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, 999)).toBe(1);
+ expect(mod._es_video_time(h)).toBeLessThan(mod._es_video_duration(h));
+ mod._es_video_set_loop(h, 1);
+ mod._es_video_advance(h, mod._es_video_duration(h) * 2);
+ expect(mod._es_video_has_ended(h)).toBe(0);
+ } finally { if (h) mod._es_video_close(h); mod._free(ptr); }
+ });
+
it('rejects bytes that are not an MPEG stream', () => {
const junk = new Uint8Array(256).fill(0x42);
const ptr = mod._malloc(junk.length);
diff --git a/src/esengine/bindings/modules/video/VideoModuleEntry.cpp b/src/esengine/bindings/modules/video/VideoModuleEntry.cpp
index 02c1cfeae..debd0e2df 100644
--- a/src/esengine/bindings/modules/video/VideoModuleEntry.cpp
+++ b/src/esengine/bindings/modules/video/VideoModuleEntry.cpp
@@ -18,6 +18,7 @@
#include <cstdio> // pl_mpeg's declarations expect size_t / FILE from the includer
#include <cstdlib>
#include <cstring>
+#include <algorithm>
#define PL_MPEG_IMPLEMENTATION
#include "pl_mpeg.h"
@@ -30,6 +31,8 @@ struct Instance {
plm_t* plm = nullptr;
plm_frame_t* frame = nullptr; // latest decoded frame; owned by plm
bool newFrame = false;
+ double duration = 0;
+ bool frameIndexedSeek = false;
};
Instance g_instances[kMaxInstances];
@@ -46,6 +49,25 @@ void onVideoFrame(plm_t*, plm_frame_t* frame, void* user) {
inst->newFrame = true;
}
+/** Count picture headers in video payloads without decoding or copying frames. */
+double singleTimestampDuration(uint8_t* bytes, uint32_t size, double fps) {
+ plm_buffer_t* buffer = plm_buffer_create_with_memory(bytes, size, FALSE);
+ if (!buffer) return 0;
+ plm_demux_t* demux = plm_demux_create(buffer, TRUE);
+ if (!demux) { plm_buffer_destroy(buffer); return 0; }
+ uint32_t code = 0xffffffff;
+ uint32_t pictures = 0;
+ while (plm_packet_t* packet = plm_demux_decode(demux)) {
+ if (packet->type != PLM_DEMUX_PACKET_VIDEO_1) continue;
+ for (size_t i = 0; i < packet->length; i++) {
+ code = (code << 8) | packet->data[i];
+ if (code == 0x00000100) pictures++;
+ }
+ }
+ plm_demux_destroy(demux);
+ return fps > 0 ? pictures / fps : 0;
+}
+
} // namespace
extern "C" {
@@ -87,6 +109,11 @@ int es_video_open(uintptr_t dataPtr, uint32_t size) {
inst.plm = plm;
inst.frame = nullptr;
inst.newFrame = false;
+ inst.duration = plm_get_duration(plm);
+ inst.frameIndexedSeek = inst.duration <= 0;
+ if (inst.frameIndexedSeek) {
+ inst.duration = singleTimestampDuration(owned, size, plm_get_framerate(plm));
+ }
plm_set_video_decode_callback(plm, onVideoFrame, &inst);
return slot + 1;
}
@@ -103,7 +130,7 @@ uint32_t es_video_height(int handle) {
double es_video_duration(int handle) {
Instance* inst = get(handle);
- return inst ? plm_get_duration(inst->plm) : 0.0;
+ return inst ? inst->duration : 0.0;
}
double es_video_framerate(int handle) {
@@ -173,6 +200,16 @@ int es_video_frame_rgba(int handle, uintptr_t outPtr, uint32_t outSize) {
int es_video_seek(int handle, double time) {
Instance* inst = get(handle);
if (!inst) return 0;
+ if (inst->frameIndexedSeek) {
+ const double fps = plm_get_framerate(inst->plm);
+ const double target = std::max(0.0, std::min(time, inst->duration - 1.0 / fps));
+ plm_rewind(inst->plm);
+ plm_frame_t* frame = plm_decode_video(inst->plm);
+ while (frame && frame->time < target) frame = plm_decode_video(inst->plm);
+ if (!frame) return 0;
+ onVideoFrame(inst->plm, frame, inst);
+ return 1;
+ }
return plm_seek(inst->plm, time, /* seek_exact */ TRUE) ? 1 : 0;
}
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 83c39c6e4..8c6bceda0 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): 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':
{ note: 'Video component playback settings update live streams.' },
'fix(video): isolate plugin cleanup and stream ownership per app':