Changing FocusMode not working using MediaStream A

2019-05-08 08:37发布

问题:

In Google Chrome Browser i was able to get live feed of my connected USB Camera using getUserMedia() API. I have a slider to change the brightness value and this is working fine. I also want focusMode to toggle from continuous to manual(The camera always starts with continuous focusMode).

I have the below Javascript code to change FocusMode.

const video_constraints ={};

//Create the following keys for Constraint
video_constraints.video = {};
video_constraints.video.width = {};
video_constraints.video.width.exact = 1920; //set video width
video_constraints.video.height = {};
video_constraints.video.height.exact = 1080; //set video height
video_constraints.video.frameRate = {};
video_constraints.video.frameRate.exact = 60; //set video frame rate

//Start stream
navigator.mediaDevices.getUserMedia(video_constraints).then(handleStreamSuccessCb).catch(handleStreamErrorCb);


function handleStreamSuccessCb()
{
    console.log("Got Stream");
        window.stream = stream;
        videoElement.srcObject = stream;
    getVideoCaps(stream);
}

function getVideoCaps(stream)
{

    var videoTrackArray = stream.getVideoTracks();
    var videoTrack = null;

    for (i=0; i<videoTrackArray.length; i++)
    {
        if (videoTrackArray[i].kind == "video")
        {
            console.log("Video track found");
            videoTrack = videoTrackArray[i];
            break;
        }
    }

    if (videoTrack != null)
    {
        setTimeout(() => {

        const capabilities = videoTrack.getCapabilities()
        console.log("Caps:");
        console.log(capabilities);

        //Brightness:
        if (capabilities.brightness)
        {
            //configure slider settings
            brightnessSliderUI.min = capabilities.brightness.min;
            brightnessSliderUI.max = capabilities.brightness.max;
            brightnessSliderUI.step = capabilities.brightness.step;
            brightnessSliderUI.value = videoTrack.getSettings().brightness;

            //set inital value
            brightnessSliderValueUI.value = brightnessSliderUI.value;

            //slider change listener
            brightnessSliderUI.oninput = function() {
                    brightnessSliderValueUI.value = brightnessSliderUI.value;
                    videoTrack.applyConstraints({advanced : [{brightness: brightnessSliderUI.value}] });
                }

        }
        else
        {
            console.log("brightnessNot supported");
        }


        //Focus Mode
                if (capabilities.focusMode)
                {   
            console.log(videoTrack.getSettings());// By default continuous value is set for focusMode

            focusButtonUI.onclick = function(){
                console.log("focusButton Clicked");

                videoTrack.applyConstraints({advanced : [{focusMode: "manual"}]});
                // I am not able to set focusMode to manual with the above statement
                            console.log(videoTrack.getSettings());

            }
        }
      }, 500);
    }
    else
    {
        showErrorDialog("No Video track found in the stream");
    }
}

With the below line i am trying to toggle the focusMode:

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

But this focus was still in continuous mode.

Can somebody tell what's wrong with the above code ? Is it possible to toggle focusMode when Preview is live ?