-
Notifications
You must be signed in to change notification settings - Fork 1
/
offer.js
43 lines (36 loc) · 1.23 KB
/
offer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const offerSdp = document.getElementById("offer-sdp");
const myVideo = document.getElementById("my-video");
const remoteVideo = document.getElementById("remote-video");
const answerSdp = document.getElementById("answer-sdp");
const peer = new RTCPeerConnection();
startCall = () => {
navigator.mediaDevices
.getUserMedia({ video: true, audio: true })
.then((stream) => {
if (myVideo) {
const videoTracks = stream?.getVideoTracks() || [];
const selectedVideoTrack = videoTracks[0];
const audiolessStream = new MediaStream();
audiolessStream.addTrack(selectedVideoTrack);
myVideo.srcObject = audiolessStream;
}
startConnection(stream);
});
};
startConnection = (stream) => {
stream.getTracks().forEach((track) => peer.addTrack(track, stream));
peer.addEventListener(
"track",
(e) => {
remoteVideo.srcObject = e.streams[0];
},
false
);
peer.onicecandidate = ({ candidate }) => {
offerSdp.innerHTML = JSON.stringify(peer.localDescription);
};
peer.createOffer().then((offer) => peer.setLocalDescription(offer));
};
acceptAnswer = () => {
peer.setRemoteDescription(JSON.parse(answerSdp.value));
};