|
@@ -0,0 +1,129 @@
|
|
1
|
+import EventEmitter from 'events';
|
|
2
|
+
|
|
3
|
+import browser from '../browser';
|
|
4
|
+import Settings from '../settings/Settings';
|
|
5
|
+import ScriptUtil from '../util/ScriptUtil';
|
|
6
|
+
|
|
7
|
+import { CALLSTATS_SCRIPT_URL } from './constants';
|
|
8
|
+
|
|
9
|
+const PRECALL_TEST_RESULTS = 'preCallTestResults';
|
|
10
|
+const emitter = new EventEmitter();
|
|
11
|
+let _initialized = false;
|
|
12
|
+let api = null;
|
|
13
|
+
|
|
14
|
+/**
|
|
15
|
+ * Loads the callstats io script.
|
|
16
|
+ *
|
|
17
|
+ * @returns {Promise<void>}
|
|
18
|
+ */
|
|
19
|
+function _loadScript() {
|
|
20
|
+ if (browser.isReactNative()) {
|
|
21
|
+ return;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ return new Promise(resolve => {
|
|
25
|
+ ScriptUtil.loadScript(
|
|
26
|
+ CALLSTATS_SCRIPT_URL,
|
|
27
|
+ /* async */ true,
|
|
28
|
+ /* prepend */ true,
|
|
29
|
+ /* relativeURL */ undefined,
|
|
30
|
+ /* loadCallback */ resolve);
|
|
31
|
+ });
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+/**
|
|
35
|
+ * Initializes the callstats lib and registers a callback to be invoked
|
|
36
|
+ * when there are 'preCallTestResults'.
|
|
37
|
+ *
|
|
38
|
+ * @typedef PrecallTestOptions
|
|
39
|
+ * @type {Object}
|
|
40
|
+ * @property {string} callStatsID - Callstats credentials - the id.
|
|
41
|
+ * @property {string} callStatsSecret - Callstats credentials - the secret.
|
|
42
|
+ * @property {string} statisticsId - The user name to use when initializing callstats.
|
|
43
|
+ * @property {string} statisticsDisplayName - The user display name.
|
|
44
|
+ *
|
|
45
|
+ * @param { PrecallTestOptions} options - The init options.
|
|
46
|
+ * @returns {Promise<void>}
|
|
47
|
+ */
|
|
48
|
+function _initialize(options) {
|
|
49
|
+ return new Promise((resolve, reject) => {
|
|
50
|
+ if (!options.disableThirdPartyRequests) {
|
|
51
|
+ const appId = options.callStatsID;
|
|
52
|
+ const appSecret = options.callStatsSecret;
|
|
53
|
+ const userId = options.statisticsId || options.statisticsDisplayName || Settings.callStatsUserName;
|
|
54
|
+
|
|
55
|
+ api.initialize(appId, appSecret, userId, (status, message) => {
|
|
56
|
+ if (status === 'success') {
|
|
57
|
+ api.on(PRECALL_TEST_RESULTS, (...args) => {
|
|
58
|
+ emitter.emit(PRECALL_TEST_RESULTS, ...args);
|
|
59
|
+ });
|
|
60
|
+ _initialized = true;
|
|
61
|
+ resolve();
|
|
62
|
+ } else {
|
|
63
|
+ reject({
|
|
64
|
+ status,
|
|
65
|
+ message
|
|
66
|
+ });
|
|
67
|
+ }
|
|
68
|
+ }, null, { disablePrecalltest: true });
|
|
69
|
+ }
|
|
70
|
+ });
|
|
71
|
+}
|
|
72
|
+
|
|
73
|
+/**
|
|
74
|
+ * Loads the callstats script and initializes the library.
|
|
75
|
+ *
|
|
76
|
+ * @param {Function} onResult - The callback to be invoked when results are received.
|
|
77
|
+ * @returns {Promise<void>}
|
|
78
|
+ */
|
|
79
|
+export async function init(options) {
|
|
80
|
+ if (_initialized) {
|
|
81
|
+ throw new Error('Precall Test already initialized');
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ await _loadScript();
|
|
85
|
+ // eslint-disable-next-line new-cap
|
|
86
|
+ api = new window.callstats();
|
|
87
|
+
|
|
88
|
+ return _initialize(options);
|
|
89
|
+}
|
|
90
|
+
|
|
91
|
+/**
|
|
92
|
+ * Executes a pre call test.
|
|
93
|
+ *
|
|
94
|
+ * @typedef PrecallTestResults
|
|
95
|
+ * @type {Object}
|
|
96
|
+ * @property {boolean} mediaConnectivity - If there is media connectivity or not.
|
|
97
|
+ * @property {number} throughput - The average throughput.
|
|
98
|
+ * @property {number} fractionalLoss - The packet loss.
|
|
99
|
+ * @property {number} rtt - The round trip time.
|
|
100
|
+ * @property {string} provider - It is usually 'callstats'.
|
|
101
|
+ *
|
|
102
|
+ * @returns {Promise<{PrecallTestResults}>}
|
|
103
|
+ */
|
|
104
|
+export function execute() {
|
|
105
|
+ if (!_initialized) {
|
|
106
|
+ return Promise.reject('uninitialized');
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ return new Promise((resolve, reject) => {
|
|
110
|
+ emitter.on(PRECALL_TEST_RESULTS, (status, payload) => {
|
|
111
|
+ if (status === 'success') {
|
|
112
|
+ resolve(payload);
|
|
113
|
+ } else {
|
|
114
|
+ reject({
|
|
115
|
+ status,
|
|
116
|
+ payload
|
|
117
|
+ });
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ });
|
|
121
|
+
|
|
122
|
+ api.makePrecallTest();
|
|
123
|
+ });
|
|
124
|
+}
|
|
125
|
+
|
|
126
|
+export default {
|
|
127
|
+ init,
|
|
128
|
+ execute
|
|
129
|
+};
|