@webex/plugin-meetings 3.12.0-webex-services-ready.4 → 3.13.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/.sdd/manifest.json +1312 -0
  2. package/.sdd/templates/.source.json +30 -0
  3. package/.sdd/templates/component-repo/module-docs/module-spec-quality.md +111 -0
  4. package/.sdd/templates/component-repo/module-docs/module-spec.template.md +321 -0
  5. package/.sdd/templates/component-repo/reference-docs/adr/_adr-example.md +59 -0
  6. package/.sdd/templates/component-repo/reference-docs/patterns/_pattern-example.md +49 -0
  7. package/.sdd/templates/component-repo/reference-docs/rules/_rule-example.md +42 -0
  8. package/.sdd/templates/component-repo/standing-docs/AGENTS.template.md +120 -0
  9. package/.sdd/templates/component-repo/standing-docs/ARCHITECTURE.template.md +206 -0
  10. package/.sdd/templates/component-repo/standing-docs/CONTRACTS.template.md +90 -0
  11. package/.sdd/templates/component-repo/standing-docs/DATA_MODEL.template.md +68 -0
  12. package/.sdd/templates/component-repo/standing-docs/GETTING_STARTED.template.md +111 -0
  13. package/.sdd/templates/component-repo/standing-docs/GLOSSARY.template.md +57 -0
  14. package/.sdd/templates/component-repo/standing-docs/REVIEW_CHECKLIST.template.md +69 -0
  15. package/.sdd/templates/component-repo/standing-docs/RULES.template.md +96 -0
  16. package/.sdd/templates/component-repo/standing-docs/SECURITY.template.md +84 -0
  17. package/.sdd/templates/component-repo/standing-docs/SERVICE_STATE.template.md +82 -0
  18. package/.sdd/templates/component-repo/standing-docs/SPEC_INDEX.template.md +94 -0
  19. package/.sdd/templates/component-repo/standing-docs/TEST_INDEX.template.md +64 -0
  20. package/.sdd/templates/component-repo/template-map.json +112 -0
  21. package/AGENTS.md +130 -4
  22. package/ai-docs/ARCHITECTURE.md +208 -0
  23. package/ai-docs/CONTRACTS.md +267 -0
  24. package/ai-docs/DATA_MODEL.md +64 -0
  25. package/ai-docs/GETTING_STARTED.md +73 -0
  26. package/ai-docs/GLOSSARY.md +56 -0
  27. package/ai-docs/REVIEW_CHECKLIST.md +45 -0
  28. package/ai-docs/RULES.md +79 -0
  29. package/ai-docs/SECURITY.md +74 -0
  30. package/ai-docs/SERVICE_STATE.md +59 -0
  31. package/ai-docs/SPEC_INDEX.md +85 -0
  32. package/ai-docs/TEST_INDEX.md +45 -0
  33. package/ai-docs/adr/0001-migrate-existing-docs-into-sdd.md +61 -0
  34. package/ai-docs/patterns/defensive-error-propagation.md +55 -0
  35. package/ai-docs/patterns/event-scope-emission.md +46 -0
  36. package/ai-docs/patterns/source-to-test-path-mirroring.md +45 -0
  37. package/ai-docs/patterns/stateless-plugin-request-classes.md +53 -0
  38. package/ai-docs/patterns/typed-constants-and-enum-values.md +47 -0
  39. package/assets/README.md +28 -0
  40. package/assets/e2ee-0.0.1.js +2 -0
  41. package/assets/e2ee-0.0.1.wasm +0 -0
  42. package/dist/aiEnableRequest/index.js +1 -1
  43. package/dist/annotation/index.js +55 -19
  44. package/dist/annotation/index.js.map +1 -1
  45. package/dist/assets/e2ee-0.0.1.js +2 -0
  46. package/dist/assets/e2ee-0.0.1.wasm +0 -0
  47. package/dist/breakouts/breakout.js +1 -1
  48. package/dist/breakouts/index.js +39 -5
  49. package/dist/breakouts/index.js.map +1 -1
  50. package/dist/common/errors/webex-errors.js +7 -2
  51. package/dist/common/errors/webex-errors.js.map +1 -1
  52. package/dist/config.js +1 -0
  53. package/dist/config.js.map +1 -1
  54. package/dist/constants.js +6 -5
  55. package/dist/constants.js.map +1 -1
  56. package/dist/hashTree/hashTreeParser.js +101 -48
  57. package/dist/hashTree/hashTreeParser.js.map +1 -1
  58. package/dist/index.js +1 -0
  59. package/dist/index.js.map +1 -1
  60. package/dist/interceptors/dataChannelAuthToken.js +41 -59
  61. package/dist/interceptors/dataChannelAuthToken.js.map +1 -1
  62. package/dist/interceptors/locusRetry.js +1 -1
  63. package/dist/interceptors/locusRetry.js.map +1 -1
  64. package/dist/interpretation/index.js +46 -12
  65. package/dist/interpretation/index.js.map +1 -1
  66. package/dist/interpretation/siLanguage.js +1 -1
  67. package/dist/locus-info/index.js +236 -80
  68. package/dist/locus-info/index.js.map +1 -1
  69. package/dist/locus-info/types.js +2 -1
  70. package/dist/locus-info/types.js.map +1 -1
  71. package/dist/media/MediaConnectionAwaiter.js +225 -175
  72. package/dist/media/MediaConnectionAwaiter.js.map +1 -1
  73. package/dist/meeting/index.js +355 -299
  74. package/dist/meeting/index.js.map +1 -1
  75. package/dist/meeting/util.js +8 -2
  76. package/dist/meeting/util.js.map +1 -1
  77. package/dist/meeting/voicea-meeting.js +2 -1
  78. package/dist/meeting/voicea-meeting.js.map +1 -1
  79. package/dist/meeting-info/meeting-info-v2.js +10 -3
  80. package/dist/meeting-info/meeting-info-v2.js.map +1 -1
  81. package/dist/meetings/index.js +72 -31
  82. package/dist/meetings/index.js.map +1 -1
  83. package/dist/multistream/remoteMediaManager.js +32 -4
  84. package/dist/multistream/remoteMediaManager.js.map +1 -1
  85. package/dist/multistream/sendSlotManager.js +11 -1
  86. package/dist/multistream/sendSlotManager.js.map +1 -1
  87. package/dist/types/annotation/index.d.ts +19 -0
  88. package/dist/types/common/errors/webex-errors.d.ts +6 -2
  89. package/dist/types/config.d.ts +1 -0
  90. package/dist/types/constants.d.ts +2 -1
  91. package/dist/types/hashTree/hashTreeParser.d.ts +12 -0
  92. package/dist/types/index.d.ts +1 -0
  93. package/dist/types/locus-info/index.d.ts +45 -1
  94. package/dist/types/locus-info/types.d.ts +1 -0
  95. package/dist/types/media/MediaConnectionAwaiter.d.ts +61 -53
  96. package/dist/types/meeting/index.d.ts +49 -12
  97. package/dist/types/meeting/util.d.ts +3 -1
  98. package/dist/types/meeting-info/meeting-info-v2.d.ts +1 -0
  99. package/dist/types/meetings/index.d.ts +2 -1
  100. package/dist/types/multistream/remoteMediaManager.d.ts +18 -0
  101. package/dist/types/multistream/sendSlotManager.d.ts +5 -1
  102. package/dist/webinar/index.js +298 -255
  103. package/dist/webinar/index.js.map +1 -1
  104. package/package.json +25 -23
  105. package/src/aiEnableRequest/ai-docs/ai-enable-request-spec.md +223 -0
  106. package/src/annotation/ai-docs/annotation-spec.md +236 -0
  107. package/src/annotation/index.ts +61 -29
  108. package/src/breakouts/ai-docs/breakouts-spec.md +324 -0
  109. package/src/breakouts/index.ts +35 -2
  110. package/src/common/errors/webex-errors.ts +8 -2
  111. package/src/config.ts +5 -1
  112. package/src/constants.ts +2 -1
  113. package/src/controls-options-manager/ai-docs/controls-options-manager-spec.md +243 -0
  114. package/src/hashTree/ai-docs/hash-tree-spec.md +267 -0
  115. package/src/hashTree/hashTreeParser.ts +67 -3
  116. package/src/index.ts +1 -0
  117. package/src/interceptors/ai-docs/interceptors-spec.md +247 -0
  118. package/src/interceptors/dataChannelAuthToken.ts +48 -77
  119. package/src/interceptors/locusRetry.ts +1 -1
  120. package/src/interpretation/ai-docs/interpretation-spec.md +256 -0
  121. package/src/interpretation/index.ts +38 -1
  122. package/src/locus-info/ai-docs/locus-info-spec.md +293 -0
  123. package/src/locus-info/index.ts +156 -35
  124. package/src/locus-info/types.ts +1 -0
  125. package/src/media/MediaConnectionAwaiter.ts +223 -207
  126. package/src/media/ai-docs/media-spec.md +267 -0
  127. package/src/meeting/ai-docs/meeting-spec.md +404 -0
  128. package/src/meeting/index.ts +332 -296
  129. package/src/meeting/util.ts +7 -4
  130. package/src/meeting/voicea-meeting.ts +1 -1
  131. package/src/meeting-info/ai-docs/meeting-info-spec.md +261 -0
  132. package/src/meeting-info/meeting-info-v2.ts +7 -2
  133. package/src/meetings/ai-docs/meetings-spec.md +296 -0
  134. package/src/meetings/index.ts +59 -23
  135. package/src/member/ai-docs/member-spec.md +196 -0
  136. package/src/members/ai-docs/members-spec.md +256 -0
  137. package/src/metrics/ai-docs/metrics-spec.md +185 -0
  138. package/src/multistream/ai-docs/multistream-spec.md +295 -0
  139. package/src/multistream/remoteMediaManager.ts +23 -1
  140. package/src/multistream/sendSlotManager.ts +15 -1
  141. package/src/personal-meeting-room/ai-docs/personal-meeting-room-spec.md +234 -0
  142. package/src/reachability/ai-docs/reachability-spec.md +261 -0
  143. package/src/reactions/ai-docs/reactions-spec.md +198 -0
  144. package/src/reconnection-manager/ai-docs/reconnection-manager-spec.md +272 -0
  145. package/src/recording-controller/ai-docs/recording-controller-spec.md +223 -0
  146. package/src/roap/ai-docs/roap-spec.md +241 -0
  147. package/src/webinar/ai-docs/webinar-spec.md +243 -0
  148. package/src/webinar/index.ts +168 -188
  149. package/test/unit/spec/annotation/index.ts +214 -182
  150. package/test/unit/spec/breakouts/index.ts +145 -13
  151. package/test/unit/spec/hashTree/hashTreeParser.ts +212 -1
  152. package/test/unit/spec/interceptors/dataChannelAuthToken.ts +81 -99
  153. package/test/unit/spec/interceptors/locusRetry.ts +29 -1
  154. package/test/unit/spec/interpretation/index.ts +93 -0
  155. package/test/unit/spec/locus-info/index.js +397 -0
  156. package/test/unit/spec/media/MediaConnectionAwaiter.ts +426 -19
  157. package/test/unit/spec/media/properties.ts +3 -3
  158. package/test/unit/spec/meeting/index.js +1219 -723
  159. package/test/unit/spec/meeting/utils.js +52 -18
  160. package/test/unit/spec/meeting/voicea-meeting.ts +85 -1
  161. package/test/unit/spec/meeting-info/meetinginfov2.js +69 -1
  162. package/test/unit/spec/meetings/index.js +145 -43
  163. package/test/unit/spec/multistream/remoteMediaManager.ts +46 -0
  164. package/test/unit/spec/multistream/sendSlotManager.ts +30 -2
  165. package/test/unit/spec/webinar/index.ts +886 -718
