Skip to content

Commit

Permalink
fix(@init): capabilities empty on some devices
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gruhn committed Jul 2, 2020
1 parent 2d3b3e6 commit 8d2fea4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 5 additions & 2 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
{{ errorMessage }}
</p>

<qrcode-stream @decode="onDecode" camera="rear" @init="onInit"></qrcode-stream>
<qrcode-stream @decode="onDecode" :torch="torch" @init="onInit"></qrcode-stream>

<button @click="torch=!torch">Torch On/Off</button>
</div>
</body>
<script>
Expand All @@ -43,7 +45,8 @@
data() {
return {
decodedContent: '',
errorMessage: ''
errorMessage: '',
torch: false
}
},

Expand Down
15 changes: 10 additions & 5 deletions src/misc/camera.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import adapterFactory from "webrtc-adapter/src/js/adapter_factory.js";
import { StreamApiNotSupportedError, InsecureContextError } from "./errors.js";
import { imageDataFromVideo } from "./image-data.js";
import { eventOn } from "callforth";
import { eventOn, timeout } from "callforth";

class Camera {
constructor(videoEl, stream) {
Expand All @@ -19,7 +19,6 @@ class Camera {

getCapabilities() {
const [track] = this.stream.getVideoTracks();

return track.getCapabilities();
}
}
Expand Down Expand Up @@ -121,12 +120,18 @@ export default async function(videoEl, { camera, torch }) {

await eventOn(videoEl, "loadeddata");

// According to: https://oberhofer.co/mediastreamtrack-and-its-capabilities/#queryingcapabilities
// On some devices, getCapabilities only returns a non-empty object after
// some delay. There is no appropriate event so we have to add a constant timeout
await timeout(500);

if (torch) {
const [track] = stream.getVideoTracks();
const capabilities = track.getCapabilities();

try {
await track.applyConstraints({ advanced: [{ torch: true }] });
} catch (error) {
if (capabilities.torch) {
track.applyConstraints({ advanced: [{ torch: true }] });
} else {
console.warn("device does not support torch capability");
}
}
Expand Down

0 comments on commit 8d2fea4

Please sign in to comment.