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' ;
2018-04-26 15:33:47 +02:00
import { ConnectionOptions } from '../OpenViduInternal/Interfaces/Private/ConnectionOptions' ;
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' ;
/ * *
* @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 ;
/ * *
* @hidden
* /
stream : Stream ;
/ * *
* @hidden
* /
options : ConnectionOptions | undefined ;
/ * *
* @hidden
* /
disposed = false ;
2019-01-14 10:18:04 +01:00
/ * *
* @hidden
* /
rpcSessionId : string ;
2018-04-26 15:33:47 +02:00
/ * *
* @hidden
* /
constructor ( private session : Session , opts? : ConnectionOptions ) {
let msg = "'Connection' created " ;
if ( ! ! opts ) {
// Connection is remote
2019-03-05 17:33:30 +01:00
msg += "(remote) with 'connectionId' [" + opts . id + ']' ;
this . options = opts ;
2018-04-26 15:33:47 +02:00
this . connectionId = opts . id ;
2019-01-09 17:35:34 +01:00
this . creationTime = opts . createdAt ;
2018-04-26 15:33:47 +02:00
if ( opts . metadata ) {
this . data = opts . metadata ;
}
if ( opts . streams ) {
this . initRemoteStreams ( opts . streams ) ;
}
2019-03-05 17:33:30 +01:00
} else {
// Connection is local
msg += '(local)' ;
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-05-04 20:01:56 +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 ) {
2020-05-04 20:01:56 +02:00
logger . error ( 'Error sending ICE candidate: '
2018-04-26 15:33:47 +02:00
+ JSON . stringify ( error ) ) ;
}
} ) ;
}
/ * *
* @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 ) ;
} ) ;
2020-05-04 20:01:56 +02:00
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 ;
}
}