2018-01-18 15:59:28 +01:00
import { Stream , StreamOptionsServer , InboundStreamOptions } from './Stream' ;
2017-05-10 10:55:31 +02:00
import { OpenViduInternal } from './OpenViduInternal' ;
import { SessionInternal } from './SessionInternal' ;
2016-10-15 00:11:17 +02:00
type ObjMap < T > = { [ s : string ] : T ; }
2017-05-17 11:46:32 +02:00
export interface ConnectionOptions {
2016-10-15 00:11:17 +02:00
id : string ;
2017-05-16 16:57:36 +02:00
metadata : string ;
2018-01-18 15:59:28 +01:00
streams : StreamOptionsServer [ ] ;
2016-10-15 00:11:17 +02:00
}
2017-05-17 11:46:32 +02:00
export class Connection {
2016-10-15 00:11:17 +02:00
2017-05-17 11:46:32 +02:00
public connectionId : string ;
public data : string ;
public creationTime : number ;
2016-10-15 00:11:17 +02:00
private streams : ObjMap < Stream > = { } ;
2018-01-18 15:59:28 +01:00
private inboundStreamsOpts : InboundStreamOptions ;
2016-10-15 00:11:17 +02:00
2017-05-17 11:46:32 +02:00
constructor ( private openVidu : OpenViduInternal , private local : boolean , private room : SessionInternal , private options? : ConnectionOptions ) {
2016-10-15 00:11:17 +02:00
2017-09-22 15:57:59 +02:00
console . info ( "'Connection' created (" + ( local ? "local" : "remote" ) + ")" + ( local ? "" : ", with 'connectionId' [" + ( options ? options . id : '' ) + "] " ) ) ;
2016-10-15 02:08:23 +02:00
if ( options ) {
2016-10-15 00:11:17 +02:00
2017-05-17 11:46:32 +02:00
this . connectionId = options . id ;
2018-01-18 15:59:28 +01:00
if ( options . metadata ) {
this . data = options . metadata ;
}
if ( options . streams ) {
this . initRemoteStreams ( options ) ;
2016-10-15 00:11:17 +02:00
}
}
2016-10-15 02:08:23 +02:00
2016-10-15 00:11:17 +02:00
}
2016-10-15 02:08:23 +02:00
addStream ( stream : Stream ) {
2017-12-12 14:36:43 +01:00
this . streams [ stream . streamId ] = stream ;
this . room . getStreams ( ) [ stream . streamId ] = stream ;
2016-10-15 00:11:17 +02:00
}
2018-01-10 15:00:52 +01:00
removeStream ( key : string ) {
delete this . streams [ key ] ;
delete this . room . getStreams ( ) [ key ] ;
2018-01-18 15:59:28 +01:00
delete this . inboundStreamsOpts ;
}
setOptions ( options : ConnectionOptions ) {
this . options = options ;
2018-01-10 15:00:52 +01:00
}
2016-10-15 00:11:17 +02:00
getStreams() {
return this . streams ;
}
dispose() {
for ( let key in this . streams ) {
this . streams [ key ] . dispose ( ) ;
}
}
sendIceCandidate ( candidate ) {
console . debug ( ( this . local ? "Local" : "Remote" ) , "candidate for" ,
2017-06-02 18:58:39 +02:00
this . connectionId , JSON . stringify ( candidate ) ) ;
2016-10-15 02:08:23 +02:00
2016-10-15 00:11:17 +02:00
this . openVidu . sendRequest ( "onIceCandidate" , {
2017-06-02 18:58:39 +02:00
endpointName : this.connectionId ,
2016-10-15 00:11:17 +02:00
candidate : candidate.candidate ,
sdpMid : candidate.sdpMid ,
sdpMLineIndex : candidate.sdpMLineIndex
} , function ( error , response ) {
if ( error ) {
console . error ( "Error sending ICE candidate: "
+ JSON . stringify ( error ) ) ;
}
} ) ;
}
2017-09-22 15:57:59 +02:00
2018-01-18 15:59:28 +01:00
initRemoteStreams ( options : ConnectionOptions ) {
let opts : StreamOptionsServer ;
for ( opts of options . streams ) {
2017-09-22 15:57:59 +02:00
2018-01-18 15:59:28 +01:00
let streamOptions : InboundStreamOptions = {
id : opts.id ,
2017-09-22 15:57:59 +02:00
connection : this ,
2018-01-18 15:59:28 +01:00
recvAudio : ( opts . audioActive == null ? true : opts . audioActive ) ,
recvVideo : ( opts . videoActive == null ? true : opts . videoActive ) ,
typeOfVideo : opts.typeOfVideo ,
2017-09-22 15:57:59 +02:00
}
2018-01-18 15:59:28 +01:00
let stream = new Stream ( this . openVidu , false , this . room , streamOptions ) ;
2017-09-22 15:57:59 +02:00
2018-01-18 15:59:28 +01:00
this . addStream ( stream ) ;
this . inboundStreamsOpts = streamOptions ;
2017-09-22 15:57:59 +02:00
}
2018-01-18 15:59:28 +01:00
console . info ( "Remote 'Connection' with 'connectionId' [" + this . connectionId + "] is now configured for receiving Streams with options: " , this . inboundStreamsOpts ) ;
2017-09-22 15:57:59 +02:00
}
2016-10-15 00:11:17 +02:00
}