openvidu-browser: change token processing after joinRoom

pull/553/head
pabloFuente 2020-10-22 13:04:20 +02:00
parent 97e02094f3
commit 4ce089acb8
8 changed files with 123 additions and 77 deletions

View File

@ -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 (!!(<LocalConnectionOptions>connectionOptions).role) {
// Connection is local
this.localOptions = <LocalConnectionOptions>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 = <RemoteConnectionOptions>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);
}

View File

@ -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<Connection>(),
streams: new Array<Stream>()
};
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 = <string>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')
}
}
}

View File

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

View File

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

View File

@ -17,7 +17,7 @@
import { StreamOptionsServer } from './StreamOptionsServer';
export interface ConnectionOptions {
export interface RemoteConnectionOptions {
id: string;
createdAt: number;
metadata: string;

View File

@ -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<String, Recording> 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);
}

View File

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

View File

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