-
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.
Merge pull request #135 from tmddus2/feature/231122-webrtc-multi-client
Feature(#133) ๋ฏธ๋์ด์๋ฒ์์ ๋ฐํ์ MediaStream์ ์ฐธ์ฌ์์๊ฒ ์ ๋ฌํ๋ค.
- Loading branch information
Showing
4 changed files
with
183 additions
and
40 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
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,115 @@ | ||
<!DOCTYPE html> | ||
<html lang="kr"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="description" content="test WebRTC"> | ||
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1, maximum-scale=1"> | ||
<link rel="stylesheet" href="../commons/css/main.css"/> | ||
</head> | ||
<body> | ||
<div id="container"> | ||
<h1>์ฐธ์ฌ์</h1> | ||
<video id="localVideo" playsinline autoplay muted></video> | ||
<canvas></canvas> | ||
<div> | ||
<button id="startButton">์์</button> | ||
<button id="callButton">๋ฐฉ ์์ฑ</button> | ||
<button id="hangupButton">๋ฐฉ ๋๊ฐ๊ธฐ</button> | ||
</div> | ||
</div> | ||
<script type="module"> | ||
import { io } from "https://cdn.socket.io/4.4.1/socket.io.esm.min.js"; | ||
const socket = io('http://localhost:3000/enter-room'); | ||
|
||
const startButton = document.getElementById('startButton'); | ||
const callButton = document.getElementById('callButton'); | ||
const hangupButton = document.getElementById('hangupButton'); | ||
callButton.disabled = true; | ||
hangupButton.disabled = true; | ||
|
||
const canvas = document.querySelector('canvas'); | ||
const localVideo = document.getElementById('localVideo'); | ||
|
||
let localStream = undefined; | ||
let presenterRTCPC; | ||
let tmp = [] | ||
const pc_config = { | ||
iceServers: [ | ||
{ | ||
urls: [ | ||
"stun:stun.l.google.com:19302", | ||
] | ||
}, | ||
], | ||
}; | ||
|
||
const start = async () => { | ||
try { | ||
presenterRTCPC = new RTCPeerConnection(); | ||
const stream = new MediaStream(); | ||
localStream = stream; | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
start().then(getStream) | ||
|
||
async function getStream() { | ||
try { | ||
await createStudentOffer(); | ||
await setServerAnswer(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
function getStudentCandidate() { | ||
presenterRTCPC.onicecandidate = (e) => { | ||
if (e.candidate) { | ||
socket.emit('studentCandidate', { | ||
candidate: e.candidate, | ||
studentSocketId: socket.id | ||
}); | ||
} | ||
} | ||
} | ||
|
||
async function createStudentOffer() { | ||
try { | ||
const SDP = await presenterRTCPC.createOffer({ | ||
offerToReceiveAudio:true, | ||
offerToReceiveVideo:true | ||
}); | ||
socket.emit('studentOffer', { | ||
socketId: socket.id, | ||
SDP: SDP | ||
}); | ||
|
||
presenterRTCPC.setLocalDescription(SDP); | ||
getStudentCandidate() | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
async function setServerAnswer() { | ||
socket.on(`${socket.id}-serverAnswer`, (data) => { | ||
presenterRTCPC.setRemoteDescription(data.SDP) | ||
}) | ||
socket.on(`${socket.id}-serverCandidate`, (data) => { | ||
presenterRTCPC.addIceCandidate(new RTCIceCandidate(data.candidate)) | ||
}); | ||
} | ||
|
||
presenterRTCPC.ontrack = (event) => { | ||
localStream.addTrack(event.track) | ||
localVideo.srcObject = localStream; | ||
} | ||
|
||
startButton.onclick = start; | ||
</script> | ||
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script> | ||
<script src="../commons/js/streamVisualizer.js"></script> | ||
</body> | ||
</html> |