2023-05-30 20:56:25 +02:00
|
|
|
import { LocalAudioTrack, LocalVideoTrack, Room } from "livekit-client";
|
2023-05-26 20:41:32 +02:00
|
|
|
import React from "react";
|
2023-06-02 19:55:41 +02:00
|
|
|
import {
|
|
|
|
useMediaDeviceSelect,
|
|
|
|
usePreviewDevice,
|
|
|
|
} from "@livekit/components-react";
|
2023-05-30 20:56:25 +02:00
|
|
|
|
|
|
|
import { MediaDevicesState, MediaDevices } from "../settings/mediaDevices";
|
2023-05-26 20:41:32 +02:00
|
|
|
import { LocalMediaInfo, MediaInfo } from "./VideoPreview";
|
|
|
|
|
|
|
|
type LiveKitState = {
|
2023-06-02 14:49:11 +02:00
|
|
|
// The state of the media devices (changing the devices will also change them in the room).
|
2023-05-26 20:41:32 +02:00
|
|
|
mediaDevices: MediaDevicesState;
|
2023-06-02 14:49:11 +02:00
|
|
|
// The local media (audio and video) that can be referenced in an e.g. lobby view.
|
2023-05-26 20:41:32 +02:00
|
|
|
localMedia: LocalMediaInfo;
|
2023-06-02 14:49:11 +02:00
|
|
|
// A reference to the newly constructed (but not yet entered) room for future use with the LiveKit hooks.
|
|
|
|
// TODO: Abstract this away, so that the user doesn't have to deal with the LiveKit room directly.
|
|
|
|
room: Room;
|
2023-05-26 20:41:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns the React state for the LiveKit's Room class.
|
|
|
|
// The actual return type should be `LiveKitState`, but since this is a React hook, the initialisation is
|
|
|
|
// delayed (done after the rendering, not during the rendering), because of that this function may return `undefined`.
|
|
|
|
// But soon this state is changed to the actual `LiveKitState` value.
|
2023-06-02 14:49:11 +02:00
|
|
|
export function useLiveKit(): LiveKitState | undefined {
|
2023-05-26 20:41:32 +02:00
|
|
|
// TODO: Pass the proper paramters to configure the room (supported codecs, simulcast, adaptive streaming, etc).
|
|
|
|
const [room] = React.useState<Room>(() => {
|
|
|
|
return new Room();
|
|
|
|
});
|
|
|
|
|
2023-05-30 20:56:25 +02:00
|
|
|
// Create a React state to store the available devices and the selected device for each kind.
|
|
|
|
const mediaDevices = useMediaDevicesState(room);
|
2023-05-26 20:41:32 +02:00
|
|
|
|
2023-05-30 20:56:25 +02:00
|
|
|
// Create local video track.
|
|
|
|
const [videoEnabled, setVideoEnabled] = React.useState<boolean>(true);
|
|
|
|
const selectedVideoId = mediaDevices.state.get("videoinput")?.selectedId;
|
|
|
|
const video = usePreviewDevice(
|
|
|
|
videoEnabled,
|
|
|
|
selectedVideoId ?? "",
|
|
|
|
"videoinput"
|
|
|
|
);
|
2023-05-26 20:41:32 +02:00
|
|
|
|
2023-05-30 20:56:25 +02:00
|
|
|
// Create local audio track.
|
|
|
|
const [audioEnabled, setAudioEnabled] = React.useState<boolean>(true);
|
|
|
|
const selectedAudioId = mediaDevices.state.get("audioinput")?.selectedId;
|
|
|
|
const audio = usePreviewDevice(
|
|
|
|
audioEnabled,
|
|
|
|
selectedAudioId ?? "",
|
|
|
|
"audioinput"
|
|
|
|
);
|
2023-05-26 20:41:32 +02:00
|
|
|
|
2023-05-30 20:56:25 +02:00
|
|
|
// Create final LiveKit state.
|
2023-05-26 20:41:32 +02:00
|
|
|
const [state, setState] = React.useState<LiveKitState | undefined>(undefined);
|
|
|
|
React.useEffect(() => {
|
2023-05-30 20:56:25 +02:00
|
|
|
// Helper to create local media without the copy-paste.
|
2023-05-26 20:41:32 +02:00
|
|
|
const createLocalMedia = (
|
2023-05-30 20:56:25 +02:00
|
|
|
track: LocalVideoTrack | LocalAudioTrack | undefined,
|
2023-05-26 20:41:32 +02:00
|
|
|
enabled: boolean,
|
2023-05-30 20:56:25 +02:00
|
|
|
setEnabled: React.Dispatch<React.SetStateAction<boolean>>
|
2023-05-26 20:41:32 +02:00
|
|
|
): MediaInfo | undefined => {
|
|
|
|
if (!track) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
track,
|
|
|
|
muted: !enabled,
|
2023-05-30 20:56:25 +02:00
|
|
|
toggle: async () => {
|
|
|
|
setEnabled(!enabled);
|
2023-05-26 20:41:32 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const state: LiveKitState = {
|
2023-05-30 20:56:25 +02:00
|
|
|
mediaDevices: mediaDevices,
|
2023-05-26 20:41:32 +02:00
|
|
|
localMedia: {
|
|
|
|
audio: createLocalMedia(
|
2023-05-30 20:56:25 +02:00
|
|
|
audio.localTrack,
|
|
|
|
audioEnabled,
|
|
|
|
setAudioEnabled
|
2023-05-26 20:41:32 +02:00
|
|
|
),
|
|
|
|
video: createLocalMedia(
|
2023-05-30 20:56:25 +02:00
|
|
|
video.localTrack,
|
|
|
|
videoEnabled,
|
|
|
|
setVideoEnabled
|
2023-05-26 20:41:32 +02:00
|
|
|
),
|
|
|
|
},
|
2023-06-02 14:49:11 +02:00
|
|
|
room,
|
2023-05-26 20:41:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
setState(state);
|
|
|
|
}, [
|
2023-05-30 20:56:25 +02:00
|
|
|
mediaDevices,
|
|
|
|
audio.localTrack,
|
|
|
|
video.localTrack,
|
|
|
|
audioEnabled,
|
|
|
|
videoEnabled,
|
2023-05-26 20:41:32 +02:00
|
|
|
room,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:56:25 +02:00
|
|
|
function useMediaDevicesState(room: Room): MediaDevicesState {
|
2023-06-02 19:55:41 +02:00
|
|
|
const {
|
|
|
|
devices: videoDevices,
|
|
|
|
activeDeviceId: activeVideoDevice,
|
|
|
|
setActiveMediaDevice: setActiveVideoDevice,
|
|
|
|
} = useMediaDeviceSelect({ kind: "videoinput", room });
|
|
|
|
const {
|
|
|
|
devices: audioDevices,
|
|
|
|
activeDeviceId: activeAudioDevice,
|
|
|
|
setActiveMediaDevice: setActiveAudioDevice,
|
|
|
|
} = useMediaDeviceSelect({
|
|
|
|
kind: "audioinput",
|
2023-05-30 20:56:25 +02:00
|
|
|
room,
|
2023-06-02 19:55:41 +02:00
|
|
|
});
|
|
|
|
const {
|
|
|
|
devices: audioOutputDevices,
|
|
|
|
activeDeviceId: activeAudioOutputDevice,
|
|
|
|
setActiveMediaDevice: setActiveAudioOutputDevice,
|
|
|
|
} = useMediaDeviceSelect({
|
|
|
|
kind: "audiooutput",
|
|
|
|
room,
|
|
|
|
});
|
2023-05-30 20:56:25 +02:00
|
|
|
|
2023-06-02 19:55:41 +02:00
|
|
|
const selectActiveDevice = React.useCallback(
|
|
|
|
async (kind: MediaDeviceKind, id: string) => {
|
|
|
|
switch (kind) {
|
|
|
|
case "audioinput":
|
|
|
|
setActiveAudioDevice(id);
|
|
|
|
break;
|
|
|
|
case "videoinput":
|
|
|
|
setActiveVideoDevice(id);
|
|
|
|
break;
|
|
|
|
case "audiooutput":
|
|
|
|
setActiveAudioOutputDevice(id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setActiveAudioDevice, setActiveVideoDevice, setActiveAudioOutputDevice]
|
|
|
|
);
|
2023-05-30 20:56:25 +02:00
|
|
|
|
|
|
|
const [mediaDevicesState, setMediaDevicesState] =
|
|
|
|
React.useState<MediaDevicesState>(() => {
|
|
|
|
const state: MediaDevicesState = {
|
|
|
|
state: new Map(),
|
|
|
|
selectActiveDevice,
|
|
|
|
};
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2023-06-02 19:55:41 +02:00
|
|
|
const state = new Map<MediaDeviceKind, MediaDevices>();
|
|
|
|
state.set("videoinput", {
|
|
|
|
available: videoDevices,
|
|
|
|
selectedId: activeVideoDevice,
|
2023-05-30 20:56:25 +02:00
|
|
|
});
|
2023-06-02 19:55:41 +02:00
|
|
|
state.set("audioinput", {
|
|
|
|
available: audioDevices,
|
|
|
|
selectedId: activeAudioDevice,
|
2023-05-30 20:56:25 +02:00
|
|
|
});
|
2023-06-02 19:55:41 +02:00
|
|
|
state.set("audiooutput", {
|
2023-05-30 20:56:25 +02:00
|
|
|
available: audioOutputDevices,
|
2023-06-02 19:55:41 +02:00
|
|
|
selectedId: activeAudioOutputDevice,
|
|
|
|
});
|
|
|
|
setMediaDevicesState({
|
|
|
|
state,
|
|
|
|
selectActiveDevice,
|
2023-05-26 20:41:32 +02:00
|
|
|
});
|
2023-05-30 20:56:25 +02:00
|
|
|
}, [
|
2023-06-02 19:55:41 +02:00
|
|
|
videoDevices,
|
|
|
|
activeVideoDevice,
|
|
|
|
audioDevices,
|
|
|
|
activeAudioDevice,
|
2023-05-30 20:56:25 +02:00
|
|
|
audioOutputDevices,
|
2023-06-02 19:55:41 +02:00
|
|
|
activeAudioOutputDevice,
|
|
|
|
selectActiveDevice,
|
2023-05-30 20:56:25 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
return mediaDevicesState;
|
|
|
|
}
|