openvidu/openvidu-browser/src/OpenVidu/Connection.ts

206 lines
7.4 KiB
TypeScript
Raw Normal View History

2018-04-26 15:33:47 +02:00
/*
2020-02-04 11:25:54 +01:00
* (C) Copyright 2017-2020 OpenVidu (https://openvidu.io)
2018-04-26 15:33:47 +02:00
*
* 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 { Session } from './Session';
import { Stream } from './Stream';
import { LocalConnectionOptions } from '../OpenViduInternal/Interfaces/Private/LocalConnectionOptions';
import { RemoteConnectionOptions } from '../OpenViduInternal/Interfaces/Private/RemoteConnectionOptions';
2018-04-26 15:33:47 +02:00
import { InboundStreamOptions } from '../OpenViduInternal/Interfaces/Private/InboundStreamOptions';
import { StreamOptionsServer } from '../OpenViduInternal/Interfaces/Private/StreamOptionsServer';
import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger';
2021-03-22 19:23:03 +01:00
import { ExceptionEvent, ExceptionEventName } from '../OpenViduInternal/Events/ExceptionEvent';
/**
* @hidden
*/
const logger: OpenViduLogger = OpenViduLogger.getInstance();
2018-04-26 15:33:47 +02:00
/**
* Represents each one of the user's connection to the session (the local one and other user's connections).
* Therefore each [[Session]] and [[Stream]] object has an attribute of type Connection
*/
export class Connection {
/**
* Unique identifier of the connection
*/
connectionId: string;
/**
* Time when this connection was created in OpenVidu Server (UTC milliseconds)
2018-04-26 15:33:47 +02:00
*/
creationTime: number;
/**
* Data associated to this connection (and therefore to certain user). This is an important field:
* it allows you to broadcast all the information you want for each user (a username, for example)
*/
data: string;
2020-10-22 20:42:54 +02:00
/**
* Role of the connection.
* - `SUBSCRIBER`: can subscribe to published Streams of other users by calling [[Session.subscribe]]
* - `PUBLISHER`: SUBSCRIBER permissions + can publish their own Streams by calling [[Session.publish]]
* - `MODERATOR`: SUBSCRIBER + PUBLISHER permissions + can force the unpublishing or disconnection over a third-party Stream or Connection by call [[Session.forceUnpublish]] and [[Session.forceDisconnect]]
*
* **Only defined for the local connection. In remote connections will be `undefined`**
*/
role: string;
/**
* Whether the streams published by this connection will be recorded or not. This only affects [INDIVIDUAL recording](/en/stable/advanced-features/recording#selecting-streams-to-be-recorded) <a href="https://docs.openvidu.io/en/stable/openvidu-pro/" target="_blank" style="display: inline-block; background-color: rgb(0, 136, 170); color: white; font-weight: bold; padding: 0px 5px; margin-right: 5px; border-radius: 3px; font-size: 13px; line-height:21px; font-family: Montserrat, sans-serif">PRO</a>
*
* **Only defined for the local connection. In remote connections will be `undefined`**
*/
record: boolean;
2018-04-26 15:33:47 +02:00
/**
* @hidden
*/
stream?: Stream;
2018-04-26 15:33:47 +02:00
/**
* @hidden
*/
localOptions: LocalConnectionOptions | undefined;
/**
* @hidden
*/
remoteOptions: RemoteConnectionOptions | undefined;
2018-04-26 15:33:47 +02:00
/**
* @hidden
*/
disposed = false;
/**
* @hidden
*/
rpcSessionId: string;
2018-04-26 15:33:47 +02:00
/**
* @hidden
*/
constructor(private session: Session, connectionOptions: LocalConnectionOptions | RemoteConnectionOptions) {
2018-04-26 15:33:47 +02:00
let msg = "'Connection' created ";
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;
2020-10-22 20:42:54 +02:00
this.role = this.localOptions.role;
this.record = this.localOptions.record;
msg += '(local)';
} else {
2018-04-26 15:33:47 +02:00
// 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;
2018-04-26 15:33:47 +02:00
}
if (this.remoteOptions.streams) {
this.initRemoteStreams(this.remoteOptions.streams);
2018-04-26 15:33:47 +02:00
}
msg += "(remote) with 'connectionId' [" + this.remoteOptions.id + ']';
2018-04-26 15:33:47 +02:00
}
logger.info(msg);
2018-04-26 15:33:47 +02:00
}
/* Hidden methods */
/**
* @hidden
*/
sendIceCandidate(candidate: RTCIceCandidate): void {
2018-04-26 15:33:47 +02:00
logger.debug((!!this.stream!.outboundStreamOpts ? 'Local' : 'Remote') + 'candidate for' +
2019-05-10 10:36:10 +02:00
this.connectionId, candidate);
2018-04-26 15:33:47 +02:00
this.session.openvidu.sendRequest('onIceCandidate', {
endpointName: this.connectionId,
candidate: candidate.candidate,
sdpMid: candidate.sdpMid,
sdpMLineIndex: candidate.sdpMLineIndex
}, (error, response) => {
if (error) {
2021-03-22 19:23:03 +01:00
logger.error('Error sending ICE candidate: ' + JSON.stringify(error));
this.session.emitEvent('exception', [new ExceptionEvent(this.session, ExceptionEventName.ICE_CANDIDATE_ERROR, this.session, "There was an unexpected error on the server-side processing an ICE candidate generated and sent by the client-side", error)]);
2018-04-26 15:33:47 +02:00
}
});
}
/**
* @hidden
*/
initRemoteStreams(options: StreamOptionsServer[]): void {
// This is ready for supporting multiple streams per Connection object. Right now the loop will always run just once
// this.stream should also be replaced by a collection of streams to support multiple streams per Connection
options.forEach(opts => {
const streamOptions: InboundStreamOptions = {
id: opts.id,
createdAt: opts.createdAt,
2018-04-26 15:33:47 +02:00
connection: this,
hasAudio: opts.hasAudio,
hasVideo: opts.hasVideo,
audioActive: opts.audioActive,
videoActive: opts.videoActive,
typeOfVideo: opts.typeOfVideo,
2018-04-26 15:33:47 +02:00
frameRate: opts.frameRate,
2018-07-27 14:32:53 +02:00
videoDimensions: !!opts.videoDimensions ? JSON.parse(opts.videoDimensions) : undefined,
filter: !!opts.filter ? opts.filter : undefined
2018-04-26 15:33:47 +02:00
};
const stream = new Stream(this.session, streamOptions);
this.addStream(stream);
});
logger.info("Remote 'Connection' with 'connectionId' [" + this.connectionId + '] is now configured for receiving Streams with options: ', this.stream!.inboundStreamOpts);
2018-04-26 15:33:47 +02:00
}
/**
* @hidden
*/
addStream(stream: Stream): void {
stream.connection = this;
this.stream = stream;
}
/**
* @hidden
*/
removeStream(streamId: string): void {
delete this.stream;
}
/**
* @hidden
*/
dispose(): void {
if (!!this.stream) {
delete this.stream;
}
this.disposed = true;
}
}