-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
166 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useEffect, useState } from "react"; | ||
import { setPeerConnection, setStream, peerConnection } from "../utils/media"; | ||
import { useSelector } from "react-redux"; | ||
|
||
const configuration = { | ||
iceServers: [ | ||
{ | ||
urls: [ | ||
"stun:stunserver.org", | ||
"stun:stun.voiparound.com", | ||
"stun:stun.voipbuster.com", | ||
"stun:stun.voipstunt.com", | ||
"stun:stun.voxgratia.org", | ||
], | ||
}, | ||
], | ||
// sdpSemantics: "plan-b", | ||
}; | ||
|
||
function useMedia(videoRef, socket) { | ||
console.log("[useMedia] socket--", socket); | ||
const { role } = useSelector((state) => state.media); | ||
|
||
useEffect(() => { | ||
const initPeerConnection = async () => { | ||
if (peerConnection == null) { | ||
console.log("create total new peer connection...."); | ||
const stream = await navigator.mediaDevices.getUserMedia({ | ||
video: true, | ||
// audio: true, | ||
}); | ||
|
||
// save local stream | ||
videoRef.current.srcObject = stream; | ||
setStream(stream); | ||
|
||
// create peer connection | ||
const peerConnection = new RTCPeerConnection(configuration); | ||
// add tracks | ||
stream.getTracks().forEach((track) => { | ||
peerConnection.addTrack(track, stream); | ||
}); | ||
|
||
// save peer connection | ||
setPeerConnection(peerConnection); | ||
} | ||
|
||
function onCandidate(event) { | ||
if (event.candidate) { | ||
console.log("iceCandidate", event.candidate); | ||
// emit candidate | ||
socket.current.emit( | ||
`candidate::${role == "" ? "initiator" : role}`, | ||
event.candidate | ||
); | ||
} | ||
} | ||
console.log("[useMedia] set onCandidate..."); | ||
peerConnection.onicecandidate = onCandidate; | ||
// peerConnection.addEventListener("icecandidate", (event) => { | ||
// if (event.candidate) { | ||
// console.log("iceCandidate", event.candidate); | ||
// // emit candidate | ||
// socket.emit( | ||
// `candidate::${role == "" ? "initiator" : role}`, | ||
// event.candidate | ||
// ); | ||
// } | ||
// }); | ||
}; | ||
initPeerConnection(); | ||
}, [role, socket]); | ||
} | ||
|
||
export default useMedia; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.