84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import { useCallback } from "react";
|
|
import { Identity } from "spacetimedb";
|
|
import { useTable, useReducer, useSpacetimeDB } from "spacetimedb/react";
|
|
import { tables, reducers } from "../../../module_bindings";
|
|
import { useLocalMedia } from "./useLocalMedia";
|
|
import { useChannelAudioWebRTC } from "./useChannelAudioWebRTC";
|
|
import { useScreenSharingWebRTC } from "./useScreenSharingWebRTC";
|
|
|
|
export const useWebRTC = (connectedChannelId: bigint | undefined) => {
|
|
const { identity } = useSpacetimeDB();
|
|
const [watching] = useTable(tables.watching);
|
|
|
|
const startWatchingReducer = useReducer(reducers.startWatching);
|
|
const stopWatchingReducer = useReducer(reducers.stopWatching);
|
|
|
|
const {
|
|
localStream,
|
|
localScreenStream,
|
|
isMuted,
|
|
isDeafened,
|
|
isTalking,
|
|
isSharingScreen,
|
|
toggleMute,
|
|
toggleDeafen,
|
|
startScreenShare: startLocalScreenShare,
|
|
stopScreenShare: stopLocalScreenShare,
|
|
requestMic,
|
|
releaseMic,
|
|
} = useLocalMedia();
|
|
|
|
// --- Specialized Hooks ---
|
|
const voice = useChannelAudioWebRTC(
|
|
connectedChannelId,
|
|
identity,
|
|
localStream,
|
|
isDeafened
|
|
);
|
|
|
|
const screen = useScreenSharingWebRTC(
|
|
connectedChannelId,
|
|
identity,
|
|
localScreenStream
|
|
);
|
|
|
|
// --- Actions ---
|
|
const startScreenShare = useCallback(() => {
|
|
startLocalScreenShare(() => {});
|
|
}, [startLocalScreenShare]);
|
|
|
|
const stopScreenShare = useCallback(() => {
|
|
stopLocalScreenShare(() => {});
|
|
}, [stopLocalScreenShare]);
|
|
|
|
const startWatching = useCallback((peerIdentity: Identity) => {
|
|
if (connectedChannelId) {
|
|
startWatchingReducer({ watchee: peerIdentity, channelId: connectedChannelId });
|
|
}
|
|
}, [connectedChannelId, startWatchingReducer]);
|
|
|
|
const stopWatching = useCallback((peerIdentity: Identity) => {
|
|
stopWatchingReducer({ watchee: peerIdentity });
|
|
}, [stopWatchingReducer]);
|
|
|
|
return {
|
|
localStream,
|
|
localScreenStream,
|
|
peerStatuses: voice.peerStatuses,
|
|
peers: screen.peers, // For VideoGrid to show streams
|
|
startScreenShare,
|
|
stopScreenShare,
|
|
isSharingScreen,
|
|
startWatching,
|
|
stopWatching,
|
|
watching,
|
|
isMuted,
|
|
isDeafened,
|
|
toggleMute,
|
|
toggleDeafen,
|
|
peerStats: voice.peerStats
|
|
};
|
|
};
|
|
|
|
export default useWebRTC;
|