-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsubscriber-block.js
739 lines (681 loc) · 23.6 KB
/
subscriber-block.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
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.red5.net/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.
*/
/**
* The SubscriberBlock is a self-contained manager for a subscriber that handles:
*
* * Setup an RTC Subscriber.
* * Graceful retry connection on failure to connect or unpublish.
*
* [Note] :: The streamUtils.subscriber is an entry point to the subscriber map maintained in this script.
*/
;((window, document, red5prosdk, getIsAvailable) => {
// Hold a Set mapping stream name to SubscriberBlock.
let subscriberMap = {}
const elementIdRegex = /(subscriber-).*(-container)/
/**
* Finds and returns SubscriberBlock associated with stream name.
*
* @param {String} name
*/
const findByStreamName = (name) => {
if (subscriberMap.hasOwnProperty(name)) {
return subscriberMap[name]
}
return undefined
}
/**
* Finds and returns the SubscriberBlock associated with the element id.
*
* @param {String} id
*/
const findByElementId = (id) => {
const name = getStreamNameFromSubscriberId(id)
if (name) {
return findByStreamName(name)
}
return undefined
}
/**
* Request to cancel subscription of a SubscriberBlock from the mapping.
*
* @param {String} name
* The stream name to look up to access the corresponding SubscriberBlock.
*/
const cancelSubscriberBlock = (name) => {
// If wildcarded, remove all.
if (name === '*') {
Object.keys(subscriberMap).forEach((key) => {
if (subscriberMap[key] instanceof SubscriberBlock) {
subscriberMap[key].cancel()
delete subscriberMap[key]
}
})
} else {
const subscriber = subscriberMap[name]
if (subscriber instanceof SubscriberBlock) {
subscriber.cancel()
delete subscriberMap[name]
}
}
}
/**
* Generic container HTML element for the subscriber block.
*/
const subscriberTemplate = `<div class="subscriber-container">
<div class="red5pro-media-container video-holder centered">
<video muted autoplay controls playsinline class="red5pro-subscriber" style="width:100%; height:100%;"></video>
</div>
<div class="subscriber-notifications"></div>
<div class="centered">
<p class="subscriber-name-field"></span></p>
</div>
</div>`
/**
* Generates the template.
*/
const templateContent = (templateHTML, streamName) => {
var div = document.createElement('div')
div.classList.add('subscriber-block')
div.classList.add('flex-parent')
div.innerHTML = templateHTML
div.classList.add(`${getSubscriberElementId(streamName)}-container`)
return div
}
/**
* Utility to generate a subscriber element id based on streamName.
*/
const getSubscriberElementId = (streamName) => {
return ['red5pro', 'subscriber', streamName].join('-')
}
/**
* Utility to get the stream name from the subscriber element id.
*/
const getStreamNameFromSubscriberId = (id) => {
const match = id.match(elementIdRegex)
if (match && match.length > 0) {
const split = match[0].split('-')
return split[1]
}
return undefined
}
/**
* Generates the encapsulating DOM element for the subscriber.
*/
const generateNewSubscriberDOM = (streamName, subId, parent) => {
const card = templateContent(subscriberTemplate, streamName)
parent.appendChild(card)
const videoId = subId
const videoElement = card.querySelector('.red5pro-subscriber')
const subscriberNameField = card.querySelector('.subscriber-name-field')
subscriberNameField.innerText = streamName
videoElement.id = videoId
card.id = [videoId, 'container'].join('-')
return card
}
/**
* Generates Red5 Pro URL to check for stream availability
*/
const getAvailableUrl = (streamManagerHost = null) => {
if (streamManagerHost != null) {
const protocol = 'https'
const host = streamManagerHost
const port = 443
return `${protocol}://${host}:${port}/streammanager/api/4.0/event/list`
}
return `http://127.0.0.1:5080/live/streams.jsp`
}
/**
* Breaks string into room and stream name
*/
function getRoomAndStreamFromStreamName(streamName) {
if (streamName.charAt(0) === '/') {
streamName = streamName.substring(1)
}
let parts = streamName.split('/')
parts.splice(0, 1)
let stream = parts[parts.length - 1]
parts.splice(parts.length - 1, 1)
let room = parts.join('/')
return { room, stream }
}
/**
* The SubscriberBlock is a self-managed subscriber that consumes and plays back a target stream.
*/
class SubscriberBlock {
/**
* @param {String} streamName
* The unique name of the stream to subscribe to.
* @param {HTMLNode} containerOrVideoElement
* Can be either the parenting container to append a generated subscriber UI to, or
* the target `video` element to assign the stream to.
* @param {Boolean} forceMute
* Flag to force the subscriber to mute or unmute.
* @param {Boolean} debug
* Flag to display debug information in UI.
* @param {Object} client
* Optional delegate that receieves method/event invocations for publisher muting.
*/
constructor(
streamName,
containerOrVideoElement,
retryDelay,
forceMute = true,
debug = true,
client = undefined
) {
this.retryDelay = retryDelay
const { room, stream } = getRoomAndStreamFromStreamName(streamName)
const uid = Math.floor(Math.random() * 0x10000).toString(16)
this.subscriptionId = [stream, 'sub', uid].join('-')
this.streamName = stream
this.roomName = room
this.subscriber = undefined
this.baseConfiguration = undefined
this.requiresStreamManager = false
this.streamManagerHost = undefined
this.preferWhipWhep = false
this.retryConnectTimeout = 0
this.forceMute = forceMute
this.next = undefined
this.client = client
this.currentStreamMode = 'Video/Audio'
this.parent = containerOrVideoElement
const tagname = this.parent.tagName.toUpperCase()
this.parentIsElement = tagname === 'VIDEO' || tagname === 'AUDIO'
// If the provided parent container IS NOT an `audio` or `video` element, we will generate one within the container.
if (!this.parentIsElement) {
this.card = generateNewSubscriberDOM(
this.streamName,
this.subscriptionId,
this.parent
)
this.subscriberNameField = this.card.querySelector(
'.subscriber-name-field'
)
this.notificationContainer = this.card.querySelector(
'.subscriber-notifications'
)
if (!debug) {
this.subscriberNameField.classList.add('hidden')
this.notificationContainer.classList.add('hidden')
}
}
this.onSubscriberEvent = this.respond.bind(this)
this.cancelled = false
this.closing = false
this.closed = false
this.unpublished = false
// Delegate callbacks
this.onevent = undefined
subscriberMap[this.streamName] = this
}
/**
* Optionally displays status info in custom UI.
*/
displayInfo(message) {
if (!this.parentIsElement) {
this.subscriberNameField.classList.remove('error-field')
this.subscriberNameField.innerText = message
}
}
/**
* Optionally displays error info in custom UI.
*/
displayError(message) {
if (!this.parentIsElement) {
this.subscriberNameField.classList.add('error-field')
this.subscriberNameField.innerText = message
}
}
/**
* Optionally displays information about audio mute from publisher.
*/
showAudioMuteNotification(flag, streamName) {
if (!this.parentIsElement) {
let notification = this.notificationContainer.querySelector(
'.audio-notification'
)
if (flag && !notification) {
notification = document.createElement('p')
notification.classList.add('audio-notification')
notification.innerText = `${streamName} has turned off audio.`
this.notificationContainer.appendChild(notification)
} else if (!flag && notification) {
this.notificationContainer.removeChild(notification)
}
}
if (this.client && this.client.onaudiomute) {
this.client.onaudiomute.call(null, flag, streamName, this)
}
}
/**
* Optionally displays information about video mute from publisher.
*/
showVideoMuteNotification(flag, streamName) {
if (!this.parentIsElement) {
let notification = this.notificationContainer.querySelector(
'.video-notification'
)
if (flag && !notification) {
notification = document.createElement('p')
notification.classList.add('video-notification')
notification.innerText = `${streamName} has turned off video.`
this.notificationContainer.appendChild(notification)
} else if (!flag && notification) {
this.notificationContainer.removeChild(notification)
}
}
if (this.client && this.client.onvideomute) {
this.client.onvideomute.call(null, flag, streamName, this)
}
}
/**
* Invoked when subscriber session is determined sufficient enough to pass on setup to
* another SubscriberBlock in a linked list.
*/
resolve() {
if (this.next) {
this.next.start(
this.baseConfiguration,
this.streamManagerHost,
this.preferWhipWhep
)
}
this.next = undefined
}
/**
* Invoked when subscriber session has failed but to pass on setup to another
* SubscriberBlock in a linked list.
*/
reject(event) {
if (event) {
console.error(event)
}
if (this.next) {
this.next.start(
this.baseConfiguration,
this.streamManagerHost,
this.preferWhipWhep
)
}
this.next = undefined
}
mergeAudioStreams() {
var streams = []
Object.keys(window.connectedSubscribers).forEach((s) => {
if (
window.connectedSubscribers[s] &&
!window.connectedSubscribers[s].forceMute
) {
const theStream =
window.connectedSubscribers[s].subscriber.getMediaStream()
console.log(theStream)
if (theStream) {
streams.push(theStream)
}
}
})
console.log(streams)
this.combineStreams(streams)
}
combineStreams(streams) {
if (streams === undefined) {
console.error('No Stream Available!')
return
}
try {
if (window.audioContext) {
window.audioContext.close()
window.audioContext = undefined
}
if (streams.length <= 0) {
return
}
// Initialize AudioContext object
window.audioContext = new AudioContext()
// Adding an AudioWorkletProcessor
// from another script with addModule method
// await audioContext.audioWorklet.addModule("/javascripts/vumeters_worklet.js");
// Creating a MediaStreamSource object
// and sending a MediaStream object granted by
// the user
var gainNode = window.audioContext.createGain()
window.gainNode = gainNode
gainNode.gain.value = 0.45
var merger = window.audioContext.createChannelMerger(streams.length)
for (var ii = 0; ii < streams.length; ii++) {
var stream = window.audioContext.createMediaStreamSource(streams[ii])
stream.connect(merger)
}
merger.connect(gainNode)
gainNode.connect(window.audioContext.destination)
console.log(window.audioContext.destination)
console.log('setup audio context')
} catch (e) {
console.log('We got an error connecting VU Meters!')
console.log(e)
return
}
}
/**
* Event listener for the Subscriber.
*/
respond(event) {
if (event.type === 'Subscribe.Time.Update') return
console.log('[subscriber:' + this.streamName + '] ' + event.type)
if (event.type !== 'Subscribe.Stop') {
this.displayInfo(`${this.streamName} - ${event.type}`)
}
if (
event.type === 'Connect.Failure' ||
event.type === 'Subscribe.Fail' ||
event.type === 'Subscribe.InvalidName'
) {
this.reject(event)
this.displayError(`${this.streamName} - ${event.type}`)
} else if (event.type === 'Subscribe.Start') {
console.log(`Set window.connectedSubscribers for ${this.streamName}`)
window.connectedSubscribers[this.streamName] = this
this.mergeAudioStreams()
this.resolve()
} else if (event.type === 'Subscribe.Play.Unpublish') {
// this.unpublished = true
this.stop()
console.log(
`Subscribe.Play.Unpublish, delete window.connectedSubscribers for ${this.streamName}`
)
delete window.connectedSubscribers[this.streamName]
this.mergeAudioStreams()
this.start(
this.baseConfiguration,
this.streamManagerHost,
this.preferWhipWhep
)
} else if (event.type === 'Subscribe.Metadata') {
const { streamingMode } = event.data
if (streamingMode && streamingMode !== this.currentStreamMode) {
if (streamingMode.match(/Video\/Audio/)) {
this.showAudioMuteNotification(false, this.streamName)
this.showVideoMuteNotification(false, this.streamName)
} else if (streamingMode.match(/Audio/)) {
this.showAudioMuteNotification(false, this.streamName)
this.showVideoMuteNotification(true, this.streamName)
} else if (streamingMode.match(/Video/)) {
this.showAudioMuteNotification(true, this.streamName)
this.showVideoMuteNotification(false, this.streamName)
} else if (streamingMode.match(/Empty/)) {
this.showVideoMuteNotification(true, this.streamName)
this.showAudioMuteNotification(true, this.streamName)
}
this.currentStreamMode = streamingMode
}
} else if (event.type === 'Subscribe.Stop') {
delete window.connectedSubscribers[this.streamName]
} else if (event.type === 'Subscribe.Connection.Closed') {
delete window.connectedSubscribers[this.streamName]
if (!this.unpublished) {
this.retryConnection()
}
}
if (this.onevent) {
this.onevent.call(null, event, this)
}
}
/**
* Request to shutdown and remove Subscriber from UI.
*/
cancel() {
this.cancelled = true
this.remove()
}
/**
* Shuts down playback and removes element from UI.
*/
async remove() {
if (this.closing) return
this.closing = true
try {
await this.stop()
} catch (e) {
console.error(`Error unsubscribing from ${this.streamName}`)
console.error(e)
} finally {
if (!this.parentIsElement) {
const el = this.parent.querySelector(
`.${getSubscriberElementId(this.streamName)}-container`
)
if (el) {
el.parentNode.removeChild(el)
}
}
delete subscriberMap[this.streamName]
this.closing = false
this.closed = true
this.next = undefined
this.client = undefined
}
}
/**
* Request to stop subscribing.
*/
async stop() {
try {
// this.unpublished = true
if (this.subscriber) {
this.subscriber.off('*', this.respond)
await this.subscriber.unsubscribe()
this.subscriber = undefined
}
return true
} catch (e) {
console.error(`Error unsubscribing from ${this.streamName}`)
console.error(e)
return false
}
}
/**
* Request to start a subscriber session.
*
* @param {Object} config
* The configuration object for initialization.
* @param {String} streamManagerHost
* Hostname of Stream Manager if used.
* @param {Boolean} preferWhipWhep
* Flag to use WHIP/WHEP protocol.
*/
async start(config, streamManagerHost = null, preferWhipWhep = false) {
const { WHEPClient, RTCSubscriber } = red5prosdk
this.streamManagerHost = streamManagerHost
this.preferWhipWhep = preferWhipWhep
this.cancelled = false
this.unpublished = false
this.baseConfiguration = JSON.parse(JSON.stringify(config))
this.baseConfiguration.app = this.baseConfiguration.app + this.roomName
// generate unique id for each time in case reconnect.
const uid = Math.floor(Math.random() * 0x10000).toString(16)
const newid = [this.streamName, 'sub', uid].join('-')
const rtcConfig = {
...this.baseConfiguration,
...{
streamName: this.streamName,
subscriptionId: newid,
},
}
// If we have generated our own UI for the subscriber, assign the unique mediaElementId to config.
if (!this.parentIsElement) {
rtcConfig.mediaElementId = this.subscriptionId
} else {
rtcConfig.mediaElementId = this.parent.id
}
this.subscriber = preferWhipWhep ? new WHEPClient() : new RTCSubscriber()
this.subscriber.on('*', this.onSubscriberEvent)
this.displayInfo(`Requesting ${this.streamName}...`)
try {
let availableUrlLocal = getAvailableUrl()
const availableLocal = await getIsAvailable(
availableUrlLocal,
this.streamName,
false
)
if (!availableLocal) {
console.log(
'Stream not available locally, searching on Stream Manager'
)
let availableUrlSM = getAvailableUrl(this.streamManagerHost)
const availableSM = await getIsAvailable(
availableUrlSM,
this.streamName,
true
)
if (!availableSM) {
throw new Error(`${this.streamName} Not Available`)
}
this.requiresStreamManager = true
}
if (this.requiresStreamManager) {
rtcConfig.protocol = 'wss'
rtcConfig.port = '443'
rtcConfig.host = this.streamManagerHost
const subscriberSM = await window.streamManagerUtil.getEdge(
rtcConfig.host,
this.baseConfiguration.app,
this.streamName
)
const { serverAddress, scope } = subscriberSM
rtcConfig.app = preferWhipWhep
? this.baseConfiguration.app
: 'streammanager'
rtcConfig.connectionParams = config.connectionParams
if (!preferWhipWhep) {
rtcConfig.connectionParams = {
...config.connectionParams,
host: serverAddress,
app: scope,
}
} else {
const { token } = rtcConfig.connectionParams
delete rtcConfig.connectionParams.host
delete rtcConfig.connectionParams.app
if (token) {
rtcConfig.connectionParams.token = encodeURIComponent(token)
}
}
}
await this.subscriber.init(rtcConfig)
await this.subscriber.subscribe()
if (this.forceMute) {
console.log('force mute for stream', this.streamName)
this.forceMuteOnSubscriber()
} else {
console.log('force unmute for stream', this.streamName)
this.forceUnmuteOnSubscriber()
}
} catch (e) {
console.error(e)
this.reject()
this.displayError(typeof e === 'string' ? e : e.message)
this.retryConnection(
config,
this.streamManagerHost,
this.preferWhipWhep
)
}
}
/**
* Request to continue trying reconnect on loss of stream.
*
* @param {Object} config
* The configuration to use in initialization of subscriber.
* @param {String} streamManagerHost
* Hostname of Stream Manager.
* @param {Boolean} preferWhipWhep
*/
async retryConnection(
config,
streamManagerHost = null,
preferWhipWhep = false
) {
try {
clearTimeout(this.retryConnectTimeout)
if (this.closing || this.closed || this.cancelled) return
if (this.subscriber) {
await this.subscriber.unsubscribe()
this.subscriber = undefined
}
this.retryConnectTimeout = setTimeout(() => {
this.displayInfo(`Retrying Connection for ${this.streamName}...`)
clearTimeout(this.retryConnectTimeout)
this.start(config, streamManagerHost, preferWhipWhep)
}, this.retryDelay)
} catch (e) {
console.error(e)
this.displayError(typeof e === 'string' ? e : e.message)
this.retryConnection(config, streamManagerHost, preferWhipWhep)
}
}
/**
* Returns the top-level DOM element of the subscriber.
*/
getElementContainer() {
return this.card
}
/**
* Returns the stream name subscribing to.
*/
getStreamName() {
if (this.roomName && this.roomName.length > 0) {
return `${this.roomName}/${this.streamName}`
}
return this.streamName
}
/**
* Request to force mute of audio playback.
*/
forceMuteOnSubscriber() {
this.forceMute = true
if (this.subscriber) {
this.subscriber.mute()
this.mergeAudioStreams()
}
}
/**
* Request to force unmute of audio playback.
*/
forceUnmuteOnSubscriber() {
this.forceMute = false
if (this.subscriber) {
this.subscriber.unmute()
this.mergeAudioStreams()
}
}
}
window.SubscriberBlock = SubscriberBlock
window.streamsUtil = {
...window.streamsUtil,
...{
subscribers: {
stop: cancelSubscriberBlock,
find: findByStreamName,
findByElementId: findByElementId,
},
},
}
})(window, document, window.red5prosdk, window.getIsStreamAvailable)