Skip to content

Commit

Permalink
VIDEO-9282 - Migrate SDK from using node.js util. (#1752)
Browse files Browse the repository at this point in the history
* VIDEO-9282 - Removing all references to node dependencies.

Co-authored-by: Makarand Patwardhan <[email protected]>
  • Loading branch information
PikaJoyce and makarandp0 authored May 17, 2022
1 parent 0a4b0bd commit b30388d
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
"SharedArrayBuffer": "readonly",
"globalThis": false
},
"parserOptions": {
"ecmaVersion": 2018,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Bug Fixes

- Fixed an issue where some extraneous errors were logged to console when a video track was stopped. (VIDEO-9511)
- Fixed an issue where the `dimensionsChanged` event was not firing when the track dimensions first became available. (VIDEO-3576)
- Removed references to node dependencies that causes build errors on some platforms. (VIDEO-9282)

2.21.1 (March 22, 2022)
=======================
Expand Down
3 changes: 1 addition & 2 deletions lib/media/track/es5/localaudiotrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// only in place so that we can support ES6 classes without requiring `new`.
'use strict';

const { inherits } = require('util');

const inherits = require('../../../vendor/inherits');
const LocalAudioTrackClass = require('../localaudiotrack');

function LocalAudioTrack(mediaStreamTrack, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/media/track/es5/localdatatrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// only in place so that we can support ES6 classes without requiring `new`.
'use strict';

const { inherits } = require('util');
const inherits = require('../../../vendor/inherits');

const LocalDataTrackClass = require('../localdatatrack');

Expand Down
2 changes: 1 addition & 1 deletion lib/media/track/es5/localvideotrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// only in place so that we can support ES6 classes without requiring `new`.
'use strict';

const { inherits } = require('util');
const inherits = require('../../../vendor/inherits');

const LocalVideoTrackClass = require('../localvideotrack');

Expand Down
9 changes: 4 additions & 5 deletions lib/signaling/v2/peerconnection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const DefaultBackoff = require('backoff');

const DefaultBackoff = require('../../util/backoff');
const {
RTCIceCandidate: DefaultRTCIceCandidate,
RTCPeerConnection: DefaultRTCPeerConnection,
Expand Down Expand Up @@ -191,7 +190,8 @@ class PeerConnectionV2 extends StateMachine {
false)
},
_iceRestartBackoff: {
value: options.Backoff.exponential(iceRestartBackoffConfig)
// eslint-disable-next-line new-cap
value: new options.Backoff(iceRestartBackoffConfig)
},
_instanceId: {
value: ++nInstances
Expand Down Expand Up @@ -377,7 +377,6 @@ class PeerConnectionV2 extends StateMachine {
peerConnection.addEventListener('icegatheringstatechange', this._handleIceGatheringStateChange.bind(this));
peerConnection.addEventListener('signalingstatechange', this._handleSignalingStateChange.bind(this));
peerConnection.addEventListener('track', this._handleTrackEvent.bind(this));
this._iceRestartBackoff.on('ready', () => this._initiateIceRestart());

const self = this;
this.on('stateChanged', function stateChanged(state) {
Expand Down Expand Up @@ -960,7 +959,7 @@ class PeerConnectionV2 extends StateMachine {
}
this._log.warn('An ICE restart has been scheduled');
this._isIceRestartBackoffInProgress = true;
this._iceRestartBackoff.backoff();
this._iceRestartBackoff.backoff(() => this._initiateIceRestart());
}

/**
Expand Down
8 changes: 3 additions & 5 deletions lib/signaling/v2/twilioconnectiontransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const StateMachine = require('../../statemachine');
const TwilioConnection = require('../../twilioconnection');
const DefaultBackoff = require('backoff');
const DefaultBackoff = require('../../util/backoff');
const { reconnectBackoffConfig } = require('../../util/constants');
const Timeout = require('../../util/timeout');
const { SDK_NAME, SDK_VERSION, SDP_FORMAT } = require('../../util/constants');
Expand Down Expand Up @@ -147,7 +147,7 @@ class TwilioConnectionTransport extends StateMachine {
writable: true
},
_reconnectBackoff: {
value: options.Backoff.exponential(reconnectBackoffConfig)
value: new options.Backoff(reconnectBackoffConfig)
},
_session: {
value: null,
Expand Down Expand Up @@ -179,7 +179,6 @@ class TwilioConnectionTransport extends StateMachine {


setupTransport(this);

}

/**
Expand Down Expand Up @@ -377,8 +376,7 @@ class TwilioConnectionTransport extends StateMachine {

// return promise that waits with exponential backoff.
return new Promise(resolve => {
this._reconnectBackoff.once('ready', resolve);
this._reconnectBackoff.backoff();
this._reconnectBackoff.backoff(resolve);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/twilioconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const WS_CLOSE_BUSY_WAIT = 3005;
const WS_CLOSE_SERVER_BUSY = 3006;
const WS_CLOSE_OPEN_TIMEOUT = 3007;

const toplevel = global.window || global;
const toplevel = globalThis;
const WebSocket = toplevel.WebSocket ? toplevel.WebSocket : require('ws');

const CloseReason = {
Expand Down
85 changes: 85 additions & 0 deletions lib/util/backoff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

/**
* Expose `Backoff`.
*/

class Backoff {
/**
* Construct a {@link Backoff}.
* @param {object} options
* @property {number} min - Initial timeout in milliseconds [100]
* @property {number} max - Max timeout [10000]
* @property {boolean} jitter - Apply jitter [0]
* @property {number} factor - Multiplication factor for Backoff operation [2]
*/
constructor(options) {
Object.defineProperties(this, {
_min: {
value: options.min || 100
},
_max: {
value: options.max || 10000
},
_jitter: {
value: options.jitter > 0 && options.jitter <= 1 ? options.jitter : 0
},
_factor: {
value: options.factor || 2
},
_attempts: {
value: 0,
writable: true
},
_duration: {
enumerable: false,
get() {
let ms = this._min * Math.pow(this._factor, this._attempts);
if (this._jitter) {
const rand = Math.random();
const deviation = Math.floor(rand * this._jitter * ms);
ms = (Math.floor(rand * 10) & 1) === 0 ? ms - deviation : ms + deviation;
}
return Math.min(ms, this._max) | 0;
}
},
_timeoutID: {
value: null,
writable: true
}
});
}

/**
* Start the backoff operation.
* @param {function} fn - Function to call
* @return {void}
* @api public
*/
backoff(fn) {
let duration = this._duration;
if (this._timeoutID) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
this._timeoutID = setTimeout(() => {
this._attempts++;
fn();
}, duration);
}

/**
* Reset the number of attempts and clear the timer.
*
* @return {void}
* @api public
*/
reset() {
this._attempts = 0;
if (this._timeoutID) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
}
}

module.exports = Backoff;
10 changes: 5 additions & 5 deletions lib/util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ module.exports.ICE_INACTIVITY_THRESHOLD_MS = 3000;

module.exports.iceRestartBackoffConfig = {
factor: 1.1,
initialDelay: 1,
maxDelay: module.exports.DEFAULT_SESSION_TIMEOUT_SEC * 1000,
randomisationFactor: 0.5
min: 1,
max: module.exports.DEFAULT_SESSION_TIMEOUT_SEC * 1000,
jitter: 1
};

module.exports.reconnectBackoffConfig = {
factor: 1.5,
initialDelay: 80,
randomisationFactor: 0.5
min: 80,
jitter: 1
};

module.exports.subscriptionMode = {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/insightspublisher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MAX_RECONNECT_ATTEMPTS = 5;
const RECONNECT_INTERVAL_MS = 50;
const WS_CLOSE_NORMAL = 1000;

const toplevel = global.window || global;
const toplevel = globalThis;
const WebSocket = toplevel.WebSocket ? toplevel.WebSocket : require('ws');
const util = require('../../util');

Expand Down
31 changes: 31 additions & 0 deletions lib/vendor/inherits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2011-2022 Isaac Z. Schlueter
* Licensed under the ISC License.
*
* Copied from https://github.com/isaacs/inherits (2.0.4)
*/

module.exports = function inherits(ctor, superCtor) {
if (ctor && superCtor) {
ctor.super_ = superCtor;
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
} else {
// old school shim for old browsers
class TempCtor {
constructor() { }
}
TempCtor.prototype = superCtor.prototype;
ctor.prototype = new TempCtor();
ctor.prototype.constructor = ctor;
}
}
};
2 changes: 1 addition & 1 deletion lib/webrtc/rtcpeerconnection/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var ChromeRTCSessionDescription = require('../rtcsessiondescription/chrome');
var EventTarget = require('../util/eventtarget');
var inherits = require('util').inherits;
var inherits = require('../../vendor/inherits');
var Latch = require('../util/latch');
var MediaStream = require('../mediastream');
var RTCRtpSenderShim = require('../rtcrtpsender');
Expand Down
2 changes: 1 addition & 1 deletion lib/webrtc/rtcpeerconnection/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var EventTarget = require('../util/eventtarget');
var FirefoxRTCSessionDescription = require('../rtcsessiondescription/firefox');
var inherits = require('util').inherits;
var inherits = require('../../vendor/inherits');
var updateTracksToSSRCs = require('../util/sdp').updateUnifiedPlanTrackIdsToSSRCs;
var util = require('../util');

Expand Down
2 changes: 1 addition & 1 deletion lib/webrtc/rtcpeerconnection/safari.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

var EventTarget = require('../util/eventtarget');
var inherits = require('util').inherits;
var inherits = require('../../vendor/inherits');
var Latch = require('../util/latch');
var sdpUtils = require('../util/sdp');
var util = require('../util');
Expand Down
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"browserify": "^17.0.0",
"cheerio": "^0.22.0",
"cheerio": "^1.0.0-rc.10",
"cors": "^2.8.5",
"electron": "^9.1.0",
"electron": "^17.2.0",
"envify": "^4.0.0",
"eslint": "^6.2.1",
"eslint-config-standard": "^14.0.0",
Expand All @@ -47,21 +47,21 @@
"eslint-plugin-standard": "^4.0.1",
"express": "^4.16.2",
"glob": "^7.1.7",
"ink-docstrap": "^1.3.2",
"ink-docstrap": "^0.5.4",
"inquirer": "^7.0.0",
"is-docker": "^2.0.0",
"jsdoc": "^3.5.5",
"jsdoc-babel": "^0.5.0",
"json-loader": "^0.5.7",
"karma": "^5.0.2",
"karma": "^6.3.17",
"karma-browserify": "^8.0.0",
"karma-chrome-launcher": "^2.0.0",
"karma-edgium-launcher": "^4.0.0-0",
"karma-electron": "^6.1.0",
"karma-firefox-launcher": "^1.3.0",
"karma-htmlfile-reporter": "^0.3.8",
"karma-junit-reporter": "^1.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha": "^2.0.1",
"karma-safari-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.32",
"karma-typescript": "^5.5.1",
Expand All @@ -74,17 +74,17 @@
"nyc": "^15.1.0",
"requirejs": "^2.3.3",
"rimraf": "^2.6.1",
"simple-git": "^1.126.0",
"simple-git": "^3.4.0",
"sinon": "^4.0.1",
"ts-node": "4.0.1",
"tslint": "5.8.0",
"twilio": "^3.49.0",
"twilio-release-tool": "^1.0.2",
"typescript": "4.2.2",
"uglify-js": "^2.8.22",
"vinyl-fs": "^2.4.4",
"vinyl-fs": "^2.4.1",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.11.1",
"watchify": "^4.0.0",
"webrtc-adapter": "^7.7.1"
},
"engines": {
Expand Down Expand Up @@ -144,7 +144,6 @@
"clean": "rimraf ./coverage ./es5 ./dist"
},
"dependencies": {
"backoff": "^2.5.0",
"events": "^3.3.0",
"util": "^0.12.4",
"ws": "^7.4.6",
Expand Down
2 changes: 1 addition & 1 deletion test/integration/spec/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ describe('connect', function() {
// eslint-disable-next-line no-warning-comments
// TODO: The following tests verifies bitrates using default codec, OPUS.
// We should also verify other codecs like ISAC, PCMU and PCMA.
describe('called with EncodingParameters', () => {
describe(`called with EncodingParameters ${isFirefox ? ' - @unstable: VIDEO-9969' : ''}`, () => {
const minAudioBitrate = 6000;
const minVideoBitrate = 20000;
combinationContext([
Expand Down
2 changes: 1 addition & 1 deletion test/lib/webrtc/fakemediastream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var inherits = require('util').inherits;
var inherits = require('../../../lib/vendor/inherits');
var randomName = require('./util').randomName;
var EventTarget = require('../../../lib/webrtc/util/eventtarget');

Expand Down
Loading

0 comments on commit b30388d

Please sign in to comment.