Victor
Victor

Reputation: 11

MediaDevice applyConstraints doesn't work

I am trying to apply new constraints to the video track, but it doesn't work:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body onload="onLoad();">
    <p id="res1"></p>
    <p id="res2"></p>
<script>
    async function onLoad() {
        var opts1 = {audio: true, video: {advanced: [{ width: 1280, height: 960 },]}};
        var stream = await navigator.mediaDevices.getUserMedia(opts1);
        var cstr1 = stream.getVideoTracks()[0].getSettings();
        document.getElementById("res1").innerHTML = cstr1.width + ", " + cstr1.height;

        var opts2 = {audio: true, video: {advanced: [{ width: 640, height: 480 },]}};
        await stream.getVideoTracks()[0].applyConstraints(opts2);
        cstr2 = stream.getVideoTracks()[0].getSettings();
        document.getElementById("res2").innerHTML = cstr2.width + ", " + cstr2.height;
    }
    </script>
</body>
</html>

I expect the answer "1280, 960" before applying the constraints and "640, 480" after. But I see "1280, 960" instead. Why? How can I apply it correctly?

Upvotes: 1

Views: 1681

Answers (1)

spaceman
spaceman

Reputation: 1167

You only need to apply the video value of opts2 as the constraints parameter of applyConstraints(constraints) for it to work.

var opts2 = { advanced: [{ width: 640, height: 480 }] }
await stream.getVideoTracks()[0].applyConstraints(opts2)

If you look at the MediaTrackConstraints properties on MDN you can see that there are no audio or video properties.

Upvotes: 0

Related Questions