|
@@ -0,0 +1,431 @@
|
|
1
|
+// @flow
|
|
2
|
+
|
|
3
|
+import React, { Component } from 'react';
|
|
4
|
+import { Text, View } from 'react-native';
|
|
5
|
+
|
|
6
|
+import { Avatar } from '../../../base/avatar';
|
|
7
|
+import { ColorSchemeRegistry } from '../../../base/color-scheme';
|
|
8
|
+import { BottomSheet, isDialogOpen, hideDialog } from '../../../base/dialog';
|
|
9
|
+import { translate } from '../../../base/i18n';
|
|
10
|
+import { IconArrowDownLarge, IconArrowUpLarge } from '../../../base/icons';
|
|
11
|
+import { getParticipantDisplayName } from '../../../base/participants';
|
|
12
|
+import { BaseIndicator } from '../../../base/react';
|
|
13
|
+import { connect } from '../../../base/redux';
|
|
14
|
+import { StyleType, ColorPalette } from '../../../base/styles';
|
|
15
|
+import statsEmitter from '../../../connection-indicator/statsEmitter';
|
|
16
|
+
|
|
17
|
+import styles from './styles';
|
|
18
|
+
|
|
19
|
+/**
|
|
20
|
+ * Size of the rendered avatar in the menu.
|
|
21
|
+ */
|
|
22
|
+const AVATAR_SIZE = 25;
|
|
23
|
+
|
|
24
|
+const CONNECTION_QUALITY = [
|
|
25
|
+ 'Low',
|
|
26
|
+ 'Medium',
|
|
27
|
+ 'Good'
|
|
28
|
+];
|
|
29
|
+
|
|
30
|
+export type Props = {
|
|
31
|
+
|
|
32
|
+ /**
|
|
33
|
+ * The Redux dispatch function.
|
|
34
|
+ */
|
|
35
|
+ dispatch: Function,
|
|
36
|
+
|
|
37
|
+ /**
|
|
38
|
+ * The ID of the participant that this button is supposed to pin.
|
|
39
|
+ */
|
|
40
|
+ participantID: string,
|
|
41
|
+
|
|
42
|
+ /**
|
|
43
|
+ * The color-schemed stylesheet of the BottomSheet.
|
|
44
|
+ */
|
|
45
|
+ _bottomSheetStyles: StyleType,
|
|
46
|
+
|
|
47
|
+ /**
|
|
48
|
+ * True if the menu is currently open, false otherwise.
|
|
49
|
+ */
|
|
50
|
+ _isOpen: boolean,
|
|
51
|
+
|
|
52
|
+ /**
|
|
53
|
+ * Display name of the participant retreived from Redux.
|
|
54
|
+ */
|
|
55
|
+ _participantDisplayName: string,
|
|
56
|
+
|
|
57
|
+ /**
|
|
58
|
+ * The function to be used to translate i18n labels.
|
|
59
|
+ */
|
|
60
|
+ t: Function
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+/**
|
|
64
|
+ * The type of the React {@code Component} state of {@link ConnectionStatusComponent}.
|
|
65
|
+ */
|
|
66
|
+type State = {
|
|
67
|
+ resolutionString: string,
|
|
68
|
+ downloadString: string,
|
|
69
|
+ uploadString: string,
|
|
70
|
+ packetLostDownloadString: string,
|
|
71
|
+ packetLostUploadString: string,
|
|
72
|
+ serverRegionString: string,
|
|
73
|
+ codecString: string,
|
|
74
|
+ connectionString: string
|
|
75
|
+};
|
|
76
|
+
|
|
77
|
+// eslint-disable-next-line prefer-const
|
|
78
|
+let ConnectionStatusComponent_;
|
|
79
|
+
|
|
80
|
+/**
|
|
81
|
+ * Class to implement a popup menu that show the connection statistics.
|
|
82
|
+ */
|
|
83
|
+class ConnectionStatusComponent extends Component<Props, State> {
|
|
84
|
+
|
|
85
|
+ /**
|
|
86
|
+ * Constructor of the component.
|
|
87
|
+ *
|
|
88
|
+ * @param {P} props - The read-only properties with which the new
|
|
89
|
+ * instance is to be initialized.
|
|
90
|
+ *
|
|
91
|
+ * @inheritdoc
|
|
92
|
+ */
|
|
93
|
+ constructor(props: Props) {
|
|
94
|
+ super(props);
|
|
95
|
+
|
|
96
|
+ this._onStatsUpdated = this._onStatsUpdated.bind(this);
|
|
97
|
+ this._onCancel = this._onCancel.bind(this);
|
|
98
|
+ this._renderMenuHeader = this._renderMenuHeader.bind(this);
|
|
99
|
+
|
|
100
|
+ this.state = {
|
|
101
|
+ resolutionString: 'N/A',
|
|
102
|
+ downloadString: 'N/A',
|
|
103
|
+ uploadString: 'N/A',
|
|
104
|
+ packetLostDownloadString: 'N/A',
|
|
105
|
+ packetLostUploadString: 'N/A',
|
|
106
|
+ serverRegionString: 'N/A',
|
|
107
|
+ codecString: 'N/A',
|
|
108
|
+ connectionString: 'N/A'
|
|
109
|
+ };
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /**
|
|
113
|
+ * Implements React's {@link Component#render()}.
|
|
114
|
+ *
|
|
115
|
+ * @inheritdoc
|
|
116
|
+ * @returns {React$Node}
|
|
117
|
+ */
|
|
118
|
+ render(): React$Node {
|
|
119
|
+ const { t } = this.props;
|
|
120
|
+
|
|
121
|
+ return (
|
|
122
|
+ <BottomSheet
|
|
123
|
+ onCancel = { this._onCancel }
|
|
124
|
+ renderHeader = { this._renderMenuHeader }>
|
|
125
|
+ <View style = { styles.statsWrapper }>
|
|
126
|
+ <View style = { styles.statsInfoCell }>
|
|
127
|
+ <Text style = { styles.statsTitleText }>
|
|
128
|
+ { `${t('connectionindicator.status')} ` }
|
|
129
|
+ </Text>
|
|
130
|
+ <Text style = { styles.statsInfoText }>
|
|
131
|
+ { this.state.connectionString }
|
|
132
|
+ </Text>
|
|
133
|
+ </View>
|
|
134
|
+ <View style = { styles.statsInfoCell }>
|
|
135
|
+ <Text style = { styles.statsTitleText }>
|
|
136
|
+ { `${t('connectionindicator.bitrate')}` }
|
|
137
|
+ </Text>
|
|
138
|
+ <BaseIndicator
|
|
139
|
+ icon = { IconArrowDownLarge }
|
|
140
|
+ iconStyle = {{
|
|
141
|
+ color: ColorPalette.darkGrey
|
|
142
|
+ }} />
|
|
143
|
+ <Text style = { styles.statsInfoText }>
|
|
144
|
+ { this.state.downloadString }
|
|
145
|
+ </Text>
|
|
146
|
+ <BaseIndicator
|
|
147
|
+ icon = { IconArrowUpLarge }
|
|
148
|
+ iconStyle = {{
|
|
149
|
+ color: ColorPalette.darkGrey
|
|
150
|
+ }} />
|
|
151
|
+ <Text style = { styles.statsInfoText }>
|
|
152
|
+ { `${this.state.uploadString} Kbps` }
|
|
153
|
+ </Text>
|
|
154
|
+ </View>
|
|
155
|
+ <View style = { styles.statsInfoCell }>
|
|
156
|
+ <Text style = { styles.statsTitleText }>
|
|
157
|
+ { `${t('connectionindicator.packetloss')}` }
|
|
158
|
+ </Text>
|
|
159
|
+ <BaseIndicator
|
|
160
|
+ icon = { IconArrowDownLarge }
|
|
161
|
+ iconStyle = {{
|
|
162
|
+ color: ColorPalette.darkGrey
|
|
163
|
+ }} />
|
|
164
|
+ <Text style = { styles.statsInfoText }>
|
|
165
|
+ { this.state.packetLostDownloadString }
|
|
166
|
+ </Text>
|
|
167
|
+ <BaseIndicator
|
|
168
|
+ icon = { IconArrowUpLarge }
|
|
169
|
+ iconStyle = {{
|
|
170
|
+ color: ColorPalette.darkGrey
|
|
171
|
+ }} />
|
|
172
|
+ <Text style = { styles.statsInfoText }>
|
|
173
|
+ { this.state.packetLostUploadString }
|
|
174
|
+ </Text>
|
|
175
|
+ </View>
|
|
176
|
+ <View style = { styles.statsInfoCell }>
|
|
177
|
+ <Text style = { styles.statsTitleText }>
|
|
178
|
+ { `${t('connectionindicator.resolution')} ` }
|
|
179
|
+ </Text>
|
|
180
|
+ <Text style = { styles.statsInfoText }>
|
|
181
|
+ { this.state.resolutionString }
|
|
182
|
+ </Text>
|
|
183
|
+ </View>
|
|
184
|
+ <View style = { styles.statsInfoCell }>
|
|
185
|
+ <Text style = { styles.statsTitleText }>
|
|
186
|
+ { `${t('connectionindicator.codecs')}` }
|
|
187
|
+ </Text>
|
|
188
|
+ <Text style = { styles.statsInfoText }>
|
|
189
|
+ { this.state.codecString }
|
|
190
|
+ </Text>
|
|
191
|
+ </View>
|
|
192
|
+ </View>
|
|
193
|
+ </BottomSheet>
|
|
194
|
+ );
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ /**
|
|
198
|
+ * Starts listening for stat updates.
|
|
199
|
+ *
|
|
200
|
+ * @inheritdoc
|
|
201
|
+ * returns {void}
|
|
202
|
+ */
|
|
203
|
+ componentDidMount() {
|
|
204
|
+ statsEmitter.subscribeToClientStats(
|
|
205
|
+ this.props.participantID, this._onStatsUpdated);
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ /**
|
|
209
|
+ * Updates which user's stats are being listened to.
|
|
210
|
+ *
|
|
211
|
+ * @inheritdoc
|
|
212
|
+ * returns {void}
|
|
213
|
+ */
|
|
214
|
+ componentDidUpdate(prevProps: Props) {
|
|
215
|
+ if (prevProps.participantID !== this.props.participantID) {
|
|
216
|
+ statsEmitter.unsubscribeToClientStats(
|
|
217
|
+ prevProps.participantID, this._onStatsUpdated);
|
|
218
|
+ statsEmitter.subscribeToClientStats(
|
|
219
|
+ this.props.participantID, this._onStatsUpdated);
|
|
220
|
+ }
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ _onStatsUpdated: Object => void;
|
|
224
|
+
|
|
225
|
+ /**
|
|
226
|
+ * Callback invoked when new connection stats associated with the passed in
|
|
227
|
+ * user ID are available. Will update the component's display of current
|
|
228
|
+ * statistics.
|
|
229
|
+ *
|
|
230
|
+ * @param {Object} stats - Connection stats from the library.
|
|
231
|
+ * @private
|
|
232
|
+ * @returns {void}
|
|
233
|
+ */
|
|
234
|
+ _onStatsUpdated(stats = {}) {
|
|
235
|
+ const newState = this._buildState(stats);
|
|
236
|
+
|
|
237
|
+ this.setState(newState);
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ /**
|
|
241
|
+ * Extracts statistics and builds the state object.
|
|
242
|
+ *
|
|
243
|
+ * @param {Object} stats - Connection stats from the library.
|
|
244
|
+ * @private
|
|
245
|
+ * @returns {State}
|
|
246
|
+ */
|
|
247
|
+ _buildState(stats) {
|
|
248
|
+ const { download: downloadBitrate, upload: uploadBitrate } = this._extractBitrate(stats) ?? {};
|
|
249
|
+
|
|
250
|
+ const { download: downloadPacketLost, upload: uploadPacketLost } = this._extractPacketLost(stats) ?? {};
|
|
251
|
+
|
|
252
|
+ return {
|
|
253
|
+ resolutionString: this._extractResolutionString(stats) ?? this.state.resolutionString,
|
|
254
|
+ downloadString: downloadBitrate ?? this.state.downloadString,
|
|
255
|
+ uploadString: uploadBitrate ?? this.state.uploadString,
|
|
256
|
+ packetLostDownloadString: downloadPacketLost === undefined
|
|
257
|
+ ? this.state.packetLostDownloadString : `${downloadPacketLost}%`,
|
|
258
|
+ packetLostUploadString: uploadPacketLost === undefined
|
|
259
|
+ ? this.state.packetLostUploadString : `${uploadPacketLost}%`,
|
|
260
|
+ serverRegionString: this._extractServer(stats) ?? this.state.serverRegionString,
|
|
261
|
+ codecString: this._extractCodecs(stats) ?? this.state.codecString,
|
|
262
|
+ connectionString: this._extractConnection(stats) ?? this.state.connectionString
|
|
263
|
+ };
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+ /**
|
|
267
|
+ * Extracts the resolution and framerate.
|
|
268
|
+ *
|
|
269
|
+ * @param {Object} stats - Connection stats from the library.
|
|
270
|
+ * @private
|
|
271
|
+ * @returns {string}
|
|
272
|
+ */
|
|
273
|
+ _extractResolutionString(stats) {
|
|
274
|
+ const { framerate, resolution } = stats;
|
|
275
|
+
|
|
276
|
+ const resolutionString = Object.keys(resolution || {})
|
|
277
|
+ .map(ssrc => {
|
|
278
|
+ const { width, height } = resolution[ssrc];
|
|
279
|
+
|
|
280
|
+ return `${width}x${height}`;
|
|
281
|
+ })
|
|
282
|
+ .join(', ') || null;
|
|
283
|
+
|
|
284
|
+ const frameRateString = Object.keys(framerate || {})
|
|
285
|
+ .map(ssrc => framerate[ssrc])
|
|
286
|
+ .join(', ') || null;
|
|
287
|
+
|
|
288
|
+ return resolutionString && frameRateString ? `${resolutionString}@${frameRateString}fps` : undefined;
|
|
289
|
+ }
|
|
290
|
+
|
|
291
|
+ /**
|
|
292
|
+ * Extracts the download and upload bitrates.
|
|
293
|
+ *
|
|
294
|
+ * @param {Object} stats - Connection stats from the library.
|
|
295
|
+ * @private
|
|
296
|
+ * @returns {{ download, upload }}
|
|
297
|
+ */
|
|
298
|
+ _extractBitrate(stats) {
|
|
299
|
+ return stats.bitrate;
|
|
300
|
+ }
|
|
301
|
+
|
|
302
|
+ /**
|
|
303
|
+ * Extracts the download and upload packet lost.
|
|
304
|
+ *
|
|
305
|
+ * @param {Object} stats - Connection stats from the library.
|
|
306
|
+ * @private
|
|
307
|
+ * @returns {{ download, upload }}
|
|
308
|
+ */
|
|
309
|
+ _extractPacketLost(stats) {
|
|
310
|
+ return stats.packetLoss;
|
|
311
|
+ }
|
|
312
|
+
|
|
313
|
+ /**
|
|
314
|
+ * Extracts the server name.
|
|
315
|
+ *
|
|
316
|
+ * @param {Object} stats - Connection stats from the library.
|
|
317
|
+ * @private
|
|
318
|
+ * @returns {string}
|
|
319
|
+ */
|
|
320
|
+ _extractServer(stats) {
|
|
321
|
+ return stats.serverRegion;
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ /**
|
|
325
|
+ * Extracts the audio and video codecs names.
|
|
326
|
+ *
|
|
327
|
+ * @param {Object} stats - Connection stats from the library.
|
|
328
|
+ * @private
|
|
329
|
+ * @returns {string}
|
|
330
|
+ */
|
|
331
|
+ _extractCodecs(stats) {
|
|
332
|
+ const { codec } = stats;
|
|
333
|
+
|
|
334
|
+ let codecString;
|
|
335
|
+
|
|
336
|
+ // Only report one codec, in case there are multiple for a user.
|
|
337
|
+ Object.keys(codec || {})
|
|
338
|
+ .forEach(ssrc => {
|
|
339
|
+ const { audio, video } = codec[ssrc];
|
|
340
|
+
|
|
341
|
+ codecString = `${audio}, ${video}`;
|
|
342
|
+ });
|
|
343
|
+
|
|
344
|
+ return codecString;
|
|
345
|
+ }
|
|
346
|
+
|
|
347
|
+ /**
|
|
348
|
+ * Extracts the connection percentage and sets connection quality.
|
|
349
|
+ *
|
|
350
|
+ * @param {Object} stats - Connection stats from the library.
|
|
351
|
+ * @private
|
|
352
|
+ * @returns {string}
|
|
353
|
+ */
|
|
354
|
+ _extractConnection(stats) {
|
|
355
|
+ const { connectionQuality } = stats;
|
|
356
|
+
|
|
357
|
+ if (connectionQuality) {
|
|
358
|
+ const signalLevel = Math.floor(connectionQuality / 33.4);
|
|
359
|
+
|
|
360
|
+ return CONNECTION_QUALITY[signalLevel];
|
|
361
|
+ }
|
|
362
|
+ }
|
|
363
|
+
|
|
364
|
+ _onCancel: () => boolean;
|
|
365
|
+
|
|
366
|
+ /**
|
|
367
|
+ * Callback to hide the {@code ConnectionStatusComponent}.
|
|
368
|
+ *
|
|
369
|
+ * @private
|
|
370
|
+ * @returns {boolean}
|
|
371
|
+ */
|
|
372
|
+ _onCancel() {
|
|
373
|
+ statsEmitter.unsubscribeToClientStats(
|
|
374
|
+ this.props.participantID, this._onStatsUpdated);
|
|
375
|
+
|
|
376
|
+ if (this.props._isOpen) {
|
|
377
|
+ this.props.dispatch(hideDialog(ConnectionStatusComponent_));
|
|
378
|
+
|
|
379
|
+ return true;
|
|
380
|
+ }
|
|
381
|
+
|
|
382
|
+ return false;
|
|
383
|
+ }
|
|
384
|
+
|
|
385
|
+ _renderMenuHeader: () => React$Element<any>;
|
|
386
|
+
|
|
387
|
+ /**
|
|
388
|
+ * Function to render the menu's header.
|
|
389
|
+ *
|
|
390
|
+ * @returns {React$Element}
|
|
391
|
+ */
|
|
392
|
+ _renderMenuHeader() {
|
|
393
|
+ const { _bottomSheetStyles, participantID } = this.props;
|
|
394
|
+
|
|
395
|
+ return (
|
|
396
|
+ <View
|
|
397
|
+ style = { [
|
|
398
|
+ _bottomSheetStyles.sheet,
|
|
399
|
+ styles.participantNameContainer ] }>
|
|
400
|
+ <Avatar
|
|
401
|
+ participantId = { participantID }
|
|
402
|
+ size = { AVATAR_SIZE } />
|
|
403
|
+ <Text style = { styles.participantNameLabel }>
|
|
404
|
+ { this.props._participantDisplayName }
|
|
405
|
+ </Text>
|
|
406
|
+ </View>
|
|
407
|
+ );
|
|
408
|
+ }
|
|
409
|
+}
|
|
410
|
+
|
|
411
|
+/**
|
|
412
|
+ * Function that maps parts of Redux state tree into component props.
|
|
413
|
+ *
|
|
414
|
+ * @param {Object} state - Redux state.
|
|
415
|
+ * @param {Object} ownProps - Properties of component.
|
|
416
|
+ * @private
|
|
417
|
+ * @returns {Props}
|
|
418
|
+ */
|
|
419
|
+function _mapStateToProps(state, ownProps) {
|
|
420
|
+ const { participantID } = ownProps;
|
|
421
|
+
|
|
422
|
+ return {
|
|
423
|
+ _bottomSheetStyles: ColorSchemeRegistry.get(state, 'BottomSheet'),
|
|
424
|
+ _isOpen: isDialogOpen(state, ConnectionStatusComponent_),
|
|
425
|
+ _participantDisplayName: getParticipantDisplayName(state, participantID)
|
|
426
|
+ };
|
|
427
|
+}
|
|
428
|
+
|
|
429
|
+ConnectionStatusComponent_ = translate(connect(_mapStateToProps)(ConnectionStatusComponent));
|
|
430
|
+
|
|
431
|
+export default ConnectionStatusComponent_;
|