Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable27] Fix checking if WebAssembly is supported for virtual background #10251

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/utils/media/pipeline/VirtualBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,19 @@ export default class VirtualBackground extends TrackSinkSource {
}

static _checkWasmSupport() {
try {
const wasmCheck = require('wasm-check')
this._wasmSupported = true

if (wasmCheck?.feature?.simd) {
this._wasmSimd = true
} else {
this._wasmSimd = false
}
} catch (error) {
const wasmCheck = require('wasm-check')

if (!wasmCheck.support()) {
this._wasmSupported = false

console.error('Looks like WebAssembly is disabled or not supported on this browser, virtual background will not be available')

return
}

this._wasmSupported = true

this._wasmSimd = wasmCheck.feature.simd
}

static isWasmSupported() {
Expand Down Expand Up @@ -293,16 +292,34 @@ export default class VirtualBackground extends TrackSinkSource {
this._outputStream = null
}

/**
* Gets the virtual background properties.
*
* @return {object|undefined} undefined if WebAssembly is not supported, an object
* with the virtual background properties otherwise.
*/
getVirtualBackground() {
if (!this.isAvailable()) {
return undefined
}

return this._jitsiStreamBackgroundEffect.getVirtualBackground()
}

/**
* Sets the virtual background properties.
*
* Nothing is set if the virtual background is not available.
*
* @param {object} virtualBackground the virtual background properties; see
* JitsiStreamBackgroundEffect.setVirtualBackground().
*/
setVirtualBackground(virtualBackground) {
return this._jitsiStreamBackgroundEffect.setVirtualBackground(virtualBackground)
if (!this.isAvailable()) {
return
}

this._jitsiStreamBackgroundEffect.setVirtualBackground(virtualBackground)
}

}
55 changes: 49 additions & 6 deletions src/utils/media/pipeline/VirtualBackground.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ describe('VirtualBackground', () => {

jest.spyOn(VirtualBackground.prototype, '_initJitsiStreamBackgroundEffect').mockImplementation(function() {
this._jitsiStreamBackgroundEffect = {
getVirtualBackground: jest.fn(() => {
return this._jitsiStreamBackgroundEffect.virtualBackground
}),
setVirtualBackground: jest.fn(() => {
}),
startEffect: jest.fn((inputStream) => {
Expand Down Expand Up @@ -118,14 +121,54 @@ describe('VirtualBackground', () => {
jest.restoreAllMocks()
})

test('set virtual background type and parameters', () => {
virtualBackground.setVirtualBackground({
objectWithoutValidation: true,
describe('get virtual background', () => {
beforeEach(() => {
virtualBackground._jitsiStreamBackgroundEffect.virtualBackground = {
objectWithoutValidation: true,
}
})

test('gets virtual background', () => {
expect(virtualBackground.getVirtualBackground()).toEqual({
objectWithoutValidation: true,
})
expect(virtualBackground._jitsiStreamBackgroundEffect.getVirtualBackground).toHaveBeenCalledTimes(1)
})

test('returns null if get when not available', () => {
available = false

expect(virtualBackground.getVirtualBackground()).toBe(undefined)
// A real VirtualBackground object would not even have a
// _jitsiStreamBackgroundEffect object if not available, but the
// mock is kept to perform the assertion.
expect(virtualBackground._jitsiStreamBackgroundEffect.getVirtualBackground).toHaveBeenCalledTimes(0)
})
})

describe('set virtual background', () => {
test('sets virtual background type and parameters', () => {
virtualBackground.setVirtualBackground({
objectWithoutValidation: true,
})

expect(virtualBackground._jitsiStreamBackgroundEffect.setVirtualBackground).toHaveBeenCalledTimes(1)
expect(virtualBackground._jitsiStreamBackgroundEffect.setVirtualBackground).toHaveBeenNthCalledWith(1, {
objectWithoutValidation: true,
})
})

test('does nothing if set when not available', () => {
available = false

virtualBackground.setVirtualBackground({
objectWithoutValidation: true,
})

expect(virtualBackground._jitsiStreamBackgroundEffect.setVirtualBackground).toHaveBeenCalledTimes(1)
expect(virtualBackground._jitsiStreamBackgroundEffect.setVirtualBackground).toHaveBeenNthCalledWith(1, {
objectWithoutValidation: true,
// A real VirtualBackground object would not even have a
// _jitsiStreamBackgroundEffect object if not available, but the
// mock is kept to perform the assertion.
expect(virtualBackground._jitsiStreamBackgroundEffect.setVirtualBackground).toHaveBeenCalledTimes(0)
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/utils/webrtc/models/LocalMediaModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ LocalMediaModel.prototype = {
this.set('videoEnabled', false)
this.set('virtualBackgroundAvailable', this._webRtc.webrtc.isVirtualBackgroundAvailable())
this.set('virtualBackgroundEnabled', this._webRtc.webrtc.isVirtualBackgroundEnabled())
this._setVirtualBackgroundTypeAndParameters(this._webRtc.webrtc.getVirtualBackground())
if (this._webRtc.webrtc.isVirtualBackgroundAvailable()) {
this._setVirtualBackgroundTypeAndParameters(this._webRtc.webrtc.getVirtualBackground())
}
this.set('localScreen', null)

this._webRtc.webrtc.on('localStreamRequested', this._handleLocalStreamRequestedBound)
Expand Down