2017-10-12 22:49:50 +08:00
import BaseAudioBridge from './base' ;
2018-07-12 06:03:56 +08:00
import logger from '/imports/startup/client/logger' ;
2020-10-01 21:16:48 +08:00
import {
fetchWebRTCMappedStunTurnServers ,
2020-10-03 03:15:54 +08:00
getMappedFallbackStun ,
2020-10-01 21:16:48 +08:00
} from '/imports/utils/fetchStunTurnServers' ;
2019-06-04 02:54:30 +08:00
import {
2019-12-19 04:49:35 +08:00
isUnifiedPlan ,
toUnifiedPlan ,
toPlanB ,
stripMDnsCandidates ,
2021-06-30 01:47:59 +08:00
filterValidIceCandidates ,
2019-12-19 04:49:35 +08:00
analyzeSdp ,
logSelectedCandidate ,
2023-02-01 01:04:17 +08:00
forceDisableStereo ,
2019-06-04 02:54:30 +08:00
} from '/imports/utils/sdpUtils' ;
2021-04-01 19:14:24 +08:00
import browserInfo from '/imports/utils/browserInfo' ;
2022-02-01 03:30:38 +08:00
import {
getAudioSessionNumber ,
getAudioConstraints ,
filterSupportedConstraints ,
2022-09-16 01:48:26 +08:00
doGUM ,
2024-04-17 06:39:29 +08:00
stereoUnsupported ,
2022-02-01 03:30:38 +08:00
} from '/imports/api/audio/client/bridge/service' ;
2017-08-01 04:54:18 +08:00
2017-10-18 03:16:42 +08:00
const MEDIA = Meteor . settings . public . media ;
const MEDIA _TAG = MEDIA . mediaTag ;
2017-10-27 01:14:56 +08:00
const CALL _HANGUP _TIMEOUT = MEDIA . callHangupTimeout ;
const CALL _HANGUP _MAX _RETRIES = MEDIA . callHangupMaximumRetries ;
2020-11-10 20:41:48 +08:00
const SIPJS _HACK _VIA _WS = MEDIA . sipjsHackViaWs ;
2022-05-06 21:33:23 +08:00
const SIPJS _ALLOW _MDNS = MEDIA . sipjsAllowMdns || false ;
2019-06-13 05:01:20 +08:00
const IPV4 _FALLBACK _DOMAIN = Meteor . settings . public . app . ipv4FallbackDomain ;
2019-11-23 05:48:46 +08:00
const CALL _CONNECT _TIMEOUT = 20000 ;
2019-04-13 06:23:22 +08:00
const ICE _NEGOTIATION _TIMEOUT = 20000 ;
2022-01-11 05:56:29 +08:00
const USER _AGENT _RECONNECTION _ATTEMPTS = MEDIA . audioReconnectionAttempts || 3 ;
const USER _AGENT _RECONNECTION _DELAY _MS = MEDIA . audioReconnectionDelay || 5000 ;
const USER _AGENT _CONNECTION _TIMEOUT _MS = MEDIA . audioConnectionTimeout || 5000 ;
2020-10-24 19:58:25 +08:00
const ICE _GATHERING _TIMEOUT = MEDIA . iceGatheringTimeout || 5000 ;
2020-12-02 02:23:14 +08:00
const BRIDGE _NAME = 'sip' ;
2020-12-11 10:48:01 +08:00
const WEBSOCKET _KEEP _ALIVE _INTERVAL = MEDIA . websocketKeepAliveInterval || 0 ;
const WEBSOCKET _KEEP _ALIVE _DEBOUNCE = MEDIA . websocketKeepAliveDebounce || 10 ;
2020-12-11 11:31:10 +08:00
const TRACE _SIP = MEDIA . traceSip || false ;
2021-03-12 11:04:55 +08:00
const SDP _SEMANTICS = MEDIA . sdpSemantics ;
2021-12-09 08:08:33 +08:00
const FORCE _RELAY = MEDIA . forceRelay ;
2019-11-14 07:57:42 +08:00
2020-04-27 19:52:46 +08:00
const UA _SERVER _VERSION = Meteor . settings . public . app . bbbServerVersion ;
const UA _CLIENT _VERSION = Meteor . settings . public . app . html5ClientBuild ;
2021-03-17 22:30:07 +08:00
2020-03-15 11:50:39 +08:00
/ * *
* Get error code from SIP . js websocket messages .
* /
const getErrorCode = ( error ) => {
try {
if ( ! error ) return error ;
const match = error . message . match ( /code: \d+/g ) ;
const _codeArray = match [ 0 ] . split ( ':' ) ;
return parseInt ( _codeArray [ 1 ] . trim ( ) , 10 ) ;
} catch ( e ) {
return 0 ;
}
} ;
2019-06-13 05:01:20 +08:00
class SIPSession {
2019-11-14 07:57:42 +08:00
constructor ( user , userData , protocol , hostname ,
2019-10-26 10:57:49 +08:00
baseCallStates , baseErrorCodes , reconnectAttempt ) {
2019-06-13 05:01:20 +08:00
this . user = user ;
this . userData = userData ;
this . protocol = protocol ;
this . hostname = hostname ;
this . baseCallStates = baseCallStates ;
2019-06-25 04:41:19 +08:00
this . baseErrorCodes = baseErrorCodes ;
2019-10-26 10:57:49 +08:00
this . reconnectAttempt = reconnectAttempt ;
2020-09-26 07:11:44 +08:00
this . currentSession = null ;
this . remoteStream = null ;
2022-01-26 04:49:38 +08:00
this . bridgeName = BRIDGE _NAME ;
2021-04-02 02:53:43 +08:00
this . _inputDeviceId = null ;
2021-03-17 22:30:07 +08:00
this . _outputDeviceId = null ;
2020-09-26 07:11:44 +08:00
this . _hangupFlag = false ;
this . _reconnecting = false ;
this . _currentSessionState = null ;
2021-09-21 22:22:05 +08:00
this . _ignoreCallState = false ;
2022-04-12 06:18:40 +08:00
this . mediaStreamFactory = this . mediaStreamFactory . bind ( this )
2017-09-29 21:38:10 +08:00
}
2017-07-24 22:15:46 +08:00
2021-01-30 06:05:51 +08:00
get inputStream ( ) {
if ( this . currentSession && this . currentSession . sessionDescriptionHandler ) {
return this . currentSession . sessionDescriptionHandler . localMediaStream ;
}
return null ;
}
2021-04-16 21:45:40 +08:00
/ * *
* Set the input stream for the peer that represents the current session .
* Internally , this will call the sender ' s replaceTrack function .
* @ param { MediaStream } stream The MediaStream object to be used as input
* stream
* @ return { Promise } A Promise that is resolved with the
* MediaStream object that was set .
* /
2022-08-20 01:22:42 +08:00
setInputStream ( stream ) {
if ( ! this . currentSession ? . sessionDescriptionHandler ) return null ;
2021-04-16 21:45:40 +08:00
2022-08-20 01:22:42 +08:00
return this . currentSession . sessionDescriptionHandler . setLocalMediaStream ( stream ) ;
2021-03-17 22:30:07 +08:00
}
get inputDeviceId ( ) {
if ( ! this . _inputDeviceId ) {
const stream = this . inputStream ;
if ( stream ) {
const track = stream . getAudioTracks ( ) . find (
2021-04-16 21:45:40 +08:00
( t ) => t . getSettings ( ) . deviceId ,
2021-03-17 22:30:07 +08:00
) ;
if ( track && ( typeof track . getSettings === 'function' ) ) {
const { deviceId } = track . getSettings ( ) ;
2021-04-27 09:02:17 +08:00
this . _inputDeviceId = deviceId ;
2021-03-17 22:30:07 +08:00
}
}
}
2021-04-27 09:02:17 +08:00
return this . _inputDeviceId ;
2021-03-17 22:30:07 +08:00
}
set inputDeviceId ( deviceId ) {
this . _inputDeviceId = deviceId ;
}
get outputDeviceId ( ) {
if ( ! this . _outputDeviceId ) {
const audioElement = document . querySelector ( MEDIA _TAG ) ;
2021-04-02 02:53:43 +08:00
if ( audioElement ) {
2021-04-27 09:02:17 +08:00
this . _outputDeviceId = audioElement . sinkId ;
2021-03-17 22:30:07 +08:00
}
}
2021-04-27 09:02:17 +08:00
return this . _outputDeviceId ;
2021-03-17 22:30:07 +08:00
}
set outputDeviceId ( deviceId ) {
this . _outputDeviceId = deviceId ;
}
2021-09-21 22:22:05 +08:00
/ * *
* This _ignoreCallState flag is set to true when we want to ignore SIP ' s
* call state retrieved directly from FreeSWITCH ESL , when doing some checks
* ( for example , when checking if call stopped ) .
* We need to ignore this , for example , when moderator is in
* breakout audio transfer ( "Join Audio" button in breakout panel ) : in this
* case , we will monitor moderator ' s lifecycle in audio conference by
* using the SIP state taken from SIP . js only ( ignoring the ESL ' s call state ) .
* @ param { boolean } value true to ignore call state , false otherwise .
* /
set ignoreCallState ( value ) {
this . _ignoreCallState = value ;
}
get ignoreCallState ( ) {
return this . _ignoreCallState ;
}
2021-04-23 02:03:43 +08:00
joinAudio ( {
isListenOnly ,
extension ,
inputDeviceId ,
outputDeviceId ,
2021-06-30 01:47:59 +08:00
validIceCandidates ,
2022-04-12 06:18:40 +08:00
inputStream ,
2021-04-23 02:03:43 +08:00
} , managerCallback ) {
2017-10-05 04:49:11 +08:00
return new Promise ( ( resolve , reject ) => {
2017-10-12 22:49:50 +08:00
const callExtension = extension ? ` ${ extension } ${ this . userData . voiceBridge } ` : this . userData . voiceBridge ;
2017-07-24 22:15:46 +08:00
2021-09-21 22:22:05 +08:00
this . ignoreCallState = false ;
2017-09-29 21:38:10 +08:00
const callback = ( message ) => {
2019-06-13 05:01:20 +08:00
// There will sometimes we erroneous errors put out like timeouts and improper shutdowns,
// but only the first error ever matters
if ( this . alreadyErrored ) {
2019-06-29 05:45:50 +08:00
logger . info ( {
logCode : 'sip_js_absorbing_callback_message' ,
extraInfo : { message } ,
} , 'Absorbing a redundant callback message.' ) ;
2019-06-13 05:01:20 +08:00
return ;
}
if ( message . status === this . baseCallStates . failed ) {
this . alreadyErrored = true ;
}
2017-10-12 05:04:10 +08:00
managerCallback ( message ) . then ( resolve ) ;
2017-09-30 04:42:34 +08:00
} ;
2017-07-24 22:15:46 +08:00
2017-10-12 05:30:38 +08:00
this . callback = callback ;
2019-06-13 05:01:20 +08:00
// If there's an extension passed it means that we're joining the echo test first
this . inEchoTest = ! ! extension ;
2021-06-30 01:47:59 +08:00
this . validIceCandidates = validIceCandidates ;
2021-04-23 02:03:43 +08:00
return this . doCall ( {
callExtension ,
isListenOnly ,
inputDeviceId ,
outputDeviceId ,
2022-04-12 06:18:40 +08:00
inputStream ,
2021-04-23 02:03:43 +08:00
} ) . catch ( ( reason ) => {
reject ( reason ) ;
} ) ;
2017-09-30 04:42:34 +08:00
} ) ;
2017-07-24 22:15:46 +08:00
}
2020-06-13 05:13:49 +08:00
async getIceServers ( sessionToken ) {
2020-05-21 12:20:46 +08:00
try {
2020-10-01 21:16:48 +08:00
const iceServers = await fetchWebRTCMappedStunTurnServers ( sessionToken ) ;
2020-05-21 12:20:46 +08:00
return iceServers ;
} catch ( error ) {
logger . error ( {
logCode : 'sip_js_fetchstunturninfo_error' ,
extraInfo : {
errorCode : error . code ,
errorMessage : error . message ,
2020-06-13 05:13:49 +08:00
callerIdName : this . user . callerIdName ,
2020-05-21 12:20:46 +08:00
} ,
} , 'Full audio bridge failed to fetch STUN/TURN info' ) ;
2020-10-03 03:15:54 +08:00
return getMappedFallbackStun ( ) ;
2020-05-21 12:20:46 +08:00
}
}
2017-10-20 18:11:51 +08:00
doCall ( options ) {
const {
isListenOnly ,
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
inputDeviceId ,
2021-04-23 02:03:43 +08:00
outputDeviceId ,
2022-04-12 06:18:40 +08:00
inputStream ,
2017-10-20 18:11:51 +08:00
} = options ;
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
this . inputDeviceId = inputDeviceId ;
2021-04-23 02:03:43 +08:00
this . outputDeviceId = outputDeviceId ;
2022-04-12 06:18:40 +08:00
// If a valid MediaStream was provided it means it was preloaded somewhere
// else - let's use it so we don't call gUM needlessly
if ( inputStream && inputStream . active ) this . preloadedInputStream = inputStream ;
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
2017-10-20 18:11:51 +08:00
const {
userId ,
name ,
sessionToken ,
} = this . user ;
const callerIdName = [
2019-11-14 07:57:42 +08:00
` ${ userId } _ ${ getAudioSessionNumber ( ) } ` ,
2017-10-20 18:11:51 +08:00
'bbbID' ,
2020-11-20 10:11:39 +08:00
isListenOnly ? ` LISTENONLY- ${ name } ` : name ,
2019-02-22 04:53:39 +08:00
] . join ( '-' ) . replace ( /"/g , "'" ) ;
2017-10-20 18:11:51 +08:00
2019-02-22 04:53:39 +08:00
this . user . callerIdName = callerIdName ;
2017-10-20 18:11:51 +08:00
this . callOptions = options ;
2020-05-21 12:20:46 +08:00
return this . getIceServers ( sessionToken )
2017-11-18 03:01:52 +08:00
. then ( this . createUserAgent . bind ( this ) )
2020-09-26 07:11:44 +08:00
. then ( this . inviteUserAgent . bind ( this ) ) ;
2017-10-20 18:11:51 +08:00
}
2020-09-26 07:11:44 +08:00
/ * *
*
* sessionSupportRTPPayloadDtmf
* tells if browser support RFC4733 DTMF .
2024-03-18 21:58:53 +08:00
* Safari 13 doesn ' t support it yet
2020-09-26 07:11:44 +08:00
* /
sessionSupportRTPPayloadDtmf ( session ) {
try {
const sessionDescriptionHandler = session
? session . sessionDescriptionHandler
: this . currentSession . sessionDescriptionHandler ;
const senders = sessionDescriptionHandler . peerConnection . getSenders ( ) ;
return ! ! ( senders [ 0 ] . dtmf ) ;
} catch ( error ) {
return false ;
}
}
/ * *
* sendDtmf - send DTMF Tones using INFO message
*
* same as SimpleUser ' s dtmf
* /
sendDtmf ( tone ) {
const dtmf = tone ;
const duration = 2000 ;
const body = {
contentDisposition : 'render' ,
contentType : 'application/dtmf-relay' ,
content : ` Signal= ${ dtmf } \r \n Duration= ${ duration } ` ,
} ;
const requestOptions = { body } ;
return this . currentSession . info ( { requestOptions } ) ;
}
2017-09-29 21:38:10 +08:00
exitAudio ( ) {
2017-10-27 01:14:56 +08:00
return new Promise ( ( resolve , reject ) => {
let hangupRetries = 0 ;
2020-09-26 07:11:44 +08:00
this . _hangupFlag = false ;
2018-07-03 00:36:54 +08:00
2019-02-21 05:58:37 +08:00
this . userRequestedHangup = true ;
2020-01-10 07:50:10 +08:00
2017-10-27 01:14:56 +08:00
const tryHangup = ( ) => {
2020-09-26 07:11:44 +08:00
if ( this . _hangupFlag ) {
resolve ( ) ;
}
if ( ( this . currentSession
&& ( this . currentSession . state === SIP . SessionState . Terminated ) )
|| ( this . userAgent && ( ! this . userAgent . isConnected ( ) ) ) ) {
this . _hangupFlag = true ;
2019-02-21 05:58:37 +08:00
return resolve ( ) ;
}
2020-09-26 07:11:44 +08:00
if ( this . currentSession
2020-03-15 11:50:39 +08:00
&& ( ( this . currentSession . state === SIP . SessionState . Establishing ) ) ) {
this . currentSession . cancel ( ) . then ( ( ) => {
this . _hangupFlag = true ;
return resolve ( ) ;
} ) ;
}
if ( this . currentSession
&& ( ( this . currentSession . state === SIP . SessionState . Established ) ) ) {
2020-09-26 07:11:44 +08:00
this . currentSession . bye ( ) . then ( ( ) => {
this . _hangupFlag = true ;
return resolve ( ) ;
} ) ;
}
if ( this . userAgent && this . userAgent . isConnected ( ) ) {
this . userAgent . stop ( ) ;
2020-03-15 11:50:39 +08:00
window . removeEventListener ( 'beforeunload' , this . onBeforeUnload ) ;
2020-09-26 07:11:44 +08:00
}
2019-06-13 05:01:20 +08:00
2017-10-27 01:14:56 +08:00
hangupRetries += 1 ;
setTimeout ( ( ) => {
if ( hangupRetries > CALL _HANGUP _MAX _RETRIES ) {
this . callback ( {
status : this . baseCallStates . failed ,
2019-02-21 05:58:37 +08:00
error : 1006 ,
2017-10-27 01:14:56 +08:00
bridgeError : 'Timeout on call hangup' ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2017-10-27 01:14:56 +08:00
} ) ;
return reject ( this . baseErrorCodes . REQUEST _TIMEOUT ) ;
}
2020-09-26 07:11:44 +08:00
if ( ! this . _hangupFlag ) return tryHangup ( ) ;
2017-10-27 01:14:56 +08:00
return resolve ( ) ;
} , CALL _HANGUP _TIMEOUT ) ;
} ;
return tryHangup ( ) ;
2017-09-30 04:42:34 +08:00
} ) ;
}
2020-10-29 01:38:33 +08:00
stopUserAgent ( ) {
if ( this . userAgent && ( typeof this . userAgent . stop === 'function' ) ) {
2020-03-15 11:50:39 +08:00
return this . userAgent . stop ( ) ;
}
return Promise . resolve ( ) ;
}
2020-10-29 01:38:33 +08:00
onBeforeUnload ( ) {
this . userRequestedHangup = true ;
return this . stopUserAgent ( ) ;
}
2022-04-12 06:18:40 +08:00
mediaStreamFactory ( constraints ) {
if ( this . preloadedInputStream && this . preloadedInputStream . active ) {
return Promise . resolve ( this . preloadedInputStream ) ;
}
2024-03-18 21:58:53 +08:00
// The rest of this mimics the default factory behavior.
2022-04-12 06:18:40 +08:00
if ( ! constraints . audio && ! constraints . video ) {
return Promise . resolve ( new MediaStream ( ) ) ;
}
2022-09-16 01:48:26 +08:00
return doGUM ( constraints , true ) ;
2022-04-12 06:18:40 +08:00
}
2020-10-01 21:16:48 +08:00
createUserAgent ( iceServers ) {
2017-09-30 04:42:34 +08:00
return new Promise ( ( resolve , reject ) => {
2020-01-10 07:50:10 +08:00
if ( this . userRequestedHangup === true ) reject ( ) ;
2017-10-18 03:16:42 +08:00
const {
hostname ,
protocol ,
} = this ;
const {
callerIdName ,
2020-07-30 00:10:17 +08:00
sessionToken ,
2017-10-18 03:16:42 +08:00
} = this . user ;
2020-06-13 05:13:49 +08:00
logger . debug ( { logCode : 'sip_js_creating_user_agent' , extraInfo : { callerIdName } } , 'Creating the user agent' ) ;
2019-02-21 05:58:37 +08:00
2019-06-13 05:01:20 +08:00
if ( this . userAgent && this . userAgent . isConnected ( ) ) {
if ( this . userAgent . configuration . hostPortParams === this . hostname ) {
2020-06-13 05:13:49 +08:00
logger . debug ( { logCode : 'sip_js_reusing_user_agent' , extraInfo : { callerIdName } } , 'Reusing the user agent' ) ;
2019-06-13 05:01:20 +08:00
resolve ( this . userAgent ) ;
return ;
}
2020-06-13 05:13:49 +08:00
logger . debug ( { logCode : 'sip_js_different_host_name' , extraInfo : { callerIdName } } , 'Different host name. need to kill' ) ;
2019-06-13 05:01:20 +08:00
}
2019-12-06 08:25:42 +08:00
const localSdpCallback = ( sdp ) => {
// For now we just need to call the utils function to parse and log the different pieces.
// In the future we're going to want to be tracking whether there were TURN candidates
// and IPv4 candidates to make informed decisions about what to do on fallbacks/reconnects.
analyzeSdp ( sdp ) ;
} ;
2019-12-19 04:49:35 +08:00
const remoteSdpCallback = ( sdp ) => {
// We have have to find the candidate that FS sends back to us to determine if the client
// is connecting with IPv4 or IPv6
const sdpInfo = analyzeSdp ( sdp , false ) ;
this . protocolIsIpv6 = sdpInfo . v6Info . found ;
} ;
2019-06-13 05:01:20 +08:00
let userAgentConnected = false ;
2020-07-30 00:10:17 +08:00
const token = ` sessionToken= ${ sessionToken } ` ;
2019-06-13 05:01:20 +08:00
2022-04-12 06:18:40 +08:00
// Create session description handler factory
const customSDHFactory = SIP . Web . defaultSessionDescriptionHandlerFactory ( this . mediaStreamFactory ) ;
2020-09-26 07:11:44 +08:00
this . userAgent = new SIP . UserAgent ( {
uri : SIP . UserAgent . makeURI ( ` sip: ${ encodeURIComponent ( callerIdName ) } @ ${ hostname } ` ) ,
transportOptions : {
server : ` ${ ( protocol === 'https:' ? 'wss://' : 'ws://' ) } ${ hostname } /ws? ${ token } ` ,
connectionTimeout : USER _AGENT _CONNECTION _TIMEOUT _MS ,
2020-12-11 10:48:01 +08:00
keepAliveInterval : WEBSOCKET _KEEP _ALIVE _INTERVAL ,
keepAliveDebounce : WEBSOCKET _KEEP _ALIVE _DEBOUNCE ,
2020-12-11 11:31:10 +08:00
traceSip : TRACE _SIP ,
2020-09-26 07:11:44 +08:00
} ,
2022-04-12 06:18:40 +08:00
sessionDescriptionHandlerFactory : customSDHFactory ,
2020-10-01 21:16:48 +08:00
sessionDescriptionHandlerFactoryOptions : {
peerConnectionConfiguration : {
iceServers ,
2021-03-12 11:04:55 +08:00
sdpSemantics : SDP _SEMANTICS ,
2021-12-09 08:08:33 +08:00
iceTransportPolicy : FORCE _RELAY ? 'relay' : undefined ,
2020-10-01 21:16:48 +08:00
} ,
} ,
2017-10-18 03:16:42 +08:00
displayName : callerIdName ,
2017-09-30 04:42:34 +08:00
register : false ,
2020-04-27 19:52:46 +08:00
userAgentString : ` BigBlueButton/ ${ UA _SERVER _VERSION } (HTML5, rv: ${ UA _CLIENT _VERSION } ) ${ window . navigator . userAgent } ` ,
2020-11-10 20:41:48 +08:00
hackViaWs : SIPJS _HACK _VIA _WS ,
2017-09-30 04:42:34 +08:00
} ) ;
2017-10-18 03:16:42 +08:00
const handleUserAgentConnection = ( ) => {
2020-03-15 11:50:39 +08:00
if ( ! userAgentConnected ) {
userAgentConnected = true ;
resolve ( this . userAgent ) ;
}
2017-10-18 03:16:42 +08:00
} ;
2019-02-21 05:58:37 +08:00
const handleUserAgentDisconnection = ( ) => {
2019-06-13 05:01:20 +08:00
if ( this . userAgent ) {
2020-03-15 11:50:39 +08:00
if ( this . userRequestedHangup ) {
userAgentConnected = false ;
return ;
}
2020-09-26 07:11:44 +08:00
let error ;
let bridgeError ;
if ( ! this . _reconnecting ) {
2020-03-15 11:50:39 +08:00
logger . info ( {
logCode : 'sip_js_session_ua_disconnected' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'User agent disconnected: trying to reconnect...'
2021-04-02 02:11:13 +08:00
+ ` (userHangup = ${ ! ! this . userRequestedHangup } ) ` ) ;
2020-09-26 07:11:44 +08:00
logger . info ( {
2020-03-15 11:50:39 +08:00
logCode : 'sip_js_session_ua_reconnecting' ,
2020-09-26 07:11:44 +08:00
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
2020-03-15 11:50:39 +08:00
} , 'User agent disconnected, reconnecting' ) ;
this . reconnect ( ) . then ( ( ) => {
logger . info ( {
logCode : 'sip_js_session_ua_reconnected' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
2024-03-18 21:58:53 +08:00
} , 'User agent successfully reconnected' ) ;
2020-03-15 11:50:39 +08:00
} ) . catch ( ( ) => {
if ( userAgentConnected ) {
error = 1001 ;
bridgeError = 'Websocket disconnected' ;
} else {
error = 1002 ;
bridgeError = 'Websocket failed to connect' ;
}
2020-10-29 01:38:33 +08:00
this . stopUserAgent ( ) ;
2020-03-15 11:50:39 +08:00
this . callback ( {
status : this . baseCallStates . failed ,
error ,
bridgeError ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2020-03-15 11:50:39 +08:00
} ) ;
reject ( this . baseErrorCodes . CONNECTION _ERROR ) ;
} ) ;
}
2019-02-21 05:58:37 +08:00
}
2020-09-26 07:11:44 +08:00
} ;
2019-02-21 05:58:37 +08:00
2020-09-26 07:11:44 +08:00
this . userAgent . transport . onConnect = handleUserAgentConnection ;
this . userAgent . transport . onDisconnect = handleUserAgentDisconnection ;
const preturn = this . userAgent . start ( ) . then ( ( ) => {
logger . info ( {
logCode : 'sip_js_session_ua_connected' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
2024-03-18 21:58:53 +08:00
} , 'User agent successfully connected' ) ;
2020-03-15 11:50:39 +08:00
window . addEventListener ( 'beforeunload' , this . onBeforeUnload . bind ( this ) ) ;
2020-09-26 07:11:44 +08:00
resolve ( ) ;
2020-03-15 11:50:39 +08:00
} ) . catch ( ( error ) => {
2020-09-26 07:11:44 +08:00
logger . info ( {
logCode : 'sip_js_session_ua_reconnecting' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'User agent failed to connect, reconnecting' ) ;
2020-03-15 11:50:39 +08:00
const code = getErrorCode ( error ) ;
2020-12-02 04:02:50 +08:00
// Websocket's 1006 is currently mapped to BBB's 1002
2020-03-15 11:50:39 +08:00
if ( code === 1006 ) {
2020-10-29 01:38:33 +08:00
this . stopUserAgent ( ) ;
2020-03-15 11:50:39 +08:00
this . callback ( {
status : this . baseCallStates . failed ,
2020-11-06 20:22:12 +08:00
error : 1002 ,
2020-03-15 11:50:39 +08:00
bridgeError : 'Websocket failed to connect' ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2020-03-15 11:50:39 +08:00
} ) ;
return reject ( {
type : this . baseErrorCodes . CONNECTION _ERROR ,
} ) ;
}
2020-09-26 07:11:44 +08:00
this . reconnect ( ) . then ( ( ) => {
logger . info ( {
logCode : 'sip_js_session_ua_reconnected' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
2024-03-18 21:58:53 +08:00
} , 'User agent successfully reconnected' ) ;
2020-09-26 07:11:44 +08:00
resolve ( ) ;
} ) . catch ( ( ) => {
2020-10-29 01:38:33 +08:00
this . stopUserAgent ( ) ;
2020-09-26 07:11:44 +08:00
logger . info ( {
logCode : 'sip_js_session_ua_disconnected' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'User agent failed to reconnect after'
2024-03-18 21:58:53 +08:00
+ ` ${ USER _AGENT _RECONNECTION _ATTEMPTS } attempts ` ) ;
2020-09-26 07:11:44 +08:00
this . callback ( {
status : this . baseCallStates . failed ,
error : 1002 ,
bridgeError : 'Websocket failed to connect' ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2020-09-26 07:11:44 +08:00
} ) ;
reject ( {
type : this . baseErrorCodes . CONNECTION _ERROR ,
} ) ;
2017-11-18 03:01:52 +08:00
} ) ;
2020-09-26 07:11:44 +08:00
} ) ;
return preturn ;
} ) ;
}
2017-09-30 04:42:34 +08:00
2020-09-26 07:11:44 +08:00
reconnect ( attempts = 1 ) {
return new Promise ( ( resolve , reject ) => {
if ( this . _reconnecting ) {
return resolve ( ) ;
}
2017-09-30 04:42:34 +08:00
2020-09-26 07:11:44 +08:00
if ( attempts > USER _AGENT _RECONNECTION _ATTEMPTS ) {
return reject ( {
type : this . baseErrorCodes . CONNECTION _ERROR ,
} ) ;
}
this . _reconnecting = true ;
2020-10-29 01:38:33 +08:00
logger . info ( {
logCode : 'sip_js_session_ua_reconnection_attempt' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , ` User agent reconnection attempt ${ attempts } ` ) ;
2020-11-23 23:40:38 +08:00
this . userAgent . reconnect ( ) . then ( ( ) => {
this . _reconnecting = false ;
resolve ( ) ;
} ) . catch ( ( ) => {
setTimeout ( ( ) => {
2020-09-26 07:11:44 +08:00
this . _reconnecting = false ;
this . reconnect ( ++ attempts ) . then ( ( ) => {
resolve ( ) ;
} ) . catch ( ( error ) => {
reject ( error ) ;
} ) ;
2020-11-23 23:40:38 +08:00
} , USER _AGENT _RECONNECTION _DELAY _MS ) ;
} ) ;
2017-09-30 04:42:34 +08:00
} ) ;
}
2021-06-30 01:47:59 +08:00
isValidIceCandidate ( event ) {
return event . candidate
&& this . validIceCandidates
&& this . validIceCandidates . find ( ( validCandidate ) => (
( validCandidate . address === event . candidate . address )
|| ( validCandidate . relatedAddress === event . candidate . address ) )
&& ( validCandidate . protocol === event . candidate . protocol ) ) ;
}
onIceGatheringStateChange ( event ) {
const iceGatheringState = event . target
? event . target . iceGatheringState
: null ;
2021-07-15 21:46:49 +08:00
if ( ( iceGatheringState === 'gathering' ) && ( ! this . _iceGatheringStartTime ) ) {
this . _iceGatheringStartTime = new Date ( ) ;
}
2021-06-30 01:47:59 +08:00
if ( iceGatheringState === 'complete' ) {
2021-07-15 21:46:49 +08:00
const secondsToGatherIce = ( new Date ( )
- ( this . _iceGatheringStartTime || this . _sessionStartTime ) ) / 1000 ;
2021-06-30 01:47:59 +08:00
logger . info ( {
logCode : 'sip_js_ice_gathering_time' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
2021-07-16 22:07:52 +08:00
secondsToGatherIce ,
2021-06-30 01:47:59 +08:00
} ,
} , ` ICE gathering candidates took (s): ${ secondsToGatherIce } ` ) ;
}
}
onIceCandidate ( sessionDescriptionHandler , event ) {
if ( this . isValidIceCandidate ( event ) ) {
logger . info ( {
logCode : 'sip_js_found_valid_candidate_from_trickle_ice' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'Found a valid candidate from trickle ICE, finishing gathering' ) ;
if ( sessionDescriptionHandler . iceGatheringCompleteResolve ) {
sessionDescriptionHandler . iceGatheringCompleteResolve ( ) ;
}
}
}
initSessionDescriptionHandler ( sessionDescriptionHandler ) {
/* eslint-disable no-param-reassign */
sessionDescriptionHandler . peerConnectionDelegate = {
onicecandidate :
this . onIceCandidate . bind ( this , sessionDescriptionHandler ) ,
onicegatheringstatechange :
this . onIceGatheringStateChange . bind ( this ) ,
} ;
/* eslint-enable no-param-reassign */
}
2017-10-18 03:16:42 +08:00
inviteUserAgent ( userAgent ) {
2020-09-26 07:11:44 +08:00
return new Promise ( ( resolve , reject ) => {
if ( this . userRequestedHangup === true ) reject ( ) ;
const {
hostname ,
} = this ;
2020-01-10 07:50:10 +08:00
2020-09-26 07:11:44 +08:00
const {
callExtension ,
isListenOnly ,
} = this . callOptions ;
2017-10-18 03:16:42 +08:00
2021-06-30 01:47:59 +08:00
this . _sessionStartTime = new Date ( ) ;
2020-09-26 07:11:44 +08:00
const target = SIP . UserAgent . makeURI ( ` sip: ${ callExtension } @ ${ hostname } ` ) ;
2022-02-01 03:30:38 +08:00
const matchConstraints = getAudioConstraints ( { deviceId : this . inputDeviceId } ) ;
2023-02-01 01:04:17 +08:00
const sessionDescriptionHandlerModifiers = [ ] ;
2022-05-06 21:33:23 +08:00
const iceModifiers = [
filterValidIceCandidates . bind ( this , this . validIceCandidates ) ,
] ;
if ( ! SIPJS _ALLOW _MDNS ) iceModifiers . push ( stripMDnsCandidates ) ;
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
2023-02-01 01:04:17 +08:00
// The current Vosk provider does not support stereo when transcribing
// microphone streams, so we need to make sure it is forcefully disabled
// via SDP munging. Having it disabled on server side FS _does not suffice_
// because the stereo parameter is client-mandated (ie replicated in the
// answer)
2024-04-17 06:39:29 +08:00
if ( stereoUnsupported ( ) ) {
2023-02-01 01:04:17 +08:00
logger . debug ( {
logCode : 'sipjs_transcription_disable_stereo' ,
} , 'Transcription provider does not support stereo, forcing stereo=0' ) ;
sessionDescriptionHandlerModifiers . push ( forceDisableStereo ) ;
}
2020-09-26 07:11:44 +08:00
const inviterOptions = {
sessionDescriptionHandlerOptions : {
constraints : {
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
audio : isListenOnly
? false
2021-01-23 03:30:42 +08:00
: matchConstraints ,
2020-09-26 07:11:44 +08:00
video : false ,
} ,
2020-10-23 22:21:20 +08:00
iceGatheringTimeout : ICE _GATHERING _TIMEOUT ,
2017-10-18 03:16:42 +08:00
} ,
2023-02-01 01:04:17 +08:00
sessionDescriptionHandlerModifiers ,
2022-05-06 21:33:23 +08:00
sessionDescriptionHandlerModifiersPostICEGathering : iceModifiers ,
2021-06-30 01:47:59 +08:00
delegate : {
onSessionDescriptionHandler :
this . initSessionDescriptionHandler . bind ( this ) ,
} ,
2020-09-26 07:11:44 +08:00
} ;
2017-10-11 02:03:29 +08:00
2020-09-26 07:11:44 +08:00
if ( isListenOnly ) {
inviterOptions . sessionDescriptionHandlerOptions . offerOptions = {
offerToReceiveAudio : true ,
} ;
}
const inviter = new SIP . Inviter ( userAgent , target , inviterOptions ) ;
this . currentSession = inviter ;
this . setupEventHandlers ( inviter ) . then ( ( ) => {
inviter . invite ( ) . then ( ( ) => {
resolve ( ) ;
} ) . catch ( e => reject ( e ) ) ;
} ) ;
} ) ;
2017-09-30 04:42:34 +08:00
}
2017-10-18 03:16:42 +08:00
setupEventHandlers ( currentSession ) {
2020-01-10 07:50:10 +08:00
return new Promise ( ( resolve , reject ) => {
if ( this . userRequestedHangup === true ) reject ( ) ;
2019-06-13 05:01:20 +08:00
let iceCompleted = false ;
let fsReady = false ;
2021-07-22 20:28:14 +08:00
let sessionTerminated = false ;
2019-06-13 05:01:20 +08:00
2020-09-26 07:11:44 +08:00
const setupRemoteMedia = ( ) => {
const mediaElement = document . querySelector ( MEDIA _TAG ) ;
2023-03-10 03:30:45 +08:00
const { sdp } = this . currentSession . sessionDescriptionHandler
. peerConnection . remoteDescription ;
2019-02-21 05:58:37 +08:00
2023-03-10 03:30:45 +08:00
logger . info ( {
logCode : 'sip_js_session_setup_remote_media' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
sdp ,
} ,
} , 'Audio call - setup remote media' ) ;
2020-09-26 07:11:44 +08:00
2023-03-10 03:30:45 +08:00
this . remoteStream = new MediaStream ( ) ;
2020-09-26 07:11:44 +08:00
this . currentSession . sessionDescriptionHandler
. peerConnection . getReceivers ( ) . forEach ( ( receiver ) => {
if ( receiver . track ) {
this . remoteStream . addTrack ( receiver . track ) ;
}
} ) ;
logger . info ( {
logCode : 'sip_js_session_playing_remote_media' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'Audio call - playing remote media' ) ;
mediaElement . srcObject = this . remoteStream ;
mediaElement . play ( ) ;
2020-10-01 21:16:48 +08:00
} ;
2019-01-30 08:11:20 +08:00
2019-05-10 05:01:34 +08:00
const checkIfCallReady = ( ) => {
2020-01-10 07:50:10 +08:00
if ( this . userRequestedHangup === true ) {
this . exitAudio ( ) ;
resolve ( ) ;
}
2020-09-26 07:11:44 +08:00
logger . info ( {
logCode : 'sip_js_session_check_if_call_ready' ,
extraInfo : {
iceCompleted ,
fsReady ,
} ,
} , 'Audio call - check if ICE is finished and FreeSWITCH is ready' ) ;
2023-03-10 03:30:45 +08:00
if ( iceCompleted ) {
2019-06-13 05:01:20 +08:00
this . webrtcConnected = true ;
2020-09-26 07:11:44 +08:00
setupRemoteMedia ( ) ;
2023-03-10 03:30:45 +08:00
}
2020-09-26 07:11:44 +08:00
2023-03-10 03:30:45 +08:00
if ( fsReady ) {
2022-01-26 04:49:38 +08:00
this . callback ( { status : this . baseCallStates . started , bridge : this . bridgeName } ) ;
2023-03-10 03:30:45 +08:00
2019-05-10 05:01:34 +08:00
resolve ( ) ;
}
} ;
2019-02-22 04:49:04 +08:00
// Sometimes FreeSWITCH just won't respond with anything and hangs. This timeout is to
2019-02-21 05:58:37 +08:00
// avoid that issue
const callTimeout = setTimeout ( ( ) => {
this . callback ( {
status : this . baseCallStates . failed ,
error : 1006 ,
2019-02-22 04:49:04 +08:00
bridgeError : ` Call timed out on start after ${ CALL _CONNECT _TIMEOUT / 1000 } s ` ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2019-02-21 05:58:37 +08:00
} ) ;
2019-11-23 05:48:46 +08:00
this . exitAudio ( ) ;
2019-02-21 05:58:37 +08:00
} , CALL _CONNECT _TIMEOUT ) ;
let iceNegotiationTimeout ;
2019-02-01 07:15:29 +08:00
const handleSessionAccepted = ( ) => {
2020-06-13 05:13:49 +08:00
logger . info ( { logCode : 'sip_js_session_accepted' , extraInfo : { callerIdName : this . user . callerIdName } } , 'Audio call session accepted' ) ;
2019-02-21 05:58:37 +08:00
clearTimeout ( callTimeout ) ;
// If ICE isn't connected yet then start timeout waiting for ICE to finish
2019-06-13 05:01:20 +08:00
if ( ! iceCompleted ) {
2019-02-21 05:58:37 +08:00
iceNegotiationTimeout = setTimeout ( ( ) => {
this . callback ( {
status : this . baseCallStates . failed ,
error : 1010 ,
2020-09-26 07:11:44 +08:00
bridgeError : 'ICE negotiation timeout after '
+ ` ${ ICE _NEGOTIATION _TIMEOUT / 1000 } s ` ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2019-02-21 05:58:37 +08:00
} ) ;
2019-11-23 05:48:46 +08:00
this . exitAudio ( ) ;
2020-09-26 07:11:44 +08:00
reject ( {
2020-10-01 21:16:48 +08:00
type : this . baseErrorCodes . CONNECTION _ERROR ,
2020-09-26 07:11:44 +08:00
} ) ;
2019-02-21 05:58:37 +08:00
} , ICE _NEGOTIATION _TIMEOUT ) ;
}
2020-09-26 07:11:44 +08:00
checkIfCallReady ( ) ;
2019-02-01 07:15:29 +08:00
} ;
2020-09-26 07:11:44 +08:00
const handleIceNegotiationFailed = ( peer ) => {
if ( iceCompleted ) {
2021-07-20 22:10:04 +08:00
logger . warn ( {
2020-09-26 07:11:44 +08:00
logCode : 'sipjs_ice_failed_after' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'ICE connection failed after success' ) ;
} else {
2021-07-20 22:10:04 +08:00
logger . warn ( {
2020-09-26 07:11:44 +08:00
logCode : 'sipjs_ice_failed_before' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'ICE connection failed before success' ) ;
}
2019-03-15 05:02:51 +08:00
clearTimeout ( callTimeout ) ;
2020-09-26 07:11:44 +08:00
clearTimeout ( iceNegotiationTimeout ) ;
this . callback ( {
status : this . baseCallStates . failed ,
error : 1007 ,
bridgeError : 'ICE negotiation failed. Current state '
+ ` - ${ peer . iceConnectionState } ` ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2020-09-26 07:11:44 +08:00
} ) ;
2019-03-15 05:02:51 +08:00
} ;
2020-09-26 07:11:44 +08:00
const handleIceConnectionTerminated = ( peer ) => {
if ( ! this . userRequestedHangup ) {
2021-07-20 22:10:04 +08:00
logger . warn ( {
2020-09-26 07:11:44 +08:00
logCode : 'sipjs_ice_closed' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'ICE connection closed' ) ;
2020-11-20 01:33:01 +08:00
} else return ;
2020-09-26 07:11:44 +08:00
this . callback ( {
status : this . baseCallStates . failed ,
error : 1012 ,
bridgeError : 'ICE connection closed. Current state -'
+ ` ${ peer . iceConnectionState } ` ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2020-09-26 07:11:44 +08:00
} ) ;
} ;
const handleSessionProgress = ( update ) => {
2019-06-29 05:45:50 +08:00
logger . info ( {
2020-09-26 07:11:44 +08:00
logCode : 'sip_js_session_progress' ,
2020-06-13 05:13:49 +08:00
extraInfo : {
callerIdName : this . user . callerIdName ,
2020-09-26 07:11:44 +08:00
update ,
2020-06-13 05:13:49 +08:00
} ,
2020-09-26 07:11:44 +08:00
} , 'Audio call session progress update' ) ;
2021-06-30 01:47:59 +08:00
this . currentSession . sessionDescriptionHandler . peerConnectionDelegate
. onconnectionstatechange = ( event ) => {
2020-11-06 20:37:58 +08:00
const peer = event . target ;
logger . info ( {
2020-11-07 03:48:49 +08:00
logCode : 'sip_js_connection_state_change' ,
2020-11-06 20:37:58 +08:00
extraInfo : {
2020-11-07 03:48:49 +08:00
connectionStateChange : peer . connectionState ,
2020-11-06 20:37:58 +08:00
callerIdName : this . user . callerIdName ,
} ,
2020-11-07 03:48:49 +08:00
} , 'ICE connection state change - Current connection state - '
2021-04-02 02:11:13 +08:00
+ ` ${ peer . connectionState } ` ) ;
2020-11-07 03:48:49 +08:00
switch ( peer . connectionState ) {
case 'failed' :
// Chrome triggers 'failed' for connectionState event, only
handleIceNegotiationFailed ( peer ) ;
break ;
default :
break ;
}
2021-06-30 01:47:59 +08:00
} ;
this . currentSession . sessionDescriptionHandler . peerConnectionDelegate
. oniceconnectionstatechange = ( event ) => {
2020-09-26 07:11:44 +08:00
const peer = event . target ;
2020-11-07 03:48:49 +08:00
switch ( peer . iceConnectionState ) {
case 'completed' :
2020-09-26 07:11:44 +08:00
case 'connected' :
2020-10-23 22:16:09 +08:00
if ( iceCompleted ) {
logger . info ( {
logCode : 'sip_js_ice_connection_success_after_success' ,
extraInfo : {
currentState : peer . connectionState ,
callerIdName : this . user . callerIdName ,
} ,
2021-08-26 01:17:27 +08:00
} , 'ICE connection success, but user is already connected, '
2021-04-02 02:11:13 +08:00
+ 'ignoring it...'
+ ` ${ peer . iceConnectionState } ` ) ;
2020-10-23 22:16:09 +08:00
return ;
}
2020-09-26 07:11:44 +08:00
logger . info ( {
logCode : 'sip_js_ice_connection_success' ,
extraInfo : {
currentState : peer . connectionState ,
callerIdName : this . user . callerIdName ,
} ,
2020-11-06 20:37:58 +08:00
} , 'ICE connection success. Current ICE Connection state - '
2021-04-02 02:11:13 +08:00
+ ` ${ peer . iceConnectionState } ` ) ;
2020-09-26 07:11:44 +08:00
clearTimeout ( callTimeout ) ;
clearTimeout ( iceNegotiationTimeout ) ;
iceCompleted = true ;
logSelectedCandidate ( peer , this . protocolIsIpv6 ) ;
checkIfCallReady ( ) ;
break ;
case 'failed' :
handleIceNegotiationFailed ( peer ) ;
break ;
case 'closed' :
handleIceConnectionTerminated ( peer ) ;
break ;
default :
break ;
}
2021-06-30 01:47:59 +08:00
} ;
2017-10-18 03:16:42 +08:00
} ;
2021-07-22 20:28:14 +08:00
const checkIfCallStopped = ( message ) => {
2021-09-25 01:48:22 +08:00
if ( ( ! this . ignoreCallState && fsReady ) || ! sessionTerminated ) {
2021-09-21 22:22:05 +08:00
return null ;
}
2019-06-13 05:01:20 +08:00
2020-10-23 22:21:20 +08:00
if ( ! message && ! ! this . userRequestedHangup ) {
2017-10-18 03:16:42 +08:00
return this . callback ( {
status : this . baseCallStates . ended ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2017-10-18 03:16:42 +08:00
} ) ;
}
2020-09-26 07:11:44 +08:00
// if session hasn't even started, we let audio-modal to handle
2024-03-18 21:58:53 +08:00
// any possible errors
2020-09-26 07:11:44 +08:00
if ( ! this . _currentSessionState ) return false ;
2019-02-01 07:15:29 +08:00
2019-02-21 05:58:37 +08:00
let mappedCause ;
2020-10-23 22:21:20 +08:00
let cause ;
2019-06-13 05:01:20 +08:00
if ( ! iceCompleted ) {
2019-02-21 05:58:37 +08:00
mappedCause = '1004' ;
2020-10-23 22:21:20 +08:00
cause = 'ICE error' ;
2019-02-21 05:58:37 +08:00
} else {
2020-10-23 22:21:20 +08:00
cause = 'Audio Conference Error' ;
2019-02-21 05:58:37 +08:00
mappedCause = '1005' ;
}
2017-10-20 18:11:51 +08:00
2021-07-20 22:10:04 +08:00
logger . warn ( {
2020-10-23 22:21:20 +08:00
logCode : 'sip_js_call_terminated' ,
extraInfo : { cause , callerIdName : this . user . callerIdName } ,
} , ` Audio call terminated. cause= ${ cause } ` ) ;
2017-10-18 03:16:42 +08:00
return this . callback ( {
status : this . baseCallStates . failed ,
error : mappedCause ,
bridgeError : cause ,
2022-01-26 04:49:38 +08:00
bridge : this . bridgeName ,
2017-10-18 03:16:42 +08:00
} ) ;
2021-07-22 20:28:14 +08:00
}
const handleSessionTerminated = ( message ) => {
logger . info ( {
logCode : 'sip_js_session_terminated' ,
extraInfo : { callerIdName : this . user . callerIdName } ,
} , 'SIP.js session terminated' ) ;
clearTimeout ( callTimeout ) ;
clearTimeout ( iceNegotiationTimeout ) ;
sessionTerminated = true ;
checkIfCallStopped ( ) ;
2017-10-18 03:16:42 +08:00
} ;
2017-10-10 04:48:10 +08:00
2020-09-26 07:11:44 +08:00
currentSession . stateChange . addListener ( ( state ) => {
switch ( state ) {
case SIP . SessionState . Initial :
break ;
case SIP . SessionState . Establishing :
handleSessionProgress ( ) ;
break ;
case SIP . SessionState . Established :
handleSessionAccepted ( ) ;
break ;
case SIP . SessionState . Terminating :
break ;
case SIP . SessionState . Terminated :
handleSessionTerminated ( ) ;
break ;
default :
2021-07-20 22:10:04 +08:00
logger . warn ( {
2020-09-26 07:11:44 +08:00
logCode : 'sipjs_ice_session_unknown_state' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'SIP.js unknown session state' ) ;
break ;
2019-05-10 05:01:34 +08:00
}
2020-09-26 07:11:44 +08:00
this . _currentSessionState = state ;
} ) ;
2018-04-03 21:50:18 +08:00
2020-09-26 07:11:44 +08:00
resolve ( ) ;
2019-06-13 05:01:20 +08:00
} ) ;
}
2021-01-23 03:30:42 +08:00
/ * *
* Update audio constraints for current local MediaStream ( microphone )
* @ param { Object } constraints MediaTrackConstraints object . See :
* https : //developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints
* @ return { Promise } A Promise for this process
* /
async updateAudioConstraints ( constraints ) {
try {
2021-01-26 10:45:27 +08:00
if ( typeof constraints !== 'object' ) return ;
2021-01-23 03:30:42 +08:00
logger . info ( {
logCode : 'sipjs_update_audio_constraint' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'SIP.js updating audio constraint' ) ;
2022-02-01 03:30:38 +08:00
const matchConstraints = filterSupportedConstraints ( constraints ) ;
2021-01-23 03:30:42 +08:00
//Chromium bug - see: https://bugs.chromium.org/p/chromium/issues/detail?id=796964&q=applyConstraints&can=2
2021-04-01 19:14:24 +08:00
const { isChrome } = browserInfo ;
if ( isChrome ) {
2021-01-23 03:30:42 +08:00
matchConstraints . deviceId = this . inputDeviceId ;
2022-09-16 01:48:26 +08:00
const stream = await doGUM ( { audio : matchConstraints } ) ;
2021-01-23 03:30:42 +08:00
this . currentSession . sessionDescriptionHandler
. setLocalMediaStream ( stream ) ;
} else {
const { localMediaStream } = this . currentSession
. sessionDescriptionHandler ;
localMediaStream . getAudioTracks ( ) . forEach (
track => track . applyConstraints ( matchConstraints ) ,
) ;
}
} catch ( error ) {
logger . error ( {
logCode : 'sipjs_audio_constraint_error' ,
extraInfo : {
callerIdName : this . user . callerIdName ,
} ,
} , 'SIP.js failed to update audio constraint' ) ;
}
}
2019-06-13 05:01:20 +08:00
}
2019-05-10 05:01:34 +08:00
2019-06-13 05:01:20 +08:00
export default class SIPBridge extends BaseAudioBridge {
constructor ( userData ) {
super ( userData ) ;
const {
userId ,
username ,
sessionToken ,
} = userData ;
this . user = {
userId ,
sessionToken ,
name : username ,
} ;
this . protocol = window . document . location . protocol ;
2021-11-19 05:52:20 +08:00
if ( MEDIA [ 'sip_ws_host' ] != null && MEDIA [ 'sip_ws_host' ] != '' ) {
this . hostname = MEDIA . sip _ws _host ;
} else {
this . hostname = window . document . location . hostname ;
}
2019-06-13 05:01:20 +08:00
2022-01-26 04:49:38 +08:00
this . bridgeName = BRIDGE _NAME ;
2019-06-13 05:01:20 +08:00
// SDP conversion utilitary methods to be used inside SIP.js
window . isUnifiedPlan = isUnifiedPlan ;
window . toUnifiedPlan = toUnifiedPlan ;
window . toPlanB = toPlanB ;
window . stripMDnsCandidates = stripMDnsCandidates ;
2020-02-29 08:38:30 +08:00
// No easy way to expose the client logger to sip.js code so we need to attach it globally
window . clientLogger = logger ;
2019-06-13 05:01:20 +08:00
}
2021-01-30 06:05:51 +08:00
get inputStream ( ) {
return this . activeSession ? this . activeSession . inputStream : null ;
}
2021-09-21 22:22:05 +08:00
/ * *
* Wrapper for SIPSession ' s ignoreCallState flag
* @ param { boolean } value
* /
set ignoreCallState ( value ) {
if ( this . activeSession ) {
this . activeSession . ignoreCallState = value ;
}
}
get ignoreCallState ( ) {
return this . activeSession ? this . activeSession . ignoreCallState : false ;
}
2022-04-12 06:18:40 +08:00
joinAudio ( {
isListenOnly ,
extension ,
validIceCandidates ,
inputStream ,
} , managerCallback ) {
2019-06-13 05:01:20 +08:00
const hasFallbackDomain = typeof IPV4 _FALLBACK _DOMAIN === 'string' && IPV4 _FALLBACK _DOMAIN !== '' ;
return new Promise ( ( resolve , reject ) => {
let { hostname } = this ;
2019-06-26 03:13:31 +08:00
this . activeSession = new SIPSession ( this . user , this . userData , this . protocol ,
2019-10-26 10:57:49 +08:00
hostname , this . baseCallStates , this . baseErrorCodes , false ) ;
2019-06-13 05:01:20 +08:00
const callback = ( message ) => {
if ( message . status === this . baseCallStates . failed ) {
let shouldTryReconnect = false ;
// Try and get the call to clean up and end on an error
2021-04-02 02:11:13 +08:00
this . activeSession . exitAudio ( ) . catch ( ( ) => { } ) ;
2019-06-13 05:01:20 +08:00
if ( this . activeSession . webrtcConnected ) {
// webrtc was able to connect so just try again
message . silenceNotifications = true ;
2022-01-26 04:49:38 +08:00
callback ( { status : this . baseCallStates . reconnecting , bridge : this . bridgeName , } ) ;
2019-06-13 05:01:20 +08:00
shouldTryReconnect = true ;
} else if ( hasFallbackDomain === true && hostname !== IPV4 _FALLBACK _DOMAIN ) {
message . silenceNotifications = true ;
2020-06-13 05:13:49 +08:00
logger . info ( { logCode : 'sip_js_attempt_ipv4_fallback' , extraInfo : { callerIdName : this . user . callerIdName } } , 'Attempting to fallback to IPv4 domain for audio' ) ;
2019-06-13 05:01:20 +08:00
hostname = IPV4 _FALLBACK _DOMAIN ;
shouldTryReconnect = true ;
}
if ( shouldTryReconnect ) {
const fallbackExtension = this . activeSession . inEchoTest ? extension : undefined ;
2019-06-26 03:13:31 +08:00
this . activeSession = new SIPSession ( this . user , this . userData , this . protocol ,
2019-10-26 10:57:49 +08:00
hostname , this . baseCallStates , this . baseErrorCodes , true ) ;
2021-04-23 02:03:43 +08:00
const { inputDeviceId , outputDeviceId } = this ;
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
this . activeSession . joinAudio ( {
isListenOnly ,
extension : fallbackExtension ,
inputDeviceId ,
2021-04-23 02:03:43 +08:00
outputDeviceId ,
2021-06-30 01:47:59 +08:00
validIceCandidates ,
2022-04-12 06:18:40 +08:00
inputStream ,
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
} , callback )
2019-06-13 05:01:20 +08:00
. then ( ( value ) => {
resolve ( value ) ;
} ) . catch ( ( reason ) => {
reject ( reason ) ;
} ) ;
}
}
return managerCallback ( message ) ;
} ;
2021-04-23 02:03:43 +08:00
const { inputDeviceId , outputDeviceId } = this ;
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
this . activeSession . joinAudio ( {
isListenOnly ,
extension ,
inputDeviceId ,
2021-04-23 02:03:43 +08:00
outputDeviceId ,
2021-06-30 01:47:59 +08:00
validIceCandidates ,
2022-04-12 06:18:40 +08:00
inputStream ,
Correctly set audio input/output devices
When refusing ("thumbs down" button) echo test, user is able to select a different input device. This should work fine for chrome, firefox and safari (once user grants permission when asked by html5client).
For output devices, we depend on setSinkId function, which is enabled by default on current chrome release (2020) but not in Firefox (user needs to enable "setSinkId in about:config page). This implementation is listed as (?) in MDN.
In other words, output device selection should work out of the box for chrome, only.
When selecting an outputDevice, all alert sounds (hangup, screenshare , polling, etc) also goes to the same output device.
This solves #10592
2020-10-07 07:37:55 +08:00
} , callback )
2019-06-13 05:01:20 +08:00
. then ( ( value ) => {
resolve ( value ) ;
} ) . catch ( ( reason ) => {
reject ( reason ) ;
} ) ;
2017-10-12 05:04:10 +08:00
} ) ;
}
2019-06-13 05:01:20 +08:00
transferCall ( onTransferSuccess ) {
2022-01-26 04:49:38 +08:00
this . activeSession . inEchoTest = false ;
logger . debug ( {
logCode : 'sip_js_rtp_payload_send_dtmf' ,
extraInfo : {
callerIdName : this . activeSession . user . callerIdName ,
} ,
} , 'Sending DTMF INFO to transfer user' ) ;
return this . trackTransferState ( onTransferSuccess ) ;
}
sendDtmf ( tones ) {
this . activeSession . sendDtmf ( tones ) ;
2019-06-13 05:01:20 +08:00
}
2019-11-30 05:48:04 +08:00
getPeerConnection ( ) {
2021-08-13 03:39:04 +08:00
if ( ! this . activeSession ) return null ;
2019-11-30 05:48:04 +08:00
const { currentSession } = this . activeSession ;
2020-09-26 07:11:44 +08:00
if ( currentSession && currentSession . sessionDescriptionHandler ) {
return currentSession . sessionDescriptionHandler . peerConnection ;
2019-11-30 05:48:04 +08:00
}
return null ;
}
2019-06-13 05:01:20 +08:00
exitAudio ( ) {
2022-10-28 00:13:27 +08:00
if ( this . activeSession == null ) return Promise . resolve ( ) ;
2019-06-13 05:01:20 +08:00
return this . activeSession . exitAudio ( ) ;
}
2022-08-20 01:22:42 +08:00
setInputStream ( stream ) {
return this . activeSession . setInputStream ( stream ) ;
2020-07-28 03:49:26 +08:00
}
2021-01-23 03:30:42 +08:00
async updateAudioConstraints ( constraints ) {
return this . activeSession . updateAudioConstraints ( constraints ) ;
}
2017-07-24 22:15:46 +08:00
}
2021-09-01 02:50:53 +08:00
module . exports = SIPBridge ;