Virtual Background token integration

pull/715/head
pabloFuente 2022-04-22 11:21:42 +02:00
parent 78d6320cad
commit f91c7b1928
5 changed files with 93 additions and 72 deletions

View File

@ -110,6 +110,10 @@ export class Session extends EventDispatcher {
* @hidden * @hidden
*/ */
options: SessionOptions; options: SessionOptions;
/**
* @hidden
*/
token: string;
/** /**
* @hidden * @hidden
*/ */
@ -1357,10 +1361,7 @@ export class Session extends EventDispatcher {
} else { } else {
// Process join room response // Process join room response
this.processJoinRoomResponse(response); this.processJoinRoomResponse(response, token);
// Configure JSNLogs
OpenViduLogger.configureJSNLog(this.openvidu, token);
// Initialize local Connection object with values returned by openvidu-server // Initialize local Connection object with values returned by openvidu-server
this.connection = new Connection(this, response); this.connection = new Connection(this, response);
@ -1510,7 +1511,7 @@ export class Session extends EventDispatcher {
} }
} }
private processJoinRoomResponse(opts: LocalConnectionOptions) { private processJoinRoomResponse(opts: LocalConnectionOptions, token: string) {
this.sessionId = opts.session; this.sessionId = opts.session;
if (opts.customIceServers != null && opts.customIceServers.length > 0) { if (opts.customIceServers != null && opts.customIceServers.length > 0) {
this.openvidu.iceServers = []; this.openvidu.iceServers = [];
@ -1557,6 +1558,12 @@ export class Session extends EventDispatcher {
+ `These versions are still compatible with each other, but openvidu-browser version must be updated as soon as possible to ${semverMajor(opts.version)}.${semverMinor(opts.version)}.x. ` + `These versions are still compatible with each other, but openvidu-browser version must be updated as soon as possible to ${semverMajor(opts.version)}.${semverMinor(opts.version)}.x. `
+ `This client using openvidu-browser ${this.openvidu.libraryVersion} will become incompatible with the next release of openvidu-server`); + `This client using openvidu-browser ${this.openvidu.libraryVersion} will become incompatible with the next release of openvidu-server`);
} }
// Configure JSNLogs
OpenViduLogger.configureJSNLog(this.openvidu, token);
// Store token
this.token = token;
} }
} }

View File

