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

fix(web): obtain media device label by requesting stream #218

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
41 changes: 31 additions & 10 deletions web/shared/tools/debugger/compat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ const WhepLayerSelect = [

const NoneDevice = { value: '', text: 'none' };

function deviceInfoToOption(info: MediaDeviceInfo) {
const value = info.deviceId;
let text = info.label;
if (text.length <= 0) {
text = `${info.kind} (${info.deviceId})`;
}
return { value, text };
}

function uniqByValue<T extends { value: unknown }>(items: T[]) {
const map = new Map<unknown, T>();
for (const item of items) {
if (!map.has(item.value)) {
map.set(item.value, item);
}
}
return Array.from(map.values());
}

export default function DebuggerCompat() {
const streamIdInput = useUrlParamsInput('id');
const idBearerTokenInput = useUrlParamsInput('token');
Expand All @@ -71,19 +90,21 @@ export default function DebuggerCompat() {

const refreshDevice = useCallback(async () => {
try {
// to obtain non-empty device label, there needs to be an active media stream or persistent permission
// https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/label#value
const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
const devices = (await navigator.mediaDevices.enumerateDevices()).filter(i => !!i.deviceId);
mediaStream.getTracks().map(track => track.stop());
const audio = devices.filter(i => i.kind === 'audioinput').map(deviceInfoToOption);
if (audio.length > 0) {
setAudioDevices(uniqByValue(audio));
}
const video = devices.filter(i => i.kind === 'videoinput').map(deviceInfoToOption);
if (video.length > 0) {
setVideoDevices(uniqByValue(video));
}
} catch (e) {
console.error('Failed to getUserMedia:', e);
}
const devices = (await navigator.mediaDevices.enumerateDevices()).filter(i => !!i.deviceId);
const audio = devices.filter(i => i.kind === 'audioinput').map(i => ({ value: i.deviceId, text: i.label }));
if (audio.length > 0) {
setAudioDevices(audio);
}
const video = devices.filter(i => i.kind === 'videoinput').map(i => ({ value: i.deviceId, text: i.label }));
if (video.length > 0) {
setVideoDevices(video);
console.error('refreshDevice failed:', e);
}
setRefreshDisabled(true);
}, []);
Expand Down