How to set the custom Virtual Background for Jitsi on user join?
I tried with blur, so far I see that it correctly selects the blur option but I can't "trigger" the actual change to be applied.
// custom-config.js
(function() {
// Function to wait for the APP object to be available
function waitForApp() {
console.log('waitForApp');
if (typeof APP !== 'undefined' && APP.conference && APP.conference._room) {
console.log('CustomBackground - setup');
setupVirtualBackground();
} else {
console.log('CustomBackground - retry');
setTimeout(waitForApp, 100); // Check every 100ms
}
}
// Function to set up the virtual background logic
function setupVirtualBackground() {
console.log('CustomBackground - waiting for conferenceJoined');
// Listen for the conferenceJoined event
APP.conference._room.on('conference.joined', function() {
// Access the Redux store state
const state = APP.store.getState();
// Get the decoded JWT
const jwt = state['features/base/jwt'].jwt;
if (jwt) {
console.log('CustomBackground - decodingJWT');
try {
// Split the JWT
const parts = jwt.split('.');
if (parts.length !== 3) {
throw new Error('Invalid JWT');
}
// Decode the payload
const payload = parts[1];
decodedToken = JSON.parse(atob(payload.replace(/-/g, '+').replace(/_/g, '/')));
console.log('CustomBackground - JWT decoded');
} catch (error) {
console.error('Error decoding JWT:', error);
}
}
console.log('CustomBackground - decodedToken', decodedToken.role);
// Check if the user has the "xyz" role
if (decodedToken &&
decodedToken.role &&
decodedToken.role === 'xyz') {
// Define the custom background image URL
const customBackgroundUrl = '.jpg';
// Set the virtual background
console.log('CustomBackground - setVirtualBackground');
const url = toDataURL(customBackgroundUrl);
const store = APP.store;
store.dispatch({
type: 'SET_VIRTUAL_BACKGROUND',
backgroundEffectEnabled: true,
backgroundType: 'blur',
blurValue: 25,
selectedThumbnail: 'blur'
});
logger.info('Image set for virtual background preview!');
}
});
}
// Start waiting for the APP object
waitForApp();
})();