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): WeChat pump survives an unsupported/throwing decoder
yhh529698d1e6c0
diff --git a/sdk/src/video/WeChatVideoBackend.ts b/sdk/src/video/WeChatVideoBackend.tsindex 17222dee6..9ff4086b6 100644--- a/sdk/src/video/WeChatVideoBackend.ts+++ b/sdk/src/video/WeChatVideoBackend.ts@@ -36,12 +36,10 @@ class WeChatVideoStreamHandle implements VideoStreamHandle {this.loop_ = options.loop ?? false;const decoder = wx.createVideoDecoder();this.decoder_ = decoder;- // mode 0 = decode by PTS. Surface start success/failure — a bad package- // path or unsupported codec otherwise just yields a silent white frame.+ // 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 })- .then(() => log.info('video', `WeChat decoder started: ${url}`)).catch((err) => { log.error('video', `WeChat decoder start failed: ${url}`, err); this.onError?.(err); });- decoder.on('start', (res: unknown) => log.info('video', `WeChat decode 'start'`, res));decoder.on('ended', () => {if (this.loop_) {this.lastPts_ = -1;@@ -73,8 +71,16 @@ class WeChatVideoStreamHandle implements VideoStreamHandle {pump(module: ESEngineModule): void {if (this.disposed_ || !this.playing_) return;// getFrameData is typed non-null but returns null on-device until a frame- // has decoded — guard for it rather than trusting the vendor type.- const frame: WechatMinigame.FrameDataOptions | null = this.decoder_.getFrameData();+ // has decoded (and throws where the API is unsupported) — guard both so a+ // stalled or unsupported decoder never crashes the video system.+ let frame: WechatMinigame.FrameDataOptions | null;+ try {+ frame = this.decoder_.getFrameData();+ } catch (err) {+ this.onError?.(err);+ this.disposed_ = true;+ return;+ }if (!frame || !frame.data || frame.data.byteLength === 0 || frame.width <= 0) return;if (frame.pkPts === this.lastPts_) return; // no new framethis.lastPts_ = frame.pkPts;@@ -91,7 +97,6 @@ class WeChatVideoStreamHandle implements VideoStreamHandle {/* flipY */ false,{ filterMode: 'linear', wrapMode: 'clamp' },);- log.info('video', `WeChat first frame ${w}x${h}`);this.ready_ = true;this.onReady?.();} else if (w === this.width_ && h === this.height_) {diff --git a/sdk/tests/wechat-video-backend.test.ts b/sdk/tests/wechat-video-backend.test.tsindex 4b1e6e9e6..3df446315 100644--- a/sdk/tests/wechat-video-backend.test.ts+++ b/sdk/tests/wechat-video-backend.test.ts@@ -50,4 +50,13 @@ describe('WeChatVideoBackend', () => {expect(() => handle.pump({} as never)).not.toThrow();expect(handle.textureHandle).toBe(0);});++ // The WeChat devtools throws on getFrameData (VideoDecoder is real-device only) —+ // pump must swallow it, not crash the whole video system.+ it('pump tolerates getFrameData() throwing (unsupported API)', () => {+ decoder.getFrameData.mockImplementation(() => { throw new Error('unsupported in devtools'); });+ const handle = backend.createStream('clip.mp4', { autoplay: true, muted: true });+ expect(() => handle.pump({} as never)).not.toThrow();+ expect(handle.textureHandle).toBe(0);+ });});