From 779a793a1d089b8cc06d14d4744a59e96d4e7487 Mon Sep 17 00:00:00 2001 From: Simon Chan <1330321+yume-chan@users.noreply.github.com> Date: Tue, 26 Aug 2025 11:53:36 +0800 Subject: [PATCH] fix(decoder): fix TypeError when converting timestamp --- .../src/video/decoder.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/libraries/scrcpy-decoder-webcodecs/src/video/decoder.ts b/libraries/scrcpy-decoder-webcodecs/src/video/decoder.ts index 5b455b5f..9453ffbf 100644 --- a/libraries/scrcpy-decoder-webcodecs/src/video/decoder.ts +++ b/libraries/scrcpy-decoder-webcodecs/src/video/decoder.ts @@ -164,13 +164,26 @@ export class WebCodecsVideoDecoder implements ScrcpyVideoDecoder { // Set `pts` to 0 as a marker for skipping rendering this frame packet.pts = 0n; } else { + const [ms, us] = performance.now().toString().split("."); + + // Multiply `performance.now()` by 1000 to get microseconds. + // Use string manipulation to improve precision. + let timestamp = ms!; + // `performance.now` might return an integer so `us` might be undefined + if (us) { + if (us.length < 3) { + timestamp += us.padEnd(3, "0"); + } else { + timestamp += us.slice(0, 3); + } + } else { + timestamp += "000"; + } + // Set `pts` to current time to track decoding time // Technically `performance.now()` can return 0 (when document starts loading), // but in practice it's impossible to call it at that time. - const [ms, us] = performance.now().toString().split("."); - // Multiply `performance.now()` by 1000 to get microseconds. - // String manipulation is used to keep precision. - packet.pts = BigInt(ms + us!.slice(0, 3).padEnd(3, "0")); + packet.pts = BigInt(timestamp); } return this.#decoder.decode(packet); },