-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.html
216 lines (174 loc) · 6.96 KB
/
client.html
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<html>
<head>
<title>Test</title>
</head>
<body>
<video autoplay></video>
<button onclick="start()">Call</button>
<button onclick="hangup(true)">Hangup</button>
<button onclick="notify()">Notify</button>
<script>
const from = 1;
const to = 2;
let sendChannel = null; // RTCDataChannel for the local (sender)
let receiveChannel = null;
const remoteView = document.querySelector('video');
class SignalingChannel {
connect() {
this.socket = new WebSocket(this.url);
let me = this;
this.socket.addEventListener('error', function (evt) {
console.log(`error is ${JSON.stringify(evt)}`);
});
this.socket.addEventListener('close', (event) => {
console.log("Reopening socket")
hangup(false);
setTimeout(function () {
me.connect();
}, 1000);
});
}
constructor(url) {
this.url = url;
}
send(data) {
this.socket.send(JSON.stringify(data))
}
addListener(handler) {
console.log("Appending listener")
this.socket.addEventListener('message', handler);
}
};
const signaling = new SignalingChannel(`wss://ezie.duckdns.org:8443/?from=${from}&to=${to}`);
signaling.connect();
const constraints = {
audio: true,
"video": {
"width": {
"min": "300",
"max": "640"
},
"height": {
"min": "200",
"max": "480"
}
}
};
const configuration = { iceServers: [{ urls: 'stun:stun.ekiga.net' }] };
let pc = null;
async function notify() {
if (sendChannel == null) {
sendChannel = pc.createDataChannel("sendChannel");
}
sendChannel.send("{\"operation\": \"switch_camera\"}");
}
function handleReceiveMessage(event) {
console.log(event.data)
}
function receiveChannelCallback(event) {
console.log("Data channel received")
receiveChannel = event.channel;
receiveChannel.onmessage = handleReceiveMessage;
// receiveChannel.onopen = handleReceiveChannelStatusChange;
// receiveChannel.onclose = handleReceiveChannelStatusChange;
}
function setupConnection() {
pc = new RTCPeerConnection(configuration);
pc.onicecandidate = ({ candidate }) => {
console.log(`ice candidate ${JSON.stringify(candidate)}`)
let msg = { "id": to, "body": JSON.stringify(candidate) }
signaling.send(msg)
};
pc.onnegotiationneeded = async () => {
console.log('Negotiation is needed')
try {
await pc.setLocalDescription(await pc.createOffer());
// send the offer to the other peer
let msg = { "id": to, "body": JSON.stringify({ desc: pc.localDescription }) }
signaling.send(msg);
// signaling.send({ desc: pc.localDescription });
} catch (err) {
console.error(err);
}
};
pc.ontrack = (event) => {
console.log("New track received")
// don't set srcObject again if it is already set.
if (remoteView.srcObject) return;
remoteView.srcObject = event.streams[0];
};
pc.ondatachannel = receiveChannelCallback;
}
async function start() {
try {
// get local stream, show it in self-view and add it to be sent
const stream =
await navigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track) =>
pc.addTrack(track, stream));
sendChannel = pc.createDataChannel("sendChannel");
} catch (err) {
console.error(err);
}
}
async function hangup(sendBack) {
try {
if (sendBack) {
let msg = { "id": to, "body": JSON.stringify({ hangup: true }) }
signaling.send(msg);
}
remoteView.srcObject = null;
pc.close();
pc.onicecandidate = null;
pc.ontrack = null;
setupConnection();
} catch (err) {
console.error(err);
}
}
const listener = async (data) => {
try {
var parsedEvent = JSON.parse(data.data);
if (parsedEvent.body != null) {
console.log(parsedEvent.body);
parse = JSON.parse(parsedEvent.body)
if (parse != null && parse.desc != null) {
console.log(`offer!!!!`)
desc = parse.desc
// if we get an offer, we need to reply with an answer
if (desc.type === 'offer' || desc.type === 'OFFER') {
await pc.setRemoteDescription(desc);
const stream =
await navigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track) =>
pc.addTrack(track, stream));
await pc.setLocalDescription(await pc.createAnswer());
let msg = { "id": to, "body": JSON.stringify({ desc: pc.localDescription }) }
signaling.send(msg);
} else if (desc.type === 'answer') {
await pc.setRemoteDescription(desc);
} else {
console.log('Unsupported SDP type.');
}
} else if (parse != null && parse.candidate != null) {
console.log(`candidate!!!! ${JSON.stringify(parse)}`);
await pc.addIceCandidate(parse);
} else if (parse != null && parse.hangup) {
console.log("hangup!!!!")
hangup(false);
}
}
} catch (err) {
console.error(err);
}
};
signaling.addListener(data => {
console.log(`adding listener again for ${JSON.stringify(data)}`)
listener(data).then(res => {
console.log("finished")
});
});
setupConnection();
</script>
</body>
</html>