-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
453 lines (418 loc) · 14.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
Copyright © 2015 Infrared5, Inc. All rights reserved.
The accompanying code comprising examples for use solely in conjunction with Red5 Pro (the "Example Code")
is licensed to you by Infrared5 Inc. in consideration of your agreement to the following
license terms and conditions. Access, use, modification, or redistribution of the accompanying
code constitutes your acceptance of the following license terms and conditions.
Permission is hereby granted, free of charge, to you to use the Example Code and associated documentation
files (collectively, the "Software") without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The Software shall be used solely in conjunction with Red5 Pro. Red5 Pro is licensed under a separate end
user license agreement (the "EULA"), which must be executed with Infrared5, Inc.
An example of the EULA can be found on our website at: https://account.red5pro.com/assets/LICENSE.txt.
The above copyright notice and this license shall be included in all copies or portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL INFRARED5, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* global red5prosdk */
import { query } from './url-util.js'
import { getCoordinates } from './coord-util.js'
import Whiteboard, { MESSAGES } from './whiteboard.js'
import DataChannelTransport from './datachannel-transport.js'
import KLVTransport from './klv-transport.js'
const { host, app, streamName, feedName, get } = query()
const { setLogLevel, WHIPClient, WHEPClient } = red5prosdk
const fit = get('fit') || 'contain'
const mode = get('mode') || 'pubsub'
const intro = get('intro') || false
const transportType = get('transport') || 'datachannel'
const strokeColorInput = document.querySelector('#stroke-color-input')
const lineWidthInput = document.querySelector('#line-width-input')
const clearButton = document.querySelector('#clear-button')
const canvas = document.querySelector('#whiteboard')
const subscriberCanvas = document.querySelector('#whiteboard-subscriber')
const pubContainer = document.querySelector('.broadcast-container')
const pubVideo = document.querySelector('#red5pro-publisher')
pubVideo.style.objectFit = fit
const pubFit = window.getComputedStyle(pubVideo).getPropertyValue('object-fit')
const subContainer = document.querySelector('.subscribe-container')
const subVideo = document.querySelector('#red5pro-subscriber')
subVideo.style.objectFit = fit
const subFit = window.getComputedStyle(subVideo).getPropertyValue('object-fit')
const introContainer = document.querySelector('#intro')
const introSubmit = document.querySelector('#intro-submit')
const NAME = '[TrueTime DataSync]'
/**
* Map of KLV message types to textual message types.
*/
export const MESSAGE_TYPES = {
1: MESSAGES.WHITEBOARD_START,
2: MESSAGES.WHITEBOARD_DRAW,
3: MESSAGES.WHITEBOARD_STOP,
4: MESSAGES.WHITEBOARD_CLEAR,
5: MESSAGES.WHITEBOARD_CHANGE,
}
setLogLevel('debug')
let publisher, whiteboard
let subscriber, whiteboardSubscriber
let feedSubscriber
// Base configuration for both publisher and subscriber.
const baseConfig = {
host: host || window.location.hostname,
app: app || 'live',
streamName: streamName || `stream-${new Date().getTime()}`,
mediaConstraints: {
audio: false,
video: {
width: 1280,
height: 720,
},
},
enableChannelSignaling: true, // Turn on data channel support.
trickleIce: true, // Flag to use trickle ice to send candidates.
}
console.log(NAME, 'baseConfig', baseConfig)
/**
* Returns MediaStream instance of rebroadcast of incoming feed stream.
* @param {String} feedName
* @returns
*/
const getFeedStream = async (feedName) => {
const elementId = 'red5pro-publisher'
const element = document.querySelector(`#${elementId}`)
const feedConfig = {
...baseConfig,
mediaElementId: elementId,
streamName: feedName,
}
feedSubscriber = new WHEPClient()
// In the event we are re-broadcasting, we need to ensure that
// subscriber video dimensions change affect the drawing area
feedSubscriber.on('Subscribe.VideoDimensions.Change', () => {
handlePublisherResize()
})
await feedSubscriber.init(feedConfig)
await feedSubscriber.subscribe()
if ('captureStream' in element) {
return element.captureStream()
}
return element.srcObject
}
/**
* Start the publisher.
*/
const startPublish = async () => {
// Create a new Publisher instance.
publisher = new WHIPClient()
publisher.on('*', (event) => {
console.log('[Publisher]', event.type)
if (event.type === 'Publish.Start') {
handlePublisherResize()
}
})
if (feedName) {
// If we have a feed name, we will consume that.
const mediaStream = await getFeedStream(feedName)
await publisher.initWithStream(baseConfig, mediaStream)
} else {
// Else we will broadcast our camera.
await publisher.init(baseConfig)
}
await publisher.publish()
// Start interactive whiteboard.
startWhiteboard(publisher)
}
/**
* Establish the interactive whiteboard.
* @param {WHIPClient} publisher
*/
const startWhiteboard = (publisher) => {
const canvasParent = canvas.parentElement
var ro = new ResizeObserver(() => {
handlePublisherResize()
})
canvasParent && ro.observe(canvasParent)
const transportName = '[Publisher:DataChannelTransport]'
const transport =
transportType === 'klv'
? new KLVTransport(transportName, publisher)
: new DataChannelTransport(transportName, publisher)
// Create a new Whiteboard instance.
whiteboard = new Whiteboard('[Publisher:Whiteboard]', canvas, transport)
// Assign input handlers.
whiteboard.onStrokeColorChange(strokeColorInput.value)
whiteboard.onLineWidthChange(parseInt(lineWidthInput.value, 10))
strokeColorInput.addEventListener('input', (e) => {
whiteboard.onStrokeColorChange(e.target.value)
})
strokeColorInput.addEventListener('change', (e) => {
whiteboard.onStrokeColorChange(e.target.value)
})
lineWidthInput.addEventListener('change', (e) => {
whiteboard.onLineWidthChange(parseInt(e.target.value, 10))
})
clearButton.addEventListener('click', () => {
whiteboard.clear()
})
}
/*
* Start the subscriber.
*/
const startSubscribe = async () => {
const transportName = '[Subscriber:DataChannelTransport]'
const transport =
transportType === 'klv'
? new KLVTransport(transportName, {
receive: (message) => {
onKLVMessage(message)
},
})
: new DataChannelTransport(transportName, {
receive: (message) => {
onDataChannelMessage(message)
},
})
// Create a new Subscriber instance.
subscriber = new WHEPClient()
subscriber.on('*', (event) => {
const { type } = event
if (type !== 'Subscribe.Time.Update') {
console.log('[Subscriber]', type)
if (type === 'Subscribe.Send.Invoke') {
const {
data: { senderName },
} = event.data
if (senderName && senderName !== baseConfig.streamName) {
return
}
// Forward along to the receiver transport.
transport.receive(event.data)
} else if (type === 'WebRTC.DataChannel.Message') {
const { data, method, type } = event.data
if (method && type.toLowerCase() === 'metadata') {
if (method.toLowerCase() === 'onklv') {
transport.receive(data)
} else if (method.toLowerCase().match(/^whiteboard/)) {
transport.receive(data)
}
}
} else if (
type === 'Subscribe.Start' ||
type === 'Subscribe.VideoDimensions.Change'
) {
handleSubscriberResize()
} else if (type === 'Subscribe.Connection.Closed') {
console.warn(NAME, 'Subscriber connection closed.')
startSubscribe()
}
}
})
await subscriber.init({
...baseConfig,
mediaElementId: 'red5pro-subscriber',
})
await subscriber.subscribe()
// Start whiteboard to receive updates from the publisher.
startSubsciberWhiteboard(subscriber)
}
/**
* Establish the whiteboard instance that will draw updates from the publisher.
* @param {WHEPClient} subscriber
*/
const startSubsciberWhiteboard = (subscriber) => {
const canvasParent = subscriberCanvas.parentElement
var ro = new ResizeObserver(() => {
handleSubscriberResize()
})
canvasParent && ro.observe(canvasParent)
// Create a new Whiteboard instance to draw updates on.
whiteboardSubscriber = new Whiteboard(
'[Subscriber:Whiteboard]',
subscriberCanvas,
undefined,
false
)
whiteboardSubscriber.onStrokeColorChange(strokeColorInput.value)
whiteboardSubscriber.onLineWidthChange(parseInt(lineWidthInput.value, 10))
}
/**
* DataChannelTransport receiver message handler.
* @param {Object} message
*/
const onDataChannelMessage = (message) => {
const { methodName, data } = message
if (whiteboardSubscriber) {
if (methodName === MESSAGES.WHITEBOARD_DRAW) {
const { x, y, xRatio, yRatio } = data
whiteboardSubscriber.update(x, y, xRatio, yRatio)
} else if (methodName === MESSAGES.WHITEBOARD_CLEAR) {
whiteboardSubscriber.clear()
} else if (methodName === MESSAGES.WHITEBOARD_START) {
if (typeof data.coordinates === 'string') {
data.coordinates = JSON.parse(data.coordinates)
}
whiteboardSubscriber.start(data)
} else if (methodName === MESSAGES.WHITEBOARD_STOP) {
whiteboardSubscriber.stop()
} else if (methodName === MESSAGES.WHITEBOARD_CHANGE) {
const { color, lineWidth } = data
whiteboardSubscriber.onStrokeColorChange(color)
whiteboardSubscriber.onLineWidthChange(lineWidth)
}
}
}
/**
* KLVTransport receiver message handler.
* @param {Object} message
*/
const onKLVMessage = (message) => {
const { ul } = message // universal label describing the message types.
const klv = new Map()
const keys = Object.keys(message)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (!isNaN(parseInt(key, 10))) {
klv.set(key, message[key])
}
}
const hexDecodeToJSON = (hexString) => {
const rawString = decodeURIComponent(hexString.replace(/(..)/g, '%$1'))
const decodedString = window.atob(rawString)
return JSON.parse(decodedString)
}
klv.forEach((value, key) => {
const type = MESSAGE_TYPES[key]
if (type === MESSAGES.WHITEBOARD_DRAW) {
try {
const { x, y, xRatio, yRatio } = hexDecodeToJSON(value)
whiteboardSubscriber.update(x, y, xRatio, yRatio)
} catch (e) {
console.error(e)
}
} else if (type === MESSAGES.WHITEBOARD_CLEAR) {
whiteboardSubscriber.clear()
} else if (type === MESSAGES.WHITEBOARD_START) {
try {
whiteboardSubscriber.start(hexDecodeToJSON(value))
} catch (e) {
console.error(e)
}
} else if (type === MESSAGES.WHITEBOARD_STOP) {
whiteboardSubscriber.stop()
} else if (type === MESSAGES.WHITEBOARD_CHANGE) {
try {
const { color, lineWidth } = hexDecodeToJSON(value)
whiteboardSubscriber.onStrokeColorChange(color)
whiteboardSubscriber.onLineWidthChange(lineWidth)
} catch (e) {
console.error(e)
}
}
})
}
/**
* Resize event handler for the publisher to provide updated view coordinates to the interactive whiteboard.
*/
const handlePublisherResize = () => {
if (whiteboard) {
const { clientWidth, clientHeight } = pubVideo
const { videoWidth, videoHeight } = pubVideo
const coordinates = getCoordinates(
videoWidth,
videoHeight,
clientWidth,
clientHeight,
pubFit
)
whiteboard.onResize(coordinates)
}
}
/**
* Resize event handler for the subscriber to provide updated view coordinates to the drawn whiteboard.
*/
const handleSubscriberResize = () => {
if (whiteboardSubscriber) {
const { clientWidth, clientHeight } = subVideo
const { videoWidth, videoHeight } = subVideo
const coordinates = getCoordinates(
videoWidth,
videoHeight,
clientWidth,
clientHeight,
subFit
)
whiteboardSubscriber.onResize(coordinates)
}
}
/**
* Window resize event handler to update the view coordinates for both publisher and subscriber.
*/
const handleWindowResize = () => {
handlePublisherResize()
handleSubscriberResize()
}
/**
* Shutdown the publisher and subscriber.
*/
const shutdown = () => {
if (feedSubscriber) {
feedSubscriber.unsubscribe()
}
if (publisher) {
publisher.unpublish()
}
if (subscriber) {
subscriber.unsubscribe()
}
}
// Global Event handlers.
window.addEventListener('pagehide', shutdown)
window.addEventListener('beforeunload', shutdown)
window.addEventListener('resize', handleWindowResize)
document.addEventListener('DOMContentLoaded', handleWindowResize)
if (intro) {
introContainer.classList.remove('hidden')
introSubmit.addEventListener('click', () => {
introContainer.classList.add('hidden')
})
}
// Start the publisher and/or subscriber based on defined mode.
const start = async () => {
try {
if (mode === 'pub') {
subContainer.style.display = 'none'
await startPublish()
} else if (mode === 'sub') {
pubContainer.style.display = 'none'
await startSubscribe()
} else {
await startPublish()
// Have to delay because it connects to fast!
let t = setTimeout(() => {
clearTimeout(t)
startSubscribe()
}, 2000)
}
} catch (e) {
console.error(e)
const { name, message } = e
switch (name) {
case 'NotAllowedError':
alert('Please allow access to camera and microphone.')
break
case 'InvalidNameError':
alert(
`Could not start a ${mode} session with the provided stream name: ${streamName}.\r\n\r\n${message}\r\n\r\nSee Dev Console for more details.`
)
break
default:
alert('Failed to start. See console for details.')
break
}
}
}
start()