From 4ce089acb8fa8cc23096977921c3317a2fa18327 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Thu, 22 Oct 2020 13:04:20 +0200 Subject: [PATCH] openvidu-browser: change token processing after joinRoom --- openvidu-browser/src/OpenVidu/Connection.ts | 43 +++++---- openvidu-browser/src/OpenVidu/Session.ts | 87 +++++++++---------- .../OpenViduInternal/Events/StreamEvent.ts | 4 +- .../Private/LocalConnectionOptions.ts | 32 +++++++ ...nOptions.ts => RemoteConnectionOptions.ts} | 2 +- .../server/core/SessionEventsHandler.java | 17 ++++ .../openvidu/server/core/TokenGenerator.java | 7 -- .../test/e2e/OpenViduTestAppE2eTest.java | 8 +- 8 files changed, 123 insertions(+), 77 deletions(-) create mode 100644 openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts rename openvidu-browser/src/OpenViduInternal/Interfaces/Private/{ConnectionOptions.ts => RemoteConnectionOptions.ts} (94%) diff --git a/openvidu-browser/src/OpenVidu/Connection.ts b/openvidu-browser/src/OpenVidu/Connection.ts index 87776d15..6c13c9ba 100644 --- a/openvidu-browser/src/OpenVidu/Connection.ts +++ b/openvidu-browser/src/OpenVidu/Connection.ts @@ -17,7 +17,8 @@ import { Session } from './Session'; import { Stream } from './Stream'; -import { ConnectionOptions } from '../OpenViduInternal/Interfaces/Private/ConnectionOptions'; +import { LocalConnectionOptions } from '../OpenViduInternal/Interfaces/Private/LocalConnectionOptions'; +import { RemoteConnectionOptions } from '../OpenViduInternal/Interfaces/Private/RemoteConnectionOptions'; import { InboundStreamOptions } from '../OpenViduInternal/Interfaces/Private/InboundStreamOptions'; import { StreamOptionsServer } from '../OpenViduInternal/Interfaces/Private/StreamOptionsServer'; import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger'; @@ -58,7 +59,12 @@ export class Connection { /** * @hidden */ - options: ConnectionOptions | undefined; + localOptions: LocalConnectionOptions | undefined; + + /** + * @hidden + */ + remoteOptions: RemoteConnectionOptions | undefined; /** * @hidden @@ -73,23 +79,28 @@ export class Connection { /** * @hidden */ - constructor(private session: Session, opts?: ConnectionOptions) { + constructor(private session: Session, connectionOptions: LocalConnectionOptions | RemoteConnectionOptions) { let msg = "'Connection' created "; - if (!!opts) { - // Connection is remote - msg += "(remote) with 'connectionId' [" + opts.id + ']'; - this.options = opts; - this.connectionId = opts.id; - this.creationTime = opts.createdAt; - if (opts.metadata) { - this.data = opts.metadata; - } - if (opts.streams) { - this.initRemoteStreams(opts.streams); - } - } else { + if (!!(connectionOptions).role) { // Connection is local + this.localOptions = connectionOptions; + this.connectionId = this.localOptions.id; + this.creationTime = this.localOptions.createdAt; + this.data = this.localOptions.metadata; + this.rpcSessionId = this.localOptions.sessionId; msg += '(local)'; + } else { + // Connection is remote + this.remoteOptions = connectionOptions; + this.connectionId = this.remoteOptions.id; + this.creationTime = this.remoteOptions.createdAt; + if (this.remoteOptions.metadata) { + this.data = this.remoteOptions.metadata; + } + if (this.remoteOptions.streams) { + this.initRemoteStreams(this.remoteOptions.streams); + } + msg += "(remote) with 'connectionId' [" + this.remoteOptions.id + ']'; } logger.info(msg); } diff --git a/openvidu-browser/src/OpenVidu/Session.ts b/openvidu-browser/src/OpenVidu/Session.ts index 14906d24..dc09d650 100644 --- a/openvidu-browser/src/OpenVidu/Session.ts +++ b/openvidu-browser/src/OpenVidu/Session.ts @@ -26,7 +26,8 @@ import { Capabilities } from '../OpenViduInternal/Interfaces/Public/Capabilities import { EventDispatcher } from './EventDispatcher'; import { SignalOptions } from '../OpenViduInternal/Interfaces/Public/SignalOptions'; import { SubscriberProperties } from '../OpenViduInternal/Interfaces/Public/SubscriberProperties'; -import { ConnectionOptions } from '../OpenViduInternal/Interfaces/Private/ConnectionOptions'; +import { RemoteConnectionOptions } from '../OpenViduInternal/Interfaces/Private/RemoteConnectionOptions'; +import { LocalConnectionOptions } from '../OpenViduInternal/Interfaces/Private/LocalConnectionOptions'; import { ObjMap } from '../OpenViduInternal/Interfaces/Private/ObjMap'; import { SessionOptions } from '../OpenViduInternal/Interfaces/Private/SessionOptions'; import { ConnectionEvent } from '../OpenViduInternal/Events/ConnectionEvent'; @@ -702,7 +703,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onParticipantJoined(response: ConnectionOptions): void { + onParticipantJoined(response: RemoteConnectionOptions): void { // Connection shouldn't exist this.getConnection(response.id, '') @@ -749,9 +750,10 @@ export class Session extends EventDispatcher { /** * @hidden */ - onParticipantPublished(response: ConnectionOptions): void { + onParticipantPublished(response: RemoteConnectionOptions): void { const afterConnectionFound = (connection) => { + this.remoteConnections[connection.connectionId] = connection; if (!this.remoteStreamsCreated[connection.stream.streamId]) { @@ -775,7 +777,7 @@ export class Session extends EventDispatcher { // Update existing Connection connection = con; response.metadata = con.data; - connection.options = response; + connection.remoteOptions = response; connection.initRemoteStreams(response.streams); afterConnectionFound(connection); }) @@ -1191,34 +1193,24 @@ export class Session extends EventDispatcher { const joinParams = this.initializeParams(token); - this.openvidu.sendRequest('joinRoom', joinParams, (error, response) => { + this.openvidu.sendRequest('joinRoom', joinParams, (error, response: LocalConnectionOptions) => { if (!!error) { reject(error); } else { - // Initialize capabilities object with the role - this.capabilities = { - subscribe: true, - publish: this.openvidu.role !== 'SUBSCRIBER', - forceUnpublish: this.openvidu.role === 'MODERATOR', - forceDisconnect: this.openvidu.role === 'MODERATOR' - }; + this.processJoinRoomResponse(response); // Initialize local Connection object with values returned by openvidu-server - this.connection = new Connection(this); - this.connection.connectionId = response.id; - this.connection.creationTime = response.createdAt; - this.connection.data = response.metadata; - this.connection.rpcSessionId = response.sessionId; + this.connection = new Connection(this, response); // Initialize remote Connections with value returned by openvidu-server const events = { connections: new Array(), streams: new Array() }; - const existingParticipants: ConnectionOptions[] = response.value; - existingParticipants.forEach(participant => { - const connection = new Connection(this, participant); + const existingParticipants: RemoteConnectionOptions[] = response.value; + existingParticipants.forEach((remoteConnectionOptions: RemoteConnectionOptions) => { + const connection = new Connection(this, remoteConnectionOptions); this.remoteConnections[connection.connectionId] = connection; events.connections.push(connection); if (!!connection.stream) { @@ -1323,12 +1315,7 @@ export class Session extends EventDispatcher { this.sessionId = queryParams['sessionId']; const secret = queryParams['secret']; const recorder = queryParams['recorder']; - const coturnIp = queryParams['coturnIp']; - const turnUsername = queryParams['turnUsername']; - const turnCredential = queryParams['turnCredential']; - const role = queryParams['role']; const webrtcStatsInterval = queryParams['webrtcStatsInterval']; - const openviduServerVersion = queryParams['version']; if (!!secret) { this.openvidu.secret = secret; @@ -1336,31 +1323,9 @@ export class Session extends EventDispatcher { if (!!recorder) { this.openvidu.recorder = true; } - if (!!turnUsername && !!turnCredential) { - const stunUrl = 'stun:' + coturnIp + ':3478'; - const turnUrl1 = 'turn:' + coturnIp + ':3478'; - const turnUrl2 = turnUrl1 + '?transport=tcp'; - this.openvidu.iceServers = [ - { urls: [stunUrl] }, - { urls: [turnUrl1, turnUrl2], username: turnUsername, credential: turnCredential } - ]; - logger.log("STUN/TURN server IP: " + coturnIp); - logger.log('TURN temp credentials [' + turnUsername + ':' + turnCredential + ']'); - } - if (!!role) { - this.openvidu.role = role; - } if (!!webrtcStatsInterval) { this.openvidu.webrtcStatsInterval = +webrtcStatsInterval; } - if (!!openviduServerVersion) { - logger.info("openvidu-server version: " + openviduServerVersion); - if (openviduServerVersion !== this.openvidu.libraryVersion) { - logger.warn('OpenVidu Server (' + openviduServerVersion + - ') and OpenVidu Browser (' + this.openvidu.libraryVersion + - ') versions do NOT match. There may be incompatibilities') - } - } this.openvidu.wsUri = 'wss://' + url.host + '/openvidu'; this.openvidu.httpUri = 'https://' + url.host; @@ -1370,4 +1335,32 @@ export class Session extends EventDispatcher { } } + private processJoinRoomResponse(opts: LocalConnectionOptions) { + this.sessionId = opts.session; + if (!!opts.coturnIp && !!opts.turnUsername && !!opts.turnCredential) { + const stunUrl = 'stun:' + opts.coturnIp + ':3478'; + const turnUrl1 = 'turn:' + opts.coturnIp + ':3478'; + const turnUrl2 = turnUrl1 + '?transport=tcp'; + this.openvidu.iceServers = [ + { urls: [stunUrl] }, + { urls: [turnUrl1, turnUrl2], username: opts.turnUsername, credential: opts.turnCredential } + ]; + logger.log("STUN/TURN server IP: " + opts.coturnIp); + logger.log('TURN temp credentials [' + opts.turnUsername + ':' + opts.turnCredential + ']'); + } + this.openvidu.role = opts.role; + this.capabilities = { + subscribe: true, + publish: this.openvidu.role !== 'SUBSCRIBER', + forceUnpublish: this.openvidu.role === 'MODERATOR', + forceDisconnect: this.openvidu.role === 'MODERATOR' + }; + logger.info("openvidu-server version: " + opts.version); + if (opts.version !== this.openvidu.libraryVersion) { + logger.warn('OpenVidu Server (' + opts.version + + ') and OpenVidu Browser (' + this.openvidu.libraryVersion + + ') versions do NOT match. There may be incompatibilities') + } + } + } diff --git a/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts index 88d57dff..93ff0809 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts @@ -101,8 +101,8 @@ export class StreamEvent extends Event { // Delete StreamOptionsServer from remote Connection const remoteConnection = this.stream.session.remoteConnections[this.stream.connection.connectionId]; - if (!!remoteConnection && !!remoteConnection.options) { - const streamOptionsServer = remoteConnection.options.streams; + if (!!remoteConnection && !!remoteConnection.remoteOptions) { + const streamOptionsServer = remoteConnection.remoteOptions.streams; for (let i = streamOptionsServer.length - 1; i >= 0; --i) { if (streamOptionsServer[i].id === this.stream.streamId) { streamOptionsServer.splice(i, 1); diff --git a/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts b/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts new file mode 100644 index 00000000..27cbd8be --- /dev/null +++ b/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts @@ -0,0 +1,32 @@ +/* + * (C) Copyright 2017-2020 OpenVidu (https://openvidu.io) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import { RemoteConnectionOptions } from './RemoteConnectionOptions'; + +export interface LocalConnectionOptions { + id: string; + createdAt: number; + metadata: string; + value: RemoteConnectionOptions[]; + session: string; // OpenVidu Session identifier + sessionId: string; // JSON-RPC session identifier + role: string; + coturnIp: string; + turnUsername: string; + turnCredential: string; + version: string; +} \ No newline at end of file diff --git a/openvidu-browser/src/OpenViduInternal/Interfaces/Private/ConnectionOptions.ts b/openvidu-browser/src/OpenViduInternal/Interfaces/Private/RemoteConnectionOptions.ts similarity index 94% rename from openvidu-browser/src/OpenViduInternal/Interfaces/Private/ConnectionOptions.ts rename to openvidu-browser/src/OpenViduInternal/Interfaces/Private/RemoteConnectionOptions.ts index 12b38df7..250d406d 100644 --- a/openvidu-browser/src/OpenViduInternal/Interfaces/Private/ConnectionOptions.ts +++ b/openvidu-browser/src/OpenViduInternal/Interfaces/Private/RemoteConnectionOptions.ts @@ -17,7 +17,7 @@ import { StreamOptionsServer } from './StreamOptionsServer'; -export interface ConnectionOptions { +export interface RemoteConnectionOptions { id: string; createdAt: number; metadata: string; diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java b/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java index 54344d8c..c23de26a 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java @@ -38,6 +38,7 @@ import io.openvidu.client.OpenViduException.Code; import io.openvidu.client.internal.ProtocolElements; import io.openvidu.java.client.OpenViduRole; import io.openvidu.server.cdr.CallDetailRecord; +import io.openvidu.server.config.OpenviduBuildInfo; import io.openvidu.server.config.OpenviduConfig; import io.openvidu.server.kurento.core.KurentoParticipant; import io.openvidu.server.kurento.endpoint.KurentoFilter; @@ -57,6 +58,9 @@ public class SessionEventsHandler { @Autowired protected OpenviduConfig openviduConfig; + @Autowired + protected OpenviduBuildInfo openviduBuildConfig; + private Map recordingsToSendClientEvents = new ConcurrentHashMap<>(); public void onSessionCreated(Session session) { @@ -151,6 +155,19 @@ public class SessionEventsHandler { result.addProperty(ProtocolElements.PARTICIPANTJOINED_METADATA_PARAM, participant.getFullMetadata()); result.add("value", resultArray); + if (participant.getToken() != null) { + result.addProperty("session", participant.getSessionId()); + result.addProperty("coturnIp", openviduConfig.getCoturnIp()); + if (participant.getToken().getRole() != null) { + result.addProperty("role", participant.getToken().getRole().name()); + } + if (participant.getToken().getTurnCredentials() != null) { + result.addProperty("turnUsername", participant.getToken().getTurnCredentials().getUsername()); + result.addProperty("turnCredential", participant.getToken().getTurnCredentials().getCredential()); + } + result.addProperty("version", openviduBuildConfig.getOpenViduServerVersion()); + } + rpcNotificationService.sendResponse(participant.getParticipantPrivateId(), transactionId, result); } diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/TokenGenerator.java b/openvidu-server/src/main/java/io/openvidu/server/core/TokenGenerator.java index 7e3c70c2..7aac70ab 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/TokenGenerator.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/TokenGenerator.java @@ -47,16 +47,9 @@ public class TokenGenerator { token += "?sessionId=" + sessionId; token += "&token=" + IdentifierPrefixes.TOKEN_ID + RandomStringUtils.randomAlphabetic(1).toUpperCase() + RandomStringUtils.randomAlphanumeric(15); - token += "&role=" + role.name(); - token += "&version=" + openviduBuildConfig.getOpenViduServerVersion(); TurnCredentials turnCredentials = null; if (this.openviduConfig.isTurnadminAvailable()) { turnCredentials = coturnCredentialsService.createUser(); - if (turnCredentials != null) { - token += "&coturnIp=" + openviduConfig.getCoturnIp(); - token += "&turnUsername=" + turnCredentials.getUsername(); - token += "&turnCredential=" + turnCredentials.getCredential(); - } } ConnectionProperties connectionProperties = new ConnectionProperties.Builder().type(ConnectionType.WEBRTC) .data(serverMetadata).record(record).role(role).kurentoOptions(kurentoOptions).build(); diff --git a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java b/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java index 06455ddc..0137b987 100644 --- a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java +++ b/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java @@ -2138,12 +2138,12 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest { KurentoOptions kurentoOptions = new KurentoOptions.Builder().videoMaxRecvBandwidth(250) .allowedFilters(new String[] { "GStreamerFilter" }).build(); - ConnectionProperties moderatorConnectionProperties = new ConnectionProperties.Builder().role(OpenViduRole.MODERATOR) - .data(serverDataModerator).kurentoOptions(kurentoOptions).build(); + ConnectionProperties moderatorConnectionProperties = new ConnectionProperties.Builder() + .role(OpenViduRole.MODERATOR).data(serverDataModerator).kurentoOptions(kurentoOptions).build(); Connection connectionModerator = session.createConnection(moderatorConnectionProperties); - ConnectionProperties subscriberConnectionProperties = new ConnectionProperties.Builder().type(ConnectionType.WEBRTC) - .role(OpenViduRole.SUBSCRIBER).data(serverDataSubscriber).build(); + ConnectionProperties subscriberConnectionProperties = new ConnectionProperties.Builder() + .type(ConnectionType.WEBRTC).role(OpenViduRole.SUBSCRIBER).data(serverDataSubscriber).build(); Connection connectionSubscriber = session.createConnection(subscriberConnectionProperties); Assert.assertFalse("Session.fetch() should return false after Session.createConnection", session.fetch());