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 ondevicechange event support to demo #1689

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Changes from 3 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
59 changes: 46 additions & 13 deletions src/content/devices/input-output/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@
const audioOutputSelect = document.querySelector('select#audioOutput');
const videoSelect = document.querySelector('select#videoSource');
const selectors = [audioInputSelect, audioOutputSelect, videoSelect];
var hasMic = false;

Check failure on line 16 in src/content/devices/input-output/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected var, use let or const instead
var hasCamera = false;

Check failure on line 17 in src/content/devices/input-output/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected var, use let or const instead
var openMic = undefined;

Check failure on line 18 in src/content/devices/input-output/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected var, use let or const instead
var openCamera = undefined;

Check failure on line 19 in src/content/devices/input-output/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected var, use let or const instead

audioOutputSelect.disabled = !('sinkId' in HTMLMediaElement.prototype);

function getDevices() {
console.log('getDevices');
navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
}

function gotDevices(deviceInfos) {
console.log('gotDevices', deviceInfos);
hasMic = false;
hasCamera = false;
// Handles being called several times to update labels. Preserve values.
const values = selectors.map(select => select.value);
selectors.forEach(select => {
Expand All @@ -26,15 +38,18 @@
});
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
console.log(deviceInfo);
const option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
hasMic = true;
option.text = deviceInfo.label || `microphone ${audioInputSelect.length + 1}`;
audioInputSelect.appendChild(option);
} else if (deviceInfo.kind === 'audiooutput') {
option.text = deviceInfo.label || `speaker ${audioOutputSelect.length + 1}`;
audioOutputSelect.appendChild(option);
} else if (deviceInfo.kind === 'videoinput') {
hasCamera = true;
option.text = deviceInfo.label || `camera ${videoSelect.length + 1}`;
videoSelect.appendChild(option);
} else {
Expand All @@ -46,10 +61,9 @@
select.value = values[selectorIndex];
}
});
start();
}

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

// Attach audio output device to video element using device/sink ID.
function attachSinkId(element, sinkId) {
if (typeof element.sinkId !== 'undefined') {
Expand Down Expand Up @@ -79,32 +93,51 @@
function gotStream(stream) {
window.stream = stream; // make stream available to console
videoElement.srcObject = stream;
// Refresh button list in case labels have become available
return navigator.mediaDevices.enumerateDevices();
if (stream.getVideoTracks()[0]) {
openCamera = stream.getVideoTracks()[0].getSettings().deviceId;
}
if (stream.getAudioTracks()[0]) {
openMic = stream.getAudioTracks()[0].getSettings().deviceId;
}
console.log('openCamera', openCamera, 'openMic', openMic);
// Refresh list in case labels have become available
return getDevices();
}

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

function start() {
const audioSource = audioInputSelect.value || undefined;
const videoSource = videoSelect.value || undefined;
console.log('audio', audioSource, 'video', videoSource);
if (openMic == audioSource && openCamera == videoSource) {
return;
}
if (window.stream) {
window.stream.getTracks().forEach(track => {
track.stop();
});
openCamera = undefined;
openMic = undefined;
}
let constraints = {};

Check failure on line 125 in src/content/devices/input-output/js/main.js

View workflow job for this annotation

GitHub Actions / lint

'constraints' is never reassigned. Use 'const' instead
if (hasMic) {
constraints['audio'] = {deviceId: audioSource ? {exact: audioSource} : undefined};
}
if (hasCamera) {
constraints['video'] = {deviceId: videoSource ? {exact: videoSource} : undefined};

Check failure on line 130 in src/content/devices/input-output/js/main.js

View workflow job for this annotation

GitHub Actions / lint

Multiple spaces found before '{'
}
console.log('start', constraints);
if (hasCamera || hasMic) {
navigator.mediaDevices.getUserMedia(constraints).then(gotStream).catch(handleError);
}
const audioSource = audioInputSelect.value;
const videoSource = videoSelect.value;
const constraints = {
audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {deviceId: videoSource ? {exact: videoSource} : undefined}
};
navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
}

audioInputSelect.onchange = start;
audioOutputSelect.onchange = changeAudioDestination;

videoSelect.onchange = start;
navigator.mediaDevices.ondevicechange = getDevices;

start();
getDevices();
Loading