Added soem things

This commit is contained in:
PxlLoewe
2025-05-20 11:33:08 -07:00
parent 0394e0a97e
commit a7372edfb5
22 changed files with 111 additions and 169 deletions

View File

@@ -1,3 +1,4 @@
import { dispatchSocket } from "dispatch/socket";
import { serverApi } from "helpers/axios";
import {
handleActiveSpeakerChange,
@@ -7,12 +8,14 @@ import {
handleTrackUnsubscribed,
} from "helpers/liveKitEventHandler";
import { ConnectionQuality, Room, RoomEvent } from "livekit-client";
import { pilotSocket } from "pilot/socket";
import { create } from "zustand";
let interval: NodeJS.Timeout;
type TalkState = {
isTalking: boolean;
source: string;
state: "connecting" | "connected" | "disconnected" | "error";
message: string | null;
connectionQuality: ConnectionQuality;
@@ -33,10 +36,17 @@ export const useAudioStore = create<TalkState>((set, get) => ({
isTalking: false,
message: null,
state: "disconnected",
source: "",
remoteParticipants: 0,
connectionQuality: ConnectionQuality.Unknown,
room: null,
toggleTalking: () => set((state) => ({ isTalking: !state.isTalking })),
toggleTalking: () => {
const { room, isTalking } = get();
if (!room) return;
room.localParticipant.setMicrophoneEnabled(!isTalking);
set((state) => ({ isTalking: !state.isTalking }));
},
connect: async (roomName) => {
set({ state: "connecting" });
console.log("Connecting to room: ", roomName);
@@ -78,6 +88,7 @@ export const useAudioStore = create<TalkState>((set, get) => ({
await room.connect(url, token);
console.log(room);
set({ room });
interval = setInterval(() => {
set({
remoteParticipants:
@@ -97,3 +108,28 @@ export const useAudioStore = create<TalkState>((set, get) => ({
get().room?.disconnect();
},
}));
interface PTTData {
shouldTransmit: boolean;
source: string;
}
const handlePTT = (data: PTTData) => {
const { shouldTransmit, source } = data;
const { room } = useAudioStore.getState();
if (!room) return;
useAudioStore.setState({
isTalking: shouldTransmit,
source,
});
if (shouldTransmit) {
room.localParticipant.setMicrophoneEnabled(true);
} else {
room.localParticipant.setMicrophoneEnabled(false);
}
};
pilotSocket.on("ptt", handlePTT);
dispatchSocket.on("ptt", handlePTT);