Skip to content

Commit

Permalink
Stop spying on user after leaving meeting
Browse files Browse the repository at this point in the history
  • Loading branch information
gf-rog committed Jan 14, 2024
1 parent 7088d6d commit 517d407
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions frontend/src/pages/VideoCallPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import Meeting from "../models/Meeting";

function VideoCallPage() {
const { userId, socket, meeting, leaveMeeting } = useUser();
const firstRefresh = useRef<boolean>(true);
const navigate = useNavigate();
const localStream = useRef<HTMLVideoElement>(null);
const remoteStream = useRef<HTMLVideoElement>(null);
const [makingOffer, setMakingOffer] = useState(false);
const peerConnectionRef = useRef<RTCPeerConnection | null>(null);

const getPeerConnection = () => peerConnectionRef.current;

useEffect(() => {
if (userId === null) navigate("/login");
Expand All @@ -25,13 +29,20 @@ function VideoCallPage() {
}, [meeting]);

useEffect(() => {
if (!firstRefresh.current) return;
firstRefresh.current = false;

if (socket && meeting) {
prepareWebRTC(socket, meeting);
peerConnectionRef.current = new RTCPeerConnection(stunServers);
prepareWebRTC(socket, meeting, peerConnectionRef.current);
}
}, []);

async function prepareWebRTC(socket: Socket, meeting: Meeting) {
const peerConnection = new RTCPeerConnection(stunServers);
async function prepareWebRTC(
socket: Socket,
meeting: Meeting,
peerConnection: RTCPeerConnection,
) {
let polite = meeting.state == "created";

try {
Expand Down Expand Up @@ -71,6 +82,9 @@ function VideoCallPage() {

let ignoreOffer = false;
socket.on("description", async (description) => {
const peerConnection = getPeerConnection();
if (!peerConnection) return;

console.log("SOCKET: description");
const offerCollision =
description.type === "offer" &&
Expand All @@ -88,6 +102,9 @@ function VideoCallPage() {
});

socket.on("iceCandidate", async (candidate) => {
const peerConnection = getPeerConnection();
if (!peerConnection) return;

console.log("SOCKET: iceCandidate");
try {
await peerConnection.addIceCandidate(candidate);
Expand All @@ -109,6 +126,24 @@ function VideoCallPage() {
}
}

const handleLeaveMeeting = async () => {
if (localStream.current) {
(localStream.current.srcObject as MediaStream)
.getTracks()
.forEach((track) => {
track.stop();
});
localStream.current.srcObject = null;
}

if (remoteStream.current) {
remoteStream.current.srcObject = null;
}
peerConnectionRef.current?.close();
peerConnectionRef.current = null;
leaveMeeting();
};

return (
<>
<Navbar />
Expand All @@ -131,7 +166,7 @@ function VideoCallPage() {
></video>
</div>
<div>
<button onClick={leaveMeeting} className="btn secondary">
<button onClick={() => handleLeaveMeeting()} className="btn secondary">
Leave the meeting
</button>
</div>
Expand Down

0 comments on commit 517d407

Please sign in to comment.