diff --git a/src/utils/media/pipeline/VirtualBackground.js b/src/utils/media/pipeline/VirtualBackground.js index ce4d0c4c85a..88a18074a3d 100644 --- a/src/utils/media/pipeline/VirtualBackground.js +++ b/src/utils/media/pipeline/VirtualBackground.js @@ -292,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) } } diff --git a/src/utils/media/pipeline/VirtualBackground.spec.js b/src/utils/media/pipeline/VirtualBackground.spec.js index a1b52314204..852d5d1e109 100644 --- a/src/utils/media/pipeline/VirtualBackground.spec.js +++ b/src/utils/media/pipeline/VirtualBackground.spec.js @@ -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) => { @@ -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) }) }) diff --git a/src/utils/webrtc/models/LocalMediaModel.js b/src/utils/webrtc/models/LocalMediaModel.js index f7b6b149cf8..d8bb2eb224a 100644 --- a/src/utils/webrtc/models/LocalMediaModel.js +++ b/src/utils/webrtc/models/LocalMediaModel.js @@ -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)