@@ -1,7 +1,7 @@
1
1
  import {Defer} from '@webex/common';
2
2
  import {ConnectionState, MediaConnectionEventNames} from '@webex/internal-media-core';
3
3
  import LoggerProxy from '../common/logs/logger-proxy';
4
- import {ICE_AND_DTLS_CONNECTION_TIMEOUT} from '../constants';
4
+ import {DTLS_CONNECTION_TIMEOUT, ICE_CONNECTION_TIMEOUT} from '../constants';
5
5
  import BEHAVIORAL_METRICS from '../metrics/constants';
6
6
  import Metrics from '../metrics';
7
7
 
@@ -12,297 +12,313 @@ export interface MediaConnectionAwaiterProps {
12
12
 
13
13
  export interface FailureResult {
14
14
  iceConnected: boolean;
15
+ iceConnectedAtLeastOnce: boolean;
15
16
  }
16
17
 
18
+ type State =
19
+ | {name: 'idle'}
20
+ | {name: 'ice'; gatheringRetried: boolean}
21
+ | {name: 'dtls'}
22
+ | {name: 'connected'}
23
+ | {name: 'failed'; result: FailureResult};
24
+
25
+ type ActiveState = Extract<State, {name: 'ice' | 'dtls'}>;
26
+ type EnteredState = Exclude<State, {name: 'idle'}>;
27
+
28
+ const PHASE_TIMEOUT: Record<ActiveState['name'], number> = {
29
+ ice: ICE_CONNECTION_TIMEOUT,
30
+ dtls: DTLS_CONNECTION_TIMEOUT,
31
+ };
32
+
17
33
  /**
18
- * @class MediaConnectionAwaiter
34
+ * Waits for ICE and DTLS connectivity using explicit lifecycle states.
19
35
  */
20
36
  export default class MediaConnectionAwaiter {
21
- private webrtcMediaConnection: any;
22
- private timer: any;
23
- private defer: Defer;
24
- private retried: boolean;
25
- private iceConnected: boolean;
26
- private correlationId: string;
27
- private onTimeoutCallback: () => void;
28
- private peerConnectionStateCallback: () => void;
29
- private iceConnectionStateCallback: () => void;
30
- private iceGatheringStateCallback: () => void;
37
+ private props: MediaConnectionAwaiterProps;
38
+ private state: State = {name: 'idle'};
39
+ private timer?: ReturnType<typeof setTimeout>;
40
+ private iceConnectedAtLeastOnce = false;
41
+ private defer = new Defer();
31
42
 
32
43
  /**
33
- * @param {MediaConnectionAwaiterProps} mediaConnectionAwaiterProps
44
+ * @param {MediaConnectionAwaiterProps} props
34
45
  */
35
- constructor({webrtcMediaConnection, correlationId}: MediaConnectionAwaiterProps) {
36
- this.webrtcMediaConnection = webrtcMediaConnection;
37
- this.defer = new Defer();
38
- this.retried = false;
39
- this.iceConnected = false;
40
- this.correlationId = correlationId;
41
- this.onTimeoutCallback = this.onTimeout.bind(this);
42
- this.peerConnectionStateCallback = this.peerConnectionStateHandler.bind(this);
43
- this.iceConnectionStateCallback = this.iceConnectionStateHandler.bind(this);
44
- this.iceGatheringStateCallback = this.iceGatheringStateHandler.bind(this);
46
+ constructor(props: MediaConnectionAwaiterProps) {
47
+ this.props = props;
45
48
  }
46
49
 
47
50
  /**
48
- * Returns true if the connection is connected, false otherwise.
49
- *
50
- * @returns {boolean}
51
+ * Registers listeners and starts the wait once, settling terminal connections immediately.
52
+ * @returns {Promise<void>} Rejects with FailureResult on failure
51
53
  */
52
- private isConnected(): boolean {
53
- return this.webrtcMediaConnection.getConnectionState() === ConnectionState.Connected;
54
- }
54
+ waitForMediaConnectionConnected(): Promise<void> {
55
+ if (this.state.name === 'idle') {
56
+ this.registerCallbacks();
57
+ this.evaluate('waitForMediaConnectionConnected');
58
+ }
55
59
 
56
- /**
57
- * Returns true if the connection is in an unrecoverable "failed" state
58
- *
59
- * @returns {boolean}
60
- */
61
- private isFailed(): boolean {
62
- return this.webrtcMediaConnection.getConnectionState() === ConnectionState.Failed;
60
+ return this.defer.promise;
63
61
  }
64
62
 
65
63
  /**
66
- * Returns true if ICE connection state indicates connectivity.
67
- *
68
- * @returns {boolean}
64
+ * Applies a transition to a new state and owns its deadline or terminal settlement.
65
+ * @param {EnteredState} next - State to enter; an active wait cannot return to idle
66
+ * @param {string} reason - Trigger or reason for the transition
67
+ * @returns {void}
69
68
  */
70
- private isIceConnected(): boolean {
71
- const state = this.webrtcMediaConnection.getIceConnectionState();
69
+ private enter(next: EnteredState, reason: string): void {
70
+ if (this.isTerminal()) {
71
+ return;
72
+ }
72
73
 
73
- return state === 'connected' || state === 'completed';
74
+ LoggerProxy.logger.log(
75
+ `Media:MediaConnectionAwaiter#enter --> ${this.state.name} -> ${next.name}: ${reason}`
76
+ );
77
+ this.state = next;
78
+
79
+ switch (next.name) {
80
+ case 'ice':
81
+ case 'dtls':
82
+ this.armTimer(next, reason);
83
+ break;
84
+ case 'connected':
85
+ this.teardown();
86
+ this.defer.resolve();
87
+ break;
88
+ case 'failed':
89
+ this.teardown();
90
+ this.defer.reject(next.result);
91
+ break;
92
+ }
74
93
  }
75
94
 
76
95
  /**
77
- * Returns true if the ICE Gathering is completed, false otherwise.
78
- *
96
+ * Returns whether the promise has settled.
79
97
  * @returns {boolean}
80
98
  */
81
- private isIceGatheringCompleted(): boolean {
82
- return this.webrtcMediaConnection.getIceGatheringState() === 'complete';
99
+ private isTerminal(): boolean {
100
+ return this.state.name === 'connected' || this.state.name === 'failed';
83
101
  }
84
102
 
85
103
  /**
86
- * Clears the callbacks.
87
- *
88
- * @returns {void}
104
+ * Returns the current state only if it owns a connection deadline.
105
+ * @returns {ActiveState | undefined}
89
106
  */
90
- private clearCallbacks(): void {
91
- this.webrtcMediaConnection.off(
92
- MediaConnectionEventNames.ICE_GATHERING_STATE_CHANGED,
93
- this.iceGatheringStateCallback
94
- );
95
- this.webrtcMediaConnection.off(
96
- MediaConnectionEventNames.PEER_CONNECTION_STATE_CHANGED,
97
- this.peerConnectionStateCallback
98
- );
99
- this.webrtcMediaConnection.off(
100
- MediaConnectionEventNames.ICE_CONNECTION_STATE_CHANGED,
101
- this.iceConnectionStateCallback
102
- );
107
+ private getActiveState(): ActiveState | undefined {
108
+ const {state} = this;
109
+
110
+ return state.name === 'ice' || state.name === 'dtls' ? state : undefined;
103
111
  }
104
112
 
105
113
  /**
106
- * On connection state change.
107
- *
108
- * @returns {void}
114
+ * Logs current browser state and transitions when connectivity or phase changes.
115
+ * @param {string} trigger - Callback or operation that triggered evaluation
116
+ * @returns {boolean} Whether a state transition occurred
109
117
  */
110
- connectionStateChange(): void {
118
+ private evaluate(trigger: string): boolean {
119
+ const connection = this.props.webrtcMediaConnection;
120
+ const connectionState = connection.getConnectionState();
121
+
111
122
  LoggerProxy.logger.log(
112
- `Media:MediaConnectionAwaiter#connectionStateChange --> connection state: ${this.webrtcMediaConnection.getConnectionState()}`
123
+ `Media:MediaConnectionAwaiter#evaluate --> triggered by ${trigger}, connection state: ${connectionState}, peer connection state: ${connection.getPeerConnectionState()}, ICE connection state: ${connection.getIceConnectionState()}, ICE gathering state: ${connection.getIceGatheringState()}, awaiter state: ${
124
+ this.state.name
125
+ }`
113
126
  );
114
127
 
115
- if (this.isFailed()) {
116
- LoggerProxy.logger.warn(
117
- 'Media:MediaConnectionAwaiter#connectionStateChange --> ICE failed, rejecting'
118
- );
119
- this.clearCallbacks();
120
-
121
- this.defer.reject({
122
- iceConnected: this.iceConnected,
123
- } satisfies FailureResult);
128
+ if (this.isTerminal()) {
129
+ return false;
124
130
  }
125
131
 
126
- if (!this.isConnected()) {
127
- return;
132
+ const connectivity = this.getConnectivity();
133
+ if (connectionState === ConnectionState.Connected) {
134
+ this.enter({name: 'connected'}, trigger);
135
+
136
+ return true;
128
137
  }
138
+ if (connectionState === ConnectionState.Failed || connectionState === ConnectionState.Closed) {
139
+ this.enter({name: 'failed', result: connectivity}, trigger);
129
140
 
130
- clearTimeout(this.timer);
141
+ return true;
142
+ }
131
143
 
132
- this.clearCallbacks();
144
+ const phase = connectivity.iceConnected ? 'dtls' : 'ice';
145
+ if (this.state.name === phase) {
146
+ return false;
147
+ }
133
148
 
134
- LoggerProxy.logger.warn('Media:MediaConnectionAwaiter#connectionStateChange --> Resolving');
149
+ this.enter(phase === 'ice' ? {name: 'ice', gatheringRetried: false} : {name: 'dtls'}, trigger);
135
150
 
136
- this.defer.resolve();
151
+ return true;
137
152
  }
138
153
 
139
154
  /**
140
- * Listener for peer connection state change.
141
- *
155
+ * Listener for peer connection state changes.
142
156
  * @returns {void}
143
157
  */
144
- peerConnectionStateHandler(): void {
145
- const peerConnectionState = this.webrtcMediaConnection.getPeerConnectionState();
146
-
147
- LoggerProxy.logger.log(
148
- `Media:MediaConnectionAwaiter#peerConnectionStateHandler --> Peer connection state change -> ${peerConnectionState}`
149
- );
150
-
151
- this.connectionStateChange();
152
- }
158
+ peerConnectionStateHandler = (): void => {
159
+ this.evaluate('peerConnectionStateHandler');
160
+ };
153
161
 
154
162
  /**
155
- * Listener for ICE connection state change.
156
- *
163
+ * Listener for ICE connection state changes.
157
164
  * @returns {void}
158
165
  */
159
- iceConnectionStateHandler(): void {
160
- const iceConnectionState = this.webrtcMediaConnection.getIceConnectionState();
166
+ iceConnectionStateHandler = (): void => {
167
+ this.evaluate('iceConnectionStateHandler');
168
+ };
161
169
 
162
- LoggerProxy.logger.log(
163
- `Media:MediaConnectionAwaiter#iceConnectionStateHandler --> ICE connection state change -> ${iceConnectionState}`
164
- );
165
-
166
- if (this.isIceConnected() && !this.iceConnected) {
167
- this.iceConnected = true;
170
+ /**
171
+ * Restarts only an unchanged ICE phase when gathering completes.
172
+ * @returns {void}
173
+ */
174
+ iceGatheringStateHandler = (): void => {
175
+ const changed = this.evaluate('iceGatheringStateHandler');
176
+ if (!changed && this.state.name === 'ice' && this.isIceGatheringCompleted()) {
177
+ this.armTimer(this.state, 'iceGatheringStateHandler: ICE gathering completed');
168
178
  }
169
-
170
- this.connectionStateChange();
171
- }
179
+ };
172
180
 
173
181
  /**
174
- * Listener for ICE gathering state change.
175
- *
182
+ * Rechecks connectivity before applying the deadline or the ICE gathering retry.
176
183
  * @returns {void}
177
184
  */
178
- iceGatheringStateHandler(): void {
179
- const iceGatheringState = this.webrtcMediaConnection.getIceGatheringState();
180
-
181
- LoggerProxy.logger.log(
182
- `Media:MediaConnectionAwaiter#iceGatheringStateHandler --> ICE gathering state change -> ${iceGatheringState}`
183
- );
185
+ onTimeout = (): void => {
186
+ if (this.evaluate('onTimeout')) {
187
+ return;
188
+ }
184
189
 
185
- if (!this.isIceGatheringCompleted()) {
190
+ const state = this.getActiveState();
191
+ if (!state) {
186
192
  return;
187
193
  }
188
194
 
189
- if (this.isConnected()) {
195
+ this.sendMetric().catch((error) => {
196
+ LoggerProxy.logger.warn('Media:MediaConnectionAwaiter#onTimeout --> metric failed', error);
197
+ });
198
+
199
+ if (state.name === 'ice' && !state.gatheringRetried && !this.isIceGatheringCompleted()) {
200
+ this.enter(
201
+ {...state, gatheringRetried: true},
202
+ 'onTimeout: ICE gathering incomplete, retrying once'
203
+ );
204
+
190
205
  return;
191
206
  }
192
207
 
193
- clearTimeout(this.timer);
208
+ this.enter({name: 'failed', result: this.getConnectivity()}, `${state.name} timeout`);
209
+ };
194
210
 
195
- this.timer = setTimeout(this.onTimeoutCallback, ICE_AND_DTLS_CONNECTION_TIMEOUT);
211
+ /**
212
+ * Arms an active phase's deadline and logs every start or restart.
213
+ * @param {ActiveState} state - Active state whose deadline is being started
214
+ * @param {string} reason - Why the deadline is being started or restarted
215
+ * @returns {void}
216
+ */
217
+ private armTimer(state: ActiveState, reason: string): void {
218
+ if (this.timer !== undefined) {
219
+ clearTimeout(this.timer);
220
+ }
221
+ const timeout = PHASE_TIMEOUT[state.name];
222
+ this.timer = setTimeout(this.onTimeout, timeout);
223
+ LoggerProxy.logger.log(
224
+ `Media:MediaConnectionAwaiter#armTimer --> ${state.name} (${timeout}ms): ${reason}`
225
+ );
196
226
  }
197
227
 
198
228
  /**
199
- * sends a metric with some additional info that might help debugging
200
- * issues where browser doesn't update the RTCPeerConnection's iceConnectionState or connectionState
201
- *
229
+ * Releases the deadline and registered listeners on either terminal transition.
202
230
  * @returns {void}
203
231
  */
204
- async sendMetric() {
205
- const stats = await this.webrtcMediaConnection.getStats();
206
-
207
- // in theory we can end up with more than one transport report in the stats,
208
- // but for the purpose of this metric it's fine to just use the first one
209
- const transportReports = Array.from(
210
- stats.values().filter((report) => report.type === 'transport')
211
- ) as Record<string, number | string>[];
212
-
213
- Metrics.sendBehavioralMetric(BEHAVIORAL_METRICS.MEDIA_STILL_NOT_CONNECTED, {
214
- correlation_id: this.correlationId,
215
- numTransports: transportReports.length,
216
- dtlsState: transportReports[0]?.dtlsState,
217
- iceState: transportReports[0]?.iceState,
218
- packetsSent: transportReports[0]?.packetsSent,
219
- packetsReceived: transportReports[0]?.packetsReceived,
220
- dataChannelState: this.webrtcMediaConnection.multistreamConnection?.dataChannel?.readyState,
221
- });
232
+ private teardown(): void {
233
+ clearTimeout(this.timer);
234
+ this.timer = undefined;
235
+ this.unregisterCallbacks();
222
236
  }
223
237
 
224
238
  /**
225
- * Function called when the timeout is reached.
226
- *
239
+ * Registers each callback once with a stable function reference.
227
240
  * @returns {void}
228
241
  */
229
- onTimeout(): void {
230
- if (this.isConnected()) {
231
- this.clearCallbacks();
232
-
233
- this.defer.resolve();
234
-
235
- return;
236
- }
237
-
238
- this.sendMetric();
239
-
240
- if (!this.isIceGatheringCompleted()) {
241
- if (!this.retried) {
242
- LoggerProxy.logger.warn(
243
- 'Media:MediaConnectionAwaiter#onTimeout --> ICE gathering did not complete within the timeout for the first time, retrying once'
244
- );
245
-
246
- // retry once if ICE gathering is not completed
247
- this.retried = true;
248
- clearTimeout(this.timer);
249
- this.timer = setTimeout(this.onTimeoutCallback, ICE_AND_DTLS_CONNECTION_TIMEOUT);
250
-
251
- return;
252
- }
253
-
254
- LoggerProxy.logger.warn(
255
- 'Media:MediaConnectionAwaiter#onTimeout --> ICE gathering did not complete within the timeout for the second time, rejecting'
256
- );
257
- } else {
258
- LoggerProxy.logger.warn(
259
- 'Media:MediaConnectionAwaiter#onTimeout --> ICE gathering completed, but connection state is not connected, rejecting'
260
- );
261
- }
262
-
263
- this.clearCallbacks();
264
-
265
- this.defer.reject({
266
- iceConnected: this.iceConnected,
267
- } satisfies FailureResult);
242
+ private registerCallbacks(): void {
243
+ const connection = this.props.webrtcMediaConnection;
244
+ connection.on(
245
+ MediaConnectionEventNames.PEER_CONNECTION_STATE_CHANGED,
246
+ this.peerConnectionStateHandler
247
+ );
248
+ connection.on(
249
+ MediaConnectionEventNames.ICE_CONNECTION_STATE_CHANGED,
250
+ this.iceConnectionStateHandler
251
+ );
252
+ connection.on(
253
+ MediaConnectionEventNames.ICE_GATHERING_STATE_CHANGED,
254
+ this.iceGatheringStateHandler
255
+ );
268
256
  }
269
257
 
270
258
  /**
271
- * Waits for the webrtc media connection to be connected.
272
- *
273
- * @returns {Promise<void>} In case of failure, the promise is rejected with FailureResult
259
+ * Removes callbacks when the wait reaches a terminal state.
260
+ * @returns {void}
274
261
  */
275
- waitForMediaConnectionConnected(): Promise<void> {
276
- if (this.isConnected()) {
277
- LoggerProxy.logger.log(
278
- 'Media:MediaConnectionAwaiter#waitForMediaConnectionConnected --> Already connected'
279
- );
280
-
281
- return Promise.resolve();
282
- }
283
- LoggerProxy.logger.log(
284
- 'Media:MediaConnectionAwaiter#waitForMediaConnectionConnected --> Waiting for media connection to be connected'
262
+ private unregisterCallbacks(): void {
263
+ const connection = this.props.webrtcMediaConnection;
264
+ connection.off(
265
+ MediaConnectionEventNames.ICE_GATHERING_STATE_CHANGED,
266
+ this.iceGatheringStateHandler
285
267
  );
286
-
287
- this.iceConnected = this.isIceConnected();
288
-
289
- this.webrtcMediaConnection.on(
268
+ connection.off(
290
269
  MediaConnectionEventNames.PEER_CONNECTION_STATE_CHANGED,
291
- this.peerConnectionStateCallback
270
+ this.peerConnectionStateHandler
292
271
  );
293
-
294
- this.webrtcMediaConnection.on(
272
+ connection.off(
295
273
  MediaConnectionEventNames.ICE_CONNECTION_STATE_CHANGED,
296
- this.iceConnectionStateCallback
274
+ this.iceConnectionStateHandler
297
275
  );
276
+ }
298
277
 
299
- this.webrtcMediaConnection.on(
300
- MediaConnectionEventNames.ICE_GATHERING_STATE_CHANGED,
301
- this.iceGatheringStateCallback
302
- );
278
+ /**
279
+ * Returns whether ICE gathering has completed.
280
+ * @returns {boolean}
281
+ */
282
+ private isIceGatheringCompleted(): boolean {
283
+ return this.props.webrtcMediaConnection.getIceGatheringState() === 'complete';
284
+ }
303
285
 
304
- this.timer = setTimeout(this.onTimeoutCallback, ICE_AND_DTLS_CONNECTION_TIMEOUT);
286
+ /**
287
+ * Captures current and historical ICE connectivity for failures and metrics.
288
+ * @returns {FailureResult}
289
+ */
290
+ private getConnectivity(): FailureResult {
291
+ const iceState = this.props.webrtcMediaConnection.getIceConnectionState();
292
+ const iceConnected = iceState === 'connected' || iceState === 'completed';
293
+ this.iceConnectedAtLeastOnce ||= iceConnected;
305
294
 
306
- return this.defer.promise;
295
+ return {iceConnected, iceConnectedAtLeastOnce: this.iceConnectedAtLeastOnce};
296
+ }
297
+
298
+ /**
299
+ * Sends connectivity diagnostics when a deadline expires.
300
+ * @returns {Promise<void>}
301
+ */
302
+ private async sendMetric(): Promise<void> {
303
+ // Capture connectivity before awaiting stats, which may finish after another state change.
304
+ const connectivity = this.getConnectivity();
305
+ const connection = this.props.webrtcMediaConnection;
306
+ const stats = await connection.getStats();
307
+
308
+ // There may be multiple transport reports; the first is sufficient for this metric.
309
+ const transportReports = Array.from(
310
+ stats.values().filter((report) => report.type === 'transport')
311
+ ) as Record<string, number | string>[];
312
+
313
+ Metrics.sendBehavioralMetric(BEHAVIORAL_METRICS.MEDIA_STILL_NOT_CONNECTED, {
314
+ correlation_id: this.props.correlationId,
315
+ ...connectivity,
316
+ numTransports: transportReports.length,
317
+ dtlsState: transportReports[0]?.dtlsState,
318
+ iceState: transportReports[0]?.iceState,
319
+ packetsSent: transportReports[0]?.packetsSent,
320
+ packetsReceived: transportReports[0]?.packetsReceived,
321
+ dataChannelState: connection.multistreamConnection?.dataChannel?.readyState,
322
+ });
307
323
  }
308
324
  }