openvidu-browser: fix VB edition checks

pull/722/head
pabloFuente 2022-04-28 11:53:15 +02:00
parent 5d354f1f0e
commit 1f1b54f111
2 changed files with 72 additions and 51 deletions

View File

@ -1344,6 +1344,46 @@ export class Session extends EventDispatcher {
return listenersInStreamManager > 0; return listenersInStreamManager > 0;
} }
/**
* @hidden
*/
getTokenParams(token: string) {
const match = token.match(/^(wss?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
if (!!match) {
const url = {
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
pathname: match[5],
search: match[6],
hash: match[7]
};
const params = token.split('?');
const queryParams = decodeURI(params[1])
.split('&')
.map(param => param.split('='))
.reduce((values, [key, value]) => {
values[key] = value
return values
}, {});
return {
url,
sessionId: queryParams['sessionId'],
secret: queryParams['secret'],
recorder: queryParams['recorder'],
webrtcStatsInterval: queryParams['webrtcStatsInterval'],
sendBrowserLogs: queryParams['sendBrowserLogs'],
edition: queryParams['edition']
};
} else {
throw new Error(`Token not valid: "${token}"`);
}
}
/* Private methods */ /* Private methods */
private connectAux(token: string): Promise<void> { private connectAux(token: string): Promise<void> {
@ -1461,54 +1501,26 @@ export class Session extends EventDispatcher {
} }
private processToken(token: string): void { private processToken(token: string): void {
const match = token.match(/^(wss?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/); const tokenParams = this.getTokenParams(token;
if (!!match) { this.sessionId = tokenParams.sessionId;
const url = {
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
pathname: match[5],
search: match[6],
hash: match[7]
};
const params = token.split('?'); if (!!tokenParams.secret) {
const queryParams = decodeURI(params[1]) this.openvidu.secret = tokenParams.secret;
.split('&')
.map(param => param.split('='))
.reduce((values, [key, value]) => {
values[key] = value
return values
}, {});
this.sessionId = <string>queryParams['sessionId'];
const secret = queryParams['secret'];
const recorder = queryParams['recorder'];
const webrtcStatsInterval = queryParams['webrtcStatsInterval'];
const sendBrowserLogs = queryParams['sendBrowserLogs'];
const edition = queryParams['edition'];
if (!!secret) {
this.openvidu.secret = secret;
}
if (!!recorder) {
this.openvidu.recorder = true;
}
if (!!webrtcStatsInterval) {
this.openvidu.webrtcStatsInterval = +webrtcStatsInterval;
}
if (!!sendBrowserLogs) {
this.openvidu.sendBrowserLogs = sendBrowserLogs;
}
this.openvidu.isAtLeastPro = !!webrtcStatsInterval && !!sendBrowserLogs;
this.openvidu.isEnterprise = edition === 'enterprise';
this.openvidu.wsUri = 'wss://' + url.host + '/openvidu';
this.openvidu.httpUri = 'https://' + url.host;
} else {
logger.error('Token "' + token + '" is not valid')
} }
if (!!tokenParams.recorder) {
this.openvidu.recorder = true;
}
if (!!tokenParams.webrtcStatsInterval) {
this.openvidu.webrtcStatsInterval = tokenParams.webrtcStatsInterval;
}
if (!!tokenParams.sendBrowserLogs) {
this.openvidu.sendBrowserLogs = tokenParams.sendBrowserLogs;
}
this.openvidu.isAtLeastPro = tokenParams.edition === 'pro' || tokenParams.edition === 'enterprise';
this.openvidu.isEnterprise = tokenParams.edition === 'enterprise';
this.openvidu.wsUri = 'wss://' + tokenParams.url.host + '/openvidu';
this.openvidu.httpUri = 'https://' + tokenParams.url.host;
} }
private processJoinRoomResponse(opts: LocalConnectionOptions, token: string) { private processJoinRoomResponse(opts: LocalConnectionOptions, token: string) {

View File

@ -348,9 +348,6 @@ export class Stream {
// Client filters // Client filters
if (!this.session.openvidu.isAtLeastPro) {
return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'OpenVidu Virtual Background API is available from OpenVidu Pro edition onwards'));
}
if (!this.hasVideo) { if (!this.hasVideo) {
return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'The Virtual Background filter requires a video track to be applied')); return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'The Virtual Background filter requires a video track to be applied'));
} }
@ -358,8 +355,6 @@ export class Stream {
return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'The StreamManager requires some video element to be attached to it in order to apply a Virtual Background filter')); return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'The StreamManager requires some video element to be attached to it in order to apply a Virtual Background filter'));
} }
logger.info('Applying Virtual Background to stream ' + this.streamId);
let openviduToken: string; let openviduToken: string;
if (!!this.session.token) { if (!!this.session.token) {
openviduToken = this.session.token; openviduToken = this.session.token;
@ -369,8 +364,22 @@ export class Stream {
if (!openviduToken) { if (!openviduToken) {
return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'Virtual Background requires the client to be connected to a Session or to have a "token" property available in "options" parameter with a valid OpenVidu token')); return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'Virtual Background requires the client to be connected to a Session or to have a "token" property available in "options" parameter with a valid OpenVidu token'));
} }
if (!!this.session.token && !this.session.openvidu.isAtLeastPro) {
// Check OpenVidu edition after connection to the Session
return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'OpenVidu Virtual Background API is available from OpenVidu Pro edition onwards'));
} else {
// Check OpenVidu edition with the token
const tokenParams = this.session.getTokenParams(openviduToken);
if (tokenParams.edition !== 'pro' && tokenParams.edition !== 'enterprise') {
return reject(new OpenViduError(OpenViduErrorName.VIRTUAL_BACKGROUND_ERROR, 'OpenVidu Virtual Background API is available from OpenVidu Pro edition onwards'));
}
}
openviduToken = encodeURIComponent(btoa(openviduToken)); openviduToken = encodeURIComponent(btoa(openviduToken));
logger.info('Applying Virtual Background to stream ' + this.streamId);
const afterScriptLoaded = async () => { const afterScriptLoaded = async () => {
try { try {
const id = this.streamId + '_' + uuidv4(); const id = this.streamId + '_' + uuidv4();