Settings before Oct 23rd
by mdg3
HTML
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API/Constraints -->
<p>Experiment with media constraints! Edit the constraint sets for the
video and audio tracks in the edit boxes on the left, then click the
"Apply Constraints" button to try them out. The actual settings the
browser selected and is using are shown in the boxes on the right.
Below all of that, you'll see the video itself.</p>
<p>Click the "Start" button to begin.</p>
<h3>Constrainable properties available:</h3>
<ul id="supportedConstraints">
</ul>
<div id="startButton" class="button">
Start
</div>
<div class="wrapper">
<div class="trackrow">
<div class="leftside">
<h3>Requested video constraints:</h3>
<textarea id="videoConstraintEditor" cols=32 rows=8></textarea>
</div>
<div class="rightside">
<h3>Actual video settings:</h3>
<textarea id="videoSettingsText" cols=32 rows=8 disabled></textarea>
</div>
</div>
<div class="trackrow">
<div class="leftside">
<h3>Requested audio constraints:</h3>
<textarea id="audioConstraintEditor" cols=32 rows=8></textarea>
</div>
<div class="rightside">
<h3>Actual audio settings:</h3>
<textarea id="audioSettingsText" cols=32 rows=8 disabled></textarea>
</div>
</div>
<div class="button" id="applyButton">
Apply Constraints
</div>
</div>
<video id="video" autoplay muted></video>
<div class="button" id="stopButton">
Stop Video
</div>
<div id="log">
</div>
CSS
body {
font: 14px "Open Sans", "Arial", sans-serif;
}
video {
margin-top: 20px;
border: 1px solid black;
}
.button {
cursor: pointer;
width: 150px;
border: 1px solid black;
font-size: 16px;
text-align: center;
padding-top: 2px;
padding-bottom: 4px;
color: white;
background-color: darkgreen;
}
.wrapper {
margin-bottom: 10px;
width: 600px;
}
.trackrow {
height: 200px;
}
.leftside {
float: left;
width: calc(calc(100%/2) - 10px);
}
.rightside {
float: right;
width: calc(calc(100%/2) - 10px);
}
textarea {
padding: 8px;
}
h3 {
margin-bottom: 3px;
}
#supportedConstraints {
column-count: 2;
-moz-column-count: 2;
}
#log {
padding-top: 10px;
}
JavaScript
let videoDefaultConstraintString = JSON.stringify({
"width": 480,
"height": 360,
"frameRate": 16,
"facingMode": "user"
});
let audioDefaultConstraintString = '{\n "sampleSize": 16,\n "channelCount": 2,\n "echoCancellation": false\n}';
let videoConstraints = null;
let audioConstraints = null;
let audioTrack = null;
let videoTrack = null;
let videoElement = document.getElementById("video");
let logElement = document.getElementById("log");
let supportedConstraintList = document.getElementById("supportedConstraints");
let videoConstraintEditor = document.getElementById("videoConstraintEditor");
let audioConstraintEditor = document.getElementById("audioConstraintEditor");
let videoSettingsText = document.getElementById("videoSettingsText");
let audioSettingsText = document.getElementById("audioSettingsText");
videoConstraintEditor.value = videoDefaultConstraintString;
audioConstraintEditor.value = audioDefaultConstraintString;function getCurrentSettings() {
if (videoTrack) {
videoSettingsText.value = JSON.stringify(videoTrack.getSettings(), null, 2);
}
if (audioTrack) {
audioSettingsText.value = JSON.stringify(audioTrack.getSettings(), null, 2);
}
}
function buildConstraints() {
try {
videoConstraints = JSON.parse(videoConstraintEditor.value);
audioConstraints = JSON.parse(audioConstraintEditor.value);
} catch(error) {
handleError(error);
}
}
function startVideo() {
buildConstraints();
navigator.mediaDevices.getUserMedia({
video: videoConstraints,
audio: audioConstraints
}).then(function(stream) {
let audioTracks = stream.getAudioTracks();
let videoTracks = stream.getVideoTracks();
videoElement.srcObject = stream;
if (audioTracks.length) {
audioTrack = audioTracks[0];
}
if (videoTracks.length) {
videoTrack = videoTracks[0];
}
}).then(function() {
new Promise(function(resolve) {
videoElement.onloadedmetadata = resolve;
});
}).then(function() {
...