Change FocusMode using MediaStream API in Google Chrome

1,573 views
Skip to first unread message

Gokul vellingiri

unread,
Dec 6, 2018, 9:00:24 AM12/6/18
to discuss-webrtc
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.



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.

1. Can somebody tell what's wrong with the above code ? 

2. Is it possible to toggle focusMode when Preview is live ?


Reply all
Reply to author
Forward
0 new messages