-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
332 lines (292 loc) · 10.3 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//@ts-check
// Copy-pasted and modified https://codepen.io/miguelao/pen/qRXrKR
/** @typedef {import('webxdc-types/global')} */
document.addEventListener('DOMContentLoaded', init);
// Slightly above the sustained send rate of Delta Chat, so that
// we never send two chunks of data in the same batch, to work around
// `appendBuffer()` throwing if it's not done processing the previous
// chunk.
// Btw, the rate is now 6.6666 for testrun: `*.testrun.org`.
// https://github.com/deltachat/deltachat-core-rust/pull/4904
const DATA_SEND_PERIOD = 11 * 1000;
function init() {
// Keep in mind that the same member could connect from two different devices.
/** @typedef {string} StreamId */
/** @type {Map<StreamId, ReturnType<typeof setUpNewVideoDisplay>>} */
const incomingStreams = new Map();
/** @typedef {string} RoomMemberAddr */
/** @type {Map<RoomMemberAddr, HTMLElement>} */
const roomMemberEls = new Map();
let handledOldMessages = false;
const handledOldMessagesP = window.webxdc.setUpdateListener(update => {
// Only handle messages that arrived after the app was opened.
// Why? Because it's a prototype.
if (!handledOldMessages) {
return;
}
switch (update.payload.type) {
case 'newRoomMember': {
addSectionForMember(
update.payload.roomMemberAddr,
update.payload.roomMemberName,
);
// Restart the stream, because `appendBuffer` apparently
// doesn't work if previous buffers are dropped.
localStreamP
?.then(stream => stream.stop())
.then(() => {
// IDK if `setTimeout` is needed.
setTimeout(() => {
localStreamP = startBroadcast(includeVideoCheckbox.checked)
})
})
break;
}
case 'newStream': {
let containerElement = roomMemberEls.get(update.payload.roomMemberAddr);
if (!containerElement) {
addSectionForMember(
update.payload.roomMemberAddr,
update.payload.roomMemberAddr // Yes, it should be member name.
);
containerElement = roomMemberEls.get(update.payload.roomMemberAddr);
}
incomingStreams.set(
update.payload.streamId,
setUpNewVideoDisplay(containerElement, update.payload.mimeType)
);
// Could be `null` if it's not the first time this member started
// a stream.
containerElement.getElementsByClassName('no-video')[0]?.remove();
break;
}
case 'data': {
const sourceBufferP = incomingStreams.get(update.payload.streamId);
sourceBufferP.then(async sourceBuffer => {
// TODO fix: if 'data' events are sent often enough, it can so happen
// that the last `appendBuffer` has not been finished, so this one
// will throw. Need to check `sourceBuffer.updating`.
const deserializedData = await deserializeData(update.payload.data);
sourceBuffer.appendBuffer(deserializedData);
})
break;
}
default:
throw new Error('Unknown message type:' + update.payload.type);
}
}, 0);
handledOldMessagesP.then(() => handledOldMessages = true);
function addSectionForMember(roomMemberAddr, roomMemberName) {
const memberSection = createElementForRoomMember(roomMemberName);
roomMemberEls.set(roomMemberAddr, memberSection);
document.getElementById('videos').appendChild(memberSection);
}
/** @type {undefined | ReturnType<typeof startBroadcast>} */
let localStreamP;
/** @type {HTMLButtonElement} */
const startBroadcastButton = document.getElementById('startBroadcast');
startBroadcastButton.addEventListener('click', () => {
startBroadcastButton.disabled = true;
includeVideoCheckbox.disabled = true;
localStreamP = startBroadcast(includeVideoCheckbox.checked)
localStreamP.then(stream => {
stopBroadcastButton.disabled = false;
});
});
/** @type {HTMLButtonElement} */
const stopBroadcastButton = document.getElementById('stopBroadcast');
stopBroadcastButton.addEventListener('click', () => {
stopBroadcastButton.disabled = true;
localStreamP?.then(stream => stream.stop());
localStreamP = undefined;
startBroadcastButton.disabled = false;
includeVideoCheckbox.disabled = false;
});
/** @type {HTMLInputElement} */
const includeVideoCheckbox = document.getElementById('includeVideo');
/** @type {HTMLInputElement} */
const startOthersStreamsButton = document.getElementById('startOthersStreams');
startOthersStreamsButton.addEventListener('click', () => {
for (const video of document.getElementsByTagName('video')) {
video.play();
video.currentTime = video.buffered.end(0)
}
})
handledOldMessagesP.then(() => {
window.webxdc.sendUpdate({
payload: {
type: 'newRoomMember',
roomMemberName: window.webxdc.selfName,
roomMemberAddr: window.webxdc.selfAddr,
},
}, '');
});
}
function createElementForRoomMember(roomMemberName) {
const memberSection = document.createElement('section');
memberSection.classList.add('member')
const nameEl = document.createElement('h3');
nameEl.textContent = roomMemberName;
memberSection.appendChild(nameEl);
const noVideoYetEl = document.createElement('p');
noVideoYetEl.classList.add('no-video');
noVideoYetEl.textContent = 'The member hasn\'t started a broadcast yet';
memberSection.appendChild(noVideoYetEl);
return memberSection;
}
/**
* @param {boolean} includeVideo
*/
async function startBroadcast(includeVideo) {
const streamId = Math.random();
const localStream = new LocalCameraMediaStream(
async (event) => {
const serializedData = await serializeData(event);
window.webxdc.sendUpdate({
payload: {
type: 'data',
streamId,
data: serializedData,
},
}, '');
},
includeVideo,
);
await localStream.init();
window.webxdc.sendUpdate({
payload: {
type: 'newStream',
roomMemberAddr: window.webxdc.selfAddr,
streamId,
mimeType: localStream.recorder.mimeType,
},
info: `${window.webxdc.selfName} started a broadcast!`,
}, '');
return localStream;
}
/**
* @param {BlobEvent} onDataAvailableEvent
*/
async function serializeData(onDataAvailableEvent) {
// const arrayBuffer = await event.data.arrayBuffer();
// return [...(new Uint8Array(arrayBuffer))];
const reader = new FileReader();
return new Promise(r => {
reader.onload = (fileReaderEvent) => {
r(fileReaderEvent.target.result);
}
reader.readAsDataURL(onDataAvailableEvent.data);
});
}
async function deserializeData(serializedData) {
// return new Uint8Array(serializedData);
// WTF?? If I remove this it stops working? Does `fetch` give different
// `arrayBuffer` for different `mimeType`?
const split = serializedData.split(',');
serializedData =
"data:application/octet-binary;base64," + split[split.length - 1];
// Btw, the data URL could be used directly as `video.src`.
// Actually - no.
// https://w3c.github.io/mediacapture-record/#mediarecorder-methods :
// > the individual Blobs need not be playable
return fetch(serializedData).then(r => r.arrayBuffer());
}
/**
* @param {HTMLElement} containerElement
* @param {string} mimeType
*/
async function setUpNewVideoDisplay(containerElement, mimeType) {
const mediaSource = new MediaSource();
const video = document.createElement('video');
// video.srcObject = mediaSource;
// TODO revokeObjectURL
video.src = URL.createObjectURL(mediaSource);
// this fails if the user hasn't interacted with the page (autoplay).
// That is they won't see the video play.
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play#usage_notes
video.play();
await new Promise(r => {
mediaSource.addEventListener("sourceopen", r, {
once: true,
passive: true,
});
})
const sourceBuffer = mediaSource.addSourceBuffer(mimeType);
containerElement.appendChild(video);
// TODO a way to clean up stuff, close `MediaSource`.
return sourceBuffer;
}
// /**
// * @typedef {Parameters<
// * MediaRecorder['ondataavailable']
// * >[0]['data']} MediaRecorderData
// */
/**
* @typedef {Parameters<
* Exclude<MediaRecorder['ondataavailable'], null>
* >[0]} MediaRecorderDataEvent
*/
class LocalCameraMediaStream {
/**
* @param {(data: MediaRecorderDataEvent) => void} onDataAvailable
*/
constructor(onDataAvailable, includeVideo) {
this._includeVideo = includeVideo;
/** @type {typeof onDataAvailable} */
this.onDataAvailable = onDataAvailable;
this._stopPromise = new Promise(r => this.stop = r);
}
async init() {
const stream = await navigator.mediaDevices.getUserMedia({
video: this._includeVideo
? {
// frameRate: {
// ideal: 5,
// },
height: {
ideal: 50,
},
width: {
ideal: 50,
},
}
: false,
audio: true,
});
this._stopPromise.then(() => {
stream.getTracks().forEach((track) => track.stop() );
});
const recorder = this.recorder = new MediaRecorder(stream, {
bitsPerSecond: 128,
// I'm not an expert, but this codec seems to be supported by a lot
// of browsers. Maybe there is a better string.
mimeType: 'video/webm;codecs=vp8',
});
recorder.ondataavailable = (e) => {
this.onDataAvailable(e);
}
recorder.start(DATA_SEND_PERIOD);
this._stopPromise.then(() => recorder.stop());
// if (recorder.state !== 'recording') {
await new Promise((r) =>
recorder.addEventListener("start", r, { once: true })
);
// }
}
}
/**
* @license
* Copyright 2023 WofWca <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/