From df3238878faf956c77ca0fbc08641bcfe44b4126 Mon Sep 17 00:00:00 2001 From: Niklas Gruhn Date: Sat, 9 May 2020 12:31:32 +0200 Subject: [PATCH] feat(QrcodeStream): capabilities in `init` payload --- src/components/QrcodeStream.vue | 56 ++++++++++++++++++++++----------- src/misc/camera.js | 6 ++++ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/components/QrcodeStream.vue b/src/components/QrcodeStream.vue index 946f1fb1..c155099b 100644 --- a/src/components/QrcodeStream.vue +++ b/src/components/QrcodeStream.vue @@ -152,16 +152,16 @@ export default { }, torch() { - this.$emit("init", this.init()); + this.init(); }, constraints() { - this.$emit("init", this.init()); + this.init(); } }, mounted() { - this.$emit("init", this.init()); + this.init(); }, beforeDestroy() { @@ -171,23 +171,41 @@ export default { }, methods: { - async init() { - this.beforeResetCamera(); - - if (this.constraints === undefined) { - this.cameraInstance = null; - } else { - this.cameraInstance = await Camera(this.constraints, this.$refs.video, { - torch: this.torch - }); - - // if the component is destroyed before `cameraInstance` resolves a - // `beforeDestroy` hook has no chance to clear the remaining camera - // stream. - if (this.destroyed) { - this.cameraInstance.stop(); + init() { + const promise = (async () => { + this.beforeResetCamera(); + + if (this.constraints === undefined) { + this.cameraInstance = null; + + return { + capabilities: {} + }; + } else { + this.cameraInstance = await Camera( + this.constraints, + this.$refs.video, + { + torch: this.torch + } + ); + + const capabilities = this.cameraInstance.getCapabilities(); + + // if the component is destroyed before `cameraInstance` resolves a + // `beforeDestroy` hook has no chance to clear the remaining camera + // stream. + if (this.destroyed) { + this.cameraInstance.stop(); + } + + return { + capabilities + }; } - } + })(); + + this.$emit("init", promise); }, startScanning() { diff --git a/src/misc/camera.js b/src/misc/camera.js index 0ba93ffd..dff3f1c4 100644 --- a/src/misc/camera.js +++ b/src/misc/camera.js @@ -16,6 +16,12 @@ class Camera { captureFrame() { return imageDataFromVideo(this.videoEl); } + + getCapabilities() { + const [track] = this.stream.getVideoTracks(); + + return track.getCapabilities(); + } } const INSECURE_CONTEXT = window.isSecureContext !== true;