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
|
/**
*
* @copyright Copyright (c) 2021, Daniel Calviño Sánchez (danxuliu@gmail.com)
*
* @copyright Copyright (c) 2021, Immanuel Pasanec (immanuel.pasanec@compaso.de)
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import TrackSinkSource from './TrackSinkSource'
import { VIRTUAL_BACKGROUND_TYPE } from '../effects/virtual-background/constants'
import JitsiStreamBackgroundEffect from '../effects/virtual-background/JitsiStreamBackgroundEffect'
/**
* Processor node to set a virtual background on a video track.
*
* A single input track slot with the default id is accepted. The input track
* must be a video track. A single output track slot with the default id is
* provided. The output track will be a video track.
*
* The virtual background node requires Web Assembly to be enabled in the
* browser as well as support for canvas filters. Whether the virtual background
* is available or not can be checked by calling
* "VirtualBackground.isSupported()". Besides that, it needs to
* download and compile a Tensor Flow Lite model; until the model has not
* finished loading or failed to load it is not possible to know for sure if
* virtual background is available, so if it is supported it is assumed to be
* available unless the model fails to load. In that case "loadFailed" is
* emitted and the virtual background is disabled. Whether the virtual
* background is available or not can be checked by calling "isAvailable()" on
* the object. If a virtual background node is tried to be used when it is not
* available its input will be just bypassed to its output.
*
* The virtual background is automatically stopped and started again when the
* input track is disabled and enabled (which changes the output track). The
* virtual background will be restarted whenever an input track is set in order
* to refresh the data if the track constraints, like its width or height,
* change.
*
* -------------------
* | |
* ---> | VirtualBackground | --->
* | |
* -------------------
*/
export default class VirtualBackground extends TrackSinkSource {
static _wasmSupported
static _wasmSimd
static _canvasFilterSupported
static isSupported() {
return this.isWasmSupported() && this.isCanvasFilterSupported()
}
static _checkWasmSupport() {
try {
const wasmCheck = require('wasm-check')
this._wasmSupported = true
if (wasmCheck?.feature?.simd) {
this._wasmSimd = true
} else {
this._wasmSimd = false
}
} catch (error) {
this._wasmSupported = false
console.error('Looks like WebAssembly is disabled or not supported on this browser, virtual background will not be available')
}
}
static isWasmSupported() {
if (this._wasmSupported === undefined) {
this._checkWasmSupport()
}
return this._wasmSupported
}
/**
* Returns whether SIMD instructions are available in WebAssembly or not.
*
* @return {boolean} undefined if WebAssembly is not supported, true if SIMD
* instructions are available in WebAssembly, or false otherwise.
*/
static isWasmSimd() {
if (this._wasmSupported === undefined) {
this._checkWasmSupport()
}
return this._wasmSimd
}
static isCanvasFilterSupported() {
if (this._canvasFilterSupported === undefined) {
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
this._canvasFilterSupported = context.filter !== undefined
canvas.remove()
}
return this._canvasFilterSupported
}
constructor() {
super()
this._addInputTrackSlot()
this._addOutputTrackSlot()
this._initJitsiStreamBackgroundEffect()
// JitsiStreamBackgroundEffect works with tracks internally, but
// requires and provides streams externally
this._inputStream = null
this._outputStream = null
this._enabled = true
}
_initJitsiStreamBackgroundEffect() {
const segmentationDimensions = {
model96: {
height: 96,
width: 160,
},
model144: {
height: 144,
width: 256,
},
}
if (!VirtualBackground.isWasmSupported()) {
return
}
const isSimd = VirtualBackground.isWasmSimd()
const virtualBackground = {
type: VIRTUAL_BACKGROUND_TYPE.NONE,
blurValue: 10,
}
const options = {
...isSimd ? segmentationDimensions.model144 : segmentationDimensions.model96,
virtualBackground,
simd: isSimd,
}
this._jitsiStreamBackgroundEffect = new JitsiStreamBackgroundEffect(options)
this._jitsiStreamBackgroundEffect.load().catch(() => {
this._trigger('loadFailed')
this.setEnabled(false)
})
}
isAvailable() {
if (!VirtualBackground.isSupported()) {
return false
}
// If VirtualBackground is supported it is assumed to be available
// unless the load has failed (so it is seen as available even when
// still loading).
return !this._jitsiStreamBackgroundEffect.didLoadFail()
}
isEnabled() {
return this._enabled
}
setEnabled(enabled) {
if (!this.isAvailable()) {
enabled = false
}
if (this.enabled === enabled) {
return
}
this._enabled = enabled
if (!enabled) {
this._stopEffect()
// If not enabled the input track is just bypassed to the output.
this._setOutputTrack('default', this.getInputTrack())
return
}
if (!this.getInputTrack()) {
return
}
this._startEffect()
}
_handleInputTrack(trackId, newTrack, oldTrack) {
// If not available or enabled the input track is just bypassed to the
// output.
if (!this.isAvailable() || !this._enabled) {
this._setOutputTrack('default', newTrack)
return
}
if (newTrack === oldTrack && newTrack !== null) {
this._jitsiStreamBackgroundEffect.updateInputStream()
return
}
this._stopEffect()
if (!newTrack) {
this._setOutputTrack('default', null)
return
}
this._startEffect()
}
_handleInputTrackEnabled(trackId, enabled) {
// If not available or enabled the input track is just bypassed to the
// output.
if (!this.isAvailable() || !this._enabled) {
this._setOutputTrackEnabled('default', enabled)
return
}
// Stop and resume the effect if the track is disabled and enabled, as
// there is no need to apply the effect (and consume CPU) on a disabled
// track.
if (!enabled) {
this._stopEffect()
this._setOutputTrack('default', this.getInputTrack())
return
}
this._startEffect()
}
_startEffect() {
if (this._inputStream) {
return
}
this._inputStream = new MediaStream()
this._inputStream.addTrack(this.getInputTrack())
this._outputStream = this._jitsiStreamBackgroundEffect.startEffect(this._inputStream)
this._setOutputTrack('default', this._outputStream.getVideoTracks()[0])
}
_stopEffect() {
if (!this._outputStream) {
return
}
this._jitsiStreamBackgroundEffect.stopEffect()
this._outputStream.getTracks().forEach(track => {
track.stop()
})
this._inputStream = null
this._outputStream = null
}
}
|