-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebrtc.js
121 lines (107 loc) · 3.75 KB
/
webrtc.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const constraints = {
'video': true,
'audio': true
}
var localStream = setUpLocalStream();
// Set up callButton
var callButton = document.getElementById("call-button");
callButton.addEventListener("click", makeOffer)
// Socket server setup
// const URL = "http://127.0.0.1:3000";
const URL = "http://157.90.119.115:3000/";
const socket = io(URL, { autoConnect: true });
// socket.onAny((event, ...args) => {
// console.log(event, args);
// });
socket.on("connect", () => {
console.log(socket.id); // "G5p5..."
});
// Setup peer connection
const configuration = {
'iceServers': [
{
urls: 'turn:157.90.119.115:3478',
username: 'test',
credential: 'test123'
}
]
}
const peerConnection = new RTCPeerConnection(configuration);
peerConnection.ontrack = () => remoteStream.addTrack(event.track, remoteStream)
peerConnection.onnegotiationneeded = (event) => console.log("Negotiation needed!");
peerConnection.onicecandidate = (event) => {
console.log("icecandidate happened")
if (event.candidate) {
console.log("icecandidate really happened")
console.log(event.candidate);
socket.emit("message", {"iceCandidate": event.candidate});
}
};
peerConnection.onconnectionstatechanged = (event) => {
// If peerConnection becomes connected
if (peerConnection.connectionState === 'connected') {
// Peers connected!
console.log("CONNECTED!!!!!!")
}
};
// Set up remote stream
const remoteStream = new MediaStream();
const remoteVideo = document.querySelector('#remoteVideo');
remoteVideo.srcObject = remoteStream;
async function setUpLocalStream() {
try {
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
console.log('Got MediaStream:', stream);
})
.catch(error => {
console.error('Error accessing media devices.', error);
});
localStream = await navigator.mediaDevices.getUserMedia(constraints);
const videoElement = document.querySelector('video#localVideo');
videoElement.srcObject = localStream;
localStream.getTracks().forEach(track => {
console.log("Sending track")
console.log(track)
peerConnection.addTrack(track, localStream);
});
return localStream
} catch(error) {
console.error('Error opening video camera.', error);
}
}
async function makeOffer() {
// Listen for an answer (response to offer)
socket.on('message', async message => {
if (message.answer) {
console.log("Got answer")
console.log(message.answer)
const remoteDesc = new RTCSessionDescription(message.answer);
await peerConnection.setRemoteDescription(remoteDesc);
}
});
// Send out an offer
const offer = await peerConnection.createOffer({offerToReceiveAudio: true, offerToReceiveVideo: true});
await peerConnection.setLocalDescription(offer);
console.log("Offer sent");
console.log(offer);
socket.emit("message", {'offer': offer});
}
// Listen out for any offer
socket.on('message', async message => {
if (message.offer) {
peerConnection.setRemoteDescription(new RTCSessionDescription(message.offer));
const answer = await peerConnection.createAnswer();
console.log(answer)
await peerConnection.setLocalDescription(answer);
socket.emit("message", {'answer': answer});
}
// Listen for local ICE candidates on the local RTCPeerConnection
if (message.iceCandidate) {
try {
await peerConnection.addIceCandidate(message.iceCandidate);
} catch (e) {
console.error('Error adding received ice candidate', e);
}
}
})