ソースを参照

feat(prejoin): Expose method to make a precall test

master
Vlad Piersec 5年前
コミット
b4bff22ac5
5個のファイルの変更137行の追加4行の削除
  1. 3
    0
      JitsiMeetJS.js
  2. 129
    0
      modules/statistics/PrecallTest.js
  3. 1
    0
      modules/statistics/constants.js
  4. 2
    3
      modules/statistics/statistics.js
  5. 2
    1
      package.json

+ 3
- 0
JitsiMeetJS.js ファイルの表示

@@ -25,6 +25,7 @@ import ProxyConnectionService
25 25
     from './modules/proxyconnection/ProxyConnectionService';
26 26
 import recordingConstants from './modules/recording/recordingConstants';
27 27
 import LocalStatsCollector from './modules/statistics/LocalStatsCollector';
28
+import precallTest from './modules/statistics/PrecallTest';
28 29
 import Statistics from './modules/statistics/statistics';
29 30
 import AuthUtil from './modules/util/AuthUtil';
30 31
 import GlobalOnErrorHandler from './modules/util/GlobalOnErrorHandler';
@@ -644,6 +645,8 @@ export default _mergeNamespaceAndModule({
644 645
         }
645 646
     },
646 647
 
648
+    precallTest,
649
+
647 650
     /* eslint-enable max-params */
648 651
 
649 652
     /**

+ 129
- 0
modules/statistics/PrecallTest.js ファイルの表示

@@ -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
+};

+ 1
- 0
modules/statistics/constants.js ファイルの表示

@@ -0,0 +1 @@
1
+export const CALLSTATS_SCRIPT_URL = 'https://api.callstats.io/static/callstats-ws.min.js';

+ 2
- 3
modules/statistics/statistics.js ファイルの表示

@@ -10,7 +10,7 @@ import analytics from './AnalyticsAdapter';
10 10
 import CallStats from './CallStats';
11 11
 import LocalStats from './LocalStatsCollector';
12 12
 import RTPStats from './RTPStatsCollector';
13
-
13
+import { CALLSTATS_SCRIPT_URL } from './constants';
14 14
 
15 15
 const logger = require('jitsi-meet-logger').getLogger(__filename);
16 16
 
@@ -40,8 +40,7 @@ let isCallstatsLoaded = false;
40 40
 function loadCallStatsAPI(options) {
41 41
     if (!isCallstatsLoaded) {
42 42
         ScriptUtil.loadScript(
43
-            options.customScriptUrl
44
-                || 'https://api.callstats.io/static/callstats-ws.min.js',
43
+            options.customScriptUrl || CALLSTATS_SCRIPT_URL,
45 44
             /* async */ true,
46 45
             /* prepend */ true,
47 46
             /* relativeURL */ undefined,

+ 2
- 1
package.json ファイルの表示

@@ -60,7 +60,8 @@
60 60
     "postinstall": "webpack -p",
61 61
     "test": "karma start karma.conf.js",
62 62
     "test-watch": "karma start karma.conf.js --no-single-run",
63
-    "validate": "npm ls"
63
+    "validate": "npm ls",
64
+    "watch": "webpack --config webpack.config.js --watch --mode development"
64 65
   },
65 66
   "main": "./index.js",
66 67
   "license": "Apache-2.0"

読み込み中…
キャンセル
保存