Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions client/src/components/MeetingPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export function MeetingPage() {
const [meetingJoined, setMeetingJoined] = useState(false);
const [videoStream, setVideoStream] = useState();
const [remoteVideoStream, setRemoteVideoStream] = useState();

const [remoteTracks, setRemoteTracks] = useState([]);

const params = useParams();
const roomId = params.roomId;

Expand All @@ -27,35 +28,36 @@ export function MeetingPage() {
}).then(async (stream) => {
setVideoStream(stream);
});
s.on("localDescription", async ({description}) => {

s.on("localDescription", async ({ description }) => {
// Receiving video -
let pc = new RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.l.google.com:19302",
},
{
urls: "stun:stun.l.google.com:19302",
},
],
});

pc.setRemoteDescription(description);
pc.ontrack = (e) => {
setRemoteVideoStream(new MediaStream([e.track]))
setRemoteTracks((prevTracks) => [...(prevTracks ?? []), e.track]);
}

s.on("iceCandidate", ({candidate}) => {
s.on("iceCandidate", ({ candidate }) => {
pc.addIceCandidate(candidate)
});

pc.onicecandidate = ({candidate}) => {
s.emit("iceCandidateReply", {candidate});
pc.onicecandidate = ({ candidate }) => {
s.emit("iceCandidateReply", { candidate });
}

await pc.setLocalDescription(await pc.createAnswer());
s.emit("remoteDescription", {description: pc.localDescription});
s.emit("remoteDescription", { description: pc.localDescription });
})
});
}, [])
}, [roomId, remoteVideoStream])

if (!videoStream) {
return <div>
Expand All @@ -64,42 +66,43 @@ export function MeetingPage() {
}

if (!meetingJoined) {
return <div style={{minHeight: "100vh",}}>
return <div style={{ minHeight: "100vh", }}>
<CentralizedCard>
<div>
<Typography textAlign={"center"} variant="h5">
Hi welcome to meeting {roomId}.
</Typography>
</div>
<br/><br/>
<div style={{display: "flex", justifyContent: "center"}}>
<br /><br />
<div style={{ display: "flex", justifyContent: "center" }}>
<Button onClick={() => {
// sending pc
let pc = new RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.l.google.com:19302",
},
{
urls: "stun:stun.l.google.com:19302",
},
],
});
pc.onicecandidate = ({candidate}) => {
socket.emit("iceCandidate", {candidate});
});
pc.onicecandidate = ({ candidate }) => {
socket.emit("iceCandidate", { candidate });
}
pc.addTrack(videoStream.getVideoTracks()[0])

pc.onnegotiationneeded = async () => {
try {
await pc.setLocalDescription(await pc.createOffer());
console.log(pc.localDescription);
socket.emit("localDescription", {description: pc.localDescription});
socket.emit("localDescription", { description: pc.localDescription });
} catch (err) {
console.error(err);
}
};
socket.on("remoteDescription", async ({description}) => {
await pc.setRemoteDescription(description);

socket.on("remoteDescription", async ({ description }) => {
await pc.setRemoteDescription(description);
});
socket.on("iceCandidateReply", ({candidate}) => {
socket.on("iceCandidateReply", ({ candidate }) => {
pc.addIceCandidate(candidate)
});
setMeetingJoined(true);
Expand All @@ -116,7 +119,7 @@ export function MeetingPage() {
<Video stream={videoStream} />
</Grid>
<Grid item xs={12} md={6} lg={4}>
<Video stream={remoteVideoStream} />
<Video stream={remoteVideoStream} tracks={remoteTracks} />
</Grid>
</Grid>
}
38 changes: 23 additions & 15 deletions client/src/components/Video.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
/* eslint-disable react/prop-types */
import { useEffect, useRef } from "react";

export const Video = ({stream}) => {
const videoRef = useRef();
useEffect(() => {
if (videoRef && videoRef.current) {
videoRef.current.srcObject = stream;
}
}, [videoRef])

return (
<div>
<div>
<video style={{borderRadius: 10}} ref={videoRef} muted width="100%" autoPlay={true} playsInline={true} />
</div>
</div>
)
// eslint-disable-next-line react/prop-types
export const Video = ({ stream, tracks }) => {
const videoRef = useRef();
useEffect(() => {
if (videoRef && videoRef.current) {
if (stream) {
videoRef.current.srcObject = stream;
} else if (tracks && tracks.length > 0) {
// Create a new MediaStream with all remote tracks
const remoteStream = new MediaStream(tracks);
videoRef.current.srcObject = remoteStream;
}
}
}, [videoRef, tracks])

return (
<div>
<div>
<video style={{ borderRadius: 10 }} ref={videoRef} muted width="100%" autoPlay={true} playsInline={true} />
</div>
</div>
)
}