-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
317 lines (275 loc) · 10 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"use strict";
const MESSAGE_TYPE = {
SDP: 'SDP',
CANDIDATE: 'CANDIDATE',
};
const MAXIMUM_MESSAGE_SIZE = 65535;
const END_OF_FILE_MESSAGE = 'EOF';
let room;
let offer_paste = '';
let px;
const senders = [];
let userMediaStream;
let displayMediaStream;
let file;
let clip = '';
const startChat = async () => {
try {
// showChatRoom();
px = createPeerConnection(); // a promise to be filled later
//Answer(); // We would call Answer here, but px is not yet initialized!
userMediaStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
userMediaStream.getTracks().forEach(track => senders.push(px.addTrack(track, userMediaStream)));
document.getElementById('self-view').srcObject = userMediaStream;
} catch (err) {
console.error(err);
}
};
const Answer = () => {
if(offer_paste)
{
console.log('Answering');
unpackClipboard(offer_paste);
}
}
const createPeerConnection = () => {
const pc = new RTCPeerConnection();
console.log(pc);
console.log('onnegotiationneeded');
pc.onnegotiationneeded = async () => {
await createAndSendOffer();
};
console.log('onicecandidate');
pc.onicecandidate = (iceEvent) => {
if (iceEvent && iceEvent.candidate) {
console.log('if (iceEvent && iceEvent.candidate) {');
writeMessage({
message_type: MESSAGE_TYPE.CANDIDATE,
content: iceEvent.candidate,
});
}
};
pc.ontrack = (event) => {
const video = document.getElementById('remote-view');
video.srcObject = event.streams[0];
};
pc.ondatachannel = (event) => {
const { channel } = event;
channel.binaryType = 'arraybuffer';
const receivedBuffers = [];
channel.onmessage = async (event) => {
const { data } = event;
try {
if (data !== END_OF_FILE_MESSAGE) {
receivedBuffers.push(data);
} else {
const arrayBuffer = receivedBuffers.reduce((acc, arrayBuffer) => {
const tmp = new Uint8Array(acc.byteLength + arrayBuffer.byteLength);
tmp.set(new Uint8Array(acc), 0);
tmp.set(new Uint8Array(arrayBuffer), acc.byteLength);
return tmp;
}, new Uint8Array());
const blob = new Blob([arrayBuffer]);
downloadFile(blob, channel.label);
channel.close();
}
} catch (err) {
console.log('File transfer failed');
}
};
};
return pc;
};
const createAndSendOffer = async () => {
const offer = await px.createOffer();
await px.setLocalDescription(offer);
writeMessage({
message_type: MESSAGE_TYPE.SDP,
content: offer,
});
};
// Write to the shared data store
const writeMessage = (message) => {
console.log(message);
copyToClipboard(message);
// if (room) {
// signaling.send(JSON.stringify({
// ...message,
// room,
// }));
// I don't understand this '...' syntax,
// but maybe how 'room' is later extracted
// using `JSON.parse(message.utf8Data);`
// }
};
const unpackClipboard = async (message) => {
message = "[ " + message + " ]";
console.log('message');
console.log(message);
const data = JSON.parse(message);
if (!data) {
console.log('could not unpack clipboard' + message);
return;
}
const [ sdp, ice, ice2 ] = data;
console.log('sdp' + JSON.stringify(sdp));
console.log('ice2' + JSON.stringify(ice2));
readMessage(sdp);
readMessage(ice2);
}
// Read from the shared data store
const readMessage = async (message) => {
console.log('message');
const data = JSON.parse(message.data);
if (!data) {
console.log('could not parse message' + message);
return;
}
try {
// When another peer calls `createPeerConnection()`
if (message_type === MESSAGE_TYPE.CANDIDATE && content) {
if(candidate)
{
console.log('candidate' + candidate);
}
await px.addIceCandidate(content);
}
// When another peer calls `createAndSendOffer()`
else if (message_type === MESSAGE_TYPE.SDP) {
if (content.type === 'offer') { // Sent by the leader
await px.setRemoteDescription(content);
const answer = await px.createAnswer();
await px.setLocalDescription(answer);
writeMessage({
message_type: MESSAGE_TYPE.SDP,
content: answer,
});
} else if (content.type === 'answer') { // Sent by the follower
await px.setRemoteDescription(content);
} else {
console.log('Unsupported SDP type.');
}
}
} catch (err) {
console.error(err);
}
};
// workaround until we can store on borg
const copyToClipboard = (message) => {
if (clip != '') clip += ","; // JSON list
clip += JSON.stringify(message)
navigator.permissions.query({name: "clipboard-write"}).then((result) => {
if (result.state === "granted" || result.state === "prompt") {
navigator.clipboard.writeText(clip).then(() => {
console.log("message appended to clipboard");
}, () => {
console.log("ERROR: could not write to clipboard");
});
}
});
};
// This is 'server' code
// const peersByRoom = {};
// const onrequest = (message) => {
// const id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
// const { room } = JSON.parse(message.utf8Data);
// if (!peersByRoom[room]) { // if room is not registered
// peersByRoom[room] = [{ connection, id }]; // Lookup peer by 'room'
// } else if (!peersByRoom[room].find(peer => peer.id === id)) { // If peer id does not exist
// peersByRoom[room].push({ connection, id }); // Add peer to list
// }
// console.log(peersByRoom);
// // Iterate through all peers,
// // peersByRoom[room]
// // .filter(peer => peer.id !== id) // but ignore yourself,
// // .forEach(peer => peer.connection.send(message.utf8Data)); // send message to other peers
// };
const showChatRoom = () => {
document.getElementById('start').style.display = 'none';
document.getElementById('chat-room').style.display = 'grid';
};
// const shareFile = () => {
// if (file) {
// const channelLabel = file.name;
// const channel = px.createDataChannel(channelLabel);
// channel.binaryType = 'arraybuffer';
// channel.onopen = async () => {
// const arrayBuffer = await file.arrayBuffer();
// for (let i = 0; i < arrayBuffer.byteLength; i += MAXIMUM_MESSAGE_SIZE) {
// channel.send(arrayBuffer.slice(i, i + MAXIMUM_MESSAGE_SIZE));
// }
// channel.send(END_OF_FILE_MESSAGE);
// };
// channel.onclose = () => {
// closeDialog();
// };
// }
// };
// const closeDialog = () => {
// document.getElementById('select-file-input').value = '';
// document.getElementById('select-file-dialog').style.display = 'none';
// }
// const downloadFile = (blob, fileName) => {
// const a = document.createElement('a');
// const url = window.URL.createObjectURL(blob);
// a.href = url;
// a.download = fileName;
// a.click();
// window.URL.revokeObjectURL(url);
// a.remove()
// }
// document.getElementById('room-input').addEventListener('input', async (event) => {
// const { value } = event.target;
// if (value.length > 8) {
// document.getElementById('start-button').disabled = false;
// room = value;
// } else {
// document.getElementById('start-button').disabled = true;
// room = null;
// }
// });
document.getElementById('offer_paste').addEventListener('input', async (event) => {
const { value } = event.target;
offer_paste = value;
});
document.getElementById('start-button').addEventListener('click', async () => {
// if (room) {
startChat();
// }
});
document.getElementById('Answer').addEventListener('click', async () => {
// if (room) {
Answer();
// }
});
document.getElementById('share-button').addEventListener('click', async () => {
if (!displayMediaStream) {
displayMediaStream = await navigator.mediaDevices.getDisplayMedia();
}
senders.find(sender => sender.track.kind === 'video').replaceTrack(displayMediaStream.getTracks()[0]);
//show what you are showing in your "self-view" video.
document.getElementById('self-view').srcObject = displayMediaStream;
//hide the share button and display the "stop-sharing" one
document.getElementById('share-button').style.display = 'none';
document.getElementById('stop-share-button').style.display = 'inline';
});
document.getElementById('stop-share-button').addEventListener('click', async () => {
senders.find(sender => sender.track.kind === 'video')
.replaceTrack(userMediaStream.getTracks().find(track => track.kind === 'video'));
document.getElementById('self-view').srcObject = userMediaStream;
document.getElementById('share-button').style.display = 'inline';
document.getElementById('stop-share-button').style.display = 'none';
});
document.getElementById('share-file-button').addEventListener('click', () => {
document.getElementById('select-file-dialog').style.display = 'block';
});
document.getElementById('cancel-button').addEventListener('click', () => {
closeDialog();
});
document.getElementById('select-file-input').addEventListener('change', (event) => {
file = event.target.files[0];
document.getElementById('ok-button').disabled = !file;
});
// document.getElementById('ok-button').addEventListener('click', () => {
// shareFile();
// });