Skip to content

Commit

Permalink
Fix getting and setting the virtual background when not available
Browse files Browse the repository at this point in the history
If the virtual background is not available
"_jitsiStreamBackgroundEffect" is not even defined, so the public getter
and setter can not delegate to it in that case.

As the getter now returns undefined rather than an actual object the
getter can not be used if an object is expected when the virtual
background is not available.

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
  • Loading branch information
danxuliu committed Aug 17, 2023
1 parent 9aef09b commit 78a4028
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/utils/media/pipeline/VirtualBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
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

0 comments on commit 78a4028

Please sign in to comment.