From 1f1b54f11184211d26813572d0cd465120f8138b Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Thu, 28 Apr 2022 11:53:15 +0200 Subject: [PATCH] openvidu-browser: fix VB edition checks --- openvidu-browser/src/OpenVidu/Session.ts | 104 +++++++++++++---------- openvidu-browser/src/OpenVidu/Stream.ts | 19 +++-- 2 files changed, 72 insertions(+), 51 deletions(-) diff --git a/openvidu-browser/src/OpenVidu/Session.ts b/openvidu-browser/src/OpenVidu/Session.ts index 7abf8a75..ee76fde7 100644 --- a/openvidu-browser/src/OpenVidu/Session.ts +++ b/openvidu-browser/src/OpenVidu/Session.ts @@ -1344,6 +1344,46 @@ export class Session extends EventDispatcher { 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 connectAux(token: string): Promise { @@ -1461,54 +1501,26 @@ export class Session extends EventDispatcher { } private processToken(token: string): void { - 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 tokenParams = this.getTokenParams(token; + this.sessionId = tokenParams.sessionId; - const params = token.split('?'); - const queryParams = decodeURI(params[1]) - .split('&') - .map(param => param.split('=')) - .reduce((values, [key, value]) => { - values[key] = value - return values - }, {}); - - this.sessionId = 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.secret) { + this.openvidu.secret = tokenParams.secret; } + 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) { diff --git a/openvidu-browser/src/OpenVidu/Stream.ts b/openvidu-browser/src/OpenVidu/Stream.ts index 46c70a31..e82c8982 100644 --- a/openvidu-browser/src/OpenVidu/Stream.ts +++ b/openvidu-browser/src/OpenVidu/Stream.ts @@ -348,9 +348,6 @@ export class Stream { // 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) { 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')); } - logger.info('Applying Virtual Background to stream ' + this.streamId); - let openviduToken: string; if (!!this.session.token) { openviduToken = this.session.token; @@ -369,8 +364,22 @@ export class Stream { 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')); } + + 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)); + logger.info('Applying Virtual Background to stream ' + this.streamId); + const afterScriptLoaded = async () => { try { const id = this.streamId + '_' + uuidv4();