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
|
require('bootstrap')
require('bootstrap/scss/bootstrap.scss')
require('@fortawesome/fontawesome-free/js/all')
require('./webchat.css')
const Server = require('./comm.js')
const WebRTC = require('./webrtc.js')
$(document).ready(function () {
// do not close the dropdown when selecting a mic or camera
$(document).on('click', '.dropdown-menu', function (e) {
if ($(this).hasClass('keep-open-on-click')) { e.stopPropagation() }
})
// update the list of mics and cameras
$('#settings').on('show.bs.dropdown', function () {
$('#mics').empty()
$('#cams').empty()
navigator.mediaDevices.enumerateDevices()
.then(devices => {
devices.forEach(function (device, idx) {
if (device.kind === 'audioinput') {
var radio = $(`
<div class="custom-control custom-radio">
<input type="radio" id="audioinput${idx}" name="audioinput" class="custom-control-input">
<label class="custom-control-label" for="audioinput${idx}"></label>
</div>
`)
radio.find('input').attr('value', device.deviceId)
radio.find('label').text(device.label || 'Unknown')
radio.find('label').prepend('<i class="fa fa-microphone small"></i> ')
$('#mics').append(radio)
} else if (device.kind === 'videoinput') {
radio = $(`
<div class="custom-control custom-radio">
<input type="radio" id="videoinput${idx}" name="videoinput" class="custom-control-input">
<label class="custom-control-label" for="videoinput${idx}"></label>
</div>
`)
radio.find('input').attr('value', device.deviceId)
radio.find('label').text(device.label || 'Unknown')
radio.find('label').prepend('<i class="fa fa-video small"></i> ')
$('#cams').append(radio)
}
})
})
})
function volumeAudioProcess(event) {
var buf = event.inputBuffer.getChannelData(0)
var bufLength = buf.length
var sum = 0
var x
// Do a root-mean-square on the samples: sum up the squares...
for (var i = 0; i < bufLength; i++) {
x = buf[i]
if (Math.abs(x) >= this.clipLevel) {
this.clipping = true
this.lastClip = window.performance.now()
}
sum += x * x
}
// ... then take the square root of the sum.
var rms = Math.sqrt(sum / bufLength)
// Now smooth this out with the averaging factor applied
// to the previous sample - take the max here because we
// want "fast attack, slow release."
this.volume = Math.max(rms, this.volume * this.averaging)
}
function createAudioMeter(audioCtx, clipLevel, averaging, clipLag) {
var processor = audioCtx.createScriptProcessor(512)
processor.onaudioprocess = volumeAudioProcess
processor.clipping = false
processor.lastClip = 0
processor.volume = 0
processor.clipLevel = clipLevel || 0.98
processor.averaging = averaging || 0.95
processor.clipLag = clipLag || 750
// this will have no effect, since we don't copy the input to the output,
// but works around a current Chrome bug.
processor.connect(audioCtx.destination)
processor.checkClipping =
function () {
if (!this.clipping) { return false }
if ((this.lastClip + this.clipLag) < window.performance.now()) { this.clipping = false }
return this.clipping
}
processor.shutdown =
function () {
this.disconnect()
this.onaudioprocess = null
}
return processor
}
function showStream(video, stream) {
// play the video
console.log('showStream', stream)
video.volume = 0
if ('srcObject' in video) {
video.srcObject = stream
} else {
video.src = URL.createObjectURL(stream)
}
video.onloadedmetadata = function (e) {
video.play()
video.muted = true
}
// set up the volume meter
var audioCtx = new AudioContext()
var source = audioCtx.createMediaStreamSource(stream)
var meter = createAudioMeter(audioCtx)
source.connect(meter)
video.meter = meter
}
// update the volume bars
setInterval(function () {
$('video').each(function () {
var volume = $(this).siblings('.volume')
if (this.meter) {
// check if we're currently clipping
if (this.meter.checkClipping()) { volume.css('background', '#ffc107') } else { volume.css('background', '#28a745') }
volume.css('height', Math.round(this.meter.volume * 140 + 5) + '%')
} else {
volume.css('height', 0)
}
})
}, 500)
navigator.mediaDevices.getUserMedia({audio: true, video: true})
.then(stream => {
console.log('Got MediaStream:', stream)
showStream($('#me')[0], stream)
})
.catch(error => {
alert('Error accessing media devices: ' + error)
})
var server = new Server()
server.ready(function () {
var webrtc = new WebRTC(server)
server.onMessage(function (msg) {
if (msg.message) {
$('#messages').append($('<div class="alert alert-primary">').text(msg.message))
// scroll to bottom
var messagesDiv = document.getElementById('messages')
messagesDiv.scrollTop = messagesDiv.scrollHeight
} else {
webrtc.handleMessage(msg)
}
})
$('#message-input').submit(function (event) {
var message = $(this).find('input').val()
if (message) {
var msg = {message: message, sender: webrtc.identity}
server.sendMessage(msg)
}
$(this).find('input').val('')
event.preventDefault()
})
})
})
|