+
+ {/* mounts and unmounts based on if in the room or now */}
+ {isUserTunedIn && (
+
+ )}
+ >
+ );
+}
+
+function StreamRoom({
+ initialTunedInUserIds,
+ currentBroadcasters,
+}: {
+ initialTunedInUserIds?: string[];
+ currentBroadcasters?: string[];
+}) {
+ // ?could move this up the tree and pass it down? or set it in the header?
+ // local stream specifically for this stream room
+ const [localStream, setLocalStream] = useState();
+ const userStreamTagRef = useRef(null);
+
+ // local peer map of userIds to peers
+ const [userPeers, setUserPeers] = useState<{ [userId: string]: Peer }>({});
+
+ useEffect(() => {
+ navigator.mediaDevices
+ .getUserMedia({ video: false, audio: true })
+ .then((userStream) => {
+ setLocalStream(userStream);
+
+ if (userStreamTagRef?.current)
+ userStreamTagRef.current.srcObject = userStream;
+
+ // const audio = new Audio();
+ // audio.autoplay = true;
+ // audio.srcObject = userStream;
+ })
+ .catch((error) => {
+ console.error(error);
+
+ toast.error(
+ "Make sure that you have permissions enabled and microphone connected"
+ );
+ });
+ }, []);
+
+ // we only loop through peers that are associated to user Ids which exist in the currentBroadcasters array
+ return (
+ <>
+
+ this is the video of people
+ {Object.entries(userPeers).map(([userId, peer]) => (
+
+ ))}
+ >
+ );
+}
+
+/**
+ * take in the specified peer and whether or not he is broadcasting
+ */
+function PeerStreamRenderer({
+ peer,
+ isBroadcasting,
+}: {
+ peer: Peer;
+ isBroadcasting: boolean;
+}) {
+ const streamRef = useRef(null);
+
+ useEffect(() => {
+ peer.on("stream", (remotePeerStream: MediaStream) => {
+ console.log(
+ "stream coming in from remote peer...BUT, only going to show once they broadcast"
+ );
+
+ if (streamRef?.current) streamRef.current.srcObject = remotePeerStream;
+ });
+ }, [streamRef]);
+
+ // when someone is broadcasting, we enable their stream
+
+ return (
+ <>
+
+ >
);
}