diff --git a/openvidu-browser/src/OpenVidu/Session.ts b/openvidu-browser/src/OpenVidu/Session.ts index c37a0448..7abf8a75 100644 --- a/openvidu-browser/src/OpenVidu/Session.ts +++ b/openvidu-browser/src/OpenVidu/Session.ts @@ -110,6 +110,10 @@ export class Session extends EventDispatcher { * @hidden */ options: SessionOptions; + /** + * @hidden + */ + token: string; /** * @hidden */ @@ -1357,10 +1361,7 @@ export class Session extends EventDispatcher { } else { // Process join room response - this.processJoinRoomResponse(response); - - // Configure JSNLogs - OpenViduLogger.configureJSNLog(this.openvidu, token); + this.processJoinRoomResponse(response, token); // Initialize local Connection object with values returned by openvidu-server 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; if (opts.customIceServers != null && opts.customIceServers.length > 0) { 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. ` + `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; } } diff --git a/openvidu-browser/src/OpenVidu/Stream.ts b/openvidu-browser/src/OpenVidu/Stream.ts index 59dac552..e81cb6f8 100644 --- a/openvidu-browser/src/OpenVidu/Stream.ts +++ b/openvidu-browser/src/OpenVidu/Stream.ts @@ -384,6 +384,7 @@ export class Stream { const VB = new VirtualBackground.VirtualBackground({ id, openviduServerUrl: new URL(this.session.openvidu.httpUri), + openviduToken: this.session.token, inputVideo: videoClone, inputResolution: '160x96', outputFramerate: 24 @@ -428,7 +429,7 @@ export class Stream { if (typeof VirtualBackground === "undefined") { let script: HTMLScriptElement = document.createElement("script"); 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 () => { try { await afterScriptLoaded(); diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/Session.java b/openvidu-server/src/main/java/io/openvidu/server/core/Session.java index 2db1deab..50cbe62e 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/Session.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/Session.java @@ -193,6 +193,10 @@ public class Session implements SessionInterface { return this.tokens.entrySet().iterator(); } + public boolean hasToken(String token) { + return this.tokens.containsKey(token); + } + public boolean isClosed() { return closed; } diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/TokenRegister.java b/openvidu-server/src/main/java/io/openvidu/server/core/TokenRegister.java index 23bffde5..05eb6cf6 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/TokenRegister.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/TokenRegister.java @@ -4,82 +4,90 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** - * This service class represents all the tokens currently registered by an active sessions - * Each time a token is created, it is registered by {@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)} + * This service class represents all the tokens currently registered by any + * active session Each time a token is created, it is registered by + * {@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 - * All maps of this class are present to be able to verify this in the most optimal way + * The purpose of this service is to know when a token was registered into a + * 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 { - private final ConcurrentHashMap tokensRegistered = new ConcurrentHashMap<>(); - private final ConcurrentHashMap> tokensRegisteredBySession = new ConcurrentHashMap<>(); - private final ConcurrentHashMap participantsByTokens = new ConcurrentHashMap<>(); + private final ConcurrentHashMap tokensRegistered = new ConcurrentHashMap<>(); + private final ConcurrentHashMap> tokensRegisteredBySession = new ConcurrentHashMap<>(); + private final ConcurrentHashMap participantsByTokens = new ConcurrentHashMap<>(); - /** - * O(1) - * Register a token of an specific active session - * @param sessionId Id of the sessions where the token is generated - * @param token Token to register - */ - protected synchronized void registerToken(String sessionId, Participant participant, Token token) { - this.tokensRegisteredBySession.putIfAbsent(sessionId, new ConcurrentHashMap<>()); - ConcurrentHashMap registeredTokensInSession = this.tokensRegisteredBySession.get(sessionId); - this.tokensRegistered.put(token.getToken(), token); - this.participantsByTokens.put(token.getToken(), participant); - registeredTokensInSession.put(token.getToken(), token); - } + /** + * O(1) Register a token of an specific active session + * + * @param sessionId Id of the sessions where the token is generated + * @param token Token to register + */ + protected synchronized void registerToken(String sessionId, Participant participant, Token token) { + this.tokensRegisteredBySession.putIfAbsent(sessionId, new ConcurrentHashMap<>()); + ConcurrentHashMap registeredTokensInSession = this.tokensRegisteredBySession.get(sessionId); + this.tokensRegistered.put(token.getToken(), token); + this.participantsByTokens.put(token.getToken(), participant); + registeredTokensInSession.put(token.getToken(), token); + } - /** - * O(n) - * Deregister all tokens of an specific session which is not active - * @param sessionId Id of the session which is no longer active - */ - protected synchronized void deregisterTokens(String sessionId) { - if (this.tokensRegisteredBySession.containsKey(sessionId)) { - for(Map.Entry tokenRegisteredInSession: tokensRegistered.entrySet()) { - this.tokensRegistered.remove(tokenRegisteredInSession.getKey()); - this.participantsByTokens.remove(tokenRegisteredInSession.getKey()); - } - this.tokensRegisteredBySession.remove(sessionId); - } - } + /** + * O(n) Deregister all tokens of an specific session which is not active + * + * @param sessionId Id of the session which is no longer active + */ + protected synchronized void deregisterTokens(String sessionId) { + if (this.tokensRegisteredBySession.containsKey(sessionId)) { + for (Map.Entry tokenRegisteredInSession : tokensRegistered.entrySet()) { + this.tokensRegistered.remove(tokenRegisteredInSession.getKey()); + this.participantsByTokens.remove(tokenRegisteredInSession.getKey()); + } + this.tokensRegisteredBySession.remove(sessionId); + } + } - /** - * O(1) - * Check if the current token string was registered in an active session - * @param token Token string to check if it is registered - * @param finalUserId userId of browser to check - * @param sessionId Id of session to check - * @return true if token was registered. false otherwise - */ - public boolean isTokenRegistered(String token, String finalUserId, String sessionId) { - if (!this.tokensRegistered.containsKey(token)) { - // False because token is not registered - return false; - } + /** + * O(1) Check if the current token string was registered in an active session + * + * @param token Token string to check if it is registered + * @param finalUserId userId of browser to check + * @param sessionId Id of session to check + * @return true if token was registered. false + * otherwise + */ + public boolean isTokenRegistered(String token, String finalUserId, String sessionId) { + if (!this.tokensRegistered.containsKey(token)) { + // False because token is not registered + return false; + } - if (!this.tokensRegisteredBySession.containsKey(sessionId)) { - // False because session is not registered with the specified token - return false; - } + if (!this.tokensRegisteredBySession.containsKey(sessionId)) { + // False because session is not registered with the specified token + return false; + } - if (!this.tokensRegisteredBySession.get(sessionId).containsKey(token)) { - // Token is not registered in the existing session - return false; - } + if (!this.tokensRegisteredBySession.get(sessionId).containsKey(token)) { + // Token is not registered in the existing session + return false; + } - if (!this.participantsByTokens.containsKey(token)) { - // Participant is not registered for specific token - return false; - } + if (!this.participantsByTokens.containsKey(token)) { + // Participant is not registered for specific token + return false; + } - // 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 - return participantsByTokens.get(token).getFinalUserId().equals(finalUserId) - && participantsByTokens.get(token).getSessionId().equals(sessionId); - - } + // 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 + return participantsByTokens.get(token).getFinalUserId().equals(finalUserId) + && participantsByTokens.get(token).getSessionId().equals(sessionId); + } + public boolean isTokenRegistered(String token) { + return this.tokensRegistered.containsKey(token); + } } diff --git a/openvidu-server/src/main/java/io/openvidu/server/rest/RequestMappings.java b/openvidu-server/src/main/java/io/openvidu/server/rest/RequestMappings.java index 9b2a7f3b..2f963d68 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/rest/RequestMappings.java +++ b/openvidu-server/src/main/java/io/openvidu/server/rest/RequestMappings.java @@ -14,6 +14,7 @@ public class RequestMappings { // Static resources final public static String RECORDINGS = "/openvidu/recordings"; 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_PRO = "/inspector";