Skip to content
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
9 changes: 9 additions & 0 deletions js/hang/src/publish/source/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,19 @@ export class Device<Kind extends "audio" | "video"> {

// Manually request permission for the device, ignoring the result.
requestPermission() {
if (this.permission.peek()) return;

navigator.mediaDevices
.getUserMedia({ [this.kind]: true })
.then((stream) => {
this.permission.set(true);

// If the user selected a device during the dialog prompt, save it as the preferred device.
const deviceId = stream.getTracks().at(0)?.getSettings().deviceId;
if (deviceId) {
this.preferred.set(deviceId);
}

stream.getTracks().forEach((track) => {
track.stop();
});
Expand Down
7 changes: 6 additions & 1 deletion js/hang/src/publish/source/microphone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Microphone {
const constraints = effect.get(this.constraints) ?? {};
const finalConstraints: MediaTrackConstraints = {
...constraints,
deviceId: device ? { exact: device } : undefined,
deviceId: device !== undefined ? { exact: device } : undefined,
};

effect.spawn(async (cancel) => {
Expand All @@ -62,6 +62,11 @@ export class Microphone {

const settings = track.getSettings();

if (device === undefined) {
// Save the device that the user selected during the dialog prompt.
this.device.preferred.set(settings.deviceId);
}

effect.set(this.device.active, settings.deviceId);
effect.set(this.stream, track);
});
Expand Down
Loading