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
*/
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;
}
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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<String, Token> tokensRegistered = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, ConcurrentHashMap<String, Token>> tokensRegisteredBySession = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Participant> participantsByTokens = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Token> tokensRegistered = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, ConcurrentHashMap<String, Token>> tokensRegisteredBySession = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Participant> 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<String, Token> 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<String, Token> 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<String, Token> 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<String, Token> 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 <code>true</code> if token was registered. <code>false</code> 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 <code>true</code> if token was registered. <code>false</code>
* 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);
}
}

View File

@ -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";