2016-10-15 00:11:17 +02:00
import { Stream , StreamOptions } 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 ;
2016-10-15 00:11:17 +02:00
streams? : StreamOptions [ ] ;
2017-09-29 20:00:00 +02:00
audioActive : boolean ;
videoActive : boolean ;
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 > = { } ;
private streamsOpts : StreamOptions [ ] = [ ] ;
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 ;
this . data = options . metadata ;
2016-10-15 00:11:17 +02:00
2016-10-15 02:08:23 +02:00
if ( options . streams ) {
2017-09-22 15:57:59 +02:00
this . initStreams ( 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
}
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
initStreams ( options ) {
for ( let streamOptions of options . streams ) {
let streamOpts = {
id : streamOptions.id ,
connection : this ,
2017-09-29 20:00:00 +02:00
sendAudio : streamOptions.sendAudio ,
sendVideo : streamOptions.sendVideo ,
recvAudio : ( streamOptions . audioActive == undefined ? true : streamOptions . audioActive ) ,
recvVideo : ( streamOptions . videoActive == undefined ? true : streamOptions . videoActive ) ,
2017-12-12 14:36:43 +01:00
typeOfVideo : streamOptions.typeOfVideo ,
2017-09-29 20:00:00 +02:00
activeAudio : streamOptions.activeAudio ,
activeVideo : streamOptions.activeVideo ,
2017-09-22 15:57:59 +02:00
data : streamOptions.data ,
2017-09-29 20:00:00 +02:00
mediaConstraints : streamOptions.mediaConstraints
2017-09-22 15:57:59 +02:00
}
let stream = new Stream ( this . openVidu , false , this . room , streamOpts ) ;
this . addStream ( stream ) ;
this . streamsOpts . push ( streamOpts ) ;
}
console . info ( "Remote 'Connection' with 'connectionId' [" + this . connectionId + "] is now configured for receiving Streams with options: " , this . streamsOpts ) ;
}
2016-10-15 00:11:17 +02:00
}