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(wechat): stage packaged video to USER_DATA_PATH before decoding

yhh03c620098395
diff --git a/sdk/src/video/WeChatVideoBackend.ts b/sdk/src/video/WeChatVideoBackend.ts
index 9ff4086b6..fa5dc8e5a 100644
--- a/sdk/src/video/WeChatVideoBackend.ts
+++ b/sdk/src/video/WeChatVideoBackend.ts
@@ -30,16 +30,13 @@ class WeChatVideoStreamHandle implements VideoStreamHandle {
private lastPts_ = -1;
private flip_: Uint8Array | null = null;
private rateWarned_ = false;
+ private stagedPath_: string | null = null;
constructor(url: string, options: VideoStreamOptions) {
this.playing_ = options.autoplay ?? true;
this.loop_ = options.loop ?? false;
const decoder = wx.createVideoDecoder();
this.decoder_ = decoder;
- // mode 0 = decode by PTS. start() rejects in the WeChat devtools —
- // VideoDecoder is real-device only — so surface the failure loudly.
- decoder.start({ source: url, mode: 0 })
- .catch((err) => { log.error('video', `WeChat decoder start failed: ${url}`, err); this.onError?.(err); });
decoder.on('ended', () => {
if (this.loop_) {
this.lastPts_ = -1;
@@ -48,9 +45,41 @@ class WeChatVideoStreamHandle implements VideoStreamHandle {
this.onEnded?.();
}
});
+ void this.start_(url, options);
+ }
+
+ // VideoDecoder can't read the read-only code package on-device ("playerStart
+ // failed") — stage the clip into the writable user dir, then start (mode 0 =
+ // decode by PTS). start() also rejects in the devtools (real-device only).
+ private async start_(url: string, options: VideoStreamOptions): Promise<void> {
+ let source = url;
+ try {
+ source = await this.stageSource_(url);
+ } catch (err) {
+ log.warn('video', `WeChat stage failed, decoding from package: ${url}`, err);
+ }
+ if (this.disposed_) return;
+ this.decoder_.start({ source, mode: 0 })
+ .then(() => log.info('video', `WeChat decoder started: ${source}`))
+ .catch((err) => { log.error('video', `WeChat decoder start failed: ${source}`, err); this.onError?.(err); });
if (!(options.muted ?? false)) this.attachAudio_(options.volume ?? 1);
}
+ private async stageSource_(url: string): Promise<string> {
+ // Remote or already-writable paths need no staging.
+ if (/^https?:\/\//.test(url) || url.startsWith('wxfile://')) return url;
+ const userDir = wx.env.USER_DATA_PATH;
+ if (!userDir) return url;
+ const name = (url.split('/').pop() || 'clip.mp4').replace(/[^\w.-]/g, '_');
+ const dest = `${userDir}/estella_video_${this.id}_${name}`;
+ const fs = wx.getFileSystemManager();
+ await new Promise<void>((resolve, reject) => {
+ fs.copyFile({ srcPath: url, destPath: dest, success: () => resolve(), fail: reject });
+ });
+ this.stagedPath_ = dest;
+ return dest;
+ }
+
private attachAudio_(volume: number): void {
// Needs base lib ≥2.14.0; degrade to silent on older runtimes.
if (typeof wx.createMediaAudioPlayer !== 'function') return;
@@ -156,6 +185,10 @@ class WeChatVideoStreamHandle implements VideoStreamHandle {
requireResourceManager().releaseTexture(this.texture_);
this.texture_ = 0;
}
+ if (this.stagedPath_) {
+ try { wx.getFileSystemManager().unlink({ filePath: this.stagedPath_, success: () => {}, fail: () => {} }); } catch { /* ignore */ }
+ this.stagedPath_ = null;
+ }
this.flip_ = null;
}
}
diff --git a/sdk/tests/wechat-video-backend.test.ts b/sdk/tests/wechat-video-backend.test.ts
index 3df446315..009295372 100644
--- a/sdk/tests/wechat-video-backend.test.ts
+++ b/sdk/tests/wechat-video-backend.test.ts
@@ -28,11 +28,13 @@ describe('WeChatVideoBackend', () => {
delete (globalThis as any).wx;
});
- it('is named WeChat and starts the decoder in PTS mode', () => {
+ it('is named WeChat and starts the decoder in PTS mode', async () => {
backend.createStream('clip.mp4', { autoplay: true, muted: true });
expect(backend.name).toBe('WeChat');
expect((globalThis as any).wx.createVideoDecoder).toHaveBeenCalled();
- expect(decoder.start).toHaveBeenCalledWith({ source: 'clip.mp4', mode: 0 });
+ // start() runs after async source staging (no fs in the mock → falls back).
+ await new Promise((r) => setTimeout(r, 0));
+ expect(decoder.start).toHaveBeenCalledWith(expect.objectContaining({ mode: 0 }));
});
// Regression: getFrameData is typed non-null but returns null on-device until