Skip to content

Commit 51921f4

Browse files
committed
Improve default device handling
1 parent 2073a8a commit 51921f4

File tree

8 files changed

+28
-17
lines changed

8 files changed

+28
-17
lines changed

packages/core/src/components/mediaDeviceSelect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function setupDeviceSelector(
5151
activeDeviceSubject.next(useDefault ? id : actualDeviceId);
5252
} else if (localTrack) {
5353
await localTrack.setDeviceId(options.exact ? { exact: id } : id);
54-
const actualId = await localTrack.getDeviceId();
54+
const actualId = await localTrack.getDeviceId(false);
5555
activeDeviceSubject.next(
5656
id === 'default' && localTrack.mediaStreamTrack.label.startsWith('Default') ? id : actualId,
5757
);

packages/core/src/observables/room.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export function createActiveDeviceObservable(room: Room, kind: MediaDeviceKind)
246246
return roomEventSelector(room, RoomEvent.ActiveDeviceChanged).pipe(
247247
filter(([kindOfDevice]) => kindOfDevice === kind),
248248
map(([kind, deviceId]) => {
249-
log.debug('activeDeviceObservable | RoomEvent.ActiveDeviceChanged', { kind, deviceId });
249+
log.info('activeDeviceObservable | RoomEvent.ActiveDeviceChanged', { kind, deviceId });
250250
return deviceId;
251251
}),
252252
);

packages/core/src/persistent-storage/user-choices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export type LocalUserChoices = {
3939
export const defaultUserChoices: LocalUserChoices = {
4040
videoEnabled: true,
4141
audioEnabled: true,
42-
videoDeviceId: '',
43-
audioDeviceId: '',
42+
videoDeviceId: 'default',
43+
audioDeviceId: 'default',
4444
username: '',
4545
} as const;
4646

packages/react/src/components/controls/MediaDeviceSelect.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ export const MediaDeviceSelect: (
113113
[className, props],
114114
);
115115

116+
const hasDefault = !!devices.find((info) => info.label.toLowerCase().startsWith('default'));
117+
116118
function isActive(deviceId: string, activeDeviceId: string, index: number) {
117-
return deviceId === activeDeviceId || (index === 0 && activeDeviceId === 'default');
119+
return (
120+
deviceId === activeDeviceId || (!hasDefault && index === 0 && activeDeviceId === 'default')
121+
);
118122
}
119123

120124
return (

packages/react/src/hooks/useMediaDeviceSelect.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createMediaDeviceObserver, setupDeviceSelector, log } from '@livekit/components-core';
2-
import type { LocalAudioTrack, LocalVideoTrack, Room } from 'livekit-client';
2+
import { getBrowser, type LocalAudioTrack, type LocalVideoTrack, type Room } from 'livekit-client';
33
import * as React from 'react';
44
import { useMaybeRoomContext } from '../context';
55
import { useObservableState } from './internal';
@@ -54,7 +54,7 @@ export function useMediaDeviceSelect({
5454
const devices = useObservableState(deviceObserver, [] as MediaDeviceInfo[]);
5555
// Active device management.
5656
const [currentDeviceId, setCurrentDeviceId] = React.useState<string>(
57-
roomContext?.getActiveDevice(kind) ?? '',
57+
roomContext?.getActiveDevice(kind) ?? 'default',
5858
);
5959
const { className, activeDeviceObservable, setActiveMediaDevice } = React.useMemo(
6060
() => setupDeviceSelector(kind, room ?? roomContext, track),
@@ -63,10 +63,11 @@ export function useMediaDeviceSelect({
6363

6464
React.useEffect(() => {
6565
const listener = activeDeviceObservable.subscribe((deviceId) => {
66-
if (deviceId && deviceId !== currentDeviceId) {
67-
log.info('setCurrentDeviceId', deviceId);
68-
setCurrentDeviceId(deviceId);
66+
if (!deviceId) {
67+
return;
6968
}
69+
log.info('setCurrentDeviceId', deviceId);
70+
setCurrentDeviceId(deviceId);
7071
});
7172
return () => {
7273
listener?.unsubscribe();

packages/react/src/prefabs/ControlBar.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface ControlBarProps extends React.HTMLAttributes<HTMLDivElement> {
5656
export function ControlBar({
5757
variation,
5858
controls,
59-
saveUserChoices = true,
59+
saveUserChoices = false,
6060
onDeviceError,
6161
...props
6262
}: ControlBarProps) {
@@ -144,7 +144,9 @@ export function ControlBar({
144144
<div className="lk-button-group-menu">
145145
<MediaDeviceMenu
146146
kind="audioinput"
147-
onActiveDeviceChange={(_kind, deviceId) => saveAudioInputDeviceId(deviceId ?? '')}
147+
onActiveDeviceChange={(_kind, deviceId) =>
148+
saveAudioInputDeviceId(deviceId ?? 'default')
149+
}
148150
/>
149151
</div>
150152
</div>
@@ -162,7 +164,9 @@ export function ControlBar({
162164
<div className="lk-button-group-menu">
163165
<MediaDeviceMenu
164166
kind="videoinput"
165-
onActiveDeviceChange={(_kind, deviceId) => saveVideoInputDeviceId(deviceId ?? '')}
167+
onActiveDeviceChange={(_kind, deviceId) =>
168+
saveVideoInputDeviceId(deviceId ?? 'default')
169+
}
166170
/>
167171
</div>
168172
</div>

packages/react/src/prefabs/PreJoin.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function usePreviewDevice<T extends LocalVideoTrack | LocalAudioTrack>(
131131
})
132132
: await createLocalAudioTrack({ deviceId });
133133

134-
const newDeviceId = await track.getDeviceId();
134+
const newDeviceId = await track.getDeviceId(false);
135135
if (newDeviceId && deviceId !== newDeviceId) {
136136
prevDeviceId.current = newDeviceId;
137137
setLocalDeviceId(newDeviceId);
@@ -248,8 +248,8 @@ export function PreJoin({
248248
saveUsername,
249249
} = usePersistentUserChoices({
250250
defaults: partialDefaults,
251-
preventSave: !persistUserChoices,
252-
preventLoad: !persistUserChoices,
251+
preventSave: true,
252+
preventLoad: true,
253253
});
254254

255255
// Initialize device settings

packages/react/src/prefabs/VoiceAssistantControlBar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ export function VoiceAssistantControlBar({
9595
<div className="lk-button-group-menu">
9696
<MediaDeviceMenu
9797
kind="audioinput"
98-
onActiveDeviceChange={(_kind, deviceId) => saveAudioInputDeviceId(deviceId ?? '')}
98+
onActiveDeviceChange={(_kind, deviceId) =>
99+
saveAudioInputDeviceId(deviceId ?? 'default')
100+
}
99101
/>
100102
</div>
101103
</div>

0 commit comments

Comments
 (0)