@stream-io/video-client 1.48.0 → 1.49.0
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.
- package/CHANGELOG.md +14 -0
- package/dist/index.browser.es.js +376 -48
- package/dist/index.browser.es.js.map +1 -1
- package/dist/index.cjs.js +376 -48
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +376 -48
- package/dist/index.es.js.map +1 -1
- package/dist/src/Call.d.ts +35 -1
- package/dist/src/StreamSfuClient.d.ts +8 -1
- package/dist/src/devices/DeviceManagerState.d.ts +13 -0
- package/dist/src/helpers/SlidingWindowRateLimiter.d.ts +28 -0
- package/dist/src/rtc/BasePeerConnection.d.ts +11 -2
- package/dist/src/rtc/index.d.ts +1 -0
- package/dist/src/rtc/types.d.ts +33 -1
- package/package.json +1 -1
- package/src/Call.ts +179 -18
- package/src/StreamSfuClient.ts +75 -12
- package/src/__tests__/Call.publishing.test.ts +103 -0
- package/src/__tests__/StreamSfuClient.test.ts +275 -0
- package/src/devices/DeviceManagerState.ts +20 -0
- package/src/devices/__tests__/ScreenShareManager.test.ts +0 -2
- package/src/devices/devices.ts +2 -1
- package/src/helpers/SlidingWindowRateLimiter.ts +49 -0
- package/src/helpers/__tests__/SlidingWindowRateLimiter.test.ts +43 -0
- package/src/rpc/retryable.ts +0 -1
- package/src/rtc/BasePeerConnection.ts +96 -6
- package/src/rtc/Publisher.ts +2 -1
- package/src/rtc/__tests__/Call.reconnect.test.ts +761 -0
- package/src/rtc/__tests__/Publisher.test.ts +210 -0
- package/src/rtc/__tests__/Subscriber.test.ts +56 -0
- package/src/rtc/index.ts +1 -0
- package/src/rtc/types.ts +38 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { SlidingWindowRateLimiter } from '../SlidingWindowRateLimiter';
|
|
3
|
+
|
|
4
|
+
describe('SlidingWindowRateLimiter', () => {
|
|
5
|
+
it('allows up to the configured number of attempts inside the window', () => {
|
|
6
|
+
const limiter = new SlidingWindowRateLimiter(3, 1000);
|
|
7
|
+
expect(limiter.tryRegister(0)).toBe(true);
|
|
8
|
+
expect(limiter.tryRegister(10)).toBe(true);
|
|
9
|
+
expect(limiter.tryRegister(20)).toBe(true);
|
|
10
|
+
// fourth attempt inside the same window is denied
|
|
11
|
+
expect(limiter.tryRegister(30)).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('prunes timestamps outside the rolling window so attempts after it pass', () => {
|
|
15
|
+
const limiter = new SlidingWindowRateLimiter(2, 1000);
|
|
16
|
+
expect(limiter.tryRegister(0)).toBe(true);
|
|
17
|
+
expect(limiter.tryRegister(500)).toBe(true);
|
|
18
|
+
expect(limiter.tryRegister(900)).toBe(false);
|
|
19
|
+
// at t=1501 cutoff=501, so both 0 and 500 fall out of window
|
|
20
|
+
expect(limiter.tryRegister(1501)).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('reset() clears the attempt history', () => {
|
|
24
|
+
const limiter = new SlidingWindowRateLimiter(2, 1000);
|
|
25
|
+
limiter.tryRegister(0);
|
|
26
|
+
limiter.tryRegister(100);
|
|
27
|
+
expect(limiter.tryRegister(200)).toBe(false);
|
|
28
|
+
limiter.reset();
|
|
29
|
+
expect(limiter.tryRegister(300)).toBe(true);
|
|
30
|
+
expect(limiter.tryRegister(400)).toBe(true);
|
|
31
|
+
expect(limiter.tryRegister(500)).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('setLimits() updates the budget and window in place', () => {
|
|
35
|
+
const limiter = new SlidingWindowRateLimiter(2, 1000);
|
|
36
|
+
limiter.tryRegister(0);
|
|
37
|
+
limiter.tryRegister(100);
|
|
38
|
+
expect(limiter.tryRegister(200)).toBe(false);
|
|
39
|
+
// raising the limit lets the next attempt through without reset
|
|
40
|
+
limiter.setLimits(5, 1000);
|
|
41
|
+
expect(limiter.tryRegister(300)).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
});
|
package/src/rpc/retryable.ts
CHANGED
|
@@ -44,7 +44,6 @@ export const retryable = async <
|
|
|
44
44
|
let result: FinishedUnaryCall<I, O> | undefined = undefined;
|
|
45
45
|
do {
|
|
46
46
|
if (attempt > 0) await sleep(retryInterval(attempt));
|
|
47
|
-
if (signal?.aborted) throw new Error(signal.reason);
|
|
48
47
|
|
|
49
48
|
try {
|
|
50
49
|
result = await rpc({ attempt });
|
|
@@ -13,7 +13,12 @@ import { StreamSfuClient } from '../StreamSfuClient';
|
|
|
13
13
|
import { AllSfuEvents, Dispatcher } from './Dispatcher';
|
|
14
14
|
import { withoutConcurrency } from '../helpers/concurrency';
|
|
15
15
|
import { StatsTracer, Tracer, traceRTCPeerConnection } from '../stats';
|
|
16
|
-
import
|
|
16
|
+
import {
|
|
17
|
+
BasePeerConnectionOpts,
|
|
18
|
+
OnIceConnected,
|
|
19
|
+
OnReconnectionNeeded,
|
|
20
|
+
ReconnectReason,
|
|
21
|
+
} from './types';
|
|
17
22
|
import type { ClientPublishOptions } from '../types';
|
|
18
23
|
|
|
19
24
|
/**
|
|
@@ -31,8 +36,11 @@ export abstract class BasePeerConnection {
|
|
|
31
36
|
protected sfuClient: StreamSfuClient;
|
|
32
37
|
|
|
33
38
|
private onReconnectionNeeded?: OnReconnectionNeeded;
|
|
39
|
+
private onIceConnected?: OnIceConnected;
|
|
34
40
|
private readonly iceRestartDelay: number;
|
|
41
|
+
private iceHasEverConnected = false;
|
|
35
42
|
private iceRestartTimeout?: NodeJS.Timeout;
|
|
43
|
+
private preConnectStuckTimeout?: NodeJS.Timeout;
|
|
36
44
|
protected isIceRestarting = false;
|
|
37
45
|
private isDisposed = false;
|
|
38
46
|
|
|
@@ -56,6 +64,7 @@ export abstract class BasePeerConnection {
|
|
|
56
64
|
state,
|
|
57
65
|
dispatcher,
|
|
58
66
|
onReconnectionNeeded,
|
|
67
|
+
onIceConnected,
|
|
59
68
|
tag,
|
|
60
69
|
enableTracing,
|
|
61
70
|
clientPublishOptions,
|
|
@@ -70,6 +79,7 @@ export abstract class BasePeerConnection {
|
|
|
70
79
|
this.clientPublishOptions = clientPublishOptions;
|
|
71
80
|
this.tag = tag;
|
|
72
81
|
this.onReconnectionNeeded = onReconnectionNeeded;
|
|
82
|
+
this.onIceConnected = onIceConnected;
|
|
73
83
|
this.logger = videoLoggerSystem.getLogger(
|
|
74
84
|
peerType === PeerType.SUBSCRIBER ? 'Subscriber' : 'Publisher',
|
|
75
85
|
{ tags: [tag] },
|
|
@@ -108,7 +118,10 @@ export abstract class BasePeerConnection {
|
|
|
108
118
|
dispose() {
|
|
109
119
|
clearTimeout(this.iceRestartTimeout);
|
|
110
120
|
this.iceRestartTimeout = undefined;
|
|
121
|
+
clearTimeout(this.preConnectStuckTimeout);
|
|
122
|
+
this.preConnectStuckTimeout = undefined;
|
|
111
123
|
this.onReconnectionNeeded = undefined;
|
|
124
|
+
this.onIceConnected = undefined;
|
|
112
125
|
this.isDisposed = true;
|
|
113
126
|
this.detachEventHandlers();
|
|
114
127
|
this.pc.close();
|
|
@@ -145,14 +158,17 @@ export abstract class BasePeerConnection {
|
|
|
145
158
|
*/
|
|
146
159
|
protected tryRestartIce = () => {
|
|
147
160
|
this.restartIce().catch((e) => {
|
|
148
|
-
|
|
149
|
-
this.logger.error(reason, e);
|
|
161
|
+
this.logger.error('restartICE() failed, initiating reconnect', e);
|
|
150
162
|
const strategy =
|
|
151
163
|
e instanceof NegotiationError &&
|
|
152
164
|
e.error.code === ErrorCode.PARTICIPANT_SIGNAL_LOST
|
|
153
165
|
? WebsocketReconnectStrategy.FAST
|
|
154
166
|
: WebsocketReconnectStrategy.REJOIN;
|
|
155
|
-
this.onReconnectionNeeded?.(
|
|
167
|
+
this.onReconnectionNeeded?.(
|
|
168
|
+
strategy,
|
|
169
|
+
ReconnectReason.RESTART_ICE_FAILED,
|
|
170
|
+
this.peerType,
|
|
171
|
+
);
|
|
156
172
|
});
|
|
157
173
|
};
|
|
158
174
|
|
|
@@ -239,6 +255,20 @@ export abstract class BasePeerConnection {
|
|
|
239
255
|
return !failedStates.has(iceState) && !failedStates.has(connectionState);
|
|
240
256
|
};
|
|
241
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Returns true only when the peer connection is currently fully established
|
|
260
|
+
* (ICE `connected`/`completed` AND connection state `connected`).
|
|
261
|
+
* Transient states like `disconnected`, `checking`, or `new` return false.
|
|
262
|
+
*/
|
|
263
|
+
isStable = () => {
|
|
264
|
+
const iceState = this.pc.iceConnectionState;
|
|
265
|
+
const connectionState = this.pc.connectionState;
|
|
266
|
+
return (
|
|
267
|
+
(iceState === 'connected' || iceState === 'completed') &&
|
|
268
|
+
connectionState === 'connected'
|
|
269
|
+
);
|
|
270
|
+
};
|
|
271
|
+
|
|
242
272
|
/**
|
|
243
273
|
* Handles the ICECandidate event and
|
|
244
274
|
* Initiates an ICE Trickle process with the SFU.
|
|
@@ -292,7 +322,7 @@ export abstract class BasePeerConnection {
|
|
|
292
322
|
if (state === 'failed') {
|
|
293
323
|
this.onReconnectionNeeded?.(
|
|
294
324
|
WebsocketReconnectStrategy.REJOIN,
|
|
295
|
-
|
|
325
|
+
ReconnectReason.CONNECTION_FAILED,
|
|
296
326
|
this.peerType,
|
|
297
327
|
);
|
|
298
328
|
return;
|
|
@@ -320,6 +350,54 @@ export abstract class BasePeerConnection {
|
|
|
320
350
|
// do nothing when ICE is restarting
|
|
321
351
|
if (this.isIceRestarting) return;
|
|
322
352
|
|
|
353
|
+
// Pre-connect handling: ICE has never reached `connected`/`completed`.
|
|
354
|
+
// Restart is futile here (the data plane was never established), but
|
|
355
|
+
// these two terminal-ish states need different treatment:
|
|
356
|
+
// - `failed` is terminal, escalate to REJOIN so a new SFU/credentials
|
|
357
|
+
// /PC configuration gets a chance, and let `Call.reconnect` count
|
|
358
|
+
// this toward the unsupported-network budget.
|
|
359
|
+
// - `disconnected` is transient, the browser may yet move back to
|
|
360
|
+
// `checking`/`connected`. Don't restart, don't escalate; wait it
|
|
361
|
+
// out. If it ultimately fails, ICE will transition to `failed` and
|
|
362
|
+
// the branch above will take over.
|
|
363
|
+
if (!this.iceHasEverConnected) {
|
|
364
|
+
if (state === 'failed') {
|
|
365
|
+
this.logger.info('ICE failed before connected, escalating to REJOIN');
|
|
366
|
+
clearTimeout(this.preConnectStuckTimeout);
|
|
367
|
+
this.preConnectStuckTimeout = undefined;
|
|
368
|
+
this.onReconnectionNeeded?.(
|
|
369
|
+
WebsocketReconnectStrategy.REJOIN,
|
|
370
|
+
ReconnectReason.ICE_NEVER_CONNECTED,
|
|
371
|
+
this.peerType,
|
|
372
|
+
);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (state === 'disconnected') {
|
|
376
|
+
this.logger.info('ICE disconnected before connected, wait to recover');
|
|
377
|
+
// Watchdog: if the browser stays in `disconnected` without ever
|
|
378
|
+
// reaching `connected` or transitioning to `failed`, escalate to
|
|
379
|
+
// REJOIN ourselves so we don't wait silently forever. Rare but
|
|
380
|
+
// observed on flaky mobile networks.
|
|
381
|
+
clearTimeout(this.preConnectStuckTimeout);
|
|
382
|
+
this.preConnectStuckTimeout = setTimeout(() => {
|
|
383
|
+
if (
|
|
384
|
+
!this.iceHasEverConnected &&
|
|
385
|
+
this.pc.iceConnectionState === 'disconnected'
|
|
386
|
+
) {
|
|
387
|
+
this.logger.info(
|
|
388
|
+
'ICE stuck in pre-connect disconnected, escalating to REJOIN',
|
|
389
|
+
);
|
|
390
|
+
this.onReconnectionNeeded?.(
|
|
391
|
+
WebsocketReconnectStrategy.REJOIN,
|
|
392
|
+
ReconnectReason.ICE_NEVER_CONNECTED,
|
|
393
|
+
this.peerType,
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
}, this.iceRestartDelay * 2);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
323
401
|
switch (state) {
|
|
324
402
|
case 'failed':
|
|
325
403
|
// in the `failed` state, we try to restart ICE immediately
|
|
@@ -341,12 +419,24 @@ export abstract class BasePeerConnection {
|
|
|
341
419
|
break;
|
|
342
420
|
|
|
343
421
|
case 'connected':
|
|
344
|
-
|
|
422
|
+
case 'completed':
|
|
423
|
+
// Fire `onIceConnected` exactly once per peer-connection lifetime —
|
|
424
|
+
// the first time ICE reaches `connected`/`completed` end-to-end.
|
|
425
|
+
// Used by `Call` to reset the unsupported-network failure counter
|
|
426
|
+
// only after WebRTC has actually recovered, not merely on SFU join.
|
|
427
|
+
if (!this.iceHasEverConnected) {
|
|
428
|
+
this.iceHasEverConnected = true;
|
|
429
|
+
this.onIceConnected?.(this.peerType);
|
|
430
|
+
}
|
|
431
|
+
// clear any scheduled restartICE since the connection is healthy
|
|
345
432
|
if (this.iceRestartTimeout) {
|
|
346
433
|
this.logger.info('connected connection, canceling restartICE');
|
|
347
434
|
clearTimeout(this.iceRestartTimeout);
|
|
348
435
|
this.iceRestartTimeout = undefined;
|
|
349
436
|
}
|
|
437
|
+
// clear the pre-connect watchdog if it was armed
|
|
438
|
+
clearTimeout(this.preConnectStuckTimeout);
|
|
439
|
+
this.preConnectStuckTimeout = undefined;
|
|
350
440
|
break;
|
|
351
441
|
}
|
|
352
442
|
};
|
package/src/rtc/Publisher.ts
CHANGED
|
@@ -460,7 +460,8 @@ export class Publisher extends BasePeerConnection {
|
|
|
460
460
|
const trackInfos: TrackInfo[] = [];
|
|
461
461
|
for (const publishOption of this.publishOptions) {
|
|
462
462
|
const bundle = this.transceiverCache.get(publishOption);
|
|
463
|
-
|
|
463
|
+
const track = bundle?.transceiver.sender.track;
|
|
464
|
+
if (!bundle || !track || track.readyState !== 'live') continue;
|
|
464
465
|
trackInfos.push(this.toTrackInfo(bundle, sdp));
|
|
465
466
|
}
|
|
466
467
|
return trackInfos;
|