-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
157 lines (139 loc) · 5.38 KB
/
main.js
File metadata and controls
157 lines (139 loc) · 5.38 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const config = {
iceServers: [
{
urls: ["stun:stun.l.google.com:19302"]
}
]
};
const rtcConnection = new RTCPeerConnection(config);
let dc;
rtcConnection.ontrack = e => {
document.getElementById("received_video").srcObject = e.streams[0];
}
rtcConnection.onicecandidate = e => {
logEvent(`New ice candiate found!! Reprinting sdp. ${JSON.stringify(rtcConnection.localDescription)}`);
document.getElementById("icecandidate").innerText = JSON.stringify(rtcConnection.localDescription);
};
rtcConnection.onicegatheringstatechange = e => {
logEvent(`ICE gathering state change to: ${rtcConnection.iceGatheringState.toString()}`);
document.getElementById("ic_gathering_state").innerText = rtcConnection.iceGatheringState.toString();
}
rtcConnection.oniceconnectionstatechange = e => {
logEvent(`ICE Connection state change to ${rtcConnection.iceConnectionState.toString()}`);
}
rtcConnection.onconnectionstatechange = e => {
logEvent(`Connection state changes to ${rtcConnection.connectionState.toString()}`);
document.getElementById("connection_status").innerText = rtcConnection.connectionState.toString();
}
rtcConnection.ondatachannel = e => {
dc = e.channel;
setDataChannel();
};
function setDataChannel() {
dc.onmessage = e => {
logEvent(`New Message: ${e.data}`);
const li = document.createElement('li');
li.innerText = `Remote: ${JSON.stringify(e.data)}`;
document.getElementById("msg_list").appendChild(li);
};
dc.onopen = e => {
logEvent("Data channel open");
};
dc.onclose = e => {
logEvent("Data channel closed");
};
dc.onerror = e => {
logEvent(`Data channel error : ${JSON.stringify(e)}`, true);
}
}
function startChannel() {
dc = rtcConnection.createDataChannel("Channel");
setDataChannel();
rtcConnection.createOffer()
.then(offer => rtcConnection.setLocalDescription(offer))
.then(e => logEvent(`Offer successfully: ${JSON.stringify(e)}`))
.catch(e => {
logEvent(`Error while creating/setting offer: ${JSON.stringify(e)}`, true);
});
document.getElementById("start_info").classList.remove('hidden');
document.getElementById("join_info").classList.add('hidden');
document.getElementById('btn_connect').classList.add('hidden');
}
function start() {
const answer_str = document.getElementById("answer").value;
const answer = JSON.parse(answer_str);
rtcConnection.setRemoteDescription(answer);
}
function joinChannel() {
const offer_str = document.getElementById("offer").value;
const offer = JSON.parse(offer_str);
rtcConnection.setRemoteDescription(offer)
.then(e => logEvent(`Remote offer set: ${JSON.stringify(e)}`))
.catch(err => {
logEvent(`Error while setting remote offer: ${err}`, true);
});
rtcConnection.createAnswer()
.then(ans => rtcConnection.setLocalDescription(ans))
.then(e => logEvent(`Answer created successfully ${JSON.stringify(e)}`))
.catch(err => {
logEvent(`Error occur while creating answer:, ${err}`, true);
});
}
function sendMessage() {
const msg = document.getElementById("msg_input").value;
dc.send(msg);
document.getElementById("msg_input").value = "";
const li = document.createElement('li');
li.innerText = `You: ${msg}`;
document.getElementById("msg_list").appendChild(li);
}
function connect() {
document.getElementById("start_info").classList.add('hidden');
document.getElementById("join_info").classList.remove('hidden');
document.getElementById('btn_start_channel').classList.add('hidden');
}
function setupVideoChat() {
const mediaConstraints = {
audio: true,
video: true
};
navigator.mediaDevices.getUserMedia(mediaConstraints)
.then(function (localStream) {
document.getElementById("local_video").srcObject = localStream;
localStream.getTracks().forEach(track => rtcConnection.addTrack(track, localStream));
})
.catch(handleGetUserMediaError);
}
function handleGetUserMediaError(err) {
switch(e.name) {
case "NotFoundError":
logEvent(`Unable to open your call because no camera and/or microphone were found: ${JSON.stringify(err)}`, true)
alert("Unable to open your call because no camera and/or microphone" +
"were found.");
break;
case "SecurityError":
case "PermissionDeniedError":
// Do nothing; this is the same as the user canceling the call.
logEvent(`User denied permission of camera and/or audio: ${JSON.stringify(err)}`, true);
break;
default:
logEvent(`Error opening your camera and/or microphone: ${err} `, true)
alert("Error opening your camera and/or microphone: " + e.message);
break;
}
}
function logEvent(event, isError = false) {
const event_msg = JSON.stringify(event);
if (isError) {
document.getElementById('last_error').innerText = event_msg;
document.getElementById('last_error').style.color = 'red';
}
const li = document.createElement('li');
li.innerText = `${Date.now().toString()}: ${event_msg}`;
if (isError) {
li.style.color = 'red';
}
document.getElementById('event_list').appendChild(li);
isError ? console.error(li.innerText) : console.log(li.innerText);
}
setupVideoChat();