-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
125 lines (100 loc) · 3.28 KB
/
main.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
121
122
123
124
125
const constraints = {
audio: false,
video: true,
};
const localVideoElement = document.querySelector("#localVideo");
const remoteVideoElement = document.querySelector("#remoteVideo");
var stream;
var remoteStream = null;
async function playLocalVideo() {
let localStream;
await navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
localStream = stream;
})
.catch(error => {
console.error('Error accessing media devices ' + error);
});
return localStream;
}
async function afterLoad() {
stream = await getLocalStream();
localVideoElement.srcObject = stream;
}
afterLoad();
async function getLocalStream() {
let localStream = await playLocalVideo();
return localStream;
}
function switchStream() {
if(remoteStream == null) return;
let currentStream = localVideoElement.srcObject;
if (currentStream == stream) {
localVideoElement.srcObject = remoteStream;
}
if (currentStream == remoteStream) {
localVideoElement.srcObject = stream;
}
}
const url = "wss://server.otanga.co.ke:9006" + window.location.pathname;
const signallingChannel = new WebSocket(url);
signallingChannel.addEventListener("message", async (message) => {
let data = JSON.parse(message.data);
console.log(data);
if(data.type === "offer") {
const remoteDesc = new RTCSessionDescription(data);
await offerReceived(remoteDesc);
//let stream = await playLocalVideo();
await stream.getTracks().forEach(track => {
localPeerConnection.addTrack(track, stream);
});
const answer = await localPeerConnection.createAnswer();
await offerCreated(answer);
signallingChannel.send(JSON.stringify(answer));
}
if(data.type === "answer") {
const remoteDesc = new RTCSessionDescription(data);
await offerReceived(remoteDesc);
}
if(data.candidate) {
try {
localPeerConnection.addIceCandidate(data);
} catch (e) {
console.error("Error adding received icecandidate ", e);
}
}
});
const configuration = {'iceServers': [{'urls': 'stun:stun.l.google.com:19302'}]};
var localPeerConnection = new RTCPeerConnection(configuration);
localPeerConnection.addEventListener("icecandidate", event => {
if(event.candidate) {
signallingChannel.send(JSON.stringify(event.candidate));
}
});
localPeerConnection.addEventListener("connectionstatechange", event => {
if(localPeerConnection.connectionState === "connected") {
console.log("Peers connected!");
}
})
localPeerConnection.addEventListener("track", event => {
console.log(event);
[remoteStream] = event.streams;
localVideoElement.srcObject = remoteStream;
})
async function makeCall() {
await stream.getTracks().forEach(track => {
localPeerConnection.addTrack(track, stream);
})
const offer = await localPeerConnection.createOffer();
await offerCreated(offer);
signallingChannel.send(JSON.stringify(offer));
}
async function offerCreated(description) {
localPeerConnection.setLocalDescription(description);
}
async function offerReceived(description) {
localPeerConnection.setRemoteDescription(description);
}
function logPeerConnections() {
console.log(localPeerConnection);
}