applyConstraints() changes focusMode when using Chrome on Linux, but not Windows

200 Views Asked by At

I am using the following code to get access to an available webcam, check to see if focusMode is among its capabilities, and if so, change focusMode from "continuous" to "manual". This method works when I use Chrome on Ubuntu 22.04, but not on Chrome in Windows 11. I haven't tested it elsewhere.

class Streamer {
    constructor() {
        this.video = document.createElement("video")
        this.canvas = document.getElementById("canvas")
        this.constraints = { video: { width: { ideal: 1280}, height: { ideal: 720 } } }
    }

    getMedia () {
        navigator.mediaDevices.getUserMedia(this.constraints)
            .then((mediastream) => {
                this.video.srcObject = mediastream
                this.videoTrack = mediastream.getVideoTracks()[0]
                this.capabilities = this.videoTrack.getCapabilities()
                this.settings = this.videoTrack.getSettings()

                console.log("capabilites:\n", this.capabilities)
                if (this.capabilities.focusMode) {
                    this.videoTrack.applyConstraints({advanced:[{focusMode: "manual"}]})
                    console.log("focusMode set to ", this.settings.focusMode)
                }
            })
            .catch ((err) => console.error(err))      
    }
1

There are 1 best solutions below

0
JHow On

Expressing the keys as strings solves the problem and works on both platforms.

this.videoTrack.applyConstraints({"advanced":[{"focusMode": "manual"}]})