2017-06-10 01:44:31 +02:00
"use strict" ;
2018-04-18 14:29:07 +02:00
/ *
2018-05-06 02:20:25 +02:00
* ( C ) Copyright 2017 - 2018 OpenVidu ( https : //openvidu.io/)
2018-04-18 14:29:07 +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 .
*
* /
2017-06-10 01:44:31 +02:00
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2018-06-04 16:03:03 +02:00
var axios _1 = require ( "axios" ) ;
2018-10-01 11:17:05 +02:00
var Publisher _1 = require ( "./Publisher" ) ;
var Recording _1 = require ( "./Recording" ) ;
var Session _1 = require ( "./Session" ) ;
2018-01-27 19:39:49 +01:00
var OpenVidu = /** @class */ ( function ( ) {
2018-04-24 15:42:23 +02:00
/ * *
* @ param urlOpenViduServer Public accessible IP where your instance of OpenVidu Server is up an running
* @ param secret Secret used on OpenVidu Server initialization
* /
2017-06-10 01:44:31 +02:00
function OpenVidu ( urlOpenViduServer , secret ) {
this . urlOpenViduServer = urlOpenViduServer ;
2018-06-04 16:03:03 +02:00
this . Buffer = require ( 'buffer/' ) . Buffer ;
2018-07-23 12:03:56 +02:00
/ * *
* Array of active sessions . * * This value will remain unchanged since the last time method [ [ OpenVidu . fetch ] ]
* was called * * . Exceptions to this rule are :
*
* - Calling [ [ Session . fetch ] ] updates that specific Session status
* - Calling [ [ Session . close ] ] automatically removes the Session from the list of active Sessions
* - Calling [ [ Session . forceDisconnect ] ] automatically updates the inner affected connections for that specific Session
* - Calling [ [ Session . forceUnpublish ] ] also automatically updates the inner affected connections for that specific Session
* - Calling [ [ OpenVidu . startRecording ] ] and [ [ OpenVidu . stopRecording ] ] automatically updates the recording status of the
* Session ( [ [ Session . recording ] ] )
*
* To get the array of active sessions with their current actual value , you must call [ [ OpenVidu . fetch ] ] before consulting
* property [ [ activeSessions ] ]
* /
this . activeSessions = [ ] ;
2018-03-14 18:48:29 +01:00
this . setHostnameAndPort ( ) ;
2018-07-23 12:03:56 +02:00
OpenVidu . basicAuth = this . getBasicAuth ( secret ) ;
OpenVidu . o = this ;
2017-06-10 01:44:31 +02:00
}
2018-04-25 17:50:55 +02:00
/ * *
2018-07-23 12:03:56 +02:00
* Creates an OpenVidu session . You can call [ [ Session . getSessionId ] ] inside the resolved promise to retrieve the ` sessionId `
2018-04-25 17:50:55 +02:00
*
2018-05-03 11:48:57 +02:00
* @ returns A Promise that is resolved to the [ [ Session ] ] if success and rejected with an Error object if not .
2018-04-25 17:50:55 +02:00
* /
2018-01-27 19:39:49 +01:00
OpenVidu . prototype . createSession = function ( properties ) {
2018-04-25 17:50:55 +02:00
var _this = this ;
return new Promise ( function ( resolve , reject ) {
2018-07-23 12:03:56 +02:00
var session = new Session _1 . Session ( properties ) ;
2018-04-25 17:50:55 +02:00
session . getSessionIdHttp ( )
. then ( function ( sessionId ) {
2018-07-23 12:03:56 +02:00
_this . activeSessions . push ( session ) ;
2018-04-25 17:50:55 +02:00
resolve ( session ) ;
} )
. catch ( function ( error ) {
reject ( error ) ;
} ) ;
} ) ;
2018-01-27 19:39:49 +01:00
} ;
2018-04-24 15:42:23 +02:00
/ * *
* Starts the recording of a [ [ Session ] ]
*
* @ param sessionId The ` sessionId ` of the [ [ Session ] ] you want to start recording
* @ param name The name you want to give to the video file . You can access this same value in your clients on recording events ( ` recordingStarted ` , ` recordingStopped ` )
2018-07-23 12:03:56 +02:00
* * * WARNING : this parameter follows an overwriting policy . * * If you name two recordings the same , the newest MP4 file will overwrite the oldest one
2018-04-24 15:42:23 +02:00
*
2018-04-25 17:50:55 +02:00
* @ returns A Promise that is resolved to the [ [ Recording ] ] if it successfully started ( the recording can be stopped with guarantees ) and rejected with an Error object if not . This Error object has as ` message ` property with the following values :
2018-04-24 15:42:23 +02:00
* - ` 404 ` : no session exists for the passed ` sessionId `
* - ` 400 ` : the session has no connected participants
* - ` 409 ` : the session is not configured for using [ [ MediaMode . ROUTED ] ] or it is already being recorded
2018-05-03 12:27:27 +02:00
* - ` 501 ` : OpenVidu Server recording module is disabled ( ` openvidu.recording ` property set to ` false ` )
2018-04-24 15:42:23 +02:00
* /
2018-04-18 14:23:16 +02:00
OpenVidu . prototype . startRecording = function ( sessionId , param2 ) {
2018-03-14 18:48:29 +01:00
var _this = this ;
return new Promise ( function ( resolve , reject ) {
2018-06-04 16:03:03 +02:00
var data ;
2018-04-18 14:23:16 +02:00
if ( ! ! param2 ) {
if ( ! ( typeof param2 === 'string' ) ) {
2018-04-20 12:04:56 +02:00
var properties = param2 ;
2018-06-04 16:03:03 +02:00
data = JSON . stringify ( {
2018-04-18 14:23:16 +02:00
session : sessionId ,
2018-04-23 11:06:16 +02:00
name : ! ! properties . name ? properties . name : '' ,
recordingLayout : ! ! properties . recordingLayout ? properties . recordingLayout : '' ,
customLayout : ! ! properties . customLayout ? properties . customLayout : ''
2018-04-18 14:23:16 +02:00
} ) ;
}
else {
2018-06-04 16:03:03 +02:00
data = JSON . stringify ( {
2018-04-18 14:23:16 +02:00
session : sessionId ,
2018-04-20 12:04:56 +02:00
name : param2 ,
2018-04-20 15:50:56 +02:00
recordingLayout : '' ,
customLayout : ''
2018-04-18 14:23:16 +02:00
} ) ;
}
}
else {
2018-06-04 16:03:03 +02:00
data = JSON . stringify ( {
2018-04-18 14:23:16 +02:00
session : sessionId ,
2018-04-20 12:04:56 +02:00
name : '' ,
2018-04-20 15:50:56 +02:00
recordingLayout : '' ,
customLayout : ''
2018-04-18 14:23:16 +02:00
} ) ;
}
2018-07-23 12:03:56 +02:00
axios _1 . default . post ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _RECORDINGS + OpenVidu . API _RECORDINGS _START , data , {
2018-03-14 18:48:29 +01:00
headers : {
2018-07-23 12:03:56 +02:00
'Authorization' : OpenVidu . basicAuth ,
2018-06-04 16:03:03 +02:00
'Content-Type' : 'application/json'
}
} )
. then ( function ( res ) {
if ( res . status === 200 ) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
2018-07-23 12:03:56 +02:00
var r _1 = new Recording _1 . Recording ( res . data ) ;
var activeSession = _this . activeSessions . find ( function ( s ) { return s . sessionId === r _1 . sessionId ; } ) ;
if ( ! ! activeSession ) {
activeSession . recording = true ;
}
else {
console . warn ( "No active session found for sessionId '" + r _1 . sessionId + "'. This instance of OpenVidu Node Client didn't create this session" ) ;
}
resolve ( r _1 ) ;
2018-06-04 16:03:03 +02:00
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
2018-03-14 18:48:29 +01:00
} ) ;
} ) ;
2018-01-27 19:39:49 +01:00
} ;
2018-04-24 15:42:23 +02:00
/ * *
* Stops the recording of a [ [ Session ] ]
*
* @ param recordingId The ` id ` property of the [ [ Recording ] ] you want to stop
*
* @ returns A Promise that is resolved to the [ [ Recording ] ] if it successfully stopped and rejected with an Error object if not . This Error object has as ` message ` property with the following values :
* - ` 404 ` : no recording exists for the passed ` recordingId `
* - ` 406 ` : recording has ` starting ` status . Wait until ` started ` status before stopping the recording
* /
2018-03-14 18:48:29 +01:00
OpenVidu . prototype . stopRecording = function ( recordingId ) {
var _this = this ;
return new Promise ( function ( resolve , reject ) {
2018-07-23 12:03:56 +02:00
axios _1 . default . post ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _RECORDINGS + OpenVidu . API _RECORDINGS _STOP + '/' + recordingId , undefined , {
2018-03-14 18:48:29 +01:00
headers : {
2018-07-23 12:03:56 +02:00
'Authorization' : OpenVidu . basicAuth ,
2018-03-14 18:48:29 +01:00
'Content-Type' : 'application/x-www-form-urlencoded'
}
2018-06-04 16:03:03 +02:00
} )
. then ( function ( res ) {
if ( res . status === 200 ) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
2018-07-23 12:03:56 +02:00
var r _2 = new Recording _1 . Recording ( res . data ) ;
var activeSession = _this . activeSessions . find ( function ( s ) { return s . sessionId === r _2 . sessionId ; } ) ;
if ( ! ! activeSession ) {
activeSession . recording = false ;
}
else {
console . warn ( "No active session found for sessionId '" + r _2 . sessionId + "'. This instance of OpenVidu Node Client didn't create this session" ) ;
}
resolve ( r _2 ) ;
2018-06-04 16:03:03 +02:00
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
2018-07-23 12:03:56 +02:00
// The request was made but no response was received `error.request` is an instance of XMLHttpRequest
// in the browser and an instance of http.ClientRequest in node.js
2018-06-04 16:03:03 +02:00
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
2018-03-14 18:48:29 +01:00
} ) ;
} ) ;
2017-06-10 01:44:31 +02:00
} ;
2018-04-24 15:42:23 +02:00
/ * *
* Gets an existing [ [ Recording ] ]
*
* @ param recordingId The ` id ` property of the [ [ Recording ] ] you want to retrieve
*
* @ returns A Promise that is resolved to the [ [ Recording ] ] if it successfully stopped and rejected with an Error object if not . This Error object has as ` message ` property with the following values :
* - ` 404 ` : no recording exists for the passed ` recordingId `
* /
2018-03-14 18:48:29 +01:00
OpenVidu . prototype . getRecording = function ( recordingId ) {
return new Promise ( function ( resolve , reject ) {
2018-07-23 12:03:56 +02:00
axios _1 . default . get ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _RECORDINGS + '/' + recordingId , {
2018-03-14 18:48:29 +01:00
headers : {
2018-07-23 12:03:56 +02:00
'Authorization' : OpenVidu . basicAuth ,
2018-03-14 18:48:29 +01:00
'Content-Type' : 'application/x-www-form-urlencoded'
}
2018-06-04 16:03:03 +02:00
} )
. then ( function ( res ) {
if ( res . status === 200 ) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve ( new Recording _1 . Recording ( res . data ) ) ;
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
2018-03-14 18:48:29 +01:00
} ) ;
} ) ;
} ;
2018-04-24 15:42:23 +02:00
/ * *
* Lists all existing recordings
*
* @ returns A Promise that is resolved to an array with all existing recordings
* /
2018-03-14 18:48:29 +01:00
OpenVidu . prototype . listRecordings = function ( ) {
return new Promise ( function ( resolve , reject ) {
2018-07-23 12:03:56 +02:00
axios _1 . default . get ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _RECORDINGS , {
2018-03-14 18:48:29 +01:00
headers : {
2018-07-23 12:03:56 +02:00
Authorization : OpenVidu . basicAuth
2018-03-14 18:48:29 +01:00
}
2018-06-04 16:03:03 +02:00
} )
. then ( function ( res ) {
if ( res . status === 200 ) {
// SUCCESS response from openvidu-server (JSON arrays of recordings in JSON format). Resolve list of new recordings
var recordingArray = [ ] ;
var responseItems = res . data . items ;
for ( var _i = 0 , responseItems _1 = responseItems ; _i < responseItems _1 . length ; _i ++ ) {
var item = responseItems _1 [ _i ] ;
recordingArray . push ( new Recording _1 . Recording ( item ) ) ;
2018-03-14 18:48:29 +01:00
}
2018-06-04 16:03:03 +02:00
resolve ( recordingArray ) ;
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
2018-03-14 18:48:29 +01:00
} ) ;
} ) ;
} ;
2018-04-24 15:42:23 +02:00
/ * *
* Deletes a [ [ Recording ] ] . The recording must have status ` stopped ` or ` available `
*
* @ param recordingId
*
* @ returns A Promise that is resolved if the Recording was successfully deleted and rejected with an Error object if not . This Error object has as ` message ` property with the following values :
* - ` 404 ` : no recording exists for the passed ` recordingId `
* - ` 409 ` : the recording has ` started ` status . Stop it before deletion
* /
2018-03-14 18:48:29 +01:00
OpenVidu . prototype . deleteRecording = function ( recordingId ) {
return new Promise ( function ( resolve , reject ) {
2018-07-23 12:03:56 +02:00
axios _1 . default . delete ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _RECORDINGS + '/' + recordingId , {
2018-03-14 18:48:29 +01:00
headers : {
2018-07-23 12:03:56 +02:00
'Authorization' : OpenVidu . basicAuth ,
2018-03-14 18:48:29 +01:00
'Content-Type' : 'application/x-www-form-urlencoded'
}
2018-06-04 16:03:03 +02:00
} )
. then ( function ( res ) {
if ( res . status === 204 ) {
// SUCCESS response from openvidu-server. Resolve undefined
resolve ( undefined ) ;
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
2018-03-14 18:48:29 +01:00
} ) ;
} ) ;
} ;
2018-07-23 12:03:56 +02:00
/ * *
* Updates every property of every active Session with the current status they have in OpenVidu Server .
* After calling this method you can access the updated array of active sessions in [ [ activeSessions ] ]
*
* @ returns A promise resolved to true if any Session status has changed with respect to the server , or to false if not .
* This applies to any property or sub - property of any of the sessions locally stored in OpenVidu Node Client
* /
OpenVidu . prototype . fetch = function ( ) {
var _this = this ;
return new Promise ( function ( resolve , reject ) {
axios _1 . default . get ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _SESSIONS , {
headers : {
Authorization : OpenVidu . basicAuth
}
} )
. then ( function ( res ) {
if ( res . status === 200 ) {
// Array to store fetched sessionIds and later remove closed sessions
var fetchedSessionIds _1 = [ ] ;
// Boolean to store if any Session has changed
var hasChanged _1 = false ;
res . data . content . forEach ( function ( session ) {
fetchedSessionIds _1 . push ( session . sessionId ) ;
2018-10-01 11:17:05 +02:00
var sessionIndex = - 1 ;
var storedSession = _this . activeSessions . find ( function ( s , index ) {
if ( s . sessionId === session . sessionId ) {
sessionIndex = index ;
return true ;
}
else {
return false ;
}
} ) ;
2018-07-23 12:03:56 +02:00
if ( ! ! storedSession ) {
2018-10-01 11:17:05 +02:00
var fetchedSession = new Session _1 . Session ( ) . resetSessionWithJson ( session ) ;
var changed = ! storedSession . equalTo ( fetchedSession ) ;
if ( changed ) {
storedSession = fetchedSession ;
_this . activeSessions [ sessionIndex ] = storedSession ;
}
2018-07-23 12:03:56 +02:00
console . log ( "Available session '" + storedSession . sessionId + "' info fetched. Any change: " + changed ) ;
hasChanged _1 = hasChanged _1 || changed ;
}
else {
_this . activeSessions . push ( new Session _1 . Session ( session ) ) ;
console . log ( "New session '" + session . sessionId + "' info fetched" ) ;
hasChanged _1 = true ;
}
} ) ;
// Remove closed sessions from activeSessions array
_this . activeSessions = _this . activeSessions . filter ( function ( session ) {
if ( fetchedSessionIds _1 . includes ( session . sessionId ) ) {
return true ;
}
else {
console . log ( "Removing closed session '" + session . sessionId + "'" ) ;
hasChanged _1 = true ;
return false ;
}
} ) ;
console . log ( 'Active sessions info fetched: ' , fetchedSessionIds _1 ) ;
resolve ( hasChanged _1 ) ;
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
} ) ;
} ) ;
} ;
2018-10-01 11:17:05 +02:00
/ * *
* @ hidden
* /
OpenVidu . prototype . fetchWebRtc = function ( ) {
var _this = this ;
// tslint:disable:no-string-literal
var addWebRtcStatsToConnections = function ( connection , connectionsExtendedInfo ) {
var connectionExtended = connectionsExtendedInfo . find ( function ( c ) { return c . connectionId === connection . connectionId ; } ) ;
if ( ! ! connectionExtended ) {
var publisherArray _1 = [ ] ;
connection . publishers . forEach ( function ( pub ) {
var publisherExtended = connectionExtended . publishers . find ( function ( p ) { return p . streamId === pub . streamId ; } ) ;
var pubAux = { } ;
// Standard properties
pubAux [ 'streamId' ] = pub . streamId ;
pubAux [ 'createdAt' ] = pub . createdAt ;
var mediaOptions = {
audioActive : pub . audioActive ,
videoActive : pub . videoActive ,
hasAudio : pub . hasAudio ,
hasVideo : pub . hasVideo ,
typeOfVideo : pub . typeOfVideo ,
frameRate : pub . frameRate ,
videoDimensions : pub . videoDimensions
} ;
pubAux [ 'mediaOptions' ] = mediaOptions ;
var newPublisher = new Publisher _1 . Publisher ( pubAux ) ;
// WebRtc properties
newPublisher [ 'webRtc' ] = {
kms : {
events : publisherExtended . events ,
localCandidate : publisherExtended . localCandidate ,
remoteCandidate : publisherExtended . remoteCandidate ,
receivedCandidates : publisherExtended . receivedCandidates ,
webrtcTagName : publisherExtended . webrtcTagName
}
} ;
newPublisher [ 'localCandidatePair' ] = parseRemoteCandidatePair ( newPublisher [ 'webRtc' ] . kms . remoteCandidate ) ;
if ( ! ! publisherExtended . serverStats ) {
newPublisher [ 'webRtc' ] . kms . serverStats = publisherExtended . serverStats ;
}
publisherArray _1 . push ( newPublisher ) ;
} ) ;
var subscriberArray _1 = [ ] ;
connection . subscribers . forEach ( function ( sub ) {
var subscriberExtended = connectionExtended . subscribers . find ( function ( s ) { return s . streamId === sub ; } ) ;
var subAux = { } ;
// Standard properties
subAux [ 'streamId' ] = sub ;
subAux [ 'publisher' ] = subscriberExtended . publisher ;
// WebRtc properties
subAux [ 'createdAt' ] = subscriberExtended . createdAt ;
subAux [ 'webRtc' ] = {
kms : {
events : subscriberExtended . events ,
localCandidate : subscriberExtended . localCandidate ,
remoteCandidate : subscriberExtended . remoteCandidate ,
receivedCandidates : subscriberExtended . receivedCandidates ,
webrtcTagName : subscriberExtended . webrtcTagName
}
} ;
subAux [ 'localCandidatePair' ] = parseRemoteCandidatePair ( subAux [ 'webRtc' ] . kms . remoteCandidate ) ;
if ( ! ! subscriberExtended . serverStats ) {
subAux [ 'webRtc' ] . kms . serverStats = subscriberExtended . serverStats ;
}
subscriberArray _1 . push ( subAux ) ;
} ) ;
connection . publishers = publisherArray _1 ;
connection . subscribers = subscriberArray _1 ;
}
} ;
var parseRemoteCandidatePair = function ( candidateStr ) {
if ( ! candidateStr ) {
return 'ERROR: No remote candidate available' ;
}
var array = candidateStr . split ( /\s+/ ) ;
return {
portNumber : array [ 5 ] ,
ipAddress : array [ 4 ] ,
transport : array [ 2 ] . toLowerCase ( ) ,
candidateType : array [ 7 ] ,
priority : array [ 3 ] ,
raw : candidateStr
} ;
} ;
return new Promise ( function ( resolve , reject ) {
axios _1 . default . get ( 'https://' + OpenVidu . hostname + ':' + OpenVidu . port + OpenVidu . API _SESSIONS + '?webRtcStats=true' , {
headers : {
Authorization : OpenVidu . basicAuth
}
} )
. then ( function ( res ) {
if ( res . status === 200 ) {
// Array to store fetched sessionIds and later remove closed sessions
var fetchedSessionIds _2 = [ ] ;
// Boolean to store if any Session has changed
var hasChanged _2 = false ;
res . data . content . forEach ( function ( session ) {
fetchedSessionIds _2 . push ( session . sessionId ) ;
var sessionIndex = - 1 ;
var storedSession = _this . activeSessions . find ( function ( s , index ) {
if ( s . sessionId === session . sessionId ) {
sessionIndex = index ;
return true ;
}
else {
return false ;
}
} ) ;
if ( ! ! storedSession ) {
var fetchedSession = new Session _1 . Session ( ) . resetSessionWithJson ( session ) ;
fetchedSession . activeConnections . forEach ( function ( connection ) {
addWebRtcStatsToConnections ( connection , session . connections . content ) ;
} ) ;
var changed _1 = ! storedSession . equalTo ( fetchedSession ) ;
if ( ! changed _1 ) { // Check if server webrtc information has changed in any Publisher object (Session.equalTo does not check Publisher.webRtc auxiliary object)
fetchedSession . activeConnections . forEach ( function ( connection , index1 ) {
for ( var index2 = 0 ; ( index2 < connection [ 'publishers' ] . length && ! changed _1 ) ; index2 ++ ) {
changed _1 = changed _1 || JSON . stringify ( connection [ 'publishers' ] [ index2 ] [ 'webRtc' ] ) !== JSON . stringify ( storedSession . activeConnections [ index1 ] [ 'publishers' ] [ index2 ] [ 'webRtc' ] ) ;
}
} ) ;
}
if ( changed _1 ) {
storedSession = fetchedSession ;
_this . activeSessions [ sessionIndex ] = storedSession ;
}
console . log ( "Available session '" + storedSession . sessionId + "' info fetched. Any change: " + changed _1 ) ;
hasChanged _2 = hasChanged _2 || changed _1 ;
}
else {
var newSession = new Session _1 . Session ( session ) ;
newSession . activeConnections . forEach ( function ( connection ) {
addWebRtcStatsToConnections ( connection , session . connections . content ) ;
} ) ;
_this . activeSessions . push ( newSession ) ;
console . log ( "New session '" + session . sessionId + "' info fetched" ) ;
hasChanged _2 = true ;
}
} ) ;
// Remove closed sessions from activeSessions array
_this . activeSessions = _this . activeSessions . filter ( function ( session ) {
if ( fetchedSessionIds _2 . includes ( session . sessionId ) ) {
return true ;
}
else {
console . log ( "Removing closed session '" + session . sessionId + "'" ) ;
hasChanged _2 = true ;
return false ;
}
} ) ;
console . log ( 'Active sessions info fetched: ' , fetchedSessionIds _2 ) ;
resolve ( hasChanged _2 ) ;
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject ( new Error ( res . status . toString ( ) ) ) ;
}
} ) . catch ( function ( error ) {
if ( error . response ) {
// The request was made and the server responded with a status code (not 2xx)
reject ( new Error ( error . response . status . toString ( ) ) ) ;
}
else if ( error . request ) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console . error ( error . request ) ;
}
else {
// Something happened in setting up the request that triggered an Error
console . error ( 'Error' , error . message ) ;
}
} ) ;
} ) ;
} ;
// tslint:enable:no-string-literal
2018-03-14 18:48:29 +01:00
OpenVidu . prototype . getBasicAuth = function ( secret ) {
2018-06-04 16:03:03 +02:00
return 'Basic ' + this . Buffer ( 'OPENVIDUAPP:' + secret ) . toString ( 'base64' ) ;
2018-03-14 18:48:29 +01:00
} ;
OpenVidu . prototype . setHostnameAndPort = function ( ) {
var urlSplitted = this . urlOpenViduServer . split ( ':' ) ;
2018-05-03 11:48:57 +02:00
if ( urlSplitted . length === 3 ) { // URL has format: http:// + hostname + :port
2018-07-23 12:03:56 +02:00
OpenVidu . hostname = this . urlOpenViduServer . split ( ':' ) [ 1 ] . replace ( /\//g , '' ) ;
OpenVidu . port = parseInt ( this . urlOpenViduServer . split ( ':' ) [ 2 ] . replace ( /\//g , '' ) ) ;
2018-03-14 18:48:29 +01:00
}
2018-05-03 11:48:57 +02:00
else if ( urlSplitted . length === 2 ) { // URL has format: hostname + :port
2018-07-23 12:03:56 +02:00
OpenVidu . hostname = this . urlOpenViduServer . split ( ':' ) [ 0 ] . replace ( /\//g , '' ) ;
OpenVidu . port = parseInt ( this . urlOpenViduServer . split ( ':' ) [ 1 ] . replace ( /\//g , '' ) ) ;
2018-03-14 18:48:29 +01:00
}
else {
console . error ( "URL format incorrect: it must contain hostname and port (current value: '" + this . urlOpenViduServer + "')" ) ;
}
} ;
2018-07-23 12:03:56 +02:00
/ * *
* @ hidden
* /
OpenVidu . getActiveSessions = function ( ) {
return this . o . activeSessions ;
} ;
/ * *
* @ hidden
* /
2018-03-14 18:48:29 +01:00
OpenVidu . API _RECORDINGS = '/api/recordings' ;
2018-07-23 12:03:56 +02:00
/ * *
* @ hidden
* /
2018-03-14 18:48:29 +01:00
OpenVidu . API _RECORDINGS _START = '/start' ;
2018-07-23 12:03:56 +02:00
/ * *
* @ hidden
* /
2018-03-14 18:48:29 +01:00
OpenVidu . API _RECORDINGS _STOP = '/stop' ;
2018-07-23 12:03:56 +02:00
/ * *
* @ hidden
* /
OpenVidu . API _SESSIONS = '/api/sessions' ;
/ * *
* @ hidden
* /
OpenVidu . API _TOKENS = '/api/tokens' ;
2017-06-10 01:44:31 +02:00
return OpenVidu ;
} ( ) ) ;
exports . OpenVidu = OpenVidu ;
//# sourceMappingURL=OpenVidu.js.map