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 .
*
* /
2018-05-08 13:01:34 +02:00
import { Session } from './Session' ;
import { Stream } from './Stream' ;
2020-10-22 13:04:20 +02:00
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' ;
2020-05-04 20:01:56 +02:00
import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger' ;
2021-03-22 19:23:03 +01:00
import { ExceptionEvent , ExceptionEventName } from '../OpenViduInternal/Events/ExceptionEvent' ;
2020-05-04 20:01:56 +02:00
/ * *
* @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 ;
/ * *
2019-01-09 17:35:34 +01:00
* 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 ] ( / e n / s t a b l e / a d v a n c e d - f e a t u r e s / r e c o r d i n g # s e l e c t i n g - s t r e a m s - t o - b e - r e c o r d e d ) < a h r e f = " h t t p s : / / d o c s . o p e n v i d u . i o / e n / s t a b l e / o p e n v i d u - p r o / " t a r g e t = " _ b l a n k " s t y l e = " d i s p l a y : i n l i n e - b l o c k ; b a c k g r o u n d - c o l o r : r g b ( 0 , 1 3 6 , 1 7 0 ) ; c o l o r : w h i t e ; f o n t - w e i g h t : b o l d ; p a d d i n g : 0 p x 5 p x ; m a r g i n - r i g h t : 5 p x ; b o r d e r - r a d i u s : 3 p x ; f o n t - s i z e : 1 3 p x ; l i n e - h e i g h t : 2 1 p x ; f o n t - f a m i l y : M o n t s e r r a t , s a n s - s e r i f " > P R O < / a >
*
* * * Only defined for the local connection . In remote connections will be ` undefined ` * *
* /
record : boolean ;
2018-04-26 15:33:47 +02:00
/ * *
* @hidden
* /
2020-11-10 18:22:14 +01:00
stream? : Stream ;
2018-04-26 15:33:47 +02:00
/ * *
* @hidden
* /
2020-10-22 13:04:20 +02:00
localOptions : LocalConnectionOptions | undefined ;
/ * *
* @hidden
* /
remoteOptions : RemoteConnectionOptions | undefined ;
2018-04-26 15:33:47 +02:00
/ * *
* @hidden
* /
disposed = false ;
2019-01-14 10:18:04 +01:00
/ * *
* @hidden
* /
rpcSessionId : string ;
2018-04-26 15:33:47 +02:00
/ * *
* @hidden
* /
2020-10-22 13:04:20 +02:00
constructor ( private session : Session , connectionOptions : LocalConnectionOptions | RemoteConnectionOptions ) {
2018-04-26 15:33:47 +02:00
let msg = "'Connection' created " ;
2020-10-22 13:04:20 +02:00
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 ;
2020-10-22 13:04:20 +02:00
msg += '(local)' ;
} else {
2018-04-26 15:33:47 +02:00
// Connection is remote
2020-10-22 13:04:20 +02:00
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
}
2020-10-22 13:04:20 +02:00
if ( this . remoteOptions . streams ) {
this . initRemoteStreams ( this . remoteOptions . streams ) ;
2018-04-26 15:33:47 +02:00
}
2020-10-22 13:04:20 +02:00
msg += "(remote) with 'connectionId' [" + this . remoteOptions . id + ']' ;
2018-04-26 15:33:47 +02:00
}
2020-05-04 20:01:56 +02:00
logger . info ( msg ) ;
2018-04-26 15:33:47 +02:00
}
/* Hidden methods */
/ * *
* @hidden
* /
2018-06-11 13:08:30 +02:00
sendIceCandidate ( candidate : RTCIceCandidate ) : void {
2018-04-26 15:33:47 +02:00
2020-11-10 18:22:14 +01: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 ,
2019-01-09 17:35:34 +01:00
createdAt : opts.createdAt ,
2018-04-26 15:33:47 +02:00
connection : this ,
2018-07-03 15:35:08 +02:00
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 ,
2018-08-27 17:06:14 +02:00
filter : ! ! opts . filter ? opts.filter : undefined
2018-04-26 15:33:47 +02:00
} ;
const stream = new Stream ( this . session , streamOptions ) ;
this . addStream ( stream ) ;
} ) ;
2021-04-29 20:47:21 +02:00
logger . debug ( "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 ;
}
}