diff --git a/.github/workflows/smoke.yml b/.github/workflows/smoke.yml new file mode 100644 index 0000000..292dfe0 --- /dev/null +++ b/.github/workflows/smoke.yml @@ -0,0 +1,159 @@ +name: Build and Test + +on: + pull_request: + types: [opened, reopened, synchronize] + push: + branches: + - master + - beta + +jobs: + + build-ubuntu-11-14: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [11.x,14.x] + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + + - name: Install TypeScript 4.1.3 + run: npm install typescript@4.1.3 --force + + - name: Install Dependencies + run: npm install --force && npm install jest --force + + - name: Build + run: npm run build + + - name: Run unit test cases on Ubuntu + run: npm run test:unit-jenkins + continue-on-error: true + + + build-ubuntu-16-18: + runs-on: ubuntu-latest + needs: build-ubuntu-11-14 + strategy: + matrix: + node-version: [16.x, 18.x] + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + + - name: Install TypeScript 4.1.3 + run: npm install typescript@4.1.3 --force + + - name: Install Dependencies + run: npm install --force && npm install jest --force + + - name: Build + run: npm run build + + - name: Run unit test cases on Ubuntu + run: npm run test:unit-jenkins + continue-on-error: true + + + build-ubuntu-20-latest: + runs-on: ubuntu-latest + needs: build-ubuntu-16-18 + strategy: + matrix: + node-version: [20.x, latest] + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + + - name: Install Dependencies + run: npm install --force && npm install jest --force + + - name: Install TypeScript 4.1.3 + run: npm install typescript@4.1.3 --force + + - name: Build + run: npm run build + + - name: Run unit test cases on Ubuntu + run: npm run test:unit-jenkins + continue-on-error: true + + + + build-macos: + runs-on: macos-latest + needs: build-ubuntu-20-latest + strategy: + matrix: + node-version: [16.x, 18.x, 20.x, latest] + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + + - name: Install Dependencies + run: npm install --force && npm install jest --force + + - name: Install TypeScript 4.1.3 + run: npm install typescript@4.1.3 --force + + - name: Build + run: npm run build + + - name: Run unit test cases on macOS + run: npm run test:unit + continue-on-error: true + + + build-windows: + runs-on: windows-latest + needs: build-macos + strategy: + matrix: + node-version: [16.x, 18.x, 20.x, latest] + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + + - name: Install Dependencies + run: npm install --force && npm install jest --force + + - name: Install TypeScript 4.1.3 + run: npm install typescript@4.1.3 --force + + - name: Build + run: npm run build + + - name: Run unit test cases on Windows + run: npm run test:unit-windows diff --git a/CHANGELOG.md b/CHANGELOG.md index a8da155..6c483ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,23 @@ All notable GA release changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## v2.2.11-beta.0 (released@ 08-07-2024) + +**Features** +* Added support for connecting and registering the SDK independently: If the `stopAutoRegisterOnConnect` flag is set to true, the `login()` method will only connect the SDK to Plivo servers. Default is set to `false`. +* Introduced a new `register()` method, which registers the SDK when called. +* Introduced the `captureSDKCrashOnly` flag, which, when set to true, captures and syncs only SDK-related crash logs. Default is `false`. +* Introduced the `redirect(uri)` method, which redirects the call to other SDK clients when invoked. +* Added helper methods such as + * `disconnect()`: Disconnect the SDK. + * `unregister()`: Unregister the SDK + * `getCurrentSession()`: Returns current active session, If any. +* Added support to send more call-related information and statistics to the call-insights service. + +**Bug fixes** +- Fixed the issue causing a mismatch between local machine time and actual time. +- Fixed issues where the speech recognition engine would enter an endless loop when mute is performed on two tabs using the same engine + ## v2.2.11 (released@ 07-06-2024) **Bug Fixes** diff --git a/customTypes/worker-loader.d.ts b/customTypes/worker-loader.d.ts new file mode 100644 index 0000000..0af15ce --- /dev/null +++ b/customTypes/worker-loader.d.ts @@ -0,0 +1,10 @@ +declare module "*.worker.ts" { + // You need to change `Worker`, if you specified a different value for the `workerType` option + class WebpackWorker extends Worker { + constructor(); + } + + // Uncomment this if you set the `esModule` option to `false` + // export = WebpackWorker; + export default WebpackWorker; +}; \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 7ffec56..f50e8f5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -20,6 +20,7 @@ declare module 'plivo-browser-sdk/client' { import { StatsSocket } from 'plivo-browser-sdk/stats/ws'; import { OutputDevices, InputDevices, RingToneDevices } from 'plivo-browser-sdk/media/audioDevice'; import { NoiseSuppression } from 'plivo-browser-sdk/rnnoise/NoiseSuppression'; + import { WorkerManager } from 'plivo-browser-sdk/managers/workerManager'; import { LoggerUtil } from 'plivo-browser-sdk/utils/loggerUtil'; export interface PlivoObject { log: typeof Logger; @@ -49,7 +50,9 @@ declare module 'plivo-browser-sdk/client' { registrationRefreshTimer?: number; enableNoiseReduction?: boolean; usePlivoStunServer?: boolean; + stopAutoRegisterOnConnect: boolean; dtmfOptions?: DtmfOptions; + captureSDKCrashOnly: boolean; } export interface BrowserDetails { browser: string; @@ -131,16 +134,6 @@ declare module 'plivo-browser-sdk/client' { * @private */ isLoggedIn: boolean; - /** - * Timer for reconnecting to the media connection if any network issue happen - * @private - */ - reconnectInterval: null | ReturnType; - /** - * Controls the number of times media reconnection happens - * @private - */ - reconnectTryCount: number; /** * Holds the JSSIP user agent for the logged in user * @private @@ -151,6 +144,11 @@ declare module 'plivo-browser-sdk/client' { * @private */ _currentSession: null | CallSession; + /** + * Difference in time(ms) between local machine and universal epoch time + * @private + */ + timeDiff: number; /** * Holds the incoming or outgoing JSSIP RTCSession(WebRTC media session) * @private @@ -229,6 +227,17 @@ declare module 'plivo-browser-sdk/client' { * @private */ accessToken: null | string; + /** + * contactUri object which holds the connection details + * @private + */ + contactUri: { + name: string | null; + ip: string; + port: string; + protocol: string; + registrarIP: string; + }; /** * Access Token object given when logging in * @private @@ -465,11 +474,32 @@ declare module 'plivo-browser-sdk/client' { * @private */ networkChangeInCurrentSession: boolean; + /** + * Holds the status of the websocket connection + * @private + */ + connectionStatus: string; + /** + * Holds a string value tp uniquely identify each tab + * @private + */ + identifier: string; + /** + * Holds the instance of WorkerManager class + * @private + */ + workerManager: WorkerManager; /** * Holds a boolean to get initial network info * @private */ didFetchInitialNetworkInfo: boolean; + /** + * Flag which when set stops auto registration post websocket connection + * Defaults value is true + * @private + */ + stopAutoRegisterOnConnect: boolean; /** * Determines which js framework sdk is running with * @private @@ -504,6 +534,19 @@ declare module 'plivo-browser-sdk/client' { * Unregister and clear stats timer, socket. */ logout: () => boolean; + /** + * Unregister the user. + */ + unregister: () => boolean; + /** + * disconnect the websocket and stop the network check timer. + */ + disconnect: () => boolean; + /** + * Register the user. + * @param {Array} extraHeaders - (Optional) Extra headers to be sent along with register. + */ + register: (extraHeaders: Array) => boolean; /** * Start an outbound call. * @param {String} phoneNumber - It can be a sip endpoint/number @@ -523,6 +566,11 @@ declare module 'plivo-browser-sdk/client' { * Hangup the call(Outgoing/Incoming). */ hangup: () => boolean; + /** + * Redirect the call. + * @param {String} contactUri - details of the contact towards which the call should be redirected + */ + redirect: (contactUri: string) => boolean; /** * Reject the Incoming call. * @param {String} callUUID - (Optional) Provide latest CallUUID to reject the call @@ -533,6 +581,11 @@ declare module 'plivo-browser-sdk/client' { * @param {String} callUUID - (Optional) Provide latest CallUUID to ignore the call */ ignore: (callUUID: string) => boolean; + /** + * Set the unique identifier. + * @param {String} identifier - Identifier to be set. + */ + setIdentifier: (identifier: string) => boolean; /** * Send DTMF for call(Outgoing/Incoming). * @param {String} digit - Send the digits as dtmf 'digit' @@ -584,20 +637,25 @@ declare module 'plivo-browser-sdk/client' { */ getCallUUID: () => string | null; /** - * Check if the client is in registered state. - * @returns Current CallUUID - */ + * Check if the client is in registered state. + */ isRegistered: () => boolean | null; /** - * Check if the client is in connecting state. - * @returns Current CallUUID - */ + * Check if the client is in connecting state. + */ isConnecting: () => boolean | null; /** - * Check if the client is in connected state. - * @returns Current CallUUID - */ + * Get the details of the contact. + */ + getContactUri: () => string | null; + /** + * Check if the client is in connected state. + */ isConnected: () => boolean | null; + /** + * Get the details of the current active call session. + */ + getCurrentSession: () => CallSession | null; /** * Get the CallUUID of the latest answered call. */ @@ -635,7 +693,6 @@ declare module 'plivo-browser-sdk/client' { * @param {Boolean} sendConsoleLogs - Send browser logs to Plivo */ submitCallQualityFeedback: (callUUID: string, starRating: string, issues: string[], note: string, sendConsoleLogs: boolean) => Promise; - clearOnLogout(): void; /** * @constructor * @param options - (Optional) client configuration parameters @@ -644,6 +701,7 @@ declare module 'plivo-browser-sdk/client' { constructor(options: ConfiguationOptions); setExpiryTimeInEpoch: (timeInEpoch: number) => void; getTokenExpiryTimeInEpoch: () => number | null; + clearOnLogout(): void; } } @@ -754,6 +812,7 @@ declare module 'plivo-browser-sdk/managers/callSession' { CANCELED: string; FAILED: string; ENDED: string; + REDIRECTED: string; }; SPEECH_STATE: { STOPPED: string; @@ -762,6 +821,7 @@ declare module 'plivo-browser-sdk/managers/callSession' { STOPPING: string; STOPPED_AFTER_DETECTION: string; STOPPED_DUE_TO_NETWORK_ERROR: string; + STOPPED_DUE_TO_ABORT: string; }; /** * Unique identifier generated for a call by server @@ -1175,6 +1235,37 @@ declare module 'plivo-browser-sdk/rnnoise/NoiseSuppression' { } } +declare module 'plivo-browser-sdk/managers/workerManager' { + export class WorkerManager { + workerInstance: Worker | null; + networkCheckRunning: boolean; + onTimerCallback: any; + responseCallback: any; + timerStartedOnMain: any; + constructor(); + /** + * Callback triggered when error is received while starting worker thread. + * @param {ErrorEvent} msg - error message received + */ + onErrorReceived: (msg: ErrorEvent) => void; + /** + * Callback triggered when message is received from the worker thread. + * @param {any} msg - message received + */ + onMessageReceived: (msg: any) => void; + /** + * Start the network check timer. + * @param {number} networkCheckInterval - time interval at which the timer is to be executed. + * @param {any} callback - callback to be trigerred when the timer executes. + */ + startNetworkCheckTimer: (networkCheckInterval: number, callback: any, responseCallback: any) => void; + /** + * Stop the network check timer. + */ + stopNetworkChecktimer: () => void; + } +} + declare module 'plivo-browser-sdk/utils/loggerUtil' { import { Client } from "plivo-browser-sdk/client"; export class LoggerUtil { @@ -1184,6 +1275,8 @@ declare module 'plivo-browser-sdk/utils/loggerUtil' { setSipCallID(value: string): void; getUserName(): string; setUserName(value: string): void; + setIdentifier(identifier: string): void; + getIdentifier(): string; } } @@ -1245,6 +1338,9 @@ declare module 'plivo-browser-sdk/constants' { Ignored: number; "call answer fail": number; "Network switch while ringing": number; + "invalid-destination-address": number; + "call-already-in-progress": number; + "incoming-invite-exist": number; }; export const DEFAULT_CODECS: string[]; export const DTMF_OPTIONS: string[]; @@ -1345,6 +1441,108 @@ declare module 'plivo-browser-sdk/constants' { declare module 'plivo-browser-sdk/stats/rtpStats' { import { Client, Storage } from 'plivo-browser-sdk/client'; import { AudioLevel } from 'plivo-browser-sdk/media/audioLevel'; + export interface LocalCandidate { + id?: string; + address?: string; + port?: string; + relatedAddress?: string; + relatedPort?: string; + candidateType?: string; + usernameFragment?: string; + } + type LocalCandidateMap = { + [timestamp: string]: LocalCandidate; + }; + export interface RemoteCandidate { + id?: string; + address?: string; + port?: string; + candidateType?: string; + usernameFragment?: string; + } + export interface CandidatePair { + availableOutgoingBitrate?: string; + consentRequestsSent?: number; + id?: string; + lastPacketReceivedTimestamp?: string; + lastPacketSentTimestamp?: string; + localCandidateId?: string; + nominated?: string; + packetsDiscardedOnSend?: string; + packetsReceived?: string; + packetsSent?: string; + remoteCandidateId?: string; + requestsReceived?: number; + requestsSent?: number; + responsesReceived?: number; + responsesSent?: number; + state?: string; + transportId?: string; + writable?: boolean; + } + export interface Transport { + id?: string; + dtlsRole?: string; + dtlsState?: string; + iceRole?: string; + iceState?: string; + packetsReceived?: string; + packetsSent?: string; + selectedCandidatePairChanges?: number; + selectedCandidatePairId?: string; + } + export interface OutboundRTP { + bytesSent?: number; + packetsSent?: number; + retransmittedBytesSent?: number; + retransmittedPacketsSent?: number; + transportId?: string; + } + export interface RemoteInboundRTP { + fractionLost?: number; + packetsLost?: number; + roundTripTime?: string; + roundTripTimeMeasurements?: number; + totalRoundTripTime?: string; + transportId?: string; + } + export interface InboundRTP { + bytesReceived?: number; + jitterBufferDelay?: string; + jitterBufferEmittedCount?: number; + jitterBufferMinimumDelay?: string; + jitterBufferTargetDelay?: string; + packetsDiscarded?: number; + packetsLost?: number; + packetsReceived?: number; + totalSamplesDuration?: string; + totalSamplesReceived?: string; + transportId?: string; + } + export interface RemoteOutboundRTP { + bytesSent?: number; + packetsSent?: number; + reportsSent?: number; + totalRoundTripTime?: string; + transportId?: string; + } + export interface StatsDump { + msg: string; + callUUID: string; + xcallUUID: string; + source: string; + timeStamp: number; + version: string; + changedCandidatedInfo: LocalCandidateMap; + localCandidate: LocalCandidate; + remoteCandidate: RemoteCandidate; + transport: Transport; + candidatePair: CandidatePair; + outboundRTP: OutboundRTP; + remoteInboundRTP: RemoteInboundRTP; + inboundRTP: InboundRTP; + remoteOutboundRTP: RemoteOutboundRTP; + } export interface StatsLocalStream { ssrc?: number; packetsLost?: number; @@ -1426,6 +1624,9 @@ declare module 'plivo-browser-sdk/stats/rtpStats' { * @private */ pc: RTCPeerConnection; + rtpsender: RTCStatsReport; + rtpreceiver: RTCStatsReport; + localCandidateInfo: LocalCandidateMap; /** * Unique identifier generated for a call by server * @private @@ -1537,10 +1738,11 @@ declare module 'plivo-browser-sdk/stats/rtpStats' { * @private */ constructor(client: Client); + sendCallStatsDump: (stream: StatsDump) => Promise; /** * Stop analysing audio levels for local and remote streams. */ - stop: () => void; + stop: () => Promise; } export {}; } @@ -1635,7 +1837,7 @@ declare module 'plivo-browser-sdk/stats/nonRTPStats' { * @param {String} userName * @returns Stat message with call information */ - export const addCallInfo: (callSession: CallSession, statMsg: any, callstatskey: string, userName: string) => object; + export const addCallInfo: (callSession: CallSession, statMsg: any, callstatskey: string, userName: string, timeStamp: number) => object; /** * Send events to plivo stats. * @param {Any} statMsg - call stats (Answered/RTP/Summary/Feedback/Failure Events) diff --git a/lib/client.ts b/lib/client.ts index 772b3fd..45afe45 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -32,9 +32,17 @@ import { import getBrowserDetails from './utils/browserDetection'; import detectFramework from './utils/frameworkDetection'; import AccessTokenInterface from './utils/token'; -import { setErrorCollector, setConectionInfo } from './managers/util'; +import { + setErrorCollector, + setConectionInfo, + hangupClearance, + clearOptionsInterval, + uuidGenerator, + getCurrentTime, +} from './managers/util'; import { NoiseSuppression } from './rnnoise/NoiseSuppression'; import { ConnectionState } from './utils/networkManager'; +import { WorkerManager } from './managers/workerManager'; import { LOCAL_ERROR_CODES, LOGCAT } from './constants'; import { LoggerUtil } from './utils/loggerUtil'; @@ -75,7 +83,9 @@ export interface ConfiguationOptions { registrationRefreshTimer?: number; enableNoiseReduction?: boolean; usePlivoStunServer?: boolean + stopAutoRegisterOnConnect: boolean, dtmfOptions?: DtmfOptions; + captureSDKCrashOnly: boolean; } export interface BrowserDetails { @@ -169,18 +179,6 @@ export class Client extends EventEmitter { */ isLoggedIn: boolean; - /** - * Timer for reconnecting to the media connection if any network issue happen - * @private - */ - reconnectInterval: null | ReturnType; - - /** - * Controls the number of times media reconnection happens - * @private - */ - reconnectTryCount: number; - /** * Holds the JSSIP user agent for the logged in user * @private @@ -193,6 +191,12 @@ export class Client extends EventEmitter { */ _currentSession: null | CallSession; + /** + * Difference in time(ms) between local machine and universal epoch time + * @private + */ + timeDiff: number; + /** * Holds the incoming or outgoing JSSIP RTCSession(WebRTC media session) * @private @@ -291,6 +295,18 @@ export class Client extends EventEmitter { */ accessToken: null | string; + /** + * contactUri object which holds the connection details + * @private + */ + contactUri: { + name: string | null; + ip: string; + port: string; + protocol: string; + registrarIP: string; + }; + /** * Access Token object given when logging in * @private @@ -566,12 +582,37 @@ export class Client extends EventEmitter { */ networkChangeInCurrentSession: boolean; + /** + * Holds the status of the websocket connection + * @private + */ + connectionStatus: string; + + /** + * Holds a string value tp uniquely identify each tab + * @private + */ + identifier: string; + + /** + * Holds the instance of WorkerManager class + * @private + */ + workerManager: WorkerManager; + /** * Holds a boolean to get initial network info * @private */ didFetchInitialNetworkInfo: boolean; + /** + * Flag which when set stops auto registration post websocket connection + * Defaults value is true + * @private + */ + stopAutoRegisterOnConnect: boolean; + /** * Determines which js framework sdk is running with * @private @@ -616,6 +657,22 @@ export class Client extends EventEmitter { */ public logout = (): boolean => this._logout(); + /** + * Unregister the user. + */ + public unregister = (): boolean => this._unregister(); + + /** + * disconnect the websocket and stop the network check timer. + */ + public disconnect = (): boolean => this._disconnect(); + + /** + * Register the user. + * @param {Array} extraHeaders - (Optional) Extra headers to be sent along with register. + */ + public register = (extraHeaders: Array): boolean => this._register(extraHeaders); + /** * Start an outbound call. * @param {String} phoneNumber - It can be a sip endpoint/number @@ -644,6 +701,14 @@ export class Client extends EventEmitter { */ public hangup = (): boolean => this._hangup(); + /** + * Redirect the call. + * @param {String} contactUri - details of the contact towards which the call should be redirected + */ + public redirect = ( + contactUri: string, + ): boolean => this._redirect(contactUri); + /** * Reject the Incoming call. * @param {String} callUUID - (Optional) Provide latest CallUUID to reject the call @@ -656,6 +721,12 @@ export class Client extends EventEmitter { */ public ignore = (callUUID: string): boolean => this._ignore(callUUID); + /** + * Set the unique identifier. + * @param {String} identifier - Identifier to be set. + */ + public setIdentifier = (identifier: string): boolean => this._setIdentifier(identifier); + /** * Send DTMF for call(Outgoing/Incoming). * @param {String} digit - Send the digits as dtmf 'digit' @@ -719,23 +790,30 @@ export class Client extends EventEmitter { public getCallUUID = (): string | null => this._getCallUUID(); /** - * Check if the client is in registered state. - * @returns Current CallUUID - */ + * Check if the client is in registered state. + */ public isRegistered = (): boolean | null => this._isRegistered(); /** -* Check if the client is in connecting state. -* @returns Current CallUUID -*/ + * Check if the client is in connecting state. + */ public isConnecting = (): boolean | null => this._isConnecting(); /** -* Check if the client is in connected state. -* @returns Current CallUUID -*/ + * Get the details of the contact. + */ + public getContactUri = (): string | null => this._getContactUri(); + + /** + * Check if the client is in connected state. + */ public isConnected = (): boolean | null => this._isConnected(); + /** + * Get the details of the current active call session. + */ + public getCurrentSession = (): CallSession | null => this._getCurrentSession(); + /** * Get the CallUUID of the latest answered call. */ @@ -789,36 +867,6 @@ export class Client extends EventEmitter { sendConsoleLogs, ); - clearOnLogout(): void { - // Store.getInstance().clear(); - // if logout is called explicitly, make all the related flags to default - if (this.isAccessToken) { - this.isAccessToken = false; - this.isOutgoingGrant = false; - this.isIncomingGrant = false; - this.accessToken = null; - } - if (this._currentSession) { - this._currentSession.addConnectionStage( - `logout()@${new Date().getTime()}`, - ); - Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Terminating an active call, before logging out`); - this._currentSession.session.terminate(); - } - this.isLogoutCalled = true; - this.noiseSuppresion.clearNoiseSupression(); - setConectionInfo(this, ConnectionState.DISCONNECTED, "Logout"); - if (this.phone && this.phone.isRegistered()) { - this.phone.stop(); - this.phone = null; - } - if (this.statsSocket) { - this.statsSocket.disconnect(); - this.statsSocket = null; - } - Plivo.log.send(this); - } - /** * @constructor * @param options - (Optional) client configuration parameters @@ -827,8 +875,6 @@ export class Client extends EventEmitter { constructor(options: ConfiguationOptions) { super(); - setErrorCollector(); - device.checkMediaDevices(); this.version = 'PLIVO_LIB_VERSION'; @@ -836,6 +882,11 @@ export class Client extends EventEmitter { // eslint-disable-next-line @typescript-eslint/naming-convention const _options = validateOptions(options); Plivo.log.enableSipLogs(_options.debug as AvailableLogMethods); + this.loggerUtil = new LoggerUtil(this); + Plivo.log.setLoggerUtil(this.loggerUtil); + this.identifier = uuidGenerator(); + Plivo.log.debug(`${C.LOGCAT.INIT} | unique identifier generated: ${this.identifier}`); + this.loggerUtil.setIdentifier(this.identifier); const data = { codecs: _options.codecs, @@ -856,6 +907,9 @@ export class Client extends EventEmitter { enableNoiseReduction: _options.enableNoiseReduction, usePlivoStunServer: _options.usePlivoStunServer, dtmfOptions: _options.dtmfOptions, + captureSDKCrashOnly: _options.captureSDKCrashOnly, + permOnClick: _options.permOnClick, + stopAutoRegisterOnConnect: _options.stopAutoRegisterOnConnect, }; Plivo.log.info(`${C.LOGCAT.INIT} | Plivo SDK initialized successfully with options:- `, JSON.stringify(data), `in ${Plivo.log.level()} mode`); // instantiates event emitter @@ -872,8 +926,6 @@ export class Client extends EventEmitter { this.ringToneBackFlag = true; this.connectToneFlag = true; this.isLoggedIn = false; - this.reconnectInterval = null; - this.reconnectTryCount = 0; this.phone = null; this._currentSession = null; this.callSession = null; @@ -887,6 +939,7 @@ export class Client extends EventEmitter { this.callStats = null; this.userName = null; this.password = null; + this.connectionStatus = ''; this.options = _options; this.callstatskey = null; this.rtp_enabled = false; @@ -900,12 +953,11 @@ export class Client extends EventEmitter { this.isOutgoingGrant = false; this.isIncomingGrant = false; this.useDefaultAudioDevice = false; + this.stopAutoRegisterOnConnect = _options.stopAutoRegisterOnConnect; if (getBrowserDetails().browser !== 'firefox') { // eslint-disable-next-line new-cap, no-undef this.speechRecognition = new webkitSpeechRecognition(); } - this.loggerUtil = new LoggerUtil(this); - Plivo.log.setLoggerUtil(this.loggerUtil); if (this.options.usePlivoStunServer === true && C.STUN_SERVERS.indexOf(C.FALLBACK_STUN_SERVER) === -1) { C.STUN_SERVERS.push(C.FALLBACK_STUN_SERVER); @@ -949,6 +1001,12 @@ export class Client extends EventEmitter { `${C.LOGCAT.INIT} | PlivoWebSdk initialized in ${Plivo.log.level()} mode, version: PLIVO_LIB_VERSION , browser: ${this.browserDetails.browser}-${this.browserDetails.version}`, ); this.jsFramework = detectFramework(); + setErrorCollector(this); + if (process.env.PLIVO_ENV) { + Plivo.log.debug(`${C.LOGCAT.INIT} | Starting the worker thread`); + + this.workerManager = new WorkerManager(); + } } private getUsernameFromToken = (parsedToken: string | any): string => { @@ -1121,7 +1179,7 @@ export class Client extends EventEmitter { private _loginWithAccessToken = (accessToken: string): boolean => { try { if (this._initJWTParams(accessToken) && this.userName) { - Plivo.log.info(C.LOGCAT.LOGIN, ' | Login initiated with AccessToken : ', accessToken); + Plivo.log.info(C.LOGCAT.LOGIN, ` | Login initiated with AccessToken : ${accessToken}`); return this.tokenLogin(this.userName, accessToken); } Plivo.log.info(C.LOGCAT.LOGIN, 'Login failed : Invalid AccessToken'); @@ -1194,21 +1252,133 @@ export class Client extends EventEmitter { return true; }; + clearOnLogout(): void { + // Store.getInstance().clear(); + // if logout is called explicitly, make all the related flags to default + if (this.isAccessToken) { + this.isAccessToken = false; + this.isOutgoingGrant = false; + this.isIncomingGrant = false; + this.accessToken = null; + } + + // check if the any call session is active + if (this.getCurrentSession() !== null) { + // if the call session is established, terminate the session + // else clear the variables holding the session details + if (this.getCurrentSession()?.session.isEstablished()) { + this.getCurrentSession()?.addConnectionStage( + `logout()@${getCurrentTime(this)}`, + ); + Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Terminating an active call, before logging out`); + this.getCurrentSession()?.session.terminate(); + } else { + Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Call Session exists, clearing the the session variables`); + this._currentSession = null; + this.lastIncomingCall = null; + } + } + this.isLogoutCalled = true; + this.noiseSuppresion.clearNoiseSupression(); + if (this.ringToneView && !this.ringToneView.paused) { + documentUtil.stopAudio(C.RINGTONE_ELEMENT_ID); + } + if (this.ringBackToneView && !this.ringBackToneView.paused) { + documentUtil.stopAudio(C.RINGBACK_ELEMENT_ID); + } + + setConectionInfo(this, ConnectionState.DISCONNECTED, "Logout"); + this.connectionStatus = ''; + this.didFetchInitialNetworkInfo = false; + if (this.phone && (this.phone.isRegistered() || this.isConnected())) { + if (this.isConnected() && !this.isRegistered() && this.stopAutoRegisterOnConnect) { + Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Emitting onLogout when phone instance is connected but not registered`); + this.userName = null; + this.password = null; + this.emit('onLogout'); + } + Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Stopping the UA and clearing the phone instance`); + this.phone.stop(); + this.phone = null; + } + clearOptionsInterval(this); + + if (this.statsSocket) { + this.statsSocket.disconnect(); + this.statsSocket = null; + } + Plivo.log.send(this); + } + private _logout = (): boolean => { - if (!this.isLoggedIn) { - Plivo.log.debug(C.LOGCAT.LOGOUT, ' | Cannot execute logout: no active login session.', this.userName); - return false; + if (!this.isLoggedIn && !this.isRegistered()) { + if (this.isConnected()) { + Plivo.log.debug(C.LOGCAT.LOGOUT, ' | SDK is not registered but connected. Disconnecting it.'); + } else { + Plivo.log.debug(C.LOGCAT.LOGOUT, ' | Cannot execute logout: no active login session.', this.userName); + return false; + } } Plivo.log.debug(C.LOGCAT.LOGOUT, ' | Logout initiated!', this.userName); this.clearOnLogout(); return true; }; + private _unregister = (): boolean => { + if (this.phone && this.isRegistered()) { + Plivo.log.debug(`${C.LOGCAT.LOGIN} | unregistering`); + this.isLoggedIn = false; + this.connectionStatus = 'unregistered'; + (this.phone as any)._registrator.close(); + return true; + } + Plivo.log.warn(`${C.LOGCAT.LOGIN} | cannot unregister. phone instance: ${this.phone !== null}, isRegistered: ${this.isRegistered()}`); + return false; + }; + + private _disconnect = (): boolean => { + if (this.phone && this.isConnected()) { + Plivo.log.debug(`${C.LOGCAT.WS} | disconnecting`); + (this.phone as any)._transport.disconnect(true); + clearOptionsInterval(this); + return true; + } + Plivo.log.warn(`${C.LOGCAT.WS} | cannot disconnect WS. phone instance: ${this.phone !== null}, isConnected: ${this.isConnected()}`); + return false; + }; + + private _register = (extraHeaders?: Array): boolean => { + if (this.phone && (this.phone as any).registrator && this.isConnected()) { + Plivo.log.debug(`${C.LOGCAT.LOGIN} | registering`); + if (!this.isRegistered() + || (this.isRegistered() + && extraHeaders + && extraHeaders?.length > 0)) { + this.isLoginCalled = true; + if (extraHeaders && extraHeaders instanceof Array && extraHeaders.length > 0) { + Plivo.log.info(`${C.LOGCAT.LOGIN} | setting extraheaders before register ${JSON.stringify(extraHeaders)}`); + (this.phone as any)._registrator.setExtraHeaders(extraHeaders); + } + (this.phone as any)._registrator.register(); + return true; + } + Plivo.log.warn(`${C.LOGCAT.LOGIN} | Already registed, cannot register again`); + return false; + } + Plivo.log.warn(`${C.LOGCAT.LOGIN} | Not connected, cannot register`); + return false; + }; + private _call = (phoneNumber: string, extraHeaders: ExtraHeaders): boolean => { this.timeTakenForStats.pdd = { init: new Date().getTime(), }; + if (this.getCurrentSession() !== null || this.callSession) { + Plivo.log.warn(`${C.LOGCAT.LOGIN} | Already on a call, cannot make another call`); + return false; + } + if (!this.isLoggedIn && (this.phone === null || (this.phone && !this.phone.isConnected() @@ -1226,38 +1396,13 @@ export class Client extends EventEmitter { Plivo.log.warn(`${C.LOGCAT.LOGIN} | Outgoing call permission not granted`); return false; } - - // const onCallFailed = (reason: string) => { - // Plivo.log.error(`${C.LOGCAT.CALL} | On call failed`, reason); - // this.emit('onCallFailed', reason); - // }; const readyForCall = () => { if (this.isAccessToken) extraHeaders['X-Plivo-Jwt'] = `${this.accessToken}`; this.owaLastDetect.isOneWay = false; return OutgoingCall.makeCall(this, extraHeaders, phoneNumber); }; - // Handle One Way Audio issues in chrome. check for every 1 hr - // if ( - // this.options.preDetectOwa - // && this.browserDetails.browser === 'chrome' - // && (new Date().getTime() - (this.owaLastDetect.time as any) > this.owaDetectTime - // || this.owaLastDetect.isOneWay) - // ) { - - // oneWayAudio.detectOWA((res, err) => { - // oneWayAudio.owaCallback.call( - // this, - // res, - // err, - // onCallFailed, - // readyForCall, - // ); - // }); - // } else { - // Browsers other than chrome go to call ready mode readyForCall(); - // } return true; }; @@ -1269,40 +1414,10 @@ export class Client extends EventEmitter { if (incomingCall && isValid) { Plivo.log.debug(`answer - ${incomingCall.callUUID}`); incomingCall.addConnectionStage(`answer()@${new Date().getTime()}`); - // const mediaError = (reason: string) => { - // Plivo.log.debug(`${C.LOGCAT.CALL} | rejecting call, Reason : ${reason}`); - // this.reject(incomingCall.callUUID as string); - // return true; - // }; - // const readyForCall = () => { - // IncomingCall.answerIncomingCall( - // incomingCall, - // actionOnOtherIncomingCalls, - // ); - // }; - // Handle One Way Audio issues in chrome. check for every 1 hr - // if ( - // this.options.preDetectOwa - // && this.browserDetails.browser === 'chrome' - // && (new Date().getTime() - (this.owaLastDetect.time as any) > this.owaDetectTime - // || this.owaLastDetect.isOneWay) - // ) { - // oneWayAudio.detectOWA((res, err) => { - // oneWayAudio.owaCallback.call( - // this, - // res, - // err, - // mediaError, - // readyForCall, - // ); - // }); - // } else { - // Browsers other than chrome go to call ready mode IncomingCall.answerIncomingCall( incomingCall, actionOnOtherIncomingCalls, ); - // } } else { Plivo.log.error(`${C.LOGCAT.LOGIN} | Incoming call answer() failed : no incoming call`); return false; @@ -1367,6 +1482,40 @@ export class Client extends EventEmitter { return true; }; + private _redirect = ( + contactUri: string | null, + ): boolean => { + if (contactUri && this.lastIncomingCall) { + Plivo.log.debug(`${LOGCAT.CALL} | redirecting to contactUri ${contactUri}`); + + const contactData = contactUri.split('&'); + const uri = contactData[0]; + const registrarIP = contactData[1]; + this.lastIncomingCall.setState(this.lastIncomingCall.STATE.REDIRECTED); + Plivo.log.debug(`${LOGCAT.CALL} | removing all the listeners on the current session before redirecting`); + (this.lastIncomingCall.session as any).removeAllListeners(); + this.lastIncomingCall.session.refer(uri, { + extraHeaders: [ + `X-PlivoUpdatedRegContact: ${uri}`, + `X-PlivoRegistrarIP: ${registrarIP}`, + `Contact: ${uri}`, + ], + }); + if (this.statsSocket) { + this.statsSocket.disconnect(); + this.statsSocket = null; + } + audioUtil.stopVolumeDataStreaming(); + IncomingCall.stopRingtone(); + Plivo.log.debug(`${LOGCAT.CALL} | clearing required session info on call redirect`); + hangupClearance.call(this, this.lastIncomingCall, true); + } else { + Plivo.log.warn(`${LOGCAT.CALL} | Cannot redirect. Either no call session exists to redirect or contact uri is null`); + return false; + } + return true; + }; + private _reject = (callUUID: string): boolean => { const incomingCall = IncomingCall.getCurrentIncomingCall(callUUID, this); if (!incomingCall) { @@ -1448,6 +1597,17 @@ export class Client extends EventEmitter { return false; }; + private _setIdentifier = (identifier: string): boolean => { + if (identifier && typeof identifier === 'string' && identifier !== '') { + Plivo.log.info(`${C.LOGCAT.INIT} | changing identifier from ${this.identifier} to ${identifier}`); + this.identifier = identifier; + this.loggerUtil.setIdentifier(identifier); + return true; + } + Plivo.log.warn(`${C.LOGCAT.INIT} | Identifier should be a non empty string`); + return false; + }; + private _sendDtmf = (digit: number | string): void => { if (!navigator.onLine) { return Plivo.log.warn(`${C.LOGCAT.CALL} | Unable to send DTMF: No internet connection`); @@ -1658,6 +1818,25 @@ export class Client extends EventEmitter { private _isConnected = (): boolean | null => this.phone && this.phone.isConnected(); + private _getContactUri = (): string | null => { + if (this.contactUri + && (typeof this.contactUri.name === 'string' && this.contactUri.name !== '') + && (typeof this.contactUri.ip === 'string' && this.contactUri.ip !== '') + && (typeof this.contactUri.port === 'number' && this.contactUri.port !== '') + && (typeof this.contactUri.registrarIP === 'string' && this.contactUri.registrarIP !== '') + && (typeof this.contactUri.protocol === 'string' && this.contactUri.protocol !== '')) { + const contactUriString = `sip:${this.contactUri.name}@${this.contactUri.ip}:${this.contactUri.port};${this.contactUri.protocol}&${this.contactUri.registrarIP}`; + Plivo.log.debug(`${C.LOGCAT.CALL} | generated contact URI string: ${contactUriString}`); + return contactUriString; + } + Plivo.log.info(`${C.LOGCAT.CALL} | Contact URI string could not be generated ${JSON.stringify(this.contactUri)}`); + return null; + }; + + private _getCurrentSession = ( + ): CallSession | null => this._currentSession + || this.lastIncomingCall; + private _startNoiseReduction = (): Promise => new Promise((resolve) => { if (!this.enableNoiseReduction) { Plivo.log.warn(`${C.LOGCAT.CALL_QUALITY} | Noise reduction cannot be started since "enableNoiseReduction" is set to false`); diff --git a/lib/constants.ts b/lib/constants.ts index 7d36074..1dc9dcd 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -2,7 +2,7 @@ /* eslint-disable no-var */ /* eslint-disable import/no-mutable-exports */ -// Signalling +// // Signalling export const DOMAIN = 'phone.plivo.com'; export const WS_SERVERS = ['wss://client.plivo.com/signalling', 'wss://client-fb.plivo.com/signalling']; @@ -67,6 +67,9 @@ export const LOCAL_ERROR_CODES = { Ignored: 11002, "call answer fail": 11003, "Network switch while ringing": 11004, + "invalid-destination-address": 11005, + "call-already-in-progress": 11006, + "incoming-invite-exist": 11007, }; // Options diff --git a/lib/logger.ts b/lib/logger.ts index 7e39d2b..2b862a3 100644 --- a/lib/logger.ts +++ b/lib/logger.ts @@ -123,7 +123,23 @@ class PlivoLogger { if (arg1.includes(LOGCAT.NIMBUS)) flag = true; if (arg1.includes(LOGCAT.WS)) flag = true; - if (flag) Storage.getInstance().setData(premsg, arg1, arg2); + if (flag) { + if (!process.env.PLIVO_ENV) return; + (navigator as any).locks.request('PLIVO_LOG', () => { + const storageInstance = Storage.getInstance(); + try { + storageInstance.setData(premsg, arg1, arg2); + } catch (e) { + if (e.name === 'QuotaExceededError') { + storageInstance.clear(); + storageInstance.setData(premsg, 'NIMBUS | Storage is full. clearing it.', ''); + } else { + storageInstance.setData(premsg, 'NIMBUS | Error while storing logs', e.name); + } + storageInstance.setData(premsg, arg1, arg2); + } + }); + } }; /** @@ -144,7 +160,7 @@ class PlivoLogger { if (enableDate) msdate = `[${date.toISOString().substring(0, 10)} ${date.toISOString().split('T')[1].split('.')[0]}.${date.getUTCMilliseconds()}]`; let premsg = ""; if (this.loggerUtil) { - premsg = `${msdate} ${loggingName} : ${this.loggerUtil?.getUserName()} ${this.loggerUtil?.getSipCallID()} [${ucFilter}] `; + premsg = `${msdate} ${loggingName} [${this.loggerUtil.getIdentifier()}] : ${this.loggerUtil?.getUserName()} ${this.loggerUtil?.getSipCallID()} [${ucFilter}] `; } else { premsg = `${msdate} ${loggingName} :[${ucFilter}] `; } @@ -207,7 +223,7 @@ class PlivoLogger { index: number, callUUID: string = "", userName: string | null = "", - ): Promise => new Promise((resolve, reject) => { + ): Promise => new Promise((resolve) => { const sdkVersionParse = getSDKVersion(); const deviceOs = getOS(); @@ -245,7 +261,7 @@ class PlivoLogger { .then((response) => { if (!response.ok) { this.info(`${LOGCAT.NIMBUS} | Error while uploading logs to nimbus`); - reject(new Error('failure')); + resolve('failure'); } else { index += 1; if (logs.length > index) { @@ -260,10 +276,10 @@ class PlivoLogger { response.text(); }) - .then((result) => console.log(result)) + .then(() => console.log('logs synced')) .catch((error) => { console.log('error', error); - reject(error); + resolve(error); }); } }); @@ -274,22 +290,23 @@ class PlivoLogger { send = (client: Client): void => { if (!process.env.PLIVO_ENV) return; - const myHeaders = new Headers(); - myHeaders.append("Content-Type", "application/json"); + (navigator as any).locks.request('PLIVO_LOG', () => { + const myHeaders = new Headers(); + myHeaders.append("Content-Type", "application/json"); + const data = Storage.getInstance().getData(); - const data = Storage.getInstance().getData(); - - if (!data) { - console.log("No data to send"); - return; - } - - if (!client.userName) return; + if (!data) { + console.log("No data to send"); + return; + } + if (!client.userName) return; - const parsedData = JSON.parse(data); - const arr = parsedData.split("\n"); - const batchData = this._getLogsBatchData(arr, 20000); - this._sendBatchedLogsToServer(client, myHeaders, batchData, 0); + const parsedData = JSON.parse(data); + const arr = parsedData.split("\n"); + const batchData = this._getLogsBatchData(arr, 20000); + // eslint-disable-next-line consistent-return + return this._sendBatchedLogsToServer(client, myHeaders, batchData, 0); + }); }; } diff --git a/lib/managers/account.ts b/lib/managers/account.ts index 04d4765..55edc9b 100644 --- a/lib/managers/account.ts +++ b/lib/managers/account.ts @@ -10,18 +10,22 @@ import { createIncomingSession, } from './incomingCall'; import { createOutgoingSession } from './outgoingCall'; -import { getCurrentTime, addMidAttribute, setConectionInfo } from './util'; -import { stopAudio } from '../media/document'; +import { + getCurrentTime, + addMidAttribute, + setConectionInfo, + clearOptionsInterval, +} from './util'; import { Client } from '../client'; import { - sendNetworkChangeEvent, - startPingPong, - getNetworkData, ConnectionState, + getNetworkData, + sendNetworkChangeEvent, socketReconnectionRetry, + startPingPong, } from '../utils/networkManager'; -import { StatsSocket } from '../stats/ws'; import { NoiseSuppression } from '../rnnoise/NoiseSuppression'; +import { StatsSocket } from '../stats/ws'; const Plivo = { log: Logger }; let urlIndex: number = 0; @@ -38,11 +42,6 @@ class Account { private isPlivoSocketConnected: boolean; - /** - * SipLib Message class to execute ping-pong - */ - private message: any; - /** * Holds the boolean whether failed event is triggered */ @@ -68,11 +67,6 @@ class Account { accessToken: string | null; }; - /** - * Hold the value of number of retry counts done - */ - private fetchIpCount: number; - /** * Validate the account credentials and session. */ @@ -109,12 +103,10 @@ class Account { this.cs = clientObject; this.credentials = { userName, password }; this.accessTokenCredentials = { accessToken }; - this.message = null; this.registerRefreshTimer = registerRefreshtimer; // for qa purpose this.reinviteCounter = 0; this.isPlivoSocketConnected = false; - this.fetchIpCount = 0; } private _validate = (callback: () => void): any => { @@ -130,7 +122,7 @@ class Account { this.cs.emit('onLoginFailed', 'Username and password must be filled out'); return false; } - if (this.cs._currentSession) { + if (!this.cs.stopAutoRegisterOnConnect && this.cs._currentSession) { Plivo.log.warn( `${C.LOGCAT.LOGIN} | Cannot login when there is an ongoing call ${this.cs._currentSession.callUUID}`, ); @@ -235,6 +227,10 @@ class Account { const sipConfig = this.setupUAConfig(); try { this.cs.phone = new SipLib.UA(sipConfig); + if (this.cs.stopAutoRegisterOnConnect) { + Plivo.log.debug(`${C.LOGCAT.LOGIN} | Setting sendRegisterOnTransportConnect flag to false`); + this.cs.phone.sendRegisterOnTransportConnect = false; + } return true; } catch (e) { Plivo.log.debug(`${C.LOGCAT.LOGIN} | Failed to create user agent ${e.message}`); @@ -243,22 +239,6 @@ class Account { } }; - private tiggerNetworkChangeEvent = () => { - this.fetchIpCount += 1; - fetchIPAddress(this.cs).then((ipAddress) => { - if (typeof ipAddress === "string") { - sendNetworkChangeEvent(this.cs, ipAddress); - } else if (this.fetchIpCount !== C.IP_ADDRESS_FETCH_RETRY_COUNT) { - setTimeout(() => { - this.tiggerNetworkChangeEvent(); - }, this.fetchIpCount * 200); - } else { - Plivo.log.warn(`${C.LOGCAT.NETWORK_CHANGE} | Could not retreive ipaddress`); - this.fetchIpCount = 0; - } - }); - }; - private _createListeners = (): void => { if (this.cs.phone) { this.cs.phone.on('connected', this._onConnected); @@ -279,14 +259,31 @@ class Account { Plivo.log.debug('websocket connection established', evt); Plivo.log.info(`${C.LOGCAT.WS} | websocket connection established`); - if (this.cs.connectionRetryInterval) { - Plivo.log.info(`${C.LOGCAT.LOGIN} | websocket connected. Clearing the connection check interval`); - clearInterval(this.cs.connectionRetryInterval); - this.cs.connectionRetryInterval = null; + if (this.cs.loginCallback) { + this.cs.loginCallback = null; } + this.cs.emit('onWebsocketConnected'); + this.cs.userName = this.credentials.userName; + this.cs.password = this.credentials.password; + this.cs.connectionStatus = 'connected'; + + // if network change happened and multi-tab-support is enabled. + if (this.cs.stopAutoRegisterOnConnect && this.cs.didFetchInitialNetworkInfo) { + if (this.cs._currentSession && this.cs.isCallMuted) { + Plivo.log.info(`${C.LOGCAT.CALL} | Speech Recognition restarted after network disruption`); + this.cs._currentSession.startSpeechRecognition(this.cs); + } + Plivo.log.info(`${C.LOGCAT.CALL} | Network changed happenend`); + this._onNetworkChange(); + } + if (!this.isPlivoSocketConnected) { this.isPlivoSocketConnected = true; if (!this.cs.didFetchInitialNetworkInfo) { + if (this.cs.stopAutoRegisterOnConnect) { + Plivo.log.info(`${C.LOGCAT.CALL} | initializing data on websocket connect`); + this._initDataOnConnect(); + } fetchIPAddress(this.cs).then((ip) => { this.cs.currentNetworkInfo = { networkType: (navigator as any).connection @@ -316,9 +313,8 @@ class Account { if (evt.code) { setConectionInfo(this.cs, ConnectionState.DISCONNECTED, evt.code.toString()); } - if (this.isPlivoSocketConnected) { - this.isPlivoSocketConnected = false; - } + Plivo.log.debug(`${C.LOGCAT.WS} | websocket disconnected with reason : ${this.cs.connectionInfo.reason}`); + this.cs.networkDisconnectedTimestamp = getCurrentTime(this.cs); if (this.cs.loginCallback) { Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Previous connection disconnected successfully, starting a new one.`); this.cs.loginCallback(); @@ -342,95 +338,47 @@ class Account { // Parse the response to get the JWT expiry in epoch // below is the example to get basic 120 sec expiry from response // To do : This needs to be changed in case of login through access Token method - if (this.cs._currentSession && this.cs.isCallMuted) { - Plivo.log.info(`${C.LOGCAT.CALL} | Speech Recognition restarted after network disruption`); - this.cs._currentSession.startSpeechRecognition(this.cs); - } - if (this.cs.loginCallback) { - this.cs.loginCallback = null; - } setConectionInfo(this.cs, ConnectionState.CONNECTED, 'registered'); Plivo.log.debug(`${C.LOGCAT.WS} | websocket connected: ${this.cs.connectionInfo.reason}`); this.cs.emit('onConnectionChange', this.cs.connectionInfo); + if (this.cs.loginCallback) { + this.cs.loginCallback = null; + } + if (!this.cs.stopAutoRegisterOnConnect && this.cs._currentSession && this.cs.isCallMuted) { + Plivo.log.info(`${C.LOGCAT.CALL} | Speech Recognition restarted after network disruption`); + this.cs._currentSession.startSpeechRecognition(this.cs); + } + if (this.cs.isAccessToken && res.response.headers['X-Plivo-Jwt']) { const expiryTimeInEpoch = res.response.headers['X-Plivo-Jwt'][0].raw.split(";")[0].split("=")[1]; this.cs.setExpiryTimeInEpoch(expiryTimeInEpoch * 1000); } - if (!this.cs.isLoggedIn && !this.cs.isLoginCalled) { - Plivo.log.info(`${C.LOGCAT.NETWORK_CHANGE} | Network changed happened. re-starting the OPTIONS interval`); - - // this is case of network change - clearInterval(this.cs.networkChangeInterval as any); - this.cs.networkChangeInterval = null; - startPingPong({ - client: this.cs, - networkChangeInterval: this.cs._currentSession - ? C.NETWORK_CHANGE_INTERVAL_ON_CALL_STATE : C.NETWORK_CHANGE_INTERVAL_IDLE_STATE, - messageCheckTimeout: C.MESSAGE_CHECK_TIMEOUT_ON_CALL_STATE, - }); - if (!this.cs._currentSession) { - // network changed when call is not active. sending network change stats to nimbus. - fetchIPAddress(this.cs).then((ipAddress) => { - const networkInfo = getNetworkData(this.cs, ipAddress); - - Plivo.log.info(`${C.LOGCAT.NETWORK_CHANGE} | Network changed from ${JSON.stringify(networkInfo.previousNetworkInfo).slice(1, -1)} - to ${JSON.stringify(networkInfo.newNetworkInfo).slice(1, -1)} in idle state`); - this.cs.currentNetworkInfo = { - networkType: networkInfo.newNetworkInfo.networkType, - ip: typeof ipAddress === "string" ? ipAddress : "", - }; - }); + // if multi-tab-support is disabled + if (!this.cs.isLoggedIn && !this.cs.isLoginCalled && !this.cs.stopAutoRegisterOnConnect) { + Plivo.log.debug(`${C.LOGCAT.LOGIN} | Processing network change after registration`); + this._onNetworkChange(); + if (!this.cs._currentSession) { this.cs.isLoggedIn = true; - return; } - - // create stats socket and trigger and trigger network change event - // when user is in in-call state - if (this.cs.statsSocket) { - this.cs.statsSocket.disconnect(); - this.cs.statsSocket = null; - } - this.cs.statsSocket = new StatsSocket(); - this.cs.statsSocket.connect(); - this.cs.networkReconnectionTimestamp = new Date().getTime(); - this.tiggerNetworkChangeEvent(); } if (!this.cs.isLoginCalled) { this.cs.isLoggedIn = true; } - this.cs.userName = this.credentials.userName; - this.cs.password = this.credentials.password; - if (this.cs.isLoggedIn === false && this.cs.isLoginCalled === true) { + + if (this.cs.isLoggedIn === false + && this.cs.isLoginCalled === true) { this.cs.isLoggedIn = true; this.cs.isLoginCalled = false; - // initialize callstats.io - this.cs.noiseSuppresion = new NoiseSuppression(this.cs); + // if multi-tab-support is not enabled + if (!this.cs.stopAutoRegisterOnConnect) { + Plivo.log.debug(`${C.LOGCAT.LOGIN} | initializing the data on registration`); + this._initDataOnConnect(); + } + this.cs.connectionStatus = 'registered'; this.cs.emit('onLogin'); Plivo.log.info(`${C.LOGCAT.LOGIN} | User logged in successfully`); - // in firefox and safari web socket re-establishes automatically after network change - startPingPong({ - client: this.cs, - networkChangeInterval: C.NETWORK_CHANGE_INTERVAL_IDLE_STATE, - messageCheckTimeout: C.MESSAGE_CHECK_TIMEOUT_ON_CALL_STATE, - }); - // get callstats key and create stats socket - let passToken: string | null; - if (this.cs.isAccessToken) { - passToken = this.cs.accessToken; - } else { - this.cs.isAccessToken = false; - passToken = this.cs.password; - } - validateCallStats(this.cs.userName, passToken, this.cs.isAccessToken) - .then((responsebody: CallStatsValidationResponse) => { - this.cs.callstatskey = responsebody.data; - this.cs.rtp_enabled = responsebody.is_rtp_enabled; - }).catch(() => { - this.cs.callstatskey = null; - }); - initCallStatsIO.call(this.cs); Plivo.log.send(this.cs); } }; @@ -444,6 +392,7 @@ class Account { if (this.cs.connectionInfo.state === "" || this.cs.connectionInfo.state === ConnectionState.CONNECTED) { setConectionInfo(this.cs, ConnectionState.DISCONNECTED, "unregistered"); } + this.cs.connectionStatus = 'unregistered'; Plivo.log.debug(`${C.LOGCAT.WS} | websocket disconnected with reason : ${this.cs.connectionInfo.reason}`); this.cs.emit('onConnectionChange', this.cs.connectionInfo); if (!this.cs.isLogoutCalled) { @@ -451,24 +400,12 @@ class Account { } Plivo.log.debug(`${C.LOGCAT.LOGOUT} | Plivo client unregistered`); - if (this.cs.ringToneView && !this.cs.ringToneView.paused) { - stopAudio(C.RINGTONE_ELEMENT_ID); - } - if (this.cs.ringBackToneView && !this.cs.ringBackToneView.paused) { - stopAudio(C.RINGBACK_ELEMENT_ID); - } - this.cs.networkDisconnectedTimestamp = new Date().getTime(); - this.cs.userName = null; - this.cs.password = null; this.cs.emit('onLogout'); - Plivo.log.info(C.LOGCAT.LOGOUT, ' | Logout successful!'); - if (this.cs.networkChangeInterval) { - clearInterval(this.cs.networkChangeInterval); - this.cs.networkChangeInterval = null; - } + Plivo.log.debug(C.LOGCAT.LOGOUT, ' | Logout successful!'); this.cs.clearOnLogout(); - this.message = null; + this.cs.userName = null; + this.cs.password = null; this.cs.isLogoutCalled = false; Plivo.log.send(this.cs); }; @@ -511,7 +448,7 @@ class Account { this.cs.loggerUtil.setSipCallID(callID); Plivo.log.info('<----- INCOMING ----->'); const callUUID = evt.transaction.request.getHeader('X-Calluuid') || null; - this.cs.incomingCallsInitiationTime.set(callUUID, getCurrentTime()); + this.cs.incomingCallsInitiationTime.set(callUUID, getCurrentTime(this.cs)); Plivo.log.debug('call initiation time, invite received from server'); } }; @@ -582,6 +519,111 @@ class Account { } return true; }; + + /** + * Initialize variables and tasks on websocket connection. + * @param {Client} client - client instance + * @param {string} credentials - credentials of the user +*/ + private _initDataOnConnect = (): void => { + // restart speech recognition. + if (this.cs._currentSession && this.cs.isCallMuted) { + Plivo.log.info(`${C.LOGCAT.CALL} | Speech Recognition restarted after network disruption`); + this.cs._currentSession.startSpeechRecognition(this.cs); + } + if (this.cs.loginCallback) { + this.cs.loginCallback = null; + } + this.cs.noiseSuppresion = new NoiseSuppression(this.cs); + this.cs.userName = this.credentials.userName; + this.cs.password = this.credentials.password; + startPingPong({ + client: this.cs, + networkChangeInterval: C.NETWORK_CHANGE_INTERVAL_IDLE_STATE, + messageCheckTimeout: C.MESSAGE_CHECK_TIMEOUT_ON_CALL_STATE, + }); + let passToken: string | null; + if (this.cs.isAccessToken) { + passToken = this.cs.accessToken; + } else { + this.cs.isAccessToken = false; + passToken = this.cs.password; + } + // get callstats key and create stats socket + validateCallStats(this.cs.userName ?? '', passToken, this.cs.isAccessToken) + .then((responsebody: CallStatsValidationResponse) => { + this.cs.callstatskey = responsebody.data; + this.cs.rtp_enabled = responsebody.is_rtp_enabled; + }).catch(() => { + this.cs.callstatskey = null; + }); + initCallStatsIO.call(this.cs); + Plivo.log.send(this.cs); + }; + + /** + * Send reinvite/info and event to call insights on network change + * @param {Client} client - client instance + * @param {number} fetchIpCount - Count to track the number of times IP address is fetched + */ + private _tiggerNetworkChangeEvent = (fetchIpCount: number): void => { + fetchIpCount += 1; + fetchIPAddress(this.cs).then((ipAddress) => { + if (typeof ipAddress === "string") { + sendNetworkChangeEvent(this.cs, ipAddress); + } else if (fetchIpCount !== C.IP_ADDRESS_FETCH_RETRY_COUNT) { + setTimeout(() => { + this._tiggerNetworkChangeEvent(fetchIpCount); + }, fetchIpCount * 200); + } else { + Plivo.log.warn(`${C.LOGCAT.NETWORK_CHANGE} | Could not retreive ipaddress`); + fetchIpCount = 0; + } + }); + }; + + /** + * Handle network change related tasks. + * @param {Client} client - client instance + */ + private _onNetworkChange = (): void => { + Plivo.log.info(`${C.LOGCAT.NETWORK_CHANGE} | Network changed happened. re-starting the OPTIONS interval`); + clearOptionsInterval(this.cs); + + startPingPong({ + client: this.cs, + networkChangeInterval: this.cs._currentSession + ? C.NETWORK_CHANGE_INTERVAL_ON_CALL_STATE : C.NETWORK_CHANGE_INTERVAL_IDLE_STATE, + messageCheckTimeout: C.MESSAGE_CHECK_TIMEOUT_ON_CALL_STATE, + }); + + if (!this.cs._currentSession) { + // network changed when call is not active. sending network change stats to nimbus. + fetchIPAddress(this.cs).then((ipAddress) => { + const networkInfo = getNetworkData(this.cs, ipAddress); + + Plivo.log.info(`${C.LOGCAT.NETWORK_CHANGE} | Network changed from ${JSON.stringify(networkInfo.previousNetworkInfo).slice(1, -1)} + to ${JSON.stringify(networkInfo.newNetworkInfo).slice(1, -1)} in idle state`); + + this.cs.currentNetworkInfo = { + networkType: networkInfo.newNetworkInfo.networkType, + ip: typeof ipAddress === "string" ? ipAddress : "", + }; + }); + return; + } + + // create stats socket and trigger network change event + // when user is in in-call state + if (this.cs.statsSocket) { + this.cs.statsSocket.disconnect(); + this.cs.statsSocket = null; + } + this.cs.statsSocket = new StatsSocket(); + this.cs.statsSocket.connect(); + this.cs.networkReconnectionTimestamp = getCurrentTime(this.cs); + this._tiggerNetworkChangeEvent(0); + }; } export default Account; diff --git a/lib/managers/callSession.ts b/lib/managers/callSession.ts index 082fc95..d39e630 100644 --- a/lib/managers/callSession.ts +++ b/lib/managers/callSession.ts @@ -109,6 +109,7 @@ export class CallSession { CANCELED: string; FAILED: string; ENDED: string; + REDIRECTED: string; }; SPEECH_STATE: { @@ -118,6 +119,7 @@ export class CallSession { STOPPING: string; STOPPED_AFTER_DETECTION: string; STOPPED_DUE_TO_NETWORK_ERROR: string; + STOPPED_DUE_TO_ABORT: string; }; /** @@ -323,10 +325,10 @@ export class CallSession { && clientObj._currentSession?.speech_state === clientObj._currentSession?.SPEECH_STATE.STOPPED) { try { - Plivo.log.info(`${LOGCAT.CALL} | Recognizing speech Starting`); + Plivo.log.info(`${LOGCAT.CALL} | Starting speech recognition`); speechListeners.call(clientObj); } catch (err) { - Plivo.log.error(`${LOGCAT.CALL} | Error in starting recognizing speech, will be restarted :: ${err.message}`); + Plivo.log.error(`${LOGCAT.CALL} | Error starting speech recognition, will be restarted :: ${err.message}`); } } } else { @@ -454,6 +456,7 @@ export class CallSession { CANCELED: 'canceled', FAILED: 'failed', ENDED: 'ended', + REDIRECTED: 'redirected', }; this.SPEECH_STATE = { @@ -463,6 +466,7 @@ export class CallSession { STOPPING: 'stopping', STOPPED_AFTER_DETECTION: 'stopped_after_detecting', STOPPED_DUE_TO_NETWORK_ERROR: "stopped_due_to_network_error", + STOPPED_DUE_TO_ABORT: "stopped_due_to_aborted", }; this.callUUID = options.callUUID ? options.callUUID : null; @@ -509,7 +513,7 @@ export class CallSession { sendCallAnsweredEvent.call(clientObject, null, true); }); this.updateSignallingInfo({ - answer_time: getCurrentTime(), + answer_time: getCurrentTime(clientObject), }); callStart.call(clientObject); startVolumeDataStreaming(clientObject); @@ -533,10 +537,10 @@ export class CallSession { }; private _onConfirmed = (clientObject: Client): void => { - this.addConnectionStage(`confirmed@${getCurrentTime()}`); + this.addConnectionStage(`confirmed@${getCurrentTime(clientObject)}`); this.setState(this.STATE.ANSWERED); this.updateSignallingInfo({ - call_confirmed_time: getCurrentTime(), + call_confirmed_time: getCurrentTime(clientObject), }); // enable expedited forwarding for dscp setEncodingParameters.call(clientObject); @@ -617,9 +621,9 @@ export class CallSession { private _onFailed = (clientObject: Client, evt: SessionFailedEvent): void => { logCandidatePairs(clientObject._currentSession); - this.addConnectionStage(`failed@${getCurrentTime()}`); + this.addConnectionStage(`failed@${getCurrentTime(clientObject)}`); this.updateSignallingInfo({ - hangup_time: getCurrentTime(), + hangup_time: getCurrentTime(clientObject), hangup_party: evt.originator, hangup_reason: evt.cause, }); @@ -632,10 +636,10 @@ export class CallSession { private _onEnded = (clientObject: Client, evt: SessionEndedEvent): void => { logCandidatePairs(clientObject._currentSession); - this.addConnectionStage(`ended@${getCurrentTime()}`); + this.addConnectionStage(`ended@${getCurrentTime(clientObject)}`); this.setState(this.STATE.ENDED); this.updateSignallingInfo({ - hangup_time: getCurrentTime(), + hangup_time: getCurrentTime(clientObject), hangup_party: evt.originator, hangup_reason: evt.cause, }); diff --git a/lib/managers/incomingCall.ts b/lib/managers/incomingCall.ts index 8a49b31..f09c86a 100644 --- a/lib/managers/incomingCall.ts +++ b/lib/managers/incomingCall.ts @@ -65,6 +65,30 @@ document.addEventListener('visibilitychange', () => { } } }); + +/** + * Play ringtone for an incoming call. + */ +export const playRingtone = () : void => { + if (cs.ringToneFlag !== false && !cs._currentSession) { + if (!mobileBrowserCheck()) { + Plivo.log.debug(`${LOGCAT.CALL} | Not a mobile browser. Playing ring tone`); + playAudio(RINGTONE_ELEMENT_ID); + } else if (!isBrowserInBackground) { + Plivo.log.debug(`${LOGCAT.CALL} | Not in background. Playing ring tone`); + playAudio(RINGTONE_ELEMENT_ID); + } + Plivo.log.debug(`${LOGCAT.CALL} | incoming call ringtone started`); + isIncomingCallRinging = true; + } +}; +export const stopRingtone = (): void => { + if (cs.ringToneView && !cs.ringToneView.paused) { + Plivo.log.debug(`${LOGCAT.CALL} | incoming call ringtone stopped`); + stopAudio(RINGTONE_ELEMENT_ID); + } +}; + /** * Update incoming call information. * @param {UserAgentNewRtcSessionEvent} evt - rtcsession information @@ -80,9 +104,9 @@ const updateSessionInfo = (evt: UserAgentNewRtcSessionEvent, call: CallSession): cs.callUUID = call.callUUID; (cs as any).direction = call.direction; } - call.addConnectionStage(`I-invite@${getCurrentTime()}`); + call.addConnectionStage(`I-invite@${getCurrentTime(cs)}`); call.updateSignallingInfo({ - invite_time: getCurrentTime(), + invite_time: getCurrentTime(cs), }); Plivo.log.debug(`callSession - ${call.callUUID}`); @@ -94,14 +118,20 @@ const updateSessionInfo = (evt: UserAgentNewRtcSessionEvent, call: CallSession): */ const onProgress = (incomingCall: CallSession) => (): void => { // allow incomming call only if permission granted - incomingCall.onRinging(cs); + // send CALL_RINGING if the tab is primary and registered + const inviteURI = (cs.getCurrentSession()?.session as any)._request.ruri._user; + if (inviteURI !== cs.userName) { + Plivo.log.debug(`${LOGCAT.CALL} | inviteURI: ${inviteURI} does not match the username`); + incomingCall.onRinging(cs); + } Plivo.log.debug(`${LOGCAT.CALL} | Incoming call ringing`); - incomingCall.addConnectionStage(`progress-180@${getCurrentTime()}`); + Plivo.log.debug(`${LOGCAT.CALL} | Incoming Call Extra Headers : ${JSON.stringify(incomingCall.extraHeaders)}`); + incomingCall.addConnectionStage(`progress-180@${getCurrentTime(cs)}`); incomingCall.updateSignallingInfo({ - call_progress_time: getCurrentTime(), + call_progress_time: getCurrentTime(cs), }); incomingCall.setState(incomingCall.STATE.RINGING); - incomingCall.setPostDialDelayEndTime(getCurrentTime()); + incomingCall.setPostDialDelayEndTime(getCurrentTime(cs)); Plivo.log.debug(`${LOGCAT.CALL} | call ringing with 180 code, incoming call in progress`); const callerUri = incomingCall.session.remote_identity.uri.toString(); // Fetch the caller name @@ -109,38 +139,39 @@ const onProgress = (incomingCall: CallSession) => (): void => { // if already on an incomingCall then do not play the ringtone Plivo.log.debug(`${LOGCAT.CALL} | ringtone enabled : ${cs.ringToneFlag}`); Plivo.log.debug(`${LOGCAT.CALL} | no session is active currently: ${!cs._currentSession}`); - if (cs.ringToneFlag !== false && !cs._currentSession) { - if (!mobileBrowserCheck()) { - Plivo.log.debug(`${LOGCAT.CALL} | Not a mobile browser. Playing ring tone`); - playAudio(RINGTONE_ELEMENT_ID); - } else if (!isBrowserInBackground) { - Plivo.log.debug(`${LOGCAT.CALL} | Not in background. Playing ring tone`); - playAudio(RINGTONE_ELEMENT_ID); - } - Plivo.log.debug(`${LOGCAT.CALL} | incoming call ringtone started`); - isIncomingCallRinging = true; - } const callerId = `${callerUri.substring( 4, callerUri.indexOf('@'), )}@${DOMAIN}`; + const emitIncomingCall = () => { + const callInfo = incomingCall.getCallInfo('local'); + if (inviteURI === cs.userName) { + Plivo.log.debug(`${LOGCAT.CALL} | setting callInfo reason to redirected since inviteURI: ${inviteURI}`); + callInfo.reason = 'redirected'; + } Plivo.log.debug(`${LOGCAT.CALL} | Emitting onIncomingCall`); cs.emit( 'onIncomingCall', callerId, incomingCall.extraHeaders, - incomingCall.getCallInfo("local"), + callInfo, callerName, ); }; + cs.noiseSuppresion.setLocalMediaStream().then(() => { + // play ringtone if the tab is primary and registered + if (inviteURI !== cs.userName) { + Plivo.log.debug(`${LOGCAT.CALL} | starting ringtone`); + playRingtone(); + } emitIncomingCall(); }).catch(() => { + Plivo.log.debug(`${LOGCAT.CALL} | Media stream cannot be fetched in ringing state. Emitting onIncomingCall`); emitIncomingCall(); }); addCloseProtectionListeners.call(cs); - Plivo.log.debug(`${LOGCAT.CALL} | Incoming Call Extra Headers : ${JSON.stringify(incomingCall.extraHeaders)}`); }; /** @@ -583,11 +614,11 @@ export const answerIncomingCall = function ( export const handleIgnoreState = (curIncomingCall: CallSession): void => { curIncomingCall.setState(curIncomingCall.STATE.IGNORED); curIncomingCall.updateSignallingInfo({ - hangup_time: getCurrentTime(), + hangup_time: getCurrentTime(cs), hangup_party: 'local', hangup_reason: 'Ignored', signalling_errors: { - timestamp: getCurrentTime(), + timestamp: getCurrentTime(cs), error_code: 'Ignored', error_description: 'Ignored', }, diff --git a/lib/managers/outgoingCall.ts b/lib/managers/outgoingCall.ts index 0950315..94b2322 100644 --- a/lib/managers/outgoingCall.ts +++ b/lib/managers/outgoingCall.ts @@ -107,10 +107,10 @@ const onTrack = (evt: RTCTrackEvent): void => { Plivo.log.debug('Outgoing remote Audio stream received'); if (!cs._currentSession) return; cs._currentSession.addConnectionStage( - `addStream-success@${getCurrentTime()}`, + `addStream-success@${getCurrentTime(cs)}`, ); cs._currentSession.updateMediaConnectionInfo({ - stream_success: getCurrentTime(), + stream_success: getCurrentTime(cs), }); if (evt.streams[0]) { // on direct 200 OK with out 18x, we get The play() request was interrupted by a new load @@ -138,10 +138,10 @@ const onTrack = (evt: RTCTrackEvent): void => { } else { Plivo.log.error(`${LOGCAT.CALL} | Outgoing call add stream failure`); cs._currentSession.addConnectionStage( - `addStream-failure@${getCurrentTime()}`, + `addStream-failure@${getCurrentTime(cs)}`, ); cs._currentSession.updateMediaConnectionInfo({ - stream_failure: getCurrentTime(), + stream_failure: getCurrentTime(cs), }); } }; @@ -152,9 +152,9 @@ const onTrack = (evt: RTCTrackEvent): void => { const onSending = (): void => { Plivo.log.debug(`${LOGCAT.CALL} | Outgoing call sending`); if (cs._currentSession) { - cs._currentSession.addConnectionStage(`O-invite@${getCurrentTime()}`); + cs._currentSession.addConnectionStage(`O-invite@${getCurrentTime(cs)}`); cs._currentSession.updateSignallingInfo({ - call_initiation_time: getCurrentTime(), + call_initiation_time: getCurrentTime(cs), }); } Plivo.log.debug('call initiation time, sending invite'); @@ -217,7 +217,7 @@ const handleProgressTone = (evt: SessionProgressEvent): void => { if (evt.response && evt.response.status_code === 183 && evt.response.body) { if (cs._currentSession) { Plivo.log.debug(`callSession - ${cs._currentSession.callUUID}`); - cs._currentSession.setPostDialDelayEndTime(getCurrentTime()); + cs._currentSession.setPostDialDelayEndTime(getCurrentTime(cs)); if (!cs.ringToneBackFlag) { if (cs.ringBackToneView && !cs.ringBackToneView.paused) { stopAudio(RINGBACK_ELEMENT_ID); @@ -237,6 +237,7 @@ const OnProgress = (evt: SessionProgressEvent): void => { cs._currentSession.onRinging(cs); const callUUID = evt.response.getHeader('X-Calluuid'); cs._currentSession.setCallUUID(callUUID); + Plivo.log.info(`${LOGCAT.CALL} | Outgoing call Ringing`); cs._currentSession.setState(cs._currentSession.STATE.RINGING); cs.callUUID = callUUID; Plivo.log.info(`${LOGCAT.CALL} | Outgoing call Ringing, CallUUID: ${cs.callUUID}`); @@ -250,13 +251,13 @@ const OnProgress = (evt: SessionProgressEvent): void => { cs._currentSession.callUUID, ); cs._currentSession.addConnectionStage( - `progress-${evt.response.status_code}@${getCurrentTime()}`, + `progress-${evt.response.status_code}@${getCurrentTime(cs)}`, ); - Plivo.log.debug(`progress-${evt.response.status_code}@${getCurrentTime()}`); + Plivo.log.debug(`progress-${evt.response.status_code}@${getCurrentTime(cs)}`); cs._currentSession.updateSignallingInfo({ - ring_start_time: getCurrentTime(), + ring_start_time: getCurrentTime(cs), }); - cs._currentSession.setPostDialDelayEndTime(getCurrentTime()); + cs._currentSession.setPostDialDelayEndTime(getCurrentTime(cs)); Plivo.log.debug('Outgoing call progress', evt.response.status_code); handleProgressTone(evt); // Will be true if user triggers mute before session is created @@ -291,7 +292,7 @@ const onAccepted = (evt: SessionAcceptedEvent): void => { cs.phone.sendReInviteOnTransportConnect = true; } cs._currentSession.onAccepted(cs); - cs._currentSession.setPostDialDelayEndTime(getCurrentTime()); + cs._currentSession.setPostDialDelayEndTime(getCurrentTime(cs)); addCallstatsIOFabric.call( cs, cs._currentSession, @@ -572,7 +573,7 @@ export const makeCall = ( Plivo.log.debug(`${LOGCAT.CALL} | Outgoing call initiating to ${phoneNumber} with headers ${JSON.stringify(extraHeaders)}`); cs = clientObject; outBoundConnectionStages = []; - outBoundConnectionStages.push(`call()@${getCurrentTime()}`); + outBoundConnectionStages.push(`call()@${getCurrentTime(cs)}`); let phoneNumberStr = ''; if (phoneNumber) { phoneNumberStr = removeSpaces(String(phoneNumber)); diff --git a/lib/managers/util.ts b/lib/managers/util.ts index cb0d360..6608901 100644 --- a/lib/managers/util.ts +++ b/lib/managers/util.ts @@ -31,8 +31,6 @@ import getBrowserDetails from '../utils/browserDetection'; const Plivo = { log: Logger, - sendEvents, - AppError, emitMetrics: _emitMetrics, }; @@ -90,9 +88,19 @@ const getSummaryEvent = async function (client: Client): Promise { * Prepare summary event when browser tab is about to close * @returns Summary event */ -export const setErrorCollector = () => { - window.onerror = (message) => { - Plivo.log.error(`${LOGCAT.CRASH} | ${message} |`, new Error().stack); +export const setErrorCollector = (client: Client) => { + window.onerror = (message, source, _c, _d, error) => { + if (!client.options.captureSDKCrashOnly) { + Plivo.log.error(`${LOGCAT.CRASH} | Error ${message} at ${error?.stack}`); + } else { + if (typeof message === 'string' && message.includes('Script error')) { + Plivo.log.error(`${LOGCAT.CRASH} | Error ${message} at ${error?.stack}`); + return; + } + if (source?.toLowerCase().includes('plivo')) { + Plivo.log.error(`${LOGCAT.CRASH} | Error ${message} at ${error?.stack}`); + } + } }; }; @@ -105,25 +113,25 @@ export const addCloseProtectionListeners = function (): void { getSummaryEvent(client).then((summaryEvent) => { if (client.options.closeProtection) { window.onbeforeunload = (event: BeforeUnloadEvent) => { - Plivo.sendEvents.call(client, summaryEvent, client._currentSession); event.preventDefault(); - // eslint-disable-next-line no-param-reassign - event.returnValue = ''; + sendEvents.call(client, summaryEvent, client._currentSession); + Plivo.log.send(client); + return 'Are you sure you want to reload'; }; } else { window.onbeforeunload = () => { - Plivo.sendEvents.call(client, summaryEvent, client._currentSession); + sendEvents.call(client, summaryEvent, client._currentSession); if (client._currentSession) { client._currentSession.session.terminate(); } + Plivo.log.send(client); }; } window.onunload = () => { if (client._currentSession) { client._currentSession.session.terminate(); } - Plivo.sendEvents.call(client, summaryEvent, client._currentSession); - Plivo.log.send(client); + sendEvents.call(client, summaryEvent, client._currentSession); }; }); }; @@ -169,7 +177,7 @@ export const replaceMdnsIceCandidates = (sdp: string): string => { /** * Get current time */ -export const getCurrentTime = (): number => Date.now(); +export const getCurrentTime = (client: Client): number => Date.now() + client.timeDiff; /** * Add stats settings to storage. @@ -339,10 +347,10 @@ export const onIceConnectionChange = async function ( Plivo.log.debug(`${LOGCAT.CALL} | oniceconnectionstatechange is ${iceState}`); fetchCandidatePairs(connection, callSession); callSession.addConnectionStage( - `iceConnectionState-${iceState}@${getCurrentTime()}`, + `iceConnectionState-${iceState}@${getCurrentTime(client)}`, ); callSession.updateMediaConnectionInfo({ - [`ice_connection_state_${iceState}`]: getCurrentTime(), + [`ice_connection_state_${iceState}`]: getCurrentTime(client), }); iceConnectionCheck.call(client, iceState); if (callSession.callUUID && connection) { @@ -372,7 +380,7 @@ export const extractReasonInfo = (reasonMessage) => { } const reasonHeader = reasonMessage.getHeader("Reason"); if (reasonHeader == null) { - Plivo.log.debug(`${LOGCAT.CALL} | No reason header present}`); + Plivo.log.debug(`${LOGCAT.CALL} | No reason header present`); return { protocol: 'none', cause: -1, text: 'none' }; } @@ -459,7 +467,7 @@ const calcConnStage = function (obj: string[]): string { * Reset and delete session information. * @param {CallSession} session - call session information */ -const clearSessionInfo = function (session: CallSession): void { +export const clearSessionInfo = function (session: CallSession): void { const client: Client = this; if (session === client._currentSession) { // audio element clearence @@ -549,16 +557,19 @@ const stopLocalStream = function (): void { /** * Clear all flags and session information. * @param {CallSession} session - call session information + * @param {boolean} isCallRedirected - redirected call */ -export const hangupClearance = function (session: CallSession) { +export const hangupClearance = function (session: CallSession, isCallRedirected: boolean = false) { try { + Plivo.log.debug(`${LOGCAT.CALL} | isCallRedirected in hangupClearance: ${isCallRedirected}`); const client: Client = this; - Plivo.AppError.call(client, calcConnStage(session.connectionStages), 'log'); + AppError.call(client, calcConnStage(session.connectionStages), 'log'); session.clearCallStats(); + client.loggerUtil.setSipCallID(''); clearSessionInfo.call(client, session); const signallingInfo = session.getSignallingInfo(); const mediaConnectionInfo = session.getMediaConnectionInfo(); - if (client.callstatskey) { + if (client.callstatskey && !isCallRedirected) { getAudioDevicesInfo .call(client) .then((deviceInfo) => { @@ -682,7 +693,7 @@ export const handleMediaError = function ( } callSession.updateSignallingInfo({ signalling_errors: { - timestamp: getCurrentTime(), + timestamp: getCurrentTime(client), error_code: errMsg, error_description: errName, }, @@ -724,6 +735,10 @@ export const setConectionInfo = function (client: Client, state: string, reason: client.connectionInfo.reason = reason; }; +/** + * Log local and remote candidate pair. + * @param {CallSession} callSession - call session instance +*/ export const logCandidatePairs = function (callSession: CallSession | null) { if (callSession) { const { candidateList } = callSession; @@ -735,6 +750,55 @@ export const logCandidatePairs = function (callSession: CallSession | null) { } }; +/** + * Remove spaces from the string passed. + * @param {string} inputString - String for which the space is to be removed. +*/ export const removeSpaces = function (inputString: string): string { return inputString.replace(/\s/g, ''); }; + +export const checkTimeDiff = (time: number): number => { + let timeDiff = 0; + let localTimeAhead: boolean = false; + if (Date.now() > time) { + timeDiff = Date.now() - time; + localTimeAhead = true; + } else { + timeDiff = time - Date.now(); + } + if (timeDiff > 2000) { + Plivo.log.info('diff in time'); + if (localTimeAhead) { + return -timeDiff; + } + return timeDiff; + } + Plivo.log.info('time in sync'); + return 0; +}; +/** + * Clears the network check options message interval. + * @param {client} Client - client instance. +*/ +export const clearOptionsInterval = function (client: Client): void { + if (client.networkChangeInterval) { + Plivo.log.debug(`${LOGCAT.NIMBUS} | network check interval running on the main thread. Clearing it`); + clearInterval(client.networkChangeInterval); + client.networkChangeInterval = null; + } else if (client.workerManager && client.workerManager.workerInstance) { + Plivo.log.debug(`${LOGCAT.NIMBUS} | network check interval running on the worker thread. Clearing it`); + client.workerManager.stopNetworkChecktimer(); + } +}; + +/** + * Generate a ramdom uuid. +*/ +export const uuidGenerator = function (): string { + const S4 = function () { + // eslint-disable-next-line no-bitwise + return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); + }; + return (`${S4()}${S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`); +}; diff --git a/lib/managers/workerManager.ts b/lib/managers/workerManager.ts new file mode 100644 index 0000000..4ea8bc7 --- /dev/null +++ b/lib/managers/workerManager.ts @@ -0,0 +1,121 @@ +/* eslint-disable import/extensions */ +/* eslint-disable no-console */ +/* eslint-disable import/prefer-default-export */ + +// eslint-disable-next-line import/extensions, import/no-cycle +import { Logger } from '../logger'; +import NetworkCheckWorker from '../networkCheckTimer.worker.ts'; +import { LOGCAT } from '../constants'; + +const Plivo = { log: Logger }; +export class WorkerManager { + workerInstance: Worker | null; + + networkCheckRunning: boolean = false; + + onTimerCallback: any; + + responseCallback: any; + + timerStartedOnMain: any; + + constructor() { + Plivo.log.debug(`${LOGCAT.NIMBUS} | starting the worker thread`); + this.workerInstance = new NetworkCheckWorker(); + this.timerStartedOnMain = false; + if (this.workerInstance) { + this.workerInstance.addEventListener('message', (msg) => { + this.onMessageReceived(msg); + }); + this.workerInstance.addEventListener('error', (error) => { + this.onErrorReceived(error); + this.workerInstance?.removeEventListener('message', () => {}); + this.workerInstance?.removeEventListener('error', () => {}); + this.workerInstance = null; + }); + } + } + + /** + * Callback triggered when error is received while starting worker thread. + * @param {ErrorEvent} msg - error message received + */ + onErrorReceived = (msg: ErrorEvent) => { + Plivo.log.debug(`${LOGCAT.NIMBUS} | received error from the worker thread ${msg}`); + this.workerInstance = null; + }; + + /** + * Callback triggered when message is received from the worker thread. + * @param {any} msg - message received + */ + onMessageReceived = (msg: any) => { + if (msg && msg.data) { + const { event } = msg.data; + if (event !== 'timerExecuted') { + Plivo.log.debug(`${LOGCAT.NIMBUS} | received event from the worker thread: ${event}`); + } + switch (event) { + case 'timerStarted': + this.responseCallback = null; + break; + + case 'timerExecuted': + if (this.timerStartedOnMain) { + Plivo.log.debug(`${LOGCAT.NIMBUS} | timer already running on the main thread`); + this.stopNetworkChecktimer(); + this.timerStartedOnMain = false; + } else if (this.onTimerCallback) { + this.onTimerCallback(); + } + break; + + case 'timerStopped': + break; + + case 'initSuccess': + break; + + default: + break; + } + } + }; + + /** + * Start the network check timer. + * @param {number} networkCheckInterval - time interval at which the timer is to be executed. + * @param {any} callback - callback to be trigerred when the timer executes. + */ + startNetworkCheckTimer = (networkCheckInterval: number, callback: any, responseCallback: any) => { + Plivo.log.debug(`${LOGCAT.NIMBUS} | starting the network check timer`); + this.networkCheckRunning = true; + if (this.workerInstance) { + this.workerInstance.postMessage({ + event: 'startTimer', + networkCheckInterval, + }); + this.responseCallback = responseCallback; + this.onTimerCallback = callback; + setTimeout(() => { + if (this.responseCallback) { + this.responseCallback(); + this.responseCallback = null; + } + }, 1000); + } + }; + + /** + * Stop the network check timer. + */ + stopNetworkChecktimer = () => { + Plivo.log.debug(`${LOGCAT.NIMBUS} | stopping the network check timer`); + this.networkCheckRunning = false; + if (this.workerInstance) { + this.workerInstance.postMessage({ + event: 'stopTimer', + }); + } + }; +} diff --git a/lib/media/audioDevice.ts b/lib/media/audioDevice.ts index 99f2f89..41ca14e 100644 --- a/lib/media/audioDevice.ts +++ b/lib/media/audioDevice.ts @@ -175,6 +175,13 @@ export const speechListeners = function (): void { client.speechRecognition.interimResults = true; client.speechRecognition.onerror = (error) => { Plivo.log.error(`${LOGCAT.CALL} | Error in Recognizing speech :`, error.error); + if (error.error === "aborted") { + Plivo.log.info(`${LOGCAT.CALL} | Speech Recognition stopped due to abort`); + client + ._currentSession?.setSpeechState(client + ._currentSession.SPEECH_STATE.STOPPED_DUE_TO_ABORT); + return; + } if (error.error === "network") { client ._currentSession?.setSpeechState(client @@ -184,12 +191,14 @@ export const speechListeners = function (): void { }; client.speechRecognition.onend = () => { - Plivo.log.info(`${LOGCAT.CALL} | Recognizing speech Stopped`); + Plivo.log.info(`${LOGCAT.CALL} | Stopped speech recognition`); if (client.isMuteCalled && (client._currentSession?.speech_state !== client._currentSession?.SPEECH_STATE.STOPPED_AFTER_DETECTION && client._currentSession?.speech_state - !== client._currentSession?.SPEECH_STATE.STOPPED_DUE_TO_NETWORK_ERROR)) { + !== client._currentSession?.SPEECH_STATE.STOPPED_DUE_TO_NETWORK_ERROR + && client._currentSession?.speech_state + !== client._currentSession?.SPEECH_STATE.STOPPED_DUE_TO_ABORT)) { client._currentSession?.setSpeechState(client._currentSession.SPEECH_STATE.STOPPED); client._currentSession?.startSpeechRecognition(client); } else { @@ -393,7 +402,7 @@ export const replaceStream = function (client: Client, constraints: any): Promis const replaceAudioTrack = function ( deviceId: string, client: Client, state: string, label: string, ): void { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} |inside replacetrack with device id : ${deviceId}`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Inside replacetrack with device id : ${deviceId}`); let constraints: MediaStreamConstraints; if (!client._currentSession) { if (currentLocalStream) { @@ -656,24 +665,24 @@ export const checkAudioDevChange = function (): void { addedDevice = device.label; if (device.kind === 'audioinput') { - Plivo.log.info(`${LOGCAT.CALL_QUALITY} Audio input device added:- `, JSON.stringify(device)); + Plivo.log.info(`${LOGCAT.CALL_QUALITY} | Audio input device added:- `, JSON.stringify(device)); setTimeout(() => { if (client && (isFirefox || isSafari)) { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting mic to ${device.deviceId} in firefox/safari`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting mic to ${device.deviceId} in firefox/safari`); client.audio.microphoneDevices.set(device.deviceId); } else if (client) { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting mic to ${isWindows && !client.options.useDefaultAudioDevice ? device.deviceId : 'default'} `); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting mic to ${isWindows && !client.options.useDefaultAudioDevice ? device.deviceId : 'default'} `); client.audio.microphoneDevices.set((isWindows && !client.options.useDefaultAudioDevice) ? device.deviceId : 'default'); } }, 200); } else if (client && device.kind === 'audiooutput') { - Plivo.log.info(`${LOGCAT.CALL_QUALITY} Audio output device added:- `, JSON.stringify(device)); + Plivo.log.info(`${LOGCAT.CALL_QUALITY} | Audio output device added:- `, JSON.stringify(device)); setTimeout(() => { if (client && isFirefox) { client.audio.speakerDevices.set(device.deviceId); - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting speaker to ${device.deviceId} in firefox`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting speaker to ${device.deviceId} in firefox`); } else if (client && !isSafari && !isFirefox) { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting speaker to ${isWindows && !client.options.useDefaultAudioDevice ? device.deviceId : 'default'} `); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting speaker to ${isWindows && !client.options.useDefaultAudioDevice ? device.deviceId : 'default'} `); client.audio.speakerDevices.set(isWindows && !client.options.useDefaultAudioDevice ? device.deviceId : 'default'); } }, 200); @@ -695,14 +704,14 @@ export const checkAudioDevChange = function (): void { } if (device.kind === 'audioinput') { - Plivo.log.info(`${LOGCAT.CALL_QUALITY} Audio input device removed:- `, JSON.stringify(device)); - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting microphone to default`); + Plivo.log.info(`${LOGCAT.CALL_QUALITY} | Audio input device removed:- `, JSON.stringify(device)); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting microphone to default`); setTimeout(() => { if (client && (isFirefox || isSafari)) { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting microphone to ${availableAudioDevices[0].deviceId} in firefox/safari`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting microphone to ${availableAudioDevices[0].deviceId} in firefox/safari`); client.audio.microphoneDevices.set(availableAudioDevices[0].deviceId); } else if (client) { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting microphone to default`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting microphone to default`); client.audio.microphoneDevices.set('default'); } }, 200); @@ -718,19 +727,19 @@ export const checkAudioDevChange = function (): void { } clientObject.remoteView = document.getElementById(REMOTE_VIEW_ID); } - Plivo.log.info(`${LOGCAT.CALL_QUALITY} Audio output device removed:- `, JSON.stringify(device)); + Plivo.log.info(`${LOGCAT.CALL_QUALITY} | Audio output device removed:- `, JSON.stringify(device)); setTimeout(() => { if (client && isFirefox) { availableAudioDevices.every((deviceObj) => { if (deviceObj.kind === 'audiooutput') { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting output to default ${deviceObj.deviceId} in firefox`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting output to default ${deviceObj.deviceId} in firefox`); if (client) client.audio.speakerDevices.set(deviceObj.deviceId); return false; } return true; }); } else if (client && !isSafari && !isFirefox) { - Plivo.log.debug(`${LOGCAT.CALL_QUALITY} Setting output to default`); + Plivo.log.debug(`${LOGCAT.CALL_QUALITY} | Setting output to default`); client.audio.speakerDevices.set('default'); } }, 200); diff --git a/lib/networkCheckTimer.worker.ts b/lib/networkCheckTimer.worker.ts new file mode 100644 index 0000000..9c2c583 --- /dev/null +++ b/lib/networkCheckTimer.worker.ts @@ -0,0 +1,39 @@ +// eslint-disable-next-line no-restricted-globals +const ctx: Worker = self as any; +let networkCheckTimer: any; + +function startTimer(interval: number) { + if (networkCheckTimer) { + clearInterval(networkCheckTimer); + networkCheckTimer = null; + } + networkCheckTimer = setInterval(() => { + ctx.postMessage({ + event: 'timerExecuted', + }); + }, interval); + + ctx.postMessage({ + event: 'timerStarted', + }); +} + +function stoptimer() { + if (networkCheckTimer) { + clearInterval(networkCheckTimer); + networkCheckTimer = null; + ctx.postMessage({ event: 'timerStopped' }); + } +} + +ctx.addEventListener("message", (msg) => { + if (msg && msg.data && msg.data.event === 'startTimer') { + startTimer(msg.data.networkCheckInterval); + } else if (msg && msg.data && msg.data.event === 'stopTimer') { + stoptimer(); + } else { + ctx.postMessage({ + event: 'initSuccess', + }); + } +}); diff --git a/lib/rnnoise/NoiseSuppressionEffect.ts b/lib/rnnoise/NoiseSuppressionEffect.ts index cb7a1de..bd7947b 100644 --- a/lib/rnnoise/NoiseSuppressionEffect.ts +++ b/lib/rnnoise/NoiseSuppressionEffect.ts @@ -139,6 +139,8 @@ export class NoiseSuppressionEffect { try { if (this.originalMediaTrack && this.outputMediaTrack) { this.originalMediaTrack.enabled = this.outputMediaTrack.enabled; + this.originalMediaTrack?.stop(); + this.outputMediaTrack?.stop(); } this.audioDestination?.disconnect(); diff --git a/lib/stats/httpRequest.ts b/lib/stats/httpRequest.ts index 993357d..472e446 100644 --- a/lib/stats/httpRequest.ts +++ b/lib/stats/httpRequest.ts @@ -8,6 +8,8 @@ import { Logger } from '../logger'; import { Client, PlivoObject } from '../client'; // eslint-disable-next-line import/no-cycle import { FeedbackObject } from '../utils/feedback'; +/* eslint-disable import/no-cycle */ +import { checkTimeDiff } from '../managers/util'; export interface CallStatsValidationResponse { data: string; @@ -207,6 +209,25 @@ export const fetchIPAddress = ( const message = new SipLib.Message(client.phone as SipLib.UA); message.on('succeeded', (data) => { if (data.response && data.response.body) { + let timestamp = data.response.getHeader('X-Timestamp'); + if (timestamp) { + timestamp = timestamp.slice(0, -3); + timestamp = timestamp.replace(".", ""); + const time = parseInt(timestamp, 10); + client.timeDiff = checkTimeDiff(time); + } else { + client.timeDiff = 0; + } + const receivedIP = (data.response.parseHeader('via') as any).received; + const receivedPort = (data.response.parseHeader('via') as any).rport; + const registrarIP = data.response.getHeaders('X-PlivoRegistrarIP')[0] ?? ''; + client.contactUri = { + name: client.userName, + ip: receivedIP, + port: receivedPort, + protocol: 'transport=ws', + registrarIP, + }; resolve(data.response.body); } else { resolve(new Error("couldn't retrieve ipaddress")); @@ -215,5 +236,8 @@ export const fetchIPAddress = ( message.on('failed', () => { resolve(new Error("couldn't retrieve ipaddress")); }); - message.send('admin', 'ipAddress', 'MESSAGE'); + + message.send('admin', 'ipAddress', 'MESSAGE', { + extraHeaders: [`X-PlivoEndpoint: ${client.userName}`], + }); }); diff --git a/lib/stats/nonRTPStats.ts b/lib/stats/nonRTPStats.ts index 3e0b7f5..cc7f083 100644 --- a/lib/stats/nonRTPStats.ts +++ b/lib/stats/nonRTPStats.ts @@ -10,6 +10,7 @@ import { Client, ConfiguationOptions } from '../client'; import * as C from '../constants'; import { FeedbackObject } from '../utils/feedback'; import getBrowserDetails from '../utils/browserDetection'; +import { getCurrentTime } from '../managers/util'; export interface AnsweredEvent{ msg: string; @@ -124,14 +125,14 @@ const Plivo = { log: Logger }; * @returns Stat message with call information */ export const addCallInfo = function ( - callSession: CallSession, statMsg: any, callstatskey: string, userName: string, + callSession: CallSession, statMsg: any, callstatskey: string, userName: string, timeStamp: number, ): object { const obj = statMsg; obj.callstats_key = callstatskey; obj.callUUID = callSession.sipCallID; obj.corelationId = callSession.sipCallID; obj.xcallUUID = callSession.callUUID; - obj.timeStamp = Date.now(); + obj.timeStamp = timeStamp; obj.userName = userName; obj.domain = C.DOMAIN; obj.source = C.STATS_SOURCE; @@ -152,7 +153,13 @@ export const sendEvents = function (statMsg: any, session: CallSession): void { && session && session.sipCallID ) { - const obj = addCallInfo(session, statMsg, client.callstatskey, client.userName as string); + const obj = addCallInfo( + session, + statMsg, + client.callstatskey, + client.userName as string, + getCurrentTime(client), + ); client.statsSocket.send(obj, client); // To do : Initiate unregister incase when call gets extended after token expiry if (client.isUnregisterPending === true) { diff --git a/lib/stats/rtpStats.ts b/lib/stats/rtpStats.ts index 50a1f4a..8a29b88 100644 --- a/lib/stats/rtpStats.ts +++ b/lib/stats/rtpStats.ts @@ -9,6 +9,119 @@ import { AudioLevel } from '../media/audioLevel'; import { processStreams } from './mediaMetrics'; import { Logger } from '../logger'; import { CallSession } from '../managers/callSession'; +import { getCurrentTime } from '../managers/util'; + +export interface LocalCandidate { + id?: string; + address?: string; + port?: string; + relatedAddress?: string; + relatedPort?: string; + candidateType?: string; + usernameFragment?: string; +} + +type LocalCandidateMap = { + [timestamp: string]: LocalCandidate; +}; + +export interface RemoteCandidate { + id?: string; + address?: string; + port?: string; + candidateType?: string; + usernameFragment?: string; +} + +export interface CandidatePair { + availableOutgoingBitrate?: string; + consentRequestsSent?: number; + id?: string; + lastPacketReceivedTimestamp?: string; + lastPacketSentTimestamp?: string; + localCandidateId?: string; + nominated?: string; + packetsDiscardedOnSend?: string; + packetsReceived?: string; + packetsSent?: string; + remoteCandidateId?: string; + requestsReceived?: number; + requestsSent?: number; + responsesReceived?: number; + responsesSent?: number; + state?: string; + transportId?: string; + writable?: boolean; +} + +export interface Transport{ + id?: string; + dtlsRole?: string; + dtlsState?: string; + iceRole?: string; + iceState?: string; + packetsReceived?: string; + packetsSent?: string; + selectedCandidatePairChanges?: number; + selectedCandidatePairId?: string; +} + +export interface OutboundRTP { + bytesSent?: number; + packetsSent?: number; + retransmittedBytesSent?: number; + retransmittedPacketsSent?: number; + transportId?: string; +} + +export interface RemoteInboundRTP { + fractionLost?: number; + packetsLost?: number; + roundTripTime?: string; + roundTripTimeMeasurements?: number; + totalRoundTripTime?: string; + transportId?: string; +} + +export interface InboundRTP { + bytesReceived?: number; + jitterBufferDelay?: string; + jitterBufferEmittedCount?: number; + jitterBufferMinimumDelay?: string; + jitterBufferTargetDelay?: string; + packetsDiscarded?: number; + packetsLost?: number; + packetsReceived?: number; + totalSamplesDuration?: string; + totalSamplesReceived?: string; + transportId?: string +} + +export interface RemoteOutboundRTP { + bytesSent?: number; + packetsSent?: number; + reportsSent?: number; + totalRoundTripTime?: string; + transportId?: string; +} + +export interface StatsDump { + msg: string; + callUUID: string; + xcallUUID: string; + source: string; + timeStamp: number, + version: string, + changedCandidatedInfo: LocalCandidateMap, + localCandidate: LocalCandidate; + remoteCandidate: RemoteCandidate; + transport: Transport; + candidatePair: CandidatePair; + outboundRTP: OutboundRTP; + remoteInboundRTP: RemoteInboundRTP; + inboundRTP: InboundRTP; + remoteOutboundRTP: RemoteOutboundRTP; +} export interface StatsLocalStream { ssrc?: number; @@ -419,6 +532,18 @@ const sendStats = function (statMsg: StatsObject): void { } }; +const sendStatsDump = function (stream: StatsDump): void { + const client: Client = this; + if ( + client.statsSocket + && client.callstatskey + && client.rtp_enabled + ) { + Plivo.log.debug(`${C.LOGCAT.CALL} | Sending CALL_STATS_DUMP Event`); + client.statsSocket.send(stream, client); + } +}; + /** * calculate media setup time * @param {Client} client @@ -444,7 +569,7 @@ const processStats = function (stream: RtpStatsStream): void { callUUID: getStatsRef.callUUID, corelationId: getStatsRef.corelationId, userName: getStatsRef.userName, - timeStamp: Date.now(), + timeStamp: getCurrentTime(getStatsRef.clientScope), domain: C.DOMAIN, source: C.STATS_SOURCE, version: C.STATS_VERSION, @@ -558,6 +683,20 @@ const handleSafariChanges = function (stream: RtpStatsStream): RtpStatsStream { return stream; }; +const checkIfIdPresent = function (candidateInfo: LocalCandidateMap, id: string) + : Promise { + return new Promise((resolve) => { + let idPresent: boolean = false; + Object.keys(candidateInfo).forEach((timestamp) => { + const localCandidate = candidateInfo[timestamp]; + if (localCandidate.id === id) { + idPresent = true; + } + }); + resolve(idPresent); + }); +}; + /** * Get RTP stats. * @param {RtpStatsStream} stream - holds local and remote stat details @@ -566,8 +705,8 @@ export const handleWebRTCStats = function (stream: RtpStatsStream): void { stream.codec = getCodecName.call(this); const senders = this.pc.getSenders(); if (senders) { - senders[0] - .getStats() + this.rtpsender = senders[0].getStats(); + this.rtpsender .then((senderResults: any[]) => { Array.from(senderResults.values()).forEach((stats: any) => { if (stats.type === 'outbound-rtp') { @@ -614,16 +753,46 @@ export const handleWebRTCStats = function (stream: RtpStatsStream): void { ) { stream.local.rtt = stats.currentRoundTripTime; } + if (stats.type === 'local-candidate') { + stream.networkType = stats.networkType; + const lc: LocalCandidate = { + id: stats.id, + address: stats.address, + port: stats.port, + relatedAddress: stats.relatedAddress, + relatedPort: stats.relatedPort, + }; + if (this.localCandidateInfo) { + const isEmpty = Object.keys(this.localCandidateInfo).length === 0; + if (isEmpty) { + this.localCandidateInfo[stats.timestamp] = lc; + } else { + try { + // Call the function and await the result + checkIfIdPresent(this.localCandidateInfo, stats.id).then( + (idPresent: boolean) => { + // Handle the result here + if (idPresent === false) { + Plivo.log.debug(`${C.LOGCAT.CALL} | Local Candidate Info Change`); + this.localCandidateInfo[stats.timestamp] = lc; + } + }, + ); + } catch (error) { + // Handle any errors that may occur during the execution of checkIfIdPresent + Plivo.log.debug(`${C.LOGCAT.CALL} | Error in getStats LocalStreams API during Local Candidate Info Check `, error.message); + } + } + } + } if (stats.type === 'media-source' && this.clientScope.browserDetails.browser === 'chrome') { stream.local.googEchoCancellationReturnLoss = Math.floor(stats.echoReturnLoss); // eslint-disable-next-line max-len stream.local.googEchoCancellationReturnLossEnhancement = Math.floor(stats.echoReturnLossEnhancement); } - if (stats.type === 'local-candidate') { - stream.networkType = stats.networkType; - } }); + this.rtpreceiver = this.pc.getReceivers()[0].getStats(); // get remote stream stats let jitterBufferDelay = 0; this.pc @@ -714,6 +883,12 @@ export class GetRTPStats { */ pc: RTCPeerConnection; + rtpsender: RTCStatsReport; + + rtpreceiver: RTCStatsReport; + + localCandidateInfo: LocalCandidateMap = {}; + /** * Unique identifier generated for a call by server * @private @@ -879,10 +1054,144 @@ export class GetRTPStats { startStatsTimer.call(this); } + public sendCallStatsDump = function sendCallStatsDump(stream: StatsDump): Promise { + return new Promise((resolve, reject) => { + const senders = this.rtpsender; + if (senders) { + senders + .then((senderResults: any[]) => { + Array.from(senderResults.values()).forEach((stats: any) => { + if (stats.type === 'local-candidate') { + stream.localCandidate.id = stats.id; + stream.localCandidate.address = stats.address; + stream.localCandidate.port = stats.port; + stream.localCandidate.relatedAddress = stats.relatedAddress; + stream.localCandidate.relatedPort = stats.relatedPort; + stream.localCandidate.candidateType = stats.candidateType; + stream.localCandidate.usernameFragment = stats.usernameFragment; + } + if (stats.type === 'remote-candidate') { + stream.remoteCandidate.id = stats.id; + stream.remoteCandidate.address = stats.address; + stream.remoteCandidate.port = stats.port; + stream.remoteCandidate.candidateType = stats.candidateType; + stream.remoteCandidate.usernameFragment = stats.usernameFragment; + } + if (stats.type === 'transport') { + stream.transport.id = stats.id; + stream.transport.dtlsRole = stats.dtlsRole; + stream.transport.dtlsState = stats.dtlsState; + stream.transport.iceRole = stats.iceRole; + stream.transport.iceState = stats.iceState; + stream.transport.packetsReceived = stats.packetsReceived; + stream.transport.packetsSent = stats.packetsSent; + stream.transport.selectedCandidatePairChanges = stats.selectedCandidatePairChanges; + stream.transport.selectedCandidatePairId = stats.selectedCandidatePairId; + } + if (stats.type === 'candidate-pair') { + stream.candidatePair.id = stats.id; + stream.candidatePair.availableOutgoingBitrate = stats.availableOutgoingBitrate; + stream.candidatePair.consentRequestsSent = stats.consentRequestsSent; + // eslint-disable-next-line max-len + stream.candidatePair.lastPacketReceivedTimestamp = stats.lastPacketReceivedTimestamp; + stream.candidatePair.lastPacketSentTimestamp = stats.lastPacketSentTimestamp; + stream.candidatePair.localCandidateId = stats.localCandidateId; + stream.candidatePair.remoteCandidateId = stats.remoteCandidateId; + stream.candidatePair.nominated = stats.nominated; + stream.candidatePair.packetsDiscardedOnSend = stats.packetsDiscardedOnSend; + stream.candidatePair.packetsReceived = stats.packetsReceived; + stream.candidatePair.packetsSent = stats.packetsSent; + stream.candidatePair.requestsReceived = stats.requestsReceived; + stream.candidatePair.requestsSent = stats.requestsSent; + stream.candidatePair.responsesReceived = stats.responsesReceived; + stream.candidatePair.responsesSent = stats.responsesSent; + stream.candidatePair.state = stats.state; + stream.candidatePair.transportId = stats.transportId; + stream.candidatePair.writable = stats.writable; + } + + if (stats.type === 'outbound-rtp') { + stream.outboundRTP.bytesSent = stats.bytesSent; + stream.outboundRTP.packetsSent = stats.packetsSent; + stream.outboundRTP.retransmittedBytesSent = stats.retransmittedBytesSent; + stream.outboundRTP.retransmittedPacketsSent = stats.retransmittedBytesSent; + stream.outboundRTP.transportId = stats.transportId; + } + if (stats.type === 'remote-inbound-rtp') { + stream.remoteInboundRTP.fractionLost = stats.fractionLost; + stream.remoteInboundRTP.packetsLost = stats.packetsLost; + stream.remoteInboundRTP.roundTripTime = stats.roundTripTime; + stream.remoteInboundRTP.roundTripTimeMeasurements = stats.roundTripTimeMeasurements; + stream.remoteInboundRTP.totalRoundTripTime = stats.totalRoundTripTime; + stream.remoteInboundRTP.transportId = stats.transportId; + } + }); + stream.changedCandidatedInfo = this.localCandidateInfo; + }) + .catch((e: any) => { + Plivo.log.debug(`${C.LOGCAT.CALL} | Error during CALL_STATS_DUMP event creation `, e.message); + }); + } + const receivers = this.rtpreceiver; + if (receivers) { + receivers + .then((receiverResults: any[]) => { + Array.from(receiverResults.values()).forEach((stats: any) => { + if (stats.type === 'inbound-rtp') { + stream.inboundRTP.bytesReceived = stats.bytesReceived; + stream.inboundRTP.jitterBufferDelay = stats.jitterBufferDelay; + stream.inboundRTP.jitterBufferEmittedCount = stats.jitterBufferEmittedCount; + stream.inboundRTP.jitterBufferMinimumDelay = stats.jitterBufferMinimumDelay; + stream.inboundRTP.jitterBufferTargetDelay = stats.jitterBufferTargetDelay; + stream.inboundRTP.packetsDiscarded = stats.packetsDiscarded; + stream.inboundRTP.packetsLost = stats.packetsLost; + stream.inboundRTP.packetsReceived = stats.packetsReceived; + stream.inboundRTP.totalSamplesDuration = stats.totalSamplesDuration; + stream.inboundRTP.totalSamplesReceived = stats.totalSamplesDuration; + stream.inboundRTP.transportId = stats.transportId; + } + if (stats.type === 'remote-outbound-rtp') { + stream.remoteOutboundRTP.bytesSent = stats.bytesSent; + stream.remoteOutboundRTP.packetsSent = stats.packetsSent; + stream.remoteOutboundRTP.reportsSent = stats.reportsSent; + stream.remoteOutboundRTP.totalRoundTripTime = stats.totalRoundTripTime; + stream.remoteOutboundRTP.transportId = stats.transportId; + } + }); + Plivo.log.debug(`${C.LOGCAT.CALL} | Created CALL_STATS_DUMP Event`); + sendStatsDump.call(this.clientScope, stream); + }) + .catch((e: any) => { + Plivo.log.debug(`${C.LOGCAT.CALL} | Error during CALL_STATS_DUMP event creation `, e.message); + reject(new Error('Error in getStats LocalStreams API')); + }); + } + resolve(); + }); + }; + /** * Stop analysing audio levels for local and remote streams. */ - public stop = (): void => { + public stop = async (): Promise => { + const stream: StatsDump = { + msg: 'CALL_STATS_DUMP', + callUUID: this.callUUID, + xcallUUID: this.xcallUUID, + source: C.STATS_SOURCE, + timeStamp: Date.now(), + version: C.STATS_VERSION, + changedCandidatedInfo: {}, + localCandidate: {}, + remoteCandidate: {}, + transport: {}, + candidatePair: {}, + outboundRTP: {}, + remoteInboundRTP: {}, + inboundRTP: {}, + remoteOutboundRTP: {}, + }; + this.sendCallStatsDump(stream); this.localAudioLevelHelper.stop(); this.remoteAudioLevelHelper.stop(); }; diff --git a/lib/stats/ws.ts b/lib/stats/ws.ts index 70f7f75..e35c71f 100644 --- a/lib/stats/ws.ts +++ b/lib/stats/ws.ts @@ -199,6 +199,9 @@ export class StatsSocket { } return true; } + if (message.msg === 'CALL_STATS_DUMP') { + Plivo.log.debug('retrying to send call stats dump event'); + } if (message.msg === 'CALL_SUMMARY') { Plivo.log.debug('retrying to send call summary event'); if (retryAttempts === C.SOCKET_SEND_STATS_RETRY_ATTEMPTS) { diff --git a/lib/utils/loggerUtil.ts b/lib/utils/loggerUtil.ts index 828b789..ba29dde 100644 --- a/lib/utils/loggerUtil.ts +++ b/lib/utils/loggerUtil.ts @@ -10,6 +10,8 @@ export class LoggerUtil { private userName: string = ""; + private identifier: string = ""; + private client: Client; constructor(client: Client) { @@ -43,4 +45,12 @@ export class LoggerUtil { setUserName(value: string): void { this.userName = value; } + + setIdentifier(identifier: string) { + this.identifier = identifier; + } + + getIdentifier(): string { + return this.identifier; + } } diff --git a/lib/utils/networkManager.ts b/lib/utils/networkManager.ts index 81a8161..bc259c1 100644 --- a/lib/utils/networkManager.ts +++ b/lib/utils/networkManager.ts @@ -7,7 +7,7 @@ import { LOGCAT, WS_RECONNECT_RETRY_COUNT, WS_RECONNECT_RETRY_INTERVAL } from '. import { Logger } from '../logger'; import { sendEvents } from '../stats/nonRTPStats'; import { createStatsSocket } from '../stats/setup'; -import { setConectionInfo } from '../managers/util'; +import { clearOptionsInterval, getCurrentTime, setConectionInfo } from '../managers/util'; interface PingPong { client: Client @@ -43,7 +43,7 @@ export const socketReconnectionRetry = (client) => { Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Starting the connection interval`); client.connectionRetryInterval = setInterval(() => { Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Checking WS connection status with count ${socketReconnectCount}`); - if ((client.phone as any)._transport.socket._ws + if (client.phone && (client.phone as any)._transport.socket._ws && (client.phone as any)._transport.socket._ws.readyState === 0) { Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | WS is not ready state`); if (socketReconnectCount >= WS_RECONNECT_RETRY_COUNT) { @@ -59,7 +59,7 @@ export const socketReconnectionRetry = (client) => { socketReconnectCount += 1; } } else { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | WS is in ready state or WS instance is available: ${!!((client.phone as any)._transport.socket._ws)}. Clearing the connection check interval`); + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | WS is in ready state or WS instance is available: ${!!(client.phone && (client.phone as any)._transport.socket._ws)}. Clearing the connection check interval`); clearInterval(client.connectionRetryInterval as any); client.connectionRetryInterval = null; } @@ -153,95 +153,132 @@ export const reconnectSocket = (client: Client) => { } }; +const sendOptions = (client: Client, messageCheckTimeout: number) => { + if (!isConnected) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Checking for network availablity`); + } + if ((client.phone && (!client.phone.isConnected() || (client.connectionStatus === 'registered' && !client.phone.isRegistered())))) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Websocket state is connecting: ${(client.phone as any)._transport.isConnecting()}, registering: ${(client.phone as any).isRegistering()}, connected: ${client.phone.isConnected()} or registered: ${client.phone.isRegistered()}. Is Internet available ${navigator.onLine}`); + } else if (!client.phone) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Websocket instance is not available. Is Internet available ${navigator.onLine}`); + } + if ( + navigator.onLine + && client.phone + && !(client.phone as any)._transport.isConnecting() + && !(client.phone as any).isRegistering() + ) { + let isFailedMessageTriggered = false; + let isReconnectionStarted = false; + let message: null | SipLib.Message = null; + client.networkDisconnectedTimestamp = getCurrentTime(client); + if (!isConnected) { + isConnected = true; + isReconnectionStarted = true; + if (client._currentSession && client._currentSession.serverFeatureFlags.indexOf('ft_info') === -1) { + const negotiationStarted = client._currentSession.session.renegotiate({ + rtcOfferConstraints: { iceRestart: true }, + }); + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Restarting Connection and Renegotiate Ice :: ${negotiationStarted}`); + } else { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Restarting Connection`); + (client.phone as any)._transport.connect(); + socketReconnectionRetry(client); + } + } + // timeout to check whether we receive failed event in 5 seconds + const eventCheckTimeout = setTimeout(() => { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Ping request timed out in ${messageCheckTimeout}. Restarting the connection if options response not received: ${!isFailedMessageTriggered}`); + if (!isFailedMessageTriggered) { + isReconnectionStarted = true; + setConectionInfo(client, ConnectionState.DISCONNECTED, "Ping Timed Out"); + reconnectSocket(client); + message = null; + } + clearTimeout(eventCheckTimeout); + }, messageCheckTimeout); + + message = new SipLib.Message(client.phone); + message.on('failed', (err) => { + isFailedMessageTriggered = true; + if (eventCheckTimeout) clearTimeout(eventCheckTimeout); + // reconnect if ping OPTIONS packet fails OR UA is not connected/registered + if (!isReconnectionStarted + && (err.cause !== 'Not Found' + || !client.phone?.isConnected() + || (client.connectionStatus === 'registered' && !client.phone?.isRegistered()))) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Ping request failed with err: ${err.cause}, Client is connected: ${client.phone?.isConnected()} and client is registered: ${client.phone?.isRegistered()}. Restarting the connection`); + setConectionInfo(client, ConnectionState.DISCONNECTED, `Ping failed with err ${err.cause}`); + reconnectSocket(client); + message = null; + } + isReconnectionStarted = false; + client.emit('onOptionsMessage', err.cause); + }); + + message.send('admin', 'pong', 'OPTIONS', { + identifier: client.identifier, + }); + } + + if (!navigator.onLine && client.phone + && !(client.phone as any)._transport.isConnecting() + && !(client.phone as any).isRegistering() + && isConnected) { + const activeSession = client.lastIncomingCall ?? client._currentSession; + if (activeSession && activeSession.state === activeSession.STATE.RINGING) { + activeSession.isCallTerminatedDuringRinging = true; + if (activeSession.direction === 'incoming') { + activeSession.session.terminate(); + } + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Terminating ${activeSession.direction} call with calluuid ${activeSession.callUUID} due to network change in ringing state`); + } + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Websocket disconnected since internet is not available`); + setConectionInfo(client, ConnectionState.DISCONNECTED, `No Internet`); + (client.phone as any)._transport.disconnect(true); + isConnected = false; + } +}; + export const startPingPong = ({ client, networkChangeInterval, messageCheckTimeout, }: PingPong) => { - if (client.networkChangeInterval == null) { + const startTimerOnMain = () => { client.networkChangeInterval = setInterval(() => { - // send message only when there is active network connect - if (!isConnected) { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Checking for network availablity`); - } - if ((client.phone && (!client.phone.isConnected() || !client.phone.isRegistered()))) { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Websocket state is connecting: ${(client.phone as any)._transport.isConnecting()}, registering: ${(client.phone as any).isRegistering()}, connected: ${client.phone.isConnected()} or registered: ${client.phone.isRegistered()}. Is Internet available ${navigator.onLine}`); - } else if (!client.phone) { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Websocket instance is not available. Is Internet available ${navigator.onLine}`); - } - if ( - navigator.onLine - && client.phone - && !(client.phone as any)._transport.isConnecting() - && !(client.phone as any).isRegistering() - ) { - let isFailedMessageTriggered = false; - let isReconnectionStarted = false; - let message: null | SipLib.Message = null; - client.networkDisconnectedTimestamp = new Date().getTime(); - if (!isConnected) { - isConnected = true; - isReconnectionStarted = true; - if (client._currentSession && client._currentSession.serverFeatureFlags.indexOf('ft_info') === -1) { - const negotiationStarted = client._currentSession.session.renegotiate({ - rtcOfferConstraints: { iceRestart: true }, - }); - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Restarting Connection and Renegotiate Ice :: ${negotiationStarted}`); - } else { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Restarting Connection`); - (client.phone as any)._transport.connect(); - socketReconnectionRetry(client); - } - } - // timeout to check whether we receive failed event in 5 seconds - const eventCheckTimeout = setTimeout(() => { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Ping request timed out in ${messageCheckTimeout}. Restarting the connection if options response not received: ${!isFailedMessageTriggered}`); - if (!isFailedMessageTriggered) { - isReconnectionStarted = true; - setConectionInfo(client, ConnectionState.DISCONNECTED, "Ping Timed Out"); - reconnectSocket(client); - message = null; - } - clearTimeout(eventCheckTimeout); - }, messageCheckTimeout); - - message = new SipLib.Message(client.phone); - message.on('failed', (err) => { - isFailedMessageTriggered = true; - if (eventCheckTimeout) clearTimeout(eventCheckTimeout); - // reconnect if ping OPTIONS packet fails OR UA is not connected/registered - if (!isReconnectionStarted - && (err.cause !== 'Not Found' - || !client.phone?.isConnected() - || !client.phone?.isRegistered())) { - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Ping request failed with err: ${err.cause}, Client is connected: ${client.phone?.isConnected()} and client is registered: ${client.phone?.isRegistered()}. Restarting the connection`); - setConectionInfo(client, ConnectionState.DISCONNECTED, `Ping failed with err ${err.cause}`); - reconnectSocket(client); - message = null; - } - isReconnectionStarted = false; - client.emit('onOptionsMessage', err.cause); - }); - message.send('admin', 'pong', 'OPTIONS'); - } - if (!navigator.onLine && client.phone - && !(client.phone as any)._transport.isConnecting() - && !(client.phone as any).isRegistering() - && isConnected) { - const activeSession = client.lastIncomingCall ?? client._currentSession; - if (activeSession && activeSession.state === activeSession.STATE.RINGING) { - activeSession.isCallTerminatedDuringRinging = true; - if (activeSession.direction === 'incoming') { - activeSession.session.terminate(); - } - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Terminating ${activeSession.direction} call with calluuid ${activeSession.callUUID} due to network change in ringing state`); - } - Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Websocket disconnected since internet is not available`); - setConectionInfo(client, ConnectionState.DISCONNECTED, `No Internet`); - (client.phone as any)._transport.disconnect(true); - isConnected = false; + if (client.workerManager) { + client.workerManager.workerInstance = null; } + sendOptions(client, messageCheckTimeout); }, networkChangeInterval); + }; + + const responseCallback = (response) => { + if (!response) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | network check timer cannot be started on the worker thread. Starting it on the main thread`); + client.workerManager.networkCheckRunning = false; + client.workerManager.timerStartedOnMain = true; + startTimerOnMain(); + } + }; + + if (client.workerManager + && client.workerManager.workerInstance) { + if (!client.workerManager.networkCheckRunning) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | starting the network check timer from the worker thread`); + const timerCallback = sendOptions.bind(null, client, messageCheckTimeout); + client.workerManager.startNetworkCheckTimer( + networkChangeInterval, + timerCallback, + responseCallback, + ); + } else { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | network check timer already running in the worker thread`); + } + } else if (client.networkChangeInterval === null) { + Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | starting the network check timer from the main thread`); + startTimerOnMain(); } }; @@ -250,9 +287,8 @@ export const resetPingPong = ({ networkChangeInterval, messageCheckTimeout, }: PingPong) => { - clearInterval(client.networkChangeInterval as any); - client.networkChangeInterval = null; Plivo.log.debug(`${LOGCAT.NETWORK_CHANGE} | Re-Starting the ping-pong with interval: ${networkChangeInterval} and timeout: ${messageCheckTimeout}`); + clearOptionsInterval(client); startPingPong({ client, messageCheckTimeout, diff --git a/lib/utils/oneWayAudio.ts b/lib/utils/oneWayAudio.ts index 9571421..f5127fa 100644 --- a/lib/utils/oneWayAudio.ts +++ b/lib/utils/oneWayAudio.ts @@ -221,7 +221,7 @@ export const owaCallback = function ( onError(`media - ${err.name}`); return false; } - Plivo.log.debug(`${LOGCAT.LOGIN} getUserMedia precheck `, res); + Plivo.log.debug(`${LOGCAT.LOGIN} | getUserMedia precheck `, res); if (Number(res.bytesSent) === 0 && Number(res.audioInputLevel) === 0) { Plivo.log.error(`${LOGCAT.CALL} | chrome lost access to microphone - restart browser`, err.message); emitMetrics.call( diff --git a/lib/utils/options.ts b/lib/utils/options.ts index 0cd572f..861662d 100644 --- a/lib/utils/options.ts +++ b/lib/utils/options.ts @@ -34,7 +34,9 @@ const _options: ConfiguationOptions = { dtmfOptions: C.DEFAULT_DTMFOPTIONS, enableNoiseReduction: false, usePlivoStunServer: false, + stopAutoRegisterOnConnect: false, registrationRefreshTimer: C.REGISTER_EXPIRES_SECONDS, + captureSDKCrashOnly: false, }; /** @@ -202,6 +204,13 @@ const validateOptions = function ( } break; + case 'stopAutoRegisterOnConnect': + if (isBoolean(key, options[key])) { + this.stopAutoRegisterOnConnect = options[key]; + _options.stopAutoRegisterOnConnect = options[key]; + } + break; + case 'audioConstraints': if (options[key] && typeof options[key] === 'object') { _options.audioConstraints = options[key]; @@ -244,6 +253,12 @@ const validateOptions = function ( _options.appSecret = options[key]; break; + case 'captureSDKCrashOnly': + if (isBoolean(key, options[key])) { + _options.captureSDKCrashOnly = options[key]; + } + break; + case 'appId': _options.appId = options[key]; break; diff --git a/package.json b/package.json index 6c3e86b..67cda07 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "plivo-browser-sdk", "title": "plivo-browser-sdk", - "version": "2.2.11", + "version": "2.2.11-beta.0", "description": "This library allows you to connect with plivo's voice enviroment from browser", "main": "./dist/plivobrowsersdk.js", "types": "index.d.ts", @@ -52,9 +52,9 @@ "debug": "^4.2.0", "events": "^3.2.0", "null-loader": "4.0.1", + "plivo-jssip": "^1.0.4", "sdp-transform": "^2.14.0", "semver-parser": "^3.0.5", - "plivo-jssip": "^1.0.3", "uuid": "^8.3.0", "wasm-feature-detect": "^1.5.1", "wasm-pack": "^0.12.1", @@ -121,6 +121,7 @@ "wasm-feature-detect": "^1.5.1", "webpack": "^4.47.0", "webpack-cli": "^3.3.12", - "webpack-dev-server": "^3.11.0" + "webpack-dev-server": "^3.11.0", + "worker-loader": "^3.0.8" } } diff --git a/test/integration/eventemitter.js b/test/integration/eventemitter.js index c1176c1..d6987a4 100644 --- a/test/integration/eventemitter.js +++ b/test/integration/eventemitter.js @@ -100,6 +100,8 @@ describe('plivoWebSdk', function () { // eslint-disable-next-line no-undef after(() => { + Client1.logout(); + Client2.logout(); spy.restore(); resetListners(); }); diff --git a/test/integration/multiTabCalling.js b/test/integration/multiTabCalling.js new file mode 100644 index 0000000..b8f915e --- /dev/null +++ b/test/integration/multiTabCalling.js @@ -0,0 +1,543 @@ +/* eslint-disable no-underscore-dangle */ +/* eslint-disable @typescript-eslint/naming-convention */ +import sinon from 'sinon'; +import { Client } from '../../lib/client'; +import { on } from 'events'; +import { replaceStream } from '../../lib/media/audioDevice'; + +const options = { + debug: "ALL", + permOnClick: true, + codecs: ["OPUS", "PCMU"], + enableIPV6: false, + audioConstraints: { optional: [{ googAutoGainControl: false }] }, + dscp: true, + enableTracking: true, + dialType: "conference", + stopAutoRegisterOnConnect: true, +}; + +const Client1 = new Client(options); +const Client2 = new Client(options); +const Client3 = new Client(options); +const Client4 = new Client(options); + +const primary_user = process.env.PLIVO_ENDPOINT1_USERNAME; +const primary_pass = process.env.PLIVO_ENDPOINT1_PASSWORD; + +const secondary_user = process.env.PLIVO_ENDPOINT2_USERNAME; +const secondary_pass = process.env.PLIVO_ENDPOINT2_PASSWORD; + +// eslint-disable-next-line no-undef +describe('plivoWebSdk', function () { + const GLOBAL_TIMEOUT = 240000; + this.timeout(GLOBAL_TIMEOUT); + const TIMEOUT = 10000; + let bailTimer; + + // eslint-disable-next-line no-undef + describe('multi-tab-calling', function () { + this.timeout(GLOBAL_TIMEOUT); + + const events = {}; + let spyOnSocket; + + + const clientEvents = [ + 'onLogin', + 'onIncomingCall', + 'onCallAnswered', + 'onCallTerminated', + 'onWebsocketConnected', + 'onCallFailed', + 'onLogout', + 'onIncomingCallCanceled', + 'onIncomingCallIgnored', + ]; + + const noOfClients = 4; + for (let i = 1; i <= noOfClients; i += 1) { + clientEvents.forEach((eventName) => { + events[`client${i}-${eventName}`] = { status: false }; + }); + } + + let bail = false; + + function waitUntilExecuted(boolObj, value, callback, delay) { + // if delay is undefined or is not an integer + const newDelay = typeof delay === "undefined" || Number.isNaN(parseInt(delay, 10)) + ? 100 + : delay; + + let executionTimer = setTimeout(() => { + let isTruthy = 0; + boolObj.forEach((event) => { + if (event.status === value) { + isTruthy += 1; + } + }); + if (isTruthy === boolObj.length) { + clearTimeout(executionTimer); + executionTimer = null; + callback(); + } else { + waitUntilExecuted(boolObj, value, callback, newDelay); + } + }, newDelay); + } + + // eslint-disable-next-line no-undef + before(() => { + Client1.login(primary_user, primary_pass); + Client4.login(secondary_user, secondary_pass); + clientEvents.forEach((event) => { + Client1.on(event, () => { + events[`client1-${event}`].status = true; + }); + Client2.on(event, () => { + events[`client2-${event}`].status = true; + }); + Client3.on(event, () => { + events[`client3-${event}`].status = true; + }); + Client4.on(event, () => { + events[`client4-${event}`].status = true; + }); + }); + }); + + function reset() { + const keys = Object.keys(events); + // reset all the flags + keys.forEach((key) => { + events[key].status = false; + }); + clearTimeout(bailTimer); + } + + // eslint-disable-next-line no-undef + beforeEach((done) => { + const keys = Object.keys(events); + // reset all the flags + keys.forEach((key) => { + if (key.includes('onIncomingCall')) { + events[key].status = false; + } + }); + done(); + clearTimeout(bailTimer); + }); + + // eslint-disable-next-line no-undef + after(() => { + Client1.logout(); + Client2.logout(); + Client3.logout(); + Client4.logout(); + spyOnSocket.restore(); + }); + + // eslint-disable-next-line no-undef + afterEach((done) => { + done(); + }); + + // #5 + // eslint-disable-next-line no-undef + it('call between two registered clients should work properly', (done) => { + console.log('call between two registered should work properly'); + if (bail) { + done(new Error('bailing')); + } + reset(); + + waitUntilExecuted([events['client1-onWebsocketConnected'], events['client4-onWebsocketConnected']], true, () => { + Client1.register(); + Client4.register(); + waitUntilExecuted([events['client1-onLogin'], events['client4-onLogin']], true, () => { + Client4.call(primary_user, { + 'X-Ph-Random': 'true', + }); + }, 500); + }, 500); + + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + waitUntilExecuted([events['client1-onIncomingCallCanceled']], true, done, 500); + setTimeout(() => { + Client4.hangup(); + }, 1000); + }, 500); + + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('incoming call should only come on registered client', (done) => { + console.log('incoming call should only come on registered client'); + if (bail) { + done(new Error('bailing')); + } + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + console.log('dafsfdgf'); + waitUntilExecuted([events['client2-onIncomingCall'], events['client3-onIncomingCall']], false, done, 1000); + }, 500); + + Client2.login(primary_user, primary_pass); + Client3.login(primary_user, primary_pass); + waitUntilExecuted([events['client2-onWebsocketConnected'], events['client3-onWebsocketConnected']], true, () => { + Client4.call(primary_user, { + 'X-Ph-Random': 'true', + }); + }, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + it('incoming call should be rejected from the registered tab', (done) => { + console.log('incoming call should be rejected from the registered tab'); + if (bail) { + done(new Error('bailing')); + } + Client1.reject(); + waitUntilExecuted([events['client1-onCallFailed']], true, done, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + it('incoming call should be ignored from the registered tab', (done) => { + console.log('incoming call should be ignored from the registered tab'); + if (bail) { + done(new Error('bailing')); + } + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + Client1.ignore(); + waitUntilExecuted([events['client1-onIncomingCallIgnored']], true, done, 1000); + }, 2000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + it('incoming call should be muted and unmuted from the registered tab', (done) => { + console.log('incoming call should be ignored from the registered tab'); + if (bail) { + done(new Error('bailing')); + } + + function unmute() { + spyOnSocket.resetHistory(); + Client1.unmute(); + console.log('value of unmute called is ', spyOnSocket.calledWith(sinon.match.has("msg", "TOGGLE_MUTE"))); + events['client1-onUnmute'] = { + status: spyOnSocket.calledWith(sinon.match.has("msg", "TOGGLE_MUTE")), + }; + waitUntilExecuted([events['client1-onUnmute']], true, done, 1000); + } + + function mute() { + Client1.mute(); + events['client1-onMute'] = { + status: spyOnSocket.calledWith(sinon.match.has("msg", "TOGGLE_MUTE")), + }; + waitUntilExecuted([events['client1-onMute']], true, unmute, 1000); + } + + Client4.hangup(); + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + spyOnSocket = sinon.spy(Client1.statsSocket, "send"); + Client1.answer(); + waitUntilExecuted([events['client1-onCallAnswered']], true, mute, 1000); + }, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be redirected to other unregistered clients', (done) => { + console.log('incoming call should be redirected to other unregistered clients'); + if (bail) { + done(new Error('bailing')); + } + + Client1.hangup(); + waitUntilExecuted([events['client4-onCallTerminated']], true, () => { + Client4.call(primary_user, { + 'X-Ph-Random': 'true', + }); + }, 1000); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + console.log('redirecting the call'); + if (!Client2.reject() && !Client2.ignore()) { + Client1.redirect(Client2.getContactUri()); + } + }, 1000); + waitUntilExecuted([events['client2-onIncomingCall']], true, () => { + // session should be present in both client1 and client2 + if (Client1.getCurrentSession() === null && Client2.getCurrentSession() !== null) { + done(); + } + }, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be answered from the unregistered tab', (done) => { + console.log('incoming call should be answered from the unregistered tab'); + if (bail) { + done(new Error('bailing')); + } + Client2.answer(); + waitUntilExecuted([events['client2-onCallAnswered']], true, () => { + // session should be present in both client1 and client2 + if (Client1.getCurrentSession() === null && Client2.getCurrentSession() !== null) { + spyOnSocket = sinon.spy(Client2.statsSocket, "send"); + done(); + } + }, 200); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + it('incoming call should be muted/unmuted from the unregistered tab', (done) => { + console.log('incoming call should be muted/unmuted from the unregistered tab'); + if (bail) { + done(new Error('bailing')); + } + + spyOnSocket.resetHistory(); + function unmute() { + spyOnSocket.resetHistory(); + Client2.unmute(); + console.log('value of unmute called is ', spyOnSocket.calledWith(sinon.match.has("msg", "TOGGLE_MUTE"))); + events['client2-onUnmute'] = { + status: spyOnSocket.calledWith(sinon.match.has("msg", "TOGGLE_MUTE")), + }; + waitUntilExecuted([events['client2-onUnmute']], true, done, 50); + } + + Client2.mute(); + events['client2-onMute'] = { + status: spyOnSocket.calledWith(sinon.match.has("msg", "TOGGLE_MUTE")), + }; + waitUntilExecuted([events['client2-onMute']], true, unmute, 50); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be hangup from the unregistered tab and registered tab', (done) => { + console.log('incoming call should be hangup from the unregistered tab and registered tab'); + if (bail) { + done(new Error('bailing')); + } + + Client2.hangup(); + waitUntilExecuted([events['client2-onCallTerminated']], true, () => { + // session should be present in both client1 and client2 + if (Client1.getCurrentSession() === null && Client2.getCurrentSession() === null) { + Client4.hangup(); + waitUntilExecuted([events['client4-onCallTerminated']], true, done, 3000); + } + }, 2000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('incoming call should sent call-insights event from registered tab when answered from it', (done) => { + console.log('incoming call should sent call-insights event from registered tab when answered from it'); + if (bail) { + done(new Error('bailing')); + } + + const listenCallInsightsEvent = (eventName, callback) => { + spyOnSocket.resetHistory(); + let interval = setInterval(() => { + const value = spyOnSocket.calledWith(sinon.match.has("msg", eventName)); + if (value) { + // events[`client1-${eventName}`].status = true; + clearInterval(interval); + interval = null; + callback(); + } + }, 10); + }; + + events['client1-onCallAnswered'].status = false; + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + spyOnSocket = sinon.spy(Client1.statsSocket, "send"); + listenCallInsightsEvent('CALL_RINGING', () => { + Client1.answer(); + listenCallInsightsEvent('CALL_ANSWERED', () => { + listenCallInsightsEvent('CALL_STATS', () => { + Client1.hangup(); + listenCallInsightsEvent('CALL_SUMMARY', () => { + done(); + }); + }); + }); + }); + }, 500); + }); + + // eslint-disable-next-line no-undef + it('incoming call should sent call-insights event from registered and unregistered tab when answered from unregistered', (done) => { + console.log('incoming call should sent call-insights event from registered and unregistered tab when answered from unregistered'); + if (bail) { + done(new Error('bailing')); + } + + const listenCallInsightsEvent = (spySocket, eventName, callback) => { + spySocket.resetHistory(); + let interval = setInterval(() => { + const value = spySocket.calledWith(sinon.match.has("msg", eventName)); + if (value) { + clearInterval(interval); + interval = null; + callback(); + } + }, 10); + }; + + spyOnSocket.resetHistory(); + Client4.hangup(); + events['client2-onCallAnswered'].status = false; + events['client1-onCallAnswered'].status = false; + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + spyOnSocket = sinon.spy(Client1.statsSocket, "send"); + listenCallInsightsEvent(spyOnSocket, 'CALL_RINGING', () => { + Client1.redirect(Client2.getContactUri()); + waitUntilExecuted([events['client2-onIncomingCall']], true, () => { + const spyOnSocket2 = sinon.spy(Client2.statsSocket, "send"); + replaceStream(Client4, { + audio: true, video: false, + }).then(() => { + Client2.answer(); + listenCallInsightsEvent(spyOnSocket2, 'CALL_ANSWERED', () => { + listenCallInsightsEvent(spyOnSocket2, 'CALL_STATS', () => { + Client2.hangup(); + listenCallInsightsEvent(spyOnSocket2, 'CALL_SUMMARY', () => { + spyOnSocket2.resetHistory(); + Client2.hangup(); + Client1.hangup(); + done(); + }); + }); + }); + }); + }, 500); + }); + }, 500); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be ended when logout is called on unregistered client in ringing state', (done) => { + console.log('incoming call should be ended when logout is called on unregistered client in ringing state'); + if (bail) { + done(new Error('bailing')); + } + + Client4.hangup(); + events['client2-onCallFailed'].status = false; + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + Client1.redirect(Client2.getContactUri()); + waitUntilExecuted([events['client2-onIncomingCall']], true, () => { + Client2.logout(); + waitUntilExecuted([events['client2-onCallFailed'], events['client2-onLogout']], true, done, 1000); + }, 1000); + }, 500); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be ended when logout is called on unregistered client in answered state', (done) => { + console.log('incoming call should be ended when logout is called on unregistered client in answered state'); + if (bail) { + done(new Error('bailing')); + } + + Client4.hangup(); + Client1.hangup(); + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + Client1.redirect(Client3.getContactUri()); + waitUntilExecuted([events['client3-onIncomingCall']], true, () => { + Client3.answer(); + waitUntilExecuted([events['client3-onCallAnswered']], true, () => { + Client3.logout(); + waitUntilExecuted([events['client3-onCallTerminated'], events['client3-onLogout']], true, done, 1000); + }, 1000); + }, 1000); + }, 500); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be ended when logout is called on registered client in ringing state', (done) => { + console.log('incoming call should be ended when logout is called on registered client in ringing state'); + if (bail) { + done(new Error('bailing')); + } + + Client4.hangup(); + Client1.hangup(); + events['client1-onCallTerminated'].status = false; + events['client1-onCallFailed'].status = false; + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + Client1.logout(); + waitUntilExecuted([events['client1-onCallFailed'], events['client1-onLogout']], true, done, 1000); + }, 500); + }); + + // eslint-disable-next-line no-undef + it('incoming call should be ended when logout is called on registered client in answered state', (done) => { + console.log('incoming call should be ended when logout is called on registered client in answered state'); + if (bail) { + done(new Error('bailing')); + } + + events['client1-onCallTerminated'].status = false; + events['client1-onCallAnswered'].status = false; + events['client1-onWebsocketConnected'].status = false; + events['client1-onLogin'].status = false; + Client1.login(primary_user, primary_pass); + waitUntilExecuted([events['client1-onWebsocketConnected']], true, () => { + Client1.register(); + waitUntilExecuted([events['client1-onLogin']], true, () => { + Client4.call(primary_user); + waitUntilExecuted([events['client1-onIncomingCall']], true, () => { + Client1.answer(); + waitUntilExecuted([events['client1-onCallAnswered']], true, () => { + Client1.logout(); + waitUntilExecuted([events['client1-onCallTerminated'], events['client1-onLogout']], true, done, 1000); + }, 1000); + }, 500); + }, 1000); + }, 1000); + }); + }); +}); diff --git a/test/integration/multiTabLogin.js b/test/integration/multiTabLogin.js new file mode 100644 index 0000000..2a458a5 --- /dev/null +++ b/test/integration/multiTabLogin.js @@ -0,0 +1,290 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { Client } from '../../lib/client'; + +const options = { + debug: "ALL", + permOnClick: true, + codecs: ["OPUS", "PCMU"], + enableIPV6: false, + audioConstraints: { optional: [{ googAutoGainControl: false }] }, + dscp: true, + enableTracking: true, + dialType: "conference", + stopAutoRegisterOnConnect: true, +}; + +const Client1 = new Client(options); +const Client2 = new Client(options); +const Client3 = new Client(options); + +const primary_user = process.env.PLIVO_ENDPOINT1_USERNAME; +const primary_pass = process.env.PLIVO_ENDPOINT1_PASSWORD; + +// eslint-disable-next-line no-undef +describe('plivoWebSdk', function () { + const GLOBAL_TIMEOUT = 240000; + this.timeout(GLOBAL_TIMEOUT); + const TIMEOUT = 20000; + let bailTimer; + + // eslint-disable-next-line no-undef + describe('multi-tab', function () { + this.timeout(GLOBAL_TIMEOUT); + + const events = {}; + + const clientEvents = [ + 'onLogin', + 'onWebsocketConnected', + 'onWebsocketDisconnected', + 'onConnectionChange', + 'onConnectionDisconnected', + 'onLogout', + 'onLoginFailed', + ]; + + const noOfClients = 3; + for (let i = 1; i <= noOfClients; i += 1) { + clientEvents.forEach((eventName) => { + events[`client${i}-${eventName}`] = { status: false }; + }); + } + + let bail = false; + + function waitUntilExecuted(boolObj, value, callback, delay) { + // if delay is undefined or is not an integer + const newDelay = typeof delay === "undefined" || Number.isNaN(parseInt(delay, 10)) + ? 100 + : delay; + setTimeout(() => { + let isTruthy = 0; + boolObj.forEach((event) => { + if (event.status === value) { + isTruthy += 1; + } + }); + if (isTruthy === boolObj.length) { + callback(); + } else { + waitUntilExecuted(boolObj, value, callback, newDelay); + } + }, newDelay); + } + + // eslint-disable-next-line no-undef + before(() => { + clientEvents.forEach((event) => { + Client1.on(event, () => { + events[`client1-${event}`].status = true; + }); + Client2.on(event, () => { + events[`client2-${event}`].status = true; + }); + Client3.on(event, () => { + events[`client3-${event}`].status = true; + }); + }); + }); + + function reset() { + const keys = Object.keys(events); + // reset all the flags + keys.forEach((key) => { + events[key].status = false; + }); + clearTimeout(bailTimer); + } + + // eslint-disable-next-line no-undef + after(() => { + Client1.logout(); + Client2.logout(); + Client3.logout(); + }); + + // eslint-disable-next-line no-undef + afterEach((done) => { + done(); + }); + + // #5 + // eslint-disable-next-line no-undef + it('All the clients should be connected to websocket', (done) => { + console.log('All the clients should be connected to websocket'); + reset(); + Client1.login(primary_user, primary_pass); + Client2.login(primary_user, primary_pass); + Client3.login(primary_user, primary_pass); + waitUntilExecuted([events['client1-onWebsocketConnected'], events['client2-onWebsocketConnected'], events['client3-onWebsocketConnected']], true, done, 1000); + }); + + // eslint-disable-next-line no-undef + it('only one client instance should be registered and rest all should be connected', (done) => { + console.log('one client instance should be registered and rest all should be connected'); + if (bail) { + done(new Error('bailing')); + } + Client1.register(); + waitUntilExecuted([events['client1-onLogin'], events['client2-onWebsocketConnected'], events['client3-onWebsocketConnected']], true, done, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('All the client instance should be registered', (done) => { + console.log('All the client instance should be registered'); + if (bail) { + done(new Error('bailing')); + } + Client2.register(); + Client3.register(); + waitUntilExecuted([events['client1-onLogin'], events['client2-onLogin'], events['client3-onLogin']], true, done, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('unregistering one of the client instance', (done) => { + console.log('unregistering one of the client instance'); + if (bail) { + done(new Error('bailing')); + } + Client1.unregister(); + Client1.on('onConnectionChange', (data) => { + if (data.state === 'disconnected' && data.reason === 'unregistered') { + events[`client1-onConnectionDisconnected`].status = true; + } + }); + waitUntilExecuted([events['client1-onConnectionDisconnected'], events['client2-onLogin'], events['client3-onLogin']], true, done, 1000); + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('disconnect all the clients', (done) => { + console.log('disconnect all the clients'); + if (bail) { + done(new Error('bailing')); + } + Client1.disconnect(); + Client2.disconnect(); + Client3.disconnect(); + if (!Client1.isConnected() && !Client2.isConnected() && !Client3.isConnected()) { + done(); + } + bailTimer = setTimeout(() => { + bail = true; + done(new Error('incoming call failed')); + }, TIMEOUT); + }); + + // eslint-disable-next-line no-undef + it('should not register before connection', (done) => { + console.log('should not register before connection'); + if (bail) { + done(new Error('bailing')); + } + reset(); + Client1.login(primary_user, primary_pass); + if (!Client1.register()) { + done(); + } + }); + + // eslint-disable-next-line no-undef + it('should not register if not connected OR already registered', (done) => { + console.log('should not register if not connected OR already registered'); + if (bail) { + done(new Error('bailing')); + } + waitUntilExecuted([events['client1-onWebsocketConnected']], true, () => { + Client1.register(); + }, 1000); + waitUntilExecuted([events['client1-onLogin']], true, () => { + let value = false; + if (!Client1.register()) { + value = true; + } + if (Client1.register(['XplivoInMemory: true']) && value) { + done(); + } + }, 1000); + }); + + // eslint-disable-next-line no-undef + it('should not disconnect if not connected', (done) => { + console.log('should not disconnect if not connected'); + if (bail) { + done(new Error('bailing')); + } + if (!Client2.disconnect()) { + done(); + } + }); + + // eslint-disable-next-line no-undef + it('should not unregister if not registered', (done) => { + console.log('should not register if not registered'); + if (bail) { + done(new Error('bailing')); + } + if (!Client2.unregister()) { + done(); + } + }); + + // eslint-disable-next-line no-undef + it('should logout if not connected and not registered', (done) => { + console.log('should logout if not connected and not registered'); + if (bail) { + done(new Error('bailing')); + } + if (!Client3.logout()) { + done(); + } + }); + + // eslint-disable-next-line no-undef + it('should not logout if not connected', (done) => { + console.log('should not logout if not connected'); + if (bail) { + done(new Error('bailing')); + } + if (!Client2.logout()) { + done(); + } + }); + + // eslint-disable-next-line no-undef + it('should logout if connected but not registered', (done) => { + console.log('should logout if connected but not registered'); + if (bail) { + done(new Error('bailing')); + } + Client2.login(primary_user, primary_pass); + waitUntilExecuted([events['client2-onWebsocketConnected']], true, () => { + if (!Client2.isRegistered()) { + Client2.logout(); + waitUntilExecuted([events['client2-onLogout']], true, done, 1000); + } + }, 1000); + }); + + // eslint-disable-next-line no-undef + it('should logout if registered', (done) => { + console.log('should logout if registered'); + if (bail) { + done(new Error('bailing')); + } + Client1.logout(); + waitUntilExecuted([events['client1-onLogout']], true, done, 1000); + }); + }); +}); diff --git a/test/integration/networkchange.js b/test/integration/networkchange.js index 98beeae..003d304 100644 --- a/test/integration/networkchange.js +++ b/test/integration/networkchange.js @@ -11,6 +11,7 @@ const options = { dscp: true, enableTracking: true, dialType: "conference", + stopAutoRegisterOnConnect: false, }; const Client1 = new Client(options); diff --git a/test/integration/outgoingCallWithJWT.js b/test/integration/outgoingCallWithJWT.js index 8f9b6bc..bd6360a 100644 --- a/test/integration/outgoingCallWithJWT.js +++ b/test/integration/outgoingCallWithJWT.js @@ -148,8 +148,8 @@ describe("plivoWebSdk JWT", function () { // eslint-disable-next-line no-undef after(() => { - // Client1.logout(); - // Client2.logout(); + Client1.logout(); + Client2.logout(); spyOnSocket.restore(); }); @@ -235,8 +235,8 @@ describe("plivoWebSdk JWT", function () { Client1.call(secondary_user, {}); bailTimer = setTimeout(() => { Client1.hangup(); - }, 2000); - waitUntilOutgoingCall(events.onCallFailed, done, 500); + }, 3000); + waitUntilOutgoingCall(events.onCallFailed, done, 1000); bailTimer = setTimeout(() => { bail = true; done(new Error("outgoing call end failed")); @@ -381,9 +381,9 @@ describe("plivoWebSdk JWT", function () { events.onPermissionDenied.status = true; }); // done - let token = await getJWTToken(false, true) - console.log("token is ", token) - plivo_jwt_without_outbound_access = token + const token = await getJWTToken(false, true); + console.log("token is ", token); + plivo_jwt_without_outbound_access = token; Client1.loginWithAccessToken(plivo_jwt_without_outbound_access); Client2.login(secondary_user, secondary_pass); }); @@ -430,6 +430,5 @@ describe("plivoWebSdk JWT", function () { done(new Error("outgoing call failed")); }, TIMEOUT); }); - }); }); diff --git a/test/integration/outgoingcall.js b/test/integration/outgoingcall.js index c4ef9e4..8782048 100644 --- a/test/integration/outgoingcall.js +++ b/test/integration/outgoingcall.js @@ -102,8 +102,8 @@ describe("plivoWebSdk", function () { // eslint-disable-next-line no-undef after(() => { - // Client1.logout(); - // Client2.logout(); + Client1.logout(); + Client2.logout(); spyOnSocket.restore(); }); diff --git a/test/unit/stats/nonRTPStats.test.ts b/test/unit/stats/nonRTPStats.test.ts index 3f4ccd9..36aedee 100644 --- a/test/unit/stats/nonRTPStats.test.ts +++ b/test/unit/stats/nonRTPStats.test.ts @@ -93,7 +93,14 @@ describe('NonRTPStats', () => { const sendFn = jest.spyOn(context.statsSocket.ws, 'send'); nonRTPStats.sendCallAnsweredEvent.call(context, deviceInfo, true); expect(sendFn).toHaveBeenCalledTimes(1); - expect(context.statsSocket.ws.message).toStrictEqual(nonRTPStatsResponse.ANSWER_EVENT); + const message = context.statsSocket.ws.message; + message.timeStamp = 1599026892574; + const testOs = process.env.USERAGENT_OS; + console.log("OS AGENT", testOs) + if (message.userAgent !== `Mozilla/5.0 (${testOs}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/16.4.0`) { + message.userAgent = `Mozilla/5.0 (${testOs}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/16.4.0`; + } + expect(message).toEqual(nonRTPStatsResponse.ANSWER_EVENT); }); it('should send call answered event for outgoing call', () => { @@ -102,7 +109,13 @@ describe('NonRTPStats', () => { expect(sendFn).toHaveBeenCalledTimes(1); delete (nonRTPStatsResponse.ANSWER_EVENT as any).audioDeviceInfo; nonRTPStatsResponse.ANSWER_EVENT.info = 'Outgoing call answered'; - expect(context.statsSocket.ws.message).toStrictEqual(nonRTPStatsResponse.ANSWER_EVENT); + const message = context.statsSocket.ws.message; + message.timeStamp = 1599026892574; + const testOs = process.env.USERAGENT_OS; + if (message.userAgent !== `Mozilla/5.0 (${testOs}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/16.4.0`) { + message.userAgent = `Mozilla/5.0 (${testOs}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/16.4.0`; + } + expect(message).toStrictEqual(nonRTPStatsResponse.ANSWER_EVENT); }); it('should not send call answered event when callstatskey is missing', () => { @@ -135,7 +148,7 @@ describe('NonRTPStats', () => { }); it('should add callinfo to stats', () => { - expect(nonRTPStats.addCallInfo(callSession, {} as any, callInfoObj.callstats_key, callInfoObj.userName)).toStrictEqual(callInfoObj); + expect(nonRTPStats.addCallInfo(callSession, {} as any, callInfoObj.callstats_key, callInfoObj.userName, 1599026892574)).toStrictEqual(callInfoObj); }); it('should send user feedback to stats socket', () => { @@ -164,6 +177,7 @@ describe('NonRTPStats', () => { const error = 'ice_timeout'; const expectedMsg = { msg: 'ICE_FAILURE', error, ...callInfoObj }; nonRTPStats.onIceFailure.call(context, callSession, new Error(error)); + context.statsSocket.ws.message.timeStamp = 1599026892574; expect(context.statsSocket.ws.message).toStrictEqual(expectedMsg); }); @@ -171,6 +185,7 @@ describe('NonRTPStats', () => { const error = 'PermissionDeniedError'; const expectedMsg = { msg: 'MEDIA_FAILURE', error, ...callInfoObj }; nonRTPStats.onMediaFailure.call(context, callSession, new Error(error)); + context.statsSocket.ws.message.timeStamp = 1599026892574; expect(context.statsSocket.ws.message).toStrictEqual(expectedMsg); }); @@ -178,6 +193,7 @@ describe('NonRTPStats', () => { const error = 'createofferfailed'; const expectedMsg = { msg: 'SDP_FAILURE', error, ...callInfoObj }; nonRTPStats.onSDPfailure.call(context, callSession, new Error(error)); + context.statsSocket.ws.message.timeStamp = 1599026892574; expect(context.statsSocket.ws.message).toStrictEqual(expectedMsg); }); @@ -185,6 +201,7 @@ describe('NonRTPStats', () => { const action = 'mute'; const expectedMsg = { msg: 'TOGGLE_MUTE', action, ...callInfoObj }; nonRTPStats.onToggleMute.call(context, callSession, action); + context.statsSocket.ws.message.timeStamp = 1599026892574; expect(context.statsSocket.ws.message).toStrictEqual(expectedMsg); }); @@ -212,4 +229,4 @@ const updateSDKVersions = (sdkVersionParse) => { nonRTPStatsResponse.SUMMARY_EVENT.sdkVersionMajor = sdkVersionParse.major; nonRTPStatsResponse.SUMMARY_EVENT.sdkVersionMinor = sdkVersionParse.minor; nonRTPStatsResponse.SUMMARY_EVENT.sdkVersionPatch = sdkVersionParse.patch; -}; \ No newline at end of file +}; diff --git a/test/unit/stats/rtpStats.test.ts b/test/unit/stats/rtpStats.test.ts index 2deb44c..bfa546c 100644 --- a/test/unit/stats/rtpStats.test.ts +++ b/test/unit/stats/rtpStats.test.ts @@ -15,6 +15,7 @@ describe('RTPStats', () => { browserDetails: { browser: '' }, + timeDiff: 0, timeTakenForStats: { mediaSetup:{ init: 0, diff --git a/test/unit/stats/timeDiff.test.ts b/test/unit/stats/timeDiff.test.ts new file mode 100644 index 0000000..71a49ad --- /dev/null +++ b/test/unit/stats/timeDiff.test.ts @@ -0,0 +1,25 @@ +import { resolve } from 'path'; +import { checkTimeDiff } from '../../../lib/managers/util'; + +describe('TimeDiff', () => { + + it('should have no time difference ', () => { + let timeDiff: number = checkTimeDiff(Date.now()); + expect(timeDiff).toBe(0) + }); + + it('should have SDK timestamp behind rambo ', () => { + let ramboServerTs: number = Date.now() + 3600000 + let timeDiff: number = checkTimeDiff(ramboServerTs); + expect(timeDiff).toBeGreaterThan(0); + }); + + it('should have SDK timestamp ahead of rambo ', () => { + let ramboServerTs: number = Date.now() - 3600000 + let timeDiff: number = checkTimeDiff(ramboServerTs); + expect(timeDiff).toBeLessThan(0); + }); +}); + + + diff --git a/test/unit/utils/options.test.ts b/test/unit/utils/options.test.ts index 3909506..ffb2c35 100644 --- a/test/unit/utils/options.test.ts +++ b/test/unit/utils/options.test.ts @@ -23,8 +23,10 @@ describe('ValidateOptions', () => { allowMultipleIncomingCalls: false, closeProtection: false, maxAverageBitrate: 48000, + captureSDKCrashOnly: false, enableNoiseReduction: true, registrationRefreshTimer: 120, + stopAutoRegisterOnConnect: false, usePlivoStunServer: false, dtmfOptions: { sendDtmfType: ['INBAND','OUTBAND'] @@ -93,6 +95,18 @@ describe('ValidateOptions', () => { expect(validateOptions(inputOptions).useDefaultAudioDevice).toStrictEqual(true); }); + it('should check if stopAutoRegisterOnConnect is valid', () => { + const inputOptions = { ...options }; + inputOptions.stopAutoRegisterOnConnect = "test"; + expect(validateOptions(inputOptions).stopAutoRegisterOnConnect).toStrictEqual(false); + inputOptions.stopAutoRegisterOnConnect = 12345; + expect(validateOptions(inputOptions).stopAutoRegisterOnConnect).toStrictEqual(false); + inputOptions.stopAutoRegisterOnConnect = 12345.12345; + expect(validateOptions(inputOptions).stopAutoRegisterOnConnect).toStrictEqual(false); + inputOptions.stopAutoRegisterOnConnect = true; + expect(validateOptions(inputOptions).stopAutoRegisterOnConnect).toStrictEqual(true); + }); + it('should check if usePlivoStunServer is valid', () => { const inputOptions = { ...options }; inputOptions.usePlivoStunServer = "true"; diff --git a/webpack.config.js b/webpack.config.js index 76cd085..d026775 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -94,6 +94,14 @@ module.exports = env => { test: path.resolve(__dirname, 'node_modules/webpack-dev-server/client'), loader: 'null-loader' }, + { + test: /\.worker\.ts$/, + loader: "worker-loader", + options: { + esModule: false, + inline: "fallback" + } + }, // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'. { test: /\.ts?$/, loader: "awesome-typescript-loader" }, // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.