|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import Peer, { Instance } from "simple-peer"; |
| 3 | +import io from "socket.io-client"; |
| 4 | + |
| 5 | +interface Props { |
| 6 | + className?: string; |
| 7 | + roomId?: String; |
| 8 | +} |
| 9 | + |
| 10 | +const socket = io("http://localhost:4001"); |
| 11 | + |
| 12 | +function CommsPanel({ className, roomId }: Props) { |
| 13 | + const [stream, setStream] = useState<MediaStream>(); |
| 14 | + |
| 15 | + const myVideo = useRef<HTMLVideoElement>(null); |
| 16 | + const userVideo = useRef<HTMLVideoElement>(null); |
| 17 | + const connectionRef = useRef<Instance>(); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + socket.removeAllListeners(); |
| 21 | + socket.open(); |
| 22 | + return () => { |
| 23 | + console.log("socket cleanup called"); |
| 24 | + if (socket) { |
| 25 | + console.log("destroying socket"); |
| 26 | + socket.close(); |
| 27 | + } |
| 28 | + if (connectionRef.current) { |
| 29 | + connectionRef.current.destroy(); |
| 30 | + } |
| 31 | + }; |
| 32 | + }, []); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + // capture the stream within the cleanup function itself. |
| 36 | + let videoElement: MediaStream | undefined; |
| 37 | + |
| 38 | + navigator.mediaDevices |
| 39 | + .getUserMedia({ video: true, audio: true }) |
| 40 | + .then((newStream) => { |
| 41 | + console.log("new stream's status is " + newStream.active); |
| 42 | + newStream.getTracks().forEach((track: MediaStreamTrack) => { |
| 43 | + console.log("media track status (ready/enabled): " + track.readyState + "/" + track.enabled); |
| 44 | + }) |
| 45 | + if (myVideo.current) { |
| 46 | + console.log("can set myVideo.current") |
| 47 | + myVideo.current.srcObject = newStream; |
| 48 | + } |
| 49 | + setStream(newStream); |
| 50 | + videoElement = newStream; |
| 51 | + }).catch((err) => console.log("failed to get stream")); |
| 52 | + |
| 53 | + return () => { |
| 54 | + console.log("cleaning up media"); |
| 55 | + if (videoElement) { |
| 56 | + console.log("destroying stream"); |
| 57 | + videoElement.getTracks().forEach((track) => track.stop()); |
| 58 | + } |
| 59 | + } |
| 60 | + }, []) |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (!roomId || !stream || !socket.connected) { |
| 64 | + return; |
| 65 | + } |
| 66 | + console.log("in hook"); |
| 67 | + |
| 68 | + // clear all listeners if we are reinitializing this. |
| 69 | + socket.removeAllListeners(); |
| 70 | + console.log("removed all listeners"); |
| 71 | + |
| 72 | + // when we receive the first peer connection, we immediately send out |
| 73 | + // a peer connection request. |
| 74 | + attachSocketInitiator(stream, roomId, userVideo, connectionRef); |
| 75 | + |
| 76 | + // as the receiver, I will propagate my data outwards now. |
| 77 | + attachSocketReceiver(stream, roomId, userVideo, connectionRef); |
| 78 | + |
| 79 | + socket.on("endCall", () => { |
| 80 | + if (userVideo.current) { |
| 81 | + (userVideo.current.srcObject as MediaStream) |
| 82 | + .getTracks().forEach((tracks: MediaStreamTrack) => { |
| 83 | + tracks.stop() |
| 84 | + }); |
| 85 | + userVideo.current.srcObject = null; |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + socket.emit("joinRoom", { |
| 90 | + target: roomId, |
| 91 | + }); |
| 92 | + console.log("applied all hooks"); |
| 93 | + }, [stream, socket.connected]); |
| 94 | + |
| 95 | + return ( |
| 96 | + <div className={className}> |
| 97 | + <div className="video"> |
| 98 | + <video |
| 99 | + playsInline |
| 100 | + muted |
| 101 | + ref={myVideo} |
| 102 | + autoPlay |
| 103 | + style={{ width: "200px" }} |
| 104 | + /> |
| 105 | + </div> |
| 106 | + <div className="video"> |
| 107 | + <video |
| 108 | + playsInline |
| 109 | + ref={userVideo} |
| 110 | + autoPlay |
| 111 | + style={{ width: "200px" }} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +function attachSocketReceiver( |
| 119 | + stream: MediaStream, roomId: String, |
| 120 | + userVideo: React.RefObject<HTMLVideoElement>, |
| 121 | + connectionRef: React.MutableRefObject<Peer.Instance|undefined> |
| 122 | +) { |
| 123 | + socket.on("startCall", (data) => { |
| 124 | + console.log("received start call signal"); |
| 125 | + const peerReceive = new Peer({ |
| 126 | + initiator: false, |
| 127 | + trickle: false, |
| 128 | + stream: stream, |
| 129 | + }); |
| 130 | + |
| 131 | + peerReceive.on("signal", (data) => { |
| 132 | + console.log("sending handshake"); |
| 133 | + socket.emit("handshakeCall", { signal: data, target: roomId }); |
| 134 | + }); |
| 135 | + |
| 136 | + peerReceive.on("stream", (stream) => { |
| 137 | + console.log("setting stream of first user"); |
| 138 | + if (userVideo.current) { |
| 139 | + console.log("user video exists"); |
| 140 | + userVideo.current.srcObject = stream; |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + connectionRef.current = peerReceive; |
| 145 | + console.log("signalling receiver"); |
| 146 | + peerReceive.signal(data.signal); |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +function attachSocketInitiator( |
| 151 | + stream: MediaStream, roomId: String, |
| 152 | + userVideo: React.RefObject<HTMLVideoElement>, |
| 153 | + connectionRef: React.MutableRefObject<Peer.Instance|undefined> |
| 154 | +) { |
| 155 | + socket.on("peerConnected", () => { |
| 156 | + console.log("peer connected, starting call"); |
| 157 | + const peerInit = new Peer({ |
| 158 | + initiator: true, |
| 159 | + trickle: false, |
| 160 | + stream: stream, |
| 161 | + }); |
| 162 | + |
| 163 | + peerInit.on("signal", (data) => { |
| 164 | + console.log("signal to start call received"); |
| 165 | + socket.emit("startCall", { signalData: data, target: roomId }); |
| 166 | + }); |
| 167 | + |
| 168 | + peerInit.on("stream", (stream) => { |
| 169 | + if (userVideo.current) { |
| 170 | + console.log("setting stream for handshake"); |
| 171 | + userVideo.current.srcObject = stream; |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + connectionRef.current = peerInit; |
| 176 | + |
| 177 | + socket.on("handshakeCall", (data) => { |
| 178 | + console.log("received handshake"); |
| 179 | + peerInit.signal(data.signal) |
| 180 | + }); |
| 181 | + }); |
| 182 | +} |
| 183 | + |
| 184 | +export default CommsPanel; |
0 commit comments