Skip to content

Commit

Permalink
fix(web): remove hardcoded capture frame rate
Browse files Browse the repository at this point in the history
  • Loading branch information
rocka committed Oct 7, 2024
1 parent 8984a58 commit 2216669
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 10 additions & 4 deletions web/shared/components/dialog-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const PreviewDialog = forwardRef<IPreviewDialog, Props>((props, ref) => {
const refDialog = useRef<HTMLDialogElement>(null);
const refVideo = useRef<HTMLVideoElement>(null);
const refStreamDecoder = useRef<QRCodeStreamDecoder>(null);
const [latency, setLatency] = useState(0);
const [latency, setLatency] = useState<string>();

useImperativeHandle(ref, () => {
return {
Expand Down Expand Up @@ -161,13 +161,14 @@ export const PreviewDialog = forwardRef<IPreviewDialog, Props>((props, ref) => {

const handleDecodeLatency = (e: TargetedEvent) => {
e.preventDefault();
setLatency('-- ms');
if (refVideo.current != null && refStreamDecoder.current == null) {
refStreamDecoder.current = new QRCodeStreamDecoder(refVideo.current);
}
const decoder = refStreamDecoder.current!;
decoder.start();
decoder.addEventListener('latency', (e: CustomEvent<number>) => {
setLatency(e.detail);
setLatency(`${e.detail} ms`);
})
};

Expand All @@ -187,8 +188,13 @@ export const PreviewDialog = forwardRef<IPreviewDialog, Props>((props, ref) => {
<form method="dialog">
<button onClick={handleCloseDialog}>Hide</button>
<button onClick={handlePreviewStop} class="text-red-500">Stop</button>
<button onClick={handleDecodeLatency}>Decode Latency</button>
{latency > 0 ? (<span>Latency: {latency}ms</span>) : null}
{
typeof latency === 'string' ? (
<span>Latency: {latency}</span>
) : (
<button onClick={handleDecodeLatency}>Decode Latency</button>
)
}
</form>
</dialog>
);
Expand Down
5 changes: 3 additions & 2 deletions web/shared/qrcode-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class QRCodeStream {
this.scheduled = true;
window.requestAnimationFrame(this.frameRequestCallback);
}
const ms = this.canvas.captureStream(60);
const ms = this.canvas.captureStream();
this.capturing = ms;
return ms;
}
Expand Down Expand Up @@ -121,6 +121,7 @@ export class QRCodeStreamDecoder extends TypedEventTarget<QRCodeStreamDecoderEve
);
}

// TODO: move decoding to worker
private decodeFrame(now: number, width: number, height: number) {
this.canvas.width = width;
this.canvas.height = height;
Expand All @@ -139,7 +140,7 @@ export class QRCodeStreamDecoder extends TypedEventTarget<QRCodeStreamDecoderEve

private videoFrameCallback_unbound(_now: DOMHighResTimeStamp, metadata: VideoFrameCallbackMetadata) {
this.seq++;
// TODO make it configurable
// TODO: make it configurable
if (this.seq >= 5) {
this.seq = 0;
const now = Date.now();
Expand Down

0 comments on commit 2216669

Please sign in to comment.