fixed force end call

This commit is contained in:
PxlLoewe
2025-06-03 23:16:46 -07:00
parent 0eb3ba8104
commit 2da27298bc
5 changed files with 58 additions and 64 deletions

View File

@@ -74,31 +74,6 @@ export const handleConnectDispatch =
io.to("dispatchers").emit("dispatchers-update", connectedDispatcherEntry);
io.to("pilots").emit("dispatchers-update", connectedDispatcherEntry);
socket.on("stop-other-transmition", async ({ ownRole, otherRole }) => {
const aircrafts = await prisma.connectedAircraft.findMany({
where: {
Station: {
bosCallsignShort: otherRole,
},
logoutTime: null,
},
include: {
Station: true,
},
});
const dispatchers = await prisma.connectedDispatcher.findMany({
where: {
zone: otherRole,
logoutTime: null,
},
});
[...aircrafts, ...dispatchers].forEach((entry) => {
io.to(`user:${entry.userId}`).emit("force-end-transmission", {
by: ownRole,
});
});
});
socket.on("disconnect", async () => {
await prisma.connectedDispatcher.update({
where: {

View File

@@ -45,10 +45,9 @@ export const Audio = () => {
});
const { selectedStation, status: pilotState } = usePilotConnectionStore((state) => state);
const { selectedZone, status: dispatcherState } = useDispatchConnectionStore((state) => state);
const session = useSession();
const [isReceivingBlick, setIsReceivingBlick] = useState(false);
const [recentSpeakers, setRecentSpeakers] = useState<typeof speakingParticipants>([]);
useEffect(() => {
@@ -63,6 +62,20 @@ export const Audio = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [speakingParticipants]);
useEffect(() => {
if (speakingParticipants.length > 0) {
setIsReceivingBlick(true);
const timeout = setInterval(() => {
setIsReceivingBlick((s) => !s);
}, 1000);
return () => {
clearTimeout(timeout);
setIsReceivingBlick(false);
};
}
}, [setIsReceivingBlick, speakingParticipants]);
useEffect(() => {
if (message && state !== "error") {
const timeout = setTimeout(() => {
@@ -93,39 +106,37 @@ export const Audio = () => {
className={cn("btn btn-sm btn-ghost border-warning bg-transparent ")}
onClick={() => {
removeMessage();
// Probably via socket event to set ppt = false for participant
if (!canStopOtherSpeakers) return;
speakingParticipants.forEach((p) => {
dispatchSocket.emit("stop-other-transmition", {
ownRole: role,
otherRole: p.attributes.role,
});
});
}}
>
{message}
</button>
</div>
)}
{(displayedSpeakers.length || message) && (
{displayedSpeakers.length > 0 && (
<div
className={cn(
"tooltip-left tooltip-error font-semibold",
canStopOtherSpeakers && "tooltip",
canStopOtherSpeakers && speakingParticipants.length > 0 && "tooltip",
)}
data-tip="Funkspruch unterbrechen"
>
<button
className={cn(
"btn btn-sm btn-soft border-none bg-transparent",
canStopOtherSpeakers && "hover:bg-error",
"btn btn-sm btn-soft bg-transparent border",
canStopOtherSpeakers && speakingParticipants.length > 0 && "hover:bg-error",
speakingParticipants.length > 0 && " hover:bg-errorborder",
isReceivingBlick && "border-warning",
)}
onClick={() => {
if (!canStopOtherSpeakers) return;
speakingParticipants.forEach((p) => {
dispatchSocket.emit("stop-other-transmition", {
ownRole: role,
otherRole: p.attributes.role,
const payload = JSON.stringify({
by: role,
});
speakingParticipants.forEach(async (p) => {
await room?.localParticipant.performRpc({
destinationIdentity: p.identity,
method: "force-mute",
payload,
});
});
}}

View File

@@ -1,5 +1,6 @@
"use client";
import { useDebounce } from "_helpers/useDebounce";
import { useAudioStore } from "_store/audioStore";
import { useEffect, useRef, useState } from "react";
export const useSounds = ({
@@ -11,6 +12,7 @@ export const useSounds = ({
isTransmitting: boolean;
unpausedTracks: unknown[];
}) => {
const { room } = useAudioStore();
// Sounds as refs
const connectionStart = useRef<HTMLAudioElement | null>(null);
const connectionEnd = useRef<HTMLAudioElement | null>(null);
@@ -70,10 +72,10 @@ export const useSounds = ({
}
}, [isReceiving]);
// Hotmic warning after 30 seconds
// Hotmic warning after 25 seconds
useEffect(() => {
if (isTransmitting) {
const timeout = setTimeout(() => {
const soundsTimeout = setTimeout(() => {
if (isTransmitting) {
callToLong.current!.loop = true;
callToLong.current!.currentTime = 0;
@@ -81,8 +83,19 @@ export const useSounds = ({
callToLong.current!.play();
}
}, 25000);
const forceEndTransmitionTimeout = setTimeout(() => {
if (isTransmitting) {
room?.localParticipant.setMicrophoneEnabled(false);
useAudioStore.setState({
isTalking: false,
message: "Hotmic erkannt! Ruf wurde beendet.",
});
}
}, 25000 + 10000);
return () => {
clearTimeout(timeout);
clearTimeout(soundsTimeout);
clearTimeout(forceEndTransmitionTimeout);
callToLong.current!.pause();
};
}

View File

@@ -5,7 +5,7 @@ import {
handleTrackSubscribed,
handleTrackUnsubscribed,
} from "_helpers/liveKitEventHandler";
import { ConnectionQuality, Participant, Room, RoomEvent } from "livekit-client";
import { ConnectionQuality, Participant, Room, RoomEvent, RpcInvocationData } from "livekit-client";
import { pilotSocket } from "pilot/socket";
import { create } from "zustand";
import axios from "axios";
@@ -129,11 +129,22 @@ export const useAudioStore = create<TalkState>((set, get) => ({
.on(RoomEvent.TrackUnsubscribed, handleTrackUnsubscribed)
.on(RoomEvent.LocalTrackUnpublished, handleLocalTrackUnpublished);
await room.connect(url, token, {});
room.localParticipant.setAttributes({
role,
});
set({ room });
room.registerRpcMethod("force-mute", async (data: RpcInvocationData) => {
const { by } = JSON.parse(data.payload);
room.localParticipant.setMicrophoneEnabled(false);
useAudioStore.setState({
isTalking: false,
message: `Ruf beendet durch ${by || "eine unsichtbare Macht"}`,
});
return `Hello, ${data.callerIdentity}!`;
});
interval = setInterval(() => {
set({
remoteParticipants: room.numParticipants === 0 ? 0 : room.numParticipants - 1, // Unreliable and delayed
@@ -174,19 +185,5 @@ const handlePTT = (data: PTTData) => {
}
};
const handleForceEndTransmission = ({ by }: { by?: string }) => {
const { room } = useAudioStore.getState();
if (!room) return;
room.localParticipant.setMicrophoneEnabled(false);
useAudioStore.setState({
isTalking: false,
message: `Ruf beendet durch ${by || "unknown"}`,
});
};
pilotSocket.on("ptt", handlePTT);
dispatchSocket.on("ptt", handlePTT);
pilotSocket.on("force-end-transmission", handleForceEndTransmission);
dispatchSocket.on("force-end-transmission", handleForceEndTransmission);

View File

@@ -25,10 +25,8 @@ export const GET = async (request: NextRequest) => {
if (!user || !user.permissions.includes("AUDIO"))
return Response.json({ message: "Missing permissions" }, { status: 401 });
const participantName = user.publicId;
const at = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, {
identity: participantName,
identity: user.publicId,
// Token to expire after 10 minutes
ttl: "1d",
});