|
| 1 | +import { WORKSPACE_EVENT } from '@wabinar/constants/socket-message'; |
| 2 | +import { useEffect, useRef } from 'react'; |
| 3 | + |
| 4 | +import { User } from './../types/user.d'; |
| 5 | +import useMyMediaStreamContext from './context/useMyMediaStreamContext'; |
| 6 | +import useSocketContext from './context/useSocketContext'; |
| 7 | +import useUserStreamContext from './context/useUserStreamsContext'; |
| 8 | + |
| 9 | +const usePeerConnection = () => { |
| 10 | + const { myMediaStream } = useMyMediaStreamContext(); |
| 11 | + const { userStreams, setUserStreams, setConnectedUsers } = |
| 12 | + useUserStreamContext(); |
| 13 | + const pcsRef = useRef<{ [socketId: string]: RTCPeerConnection }>({}); |
| 14 | + const { workspaceSocket } = useSocketContext(); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const createPeerConnection = (sid: string) => { |
| 18 | + try { |
| 19 | + const RTCConfig = { |
| 20 | + iceServers: [ |
| 21 | + { |
| 22 | + urls: [ |
| 23 | + 'stun:stun.l.google.com:19302', |
| 24 | + 'stun:stun1.l.google.com:19302', |
| 25 | + 'stun:stun2.l.google.com:19302', |
| 26 | + 'stun:stun3.l.google.com:19302', |
| 27 | + 'stun:stun4.l.google.com:19302', |
| 28 | + ], |
| 29 | + }, |
| 30 | + ], |
| 31 | + }; |
| 32 | + |
| 33 | + const peerConnection = new RTCPeerConnection(RTCConfig); |
| 34 | + |
| 35 | + peerConnection.onicecandidate = (event: RTCPeerConnectionIceEvent) => { |
| 36 | + if (event.candidate) { |
| 37 | + workspaceSocket.emit( |
| 38 | + WORKSPACE_EVENT.SEND_ICE, |
| 39 | + event.candidate, |
| 40 | + sid, |
| 41 | + ); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + peerConnection.ontrack = (event: RTCTrackEvent) => { |
| 46 | + const stream = event.streams[0]; |
| 47 | + |
| 48 | + setUserStreams((prev) => ({ ...prev, [sid]: stream })); |
| 49 | + }; |
| 50 | + |
| 51 | + myMediaStream?.getTracks().forEach((track) => { |
| 52 | + peerConnection.addTrack(track, myMediaStream); |
| 53 | + }); |
| 54 | + |
| 55 | + peerConnection.onnegotiationneeded = async () => { |
| 56 | + try { |
| 57 | + const offer = await peerConnection.createOffer(); |
| 58 | + await peerConnection.setLocalDescription(offer); |
| 59 | + |
| 60 | + workspaceSocket.emit(WORKSPACE_EVENT.SEND_OFFER, offer, sid); |
| 61 | + } catch (err) { |
| 62 | + console.error(err); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + return peerConnection; |
| 67 | + } catch (err) { |
| 68 | + console.debug(err); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const onReceivedOffer = async ( |
| 73 | + offer: RTCSessionDescriptionInit, |
| 74 | + sid: string, |
| 75 | + ) => { |
| 76 | + const pc = createPeerConnection(sid); |
| 77 | + if (!pc) return; |
| 78 | + |
| 79 | + pcsRef.current = { ...pcsRef.current, [sid]: pc }; |
| 80 | + |
| 81 | + try { |
| 82 | + await pc.setRemoteDescription(offer); |
| 83 | + const answer = await pc.createAnswer(); |
| 84 | + await pc.setLocalDescription(answer); |
| 85 | + |
| 86 | + workspaceSocket.emit(WORKSPACE_EVENT.SEND_ANSWER, answer, sid); |
| 87 | + } catch (err) { |
| 88 | + console.debug(err); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + const onReceivedAnswer = async ( |
| 93 | + answer: RTCSessionDescriptionInit, |
| 94 | + sid: string, |
| 95 | + ) => { |
| 96 | + const pc = pcsRef.current?.[sid]; |
| 97 | + |
| 98 | + if (!pc) return; |
| 99 | + await pc.setRemoteDescription(answer); |
| 100 | + }; |
| 101 | + |
| 102 | + const onReceivedIceCandidate = async ( |
| 103 | + iceCandidate: RTCIceCandidateInit, |
| 104 | + sid: string, |
| 105 | + ) => { |
| 106 | + const pc = pcsRef.current?.[sid]; |
| 107 | + |
| 108 | + if (!pc) return; |
| 109 | + await pc.addIceCandidate(iceCandidate); |
| 110 | + }; |
| 111 | + |
| 112 | + const onReceivedUser = async (sid: string, user: User) => { |
| 113 | + const pc = createPeerConnection(sid); |
| 114 | + |
| 115 | + if (!pc) return; |
| 116 | + |
| 117 | + const offer = await pc.createOffer(); |
| 118 | + await pc.setLocalDescription(offer); |
| 119 | + |
| 120 | + pcsRef.current = { ...pcsRef.current, [sid]: pc }; |
| 121 | + |
| 122 | + setConnectedUsers((prev) => [ |
| 123 | + ...prev, |
| 124 | + { |
| 125 | + sid: sid, |
| 126 | + uid: user.id, |
| 127 | + name: user.name, |
| 128 | + avatarUrl: user.avatarUrl, |
| 129 | + }, |
| 130 | + ]); |
| 131 | + |
| 132 | + workspaceSocket.emit(WORKSPACE_EVENT.SEND_OFFER, offer, sid); |
| 133 | + }; |
| 134 | + |
| 135 | + workspaceSocket.on(WORKSPACE_EVENT.RECEIVE_HELLO, onReceivedUser); |
| 136 | + workspaceSocket.on(WORKSPACE_EVENT.RECEIVE_OFFER, onReceivedOffer); |
| 137 | + workspaceSocket.on(WORKSPACE_EVENT.RECEIVE_ANSWER, onReceivedAnswer); |
| 138 | + workspaceSocket.on(WORKSPACE_EVENT.RECEIVE_ICE, onReceivedIceCandidate); |
| 139 | + |
| 140 | + return () => { |
| 141 | + workspaceSocket.off(WORKSPACE_EVENT.RECEIVE_OFFER); |
| 142 | + workspaceSocket.off(WORKSPACE_EVENT.RECEIVE_ANSWER); |
| 143 | + workspaceSocket.off(WORKSPACE_EVENT.RECEIVE_ICE); |
| 144 | + |
| 145 | + if (userStreams) { |
| 146 | + Object.keys(userStreams).forEach((sid) => { |
| 147 | + pcsRef.current?.[sid].close(); |
| 148 | + delete pcsRef.current[sid]; |
| 149 | + }); |
| 150 | + } |
| 151 | + }; |
| 152 | + }, [userStreams, myMediaStream]); |
| 153 | +}; |
| 154 | + |
| 155 | +export default usePeerConnection; |
0 commit comments