Skip to content

Commit

Permalink
Improve default device handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Dec 19, 2024
1 parent 2073a8a commit 51921f4
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/components/mediaDeviceSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function setupDeviceSelector(
activeDeviceSubject.next(useDefault ? id : actualDeviceId);
} else if (localTrack) {
await localTrack.setDeviceId(options.exact ? { exact: id } : id);
const actualId = await localTrack.getDeviceId();
const actualId = await localTrack.getDeviceId(false);
activeDeviceSubject.next(
id === 'default' && localTrack.mediaStreamTrack.label.startsWith('Default') ? id : actualId,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/observables/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export function createActiveDeviceObservable(room: Room, kind: MediaDeviceKind)
return roomEventSelector(room, RoomEvent.ActiveDeviceChanged).pipe(
filter(([kindOfDevice]) => kindOfDevice === kind),
map(([kind, deviceId]) => {
log.debug('activeDeviceObservable | RoomEvent.ActiveDeviceChanged', { kind, deviceId });
log.info('activeDeviceObservable | RoomEvent.ActiveDeviceChanged', { kind, deviceId });
return deviceId;
}),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/persistent-storage/user-choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export type LocalUserChoices = {
export const defaultUserChoices: LocalUserChoices = {
videoEnabled: true,
audioEnabled: true,
videoDeviceId: '',
audioDeviceId: '',
videoDeviceId: 'default',
audioDeviceId: 'default',
username: '',
} as const;

Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/controls/MediaDeviceSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ export const MediaDeviceSelect: (
[className, props],
);

const hasDefault = !!devices.find((info) => info.label.toLowerCase().startsWith('default'));

function isActive(deviceId: string, activeDeviceId: string, index: number) {
return deviceId === activeDeviceId || (index === 0 && activeDeviceId === 'default');
return (
deviceId === activeDeviceId || (!hasDefault && index === 0 && activeDeviceId === 'default')
);
}

return (
Expand Down
11 changes: 6 additions & 5 deletions packages/react/src/hooks/useMediaDeviceSelect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMediaDeviceObserver, setupDeviceSelector, log } from '@livekit/components-core';
import type { LocalAudioTrack, LocalVideoTrack, Room } from 'livekit-client';
import { getBrowser, type LocalAudioTrack, type LocalVideoTrack, type Room } from 'livekit-client';
import * as React from 'react';
import { useMaybeRoomContext } from '../context';
import { useObservableState } from './internal';
Expand Down Expand Up @@ -54,7 +54,7 @@ export function useMediaDeviceSelect({
const devices = useObservableState(deviceObserver, [] as MediaDeviceInfo[]);
// Active device management.
const [currentDeviceId, setCurrentDeviceId] = React.useState<string>(
roomContext?.getActiveDevice(kind) ?? '',
roomContext?.getActiveDevice(kind) ?? 'default',
);
const { className, activeDeviceObservable, setActiveMediaDevice } = React.useMemo(
() => setupDeviceSelector(kind, room ?? roomContext, track),
Expand All @@ -63,10 +63,11 @@ export function useMediaDeviceSelect({

React.useEffect(() => {
const listener = activeDeviceObservable.subscribe((deviceId) => {
if (deviceId && deviceId !== currentDeviceId) {
log.info('setCurrentDeviceId', deviceId);
setCurrentDeviceId(deviceId);
if (!deviceId) {
return;
}
log.info('setCurrentDeviceId', deviceId);
setCurrentDeviceId(deviceId);
});
return () => {
listener?.unsubscribe();
Expand Down
10 changes: 7 additions & 3 deletions packages/react/src/prefabs/ControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ControlBarProps extends React.HTMLAttributes<HTMLDivElement> {
export function ControlBar({
variation,
controls,
saveUserChoices = true,
saveUserChoices = false,
onDeviceError,
...props
}: ControlBarProps) {
Expand Down Expand Up @@ -144,7 +144,9 @@ export function ControlBar({
<div className="lk-button-group-menu">
<MediaDeviceMenu
kind="audioinput"
onActiveDeviceChange={(_kind, deviceId) => saveAudioInputDeviceId(deviceId ?? '')}
onActiveDeviceChange={(_kind, deviceId) =>
saveAudioInputDeviceId(deviceId ?? 'default')
}
/>
</div>
</div>
Expand All @@ -162,7 +164,9 @@ export function ControlBar({
<div className="lk-button-group-menu">
<MediaDeviceMenu
kind="videoinput"
onActiveDeviceChange={(_kind, deviceId) => saveVideoInputDeviceId(deviceId ?? '')}
onActiveDeviceChange={(_kind, deviceId) =>
saveVideoInputDeviceId(deviceId ?? 'default')
}
/>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/prefabs/PreJoin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function usePreviewDevice<T extends LocalVideoTrack | LocalAudioTrack>(
})
: await createLocalAudioTrack({ deviceId });

const newDeviceId = await track.getDeviceId();
const newDeviceId = await track.getDeviceId(false);
if (newDeviceId && deviceId !== newDeviceId) {
prevDeviceId.current = newDeviceId;
setLocalDeviceId(newDeviceId);
Expand Down Expand Up @@ -248,8 +248,8 @@ export function PreJoin({
saveUsername,
} = usePersistentUserChoices({
defaults: partialDefaults,
preventSave: !persistUserChoices,
preventLoad: !persistUserChoices,
preventSave: true,
preventLoad: true,
});

// Initialize device settings
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/prefabs/VoiceAssistantControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export function VoiceAssistantControlBar({
<div className="lk-button-group-menu">
<MediaDeviceMenu
kind="audioinput"
onActiveDeviceChange={(_kind, deviceId) => saveAudioInputDeviceId(deviceId ?? '')}
onActiveDeviceChange={(_kind, deviceId) =>
saveAudioInputDeviceId(deviceId ?? 'default')
}
/>
</div>
</div>
Expand Down

0 comments on commit 51921f4

Please sign in to comment.