Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszFilimowski committed Jan 12, 2025
1 parent 0a16d02 commit 678becb
Showing 1 changed file with 44 additions and 45 deletions.
89 changes: 44 additions & 45 deletions packages/react-client/src/hooks/devices/useInitializeDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,59 @@ export type UseInitializeDevicesResult = {
export const useInitializeDevices = (): UseInitializeDevicesResult => {
const { videoDeviceManagerRef, audioDeviceManagerRef, hasDevicesBeenInitializedRef } = useFishjamContext();

const initializeDevices = useCallback(async (params?: UseInitializeDevicesParams) => {
const { enableVideo = true, enableAudio = true } = params || {};
const initializeDevices = useCallback(
async (params?: UseInitializeDevicesParams) => {
const { enableVideo = true, enableAudio = true } = params || {};

if (hasDevicesBeenInitializedRef.current) return;
hasDevicesBeenInitializedRef.current = true;
if (hasDevicesBeenInitializedRef.current) return;
hasDevicesBeenInitializedRef.current = true;

const videoManager = videoDeviceManagerRef.current;
const audioManager = audioDeviceManagerRef.current;
const videoManager = videoDeviceManagerRef.current;
const audioManager = audioDeviceManagerRef.current;

const constraints = {
video: enableVideo ? videoManager.getConstraints() : false,
audio: enableAudio ? audioManager.getConstraints() : false,
};
const constraints = {
video: enableVideo ? videoManager.getConstraints() : false,
audio: enableAudio ? audioManager.getConstraints() : false,
};

const previousDevices = {
video: videoManager.getLastDevice(),
audio: audioManager.getLastDevice(),
};
const previousDevices = {
video: videoManager.getLastDevice(),
audio: audioManager.getLastDevice(),
};

// Attempt to start the last selected device to avoid an unnecessary restart.
// Without this, the first device will start, and `getCorrectedResult` will attempt to fix it.
let [stream, deviceErrors] = await getAvailableMedia({
video: enableVideo
? prepareConstraints(previousDevices.video?.deviceId, constraints.video)
: false,
audio: enableAudio
? prepareConstraints(previousDevices.audio?.deviceId, constraints.audio)
: false,
});
// Attempt to start the last selected device to avoid an unnecessary restart.
// Without this, the first device will start, and `getCorrectedResult` will attempt to fix it.
let [stream, deviceErrors] = await getAvailableMedia({
video: enableVideo ? prepareConstraints(previousDevices.video?.deviceId, constraints.video) : false,
audio: enableAudio ? prepareConstraints(previousDevices.audio?.deviceId, constraints.audio) : false,
});

const devices = await navigator.mediaDevices.enumerateDevices();
const devices = await navigator.mediaDevices.enumerateDevices();

const videoDevices = devices.filter(({ kind }) => kind === "videoinput");
const audioDevices = devices.filter(({ kind }) => kind === "audioinput");
const videoDevices = devices.filter(({ kind }) => kind === "videoinput");
const audioDevices = devices.filter(({ kind }) => kind === "audioinput");

if (stream) {
[stream, deviceErrors] = await getCorrectedResult(stream, deviceErrors, devices, constraints, previousDevices);
}
if (stream) {
[stream, deviceErrors] = await getCorrectedResult(stream, deviceErrors, devices, constraints, previousDevices);
}

videoManager.initialize(
stream,
stream?.getVideoTracks()?.[0] ?? null,
videoDevices,
!!constraints.video,
deviceErrors.video,
);
audioManager.initialize(
stream,
stream?.getAudioTracks()?.[0] ?? null,
audioDevices,
!!constraints.audio,
deviceErrors.audio,
);
}, [videoDeviceManagerRef, audioDeviceManagerRef, hasDevicesBeenInitializedRef]);
videoManager.initialize(
stream,
stream?.getVideoTracks()?.[0] ?? null,
videoDevices,
!!constraints.video,
deviceErrors.video,
);
audioManager.initialize(
stream,
stream?.getAudioTracks()?.[0] ?? null,
audioDevices,
!!constraints.audio,
deviceErrors.audio,
);
},
[videoDeviceManagerRef, audioDeviceManagerRef, hasDevicesBeenInitializedRef],
);

return { initializeDevices };
};

0 comments on commit 678becb

Please sign in to comment.