Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add camera selector. #1677

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/content/getusermedia/resolution/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ <h1><a href="//webrtc.github.io/samples/" title="WebRTC samples homepage">WebRTC
title="W3C getusermedia specification - constraints section">constraints</a>.</p>

<p>Click a button to call <code>getUserMedia()</code> with appropriate resolution.</p>

<div class="select">
<label for="videoSource">Video source: </label><select id="videoSource"></select>
</div>
<div id="buttons">
<button id="p180">180p (320x180)</button>
<button id="qvga">QVGA (320x240)</button>
Expand Down
26 changes: 26 additions & 0 deletions src/content/getusermedia/resolution/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const aspectLock = document.querySelector('#aspectlock');
const sizeLock = document.querySelector('#sizelock');
const pauseVideo = document.querySelector('#pausevideo');

const videoSelect = document.querySelector('select#videoSource');

let currentWidth = 0;
let currentHeight = 0;

Expand Down Expand Up @@ -114,6 +116,28 @@ const eightKConstraints = {
video: {width: {exact: 7680}, height: {exact: 4320}}
};

function gotDevices(deviceInfos) {
// Handles being called several times to update labels. Preserve values.
while (videoSelect.firstChild) {
videoSelect.removeChild(videoSelect.firstChild);
}
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
const option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || `camera ${videoSelect.length + 1}`;
videoSelect.appendChild(option);
}
}
}

function handleError(error) {
console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);

function gotStream(mediaStream) {
stream = window.stream = mediaStream; // stream available to console
video.srcObject = mediaStream;
Expand Down Expand Up @@ -212,6 +236,8 @@ function getMedia(constraints) {

clearErrorMessage();
videoblock.style.display = 'none';
constraints.video.deviceId = {exact: videoSelect.value};
console.log('getUserMedia constraints: ' + JSON.stringify(constraints));
navigator.mediaDevices.getUserMedia(constraints)
.then(gotStream)
.catch(e => {
Expand Down