@ -384,6 +384,7 @@ export class Stream {
const VB = new VirtualBackground.VirtualBackground({ const VB = new VirtualBackground.VirtualBackground({
id, id,
openviduServerUrl: new URL(this.session.openvidu.httpUri), openviduServerUrl: new URL(this.session.openvidu.httpUri),
openviduToken: this.session.token,
inputVideo: videoClone, inputVideo: videoClone,
inputResolution: '160x96', inputResolution: '160x96',
outputFramerate: 24 outputFramerate: 24
@ -428,7 +429,7 @@ export class Stream {
if (typeof VirtualBackground === "undefined") { if (typeof VirtualBackground === "undefined") {
let script: HTMLScriptElement = document.createElement("script"); let script: HTMLScriptElement = document.createElement("script");
script.type = "text/javascript"; script.type = "text/javascript";
script.src = this.session.openvidu.httpUri + '/virtual-background/openvidu-virtual-background.js'; script.src = this.session.openvidu.httpUri + '/openvidu/virtual-background/openvidu-virtual-background.js?token=' + encodeURIComponent(this.session.token);
script.onload = async () => { script.onload = async () => {
try { try {
await afterScriptLoaded(); await afterScriptLoaded();

View File

@ -193,6 +193,10 @@ public class Session implements SessionInterface {
return this.tokens.entrySet().iterator(); return this.tokens.entrySet().iterator();
} }
public boolean hasToken(String token) {
return this.tokens.containsKey(token);
}
public boolean isClosed() { public boolean isClosed() {
return closed; return closed;
} }

View File

@ -4,12 +4,16 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
* This service class represents all the tokens currently registered by an active sessions * This service class represents all the tokens currently registered by any
* Each time a token is created, it is registered by {@link SessionManager} using {@link #registerToken(String, Participant, Token)} (String, Participant, Token)} * active session Each time a token is created, it is registered by
* All registered tokens will be present until {@link SessionManager} calls the method {@link #deregisterTokens(String)} * {@link SessionManager} using
* {@link #registerToken(String, Participant, Token)} (String, Participant,
* Token)} All registered tokens will be present until {@link SessionManager}
* calls the method {@link #deregisterTokens(String)}
* *
* The purpose of this service is to know when a token was registered into a session with its correspondent finalUserId * The purpose of this service is to know when a token was registered into a
* All maps of this class are present to be able to verify this in the most optimal way * session with its correspondent finalUserId. All maps of this class are
* present to be able to verify this in the most optimal way
*/ */
public class TokenRegister { public class TokenRegister {
@ -18,8 +22,8 @@ public class TokenRegister {
private final ConcurrentHashMap<String, Participant> participantsByTokens = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, Participant> participantsByTokens = new ConcurrentHashMap<>();
/** /**
* O(1) * O(1) Register a token of an specific active session
* Register a token of an specific active session *
* @param sessionId Id of the sessions where the token is generated * @param sessionId Id of the sessions where the token is generated
* @param token Token to register * @param token Token to register
*/ */
@ -32,13 +36,13 @@ public class TokenRegister {
} }
/** /**
* O(n) * O(n) Deregister all tokens of an specific session which is not active
* Deregister all tokens of an specific session which is not active *
* @param sessionId Id of the session which is no longer active * @param sessionId Id of the session which is no longer active
*/ */
protected synchronized void deregisterTokens(String sessionId) { protected synchronized void deregisterTokens(String sessionId) {
if (this.tokensRegisteredBySession.containsKey(sessionId)) { if (this.tokensRegisteredBySession.containsKey(sessionId)) {
for(Map.Entry<String, Token> tokenRegisteredInSession: tokensRegistered.entrySet()) { for (Map.Entry<String, Token> tokenRegisteredInSession : tokensRegistered.entrySet()) {
this.tokensRegistered.remove(tokenRegisteredInSession.getKey()); this.tokensRegistered.remove(tokenRegisteredInSession.getKey());
this.participantsByTokens.remove(tokenRegisteredInSession.getKey()); this.participantsByTokens.remove(tokenRegisteredInSession.getKey());
} }
@ -47,12 +51,13 @@ public class TokenRegister {
} }
/** /**
* O(1) * O(1) Check if the current token string was registered in an active session
* Check if the current token string was registered in an active session *
* @param token Token string to check if it is registered * @param token Token string to check if it is registered
* @param finalUserId userId of browser to check * @param finalUserId userId of browser to check
* @param sessionId Id of session to check * @param sessionId Id of session to check
* @return <code>true</code> if token was registered. <code>false</code> otherwise * @return <code>true</code> if token was registered. <code>false</code>
* otherwise
*/ */
public boolean isTokenRegistered(String token, String finalUserId, String sessionId) { public boolean isTokenRegistered(String token, String finalUserId, String sessionId) {
if (!this.tokensRegistered.containsKey(token)) { if (!this.tokensRegistered.containsKey(token)) {
@ -75,11 +80,14 @@ public class TokenRegister {
return false; return false;
} }
// In this final state, if finalUserId is equal to participant public id and session Id is the same, // In this final state, if finalUserId is equal to participant public id and
// session Id is the same,
// the token is registered correctly in the specific finalUserId and sessionId // the token is registered correctly in the specific finalUserId and sessionId
return participantsByTokens.get(token).getFinalUserId().equals(finalUserId) return participantsByTokens.get(token).getFinalUserId().equals(finalUserId)
&& participantsByTokens.get(token).getSessionId().equals(sessionId); && participantsByTokens.get(token).getSessionId().equals(sessionId);
} }
public boolean isTokenRegistered(String token) {
return this.tokensRegistered.containsKey(token);
}
} }

View File

@ -14,6 +14,7 @@ public class RequestMappings {
// Static resources // Static resources
final public static String RECORDINGS = "/openvidu/recordings"; final public static String RECORDINGS = "/openvidu/recordings";
final public static String CUSTOM_LAYOUTS = "/openvidu/layouts"; final public static String CUSTOM_LAYOUTS = "/openvidu/layouts";
final public static String VIRTUAL_BACKGROUND = "/openvidu/virtual-background";
final public static String FRONTEND_CE = "/dashboard"; final public static String FRONTEND_CE = "/dashboard";
final public static String FRONTEND_PRO = "/inspector"; final public static String FRONTEND_PRO = "/inspector";