Skip to content

Commit 8d2fea4

Browse files
committed
fix(@init): capabilities empty on some devices
According to https://oberhofer.co/mediastreamtrack-and-its-capabilities/#queryingcapabilities on some devices, getCapabilities only returns a non-empty object aftersome delay. There is no appropriate event so we have to add some constant timeout.
1 parent 2d3b3e6 commit 8d2fea4

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

demo.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
{{ errorMessage }}
3434
</p>
3535

36-
<qrcode-stream @decode="onDecode" camera="rear" @init="onInit"></qrcode-stream>
36+
<qrcode-stream @decode="onDecode" :torch="torch" @init="onInit"></qrcode-stream>
37+
38+
<button @click="torch=!torch">Torch On/Off</button>
3739
</div>
3840
</body>
3941
<script>
@@ -43,7 +45,8 @@
4345
data() {
4446
return {
4547
decodedContent: '',
46-
errorMessage: ''
48+
errorMessage: '',
49+
torch: false
4750
}
4851
},
4952

src/misc/camera.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import adapterFactory from "webrtc-adapter/src/js/adapter_factory.js";
22
import { StreamApiNotSupportedError, InsecureContextError } from "./errors.js";
33
import { imageDataFromVideo } from "./image-data.js";
4-
import { eventOn } from "callforth";
4+
import { eventOn, timeout } from "callforth";
55

66
class Camera {
77
constructor(videoEl, stream) {
@@ -19,7 +19,6 @@ class Camera {
1919

2020
getCapabilities() {
2121
const [track] = this.stream.getVideoTracks();
22-
2322
return track.getCapabilities();
2423
}
2524
}
@@ -121,12 +120,18 @@ export default async function(videoEl, { camera, torch }) {
121120

122121
await eventOn(videoEl, "loadeddata");
123122

123+
// According to: https://oberhofer.co/mediastreamtrack-and-its-capabilities/#queryingcapabilities
124+
// On some devices, getCapabilities only returns a non-empty object after
125+
// some delay. There is no appropriate event so we have to add a constant timeout
126+
await timeout(500);
127+
124128
if (torch) {
125129
const [track] = stream.getVideoTracks();
130+
const capabilities = track.getCapabilities();
126131

127-
try {
128-
await track.applyConstraints({ advanced: [{ torch: true }] });
129-
} catch (error) {
132+
if (capabilities.torch) {
133+
track.applyConstraints({ advanced: [{ torch: true }] });
134+
} else {
130135
console.warn("device does not support torch capability");
131136
}
132137
}

0 commit comments

Comments
 (0)