@openpeepshq/rn-components 0.24.0 → 0.28.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.
Files changed (26) hide show
  1. package/package.json +2 -2
  2. package/src/components/conversations/ConversationPreviewCard.test.tsx +345 -0
  3. package/src/components/conversations/ConversationPreviewCard.tsx +29 -24
  4. package/src/components/custom/navigation/side-menu.test.tsx +9 -0
  5. package/src/components/custom/navigation/side-menu.tsx +19 -3
  6. package/src/components/groups/GroupAvatar.test.tsx +20 -2
  7. package/src/components/groups/GroupAvatar.tsx +7 -1
  8. package/src/components/jams/JamCallParticipant.test.tsx +303 -0
  9. package/src/components/jams/JamCallParticipant.tsx +7 -1
  10. package/src/components/jams/useLateTrackMediaStream.test.ts +139 -25
  11. package/src/components/jams/useLateTrackMediaStream.ts +62 -19
  12. package/src/components/post/pieces/ShareMenu.test.tsx +105 -11
  13. package/src/components/post/pieces/ShareMenu.tsx +55 -10
  14. package/src/components/post/pieces/modals/SendToConversationModal.tsx +140 -0
  15. package/src/components/post/pieces/modals/ShareIntoGroupModal.tsx +121 -0
  16. package/src/components/post/pieces/modals/index.ts +2 -0
  17. package/src/components/post/pieces/sharePickers.test.tsx +819 -0
  18. package/src/components/profile/Avatar.test.tsx +19 -0
  19. package/src/components/profile/Avatar.tsx +9 -1
  20. package/src/lib/push-notifications.test.ts +244 -0
  21. package/src/lib/push-notifications.ts +65 -0
  22. package/src/pages/auth/Login.tsx +16 -2
  23. package/src/pages/conversations/Index.test.tsx +235 -0
  24. package/src/pages/conversations/Index.tsx +15 -16
  25. package/src/pages/settings/Account.test.tsx +4 -0
  26. package/src/pages/settings/Account.tsx +14 -0
@@ -0,0 +1,303 @@
1
+ /**
2
+ * Wiring test for the camera tile: the `VideoTrack` in `JamCallParticipant`
3
+ * is keyed by the late-track `remountKey`, so when a first mid-Jam remote
4
+ * camera publish delivers its video track late, the native view remounts and
5
+ * re-runs its one-shot `streamURL` attach (one transition remount plus the
6
+ * hook's retry schedule — tight burst first, then the sparse tail).
7
+ *
8
+ * The real `JamCallParticipant` and the real `useLateTrackMediaStream` run;
9
+ * only the module surfaces the test cannot load under jest are stubbed
10
+ * (react-native, `@livekit/react-native` with a mount-counting `VideoTrack`,
11
+ * `@openpeepshq/react`, i18n, toast, `react-native-sound`, icons, dropdown/
12
+ * button UI, connection and theme hooks, avatar). The `.wav` asset in the
13
+ * overlay needs an explicit mock because jest cannot require audio files.
14
+ */
15
+
16
+ const React = require('react');
17
+ const ReactTestRenderer = require('react-test-renderer');
18
+
19
+ // Counts how many times the stub VideoTrack actually mounts. A key change
20
+ // unmounts the old instance and mounts a new one, so this is a direct count
21
+ // of the tile's VideoTrack remounts (the `key` prop is consumed by the
22
+ // reconciler and is not visible to the stub).
23
+ let videoTrackMountCount = 0;
24
+
25
+ jest.mock('react-native-sound', () => ({
26
+ __esModule: true,
27
+ default: jest.fn().mockImplementation(() => ({
28
+ setVolume: jest.fn(),
29
+ play: jest.fn(),
30
+ release: jest.fn(),
31
+ })),
32
+ }));
33
+
34
+ jest.mock(
35
+ '../../assets/audio/click-notification.wav',
36
+ () => 'click-notification.wav',
37
+ { virtual: true }
38
+ );
39
+
40
+ jest.mock('react-native', () => ({
41
+ Alert: { alert: jest.fn() },
42
+ Pressable: (props: {
43
+ accessibilityLabel?: string;
44
+ onPress?: () => void;
45
+ children?: unknown;
46
+ }) =>
47
+ React.createElement(
48
+ 'Pressable',
49
+ {
50
+ accessibilityLabel: props.accessibilityLabel,
51
+ onPress: props.onPress,
52
+ },
53
+ props.children
54
+ ),
55
+ StyleSheet: {
56
+ absoluteFill: { position: 'absolute', inset: 0 },
57
+ create: (o: Record<string, unknown>) => o,
58
+ },
59
+ View: (props: { style?: unknown; className?: string; children?: unknown }) =>
60
+ React.createElement('View', null, props.children),
61
+ }));
62
+
63
+ jest.mock('@livekit/react-native', () => ({
64
+ VideoTrack: ({
65
+ trackRef,
66
+ mirror,
67
+ }: {
68
+ trackRef: { publication: { trackSid: string } };
69
+ mirror: boolean;
70
+ }) => {
71
+ const { useEffect } = require('react');
72
+ useEffect(() => {
73
+ videoTrackMountCount++;
74
+ }, []);
75
+ return React.createElement('VideoTrack', {
76
+ mirror,
77
+ trackSid: trackRef.publication.trackSid,
78
+ });
79
+ },
80
+ isTrackReference: (ref: { publication?: unknown }) => !!ref.publication,
81
+ useIsSpeaking: () => false,
82
+ useRoomContext: () => ({ localParticipant: {} }),
83
+ }));
84
+
85
+ jest.mock('@openpeepshq/react', () => ({
86
+ useCurrentProfile: () => null,
87
+ participantTileProfile: () => null,
88
+ useJamContext: () => ({ jam: { moderators: [] } }),
89
+ useJamEventsContext: () => ({
90
+ ownReactions: [],
91
+ reactionsForParticipant: () => [],
92
+ }),
93
+ useJamParticipantMute: () => jest.fn(),
94
+ useJamParticipantRemove: () => jest.fn(),
95
+ useRaisedHands: () => new Set(),
96
+ }));
97
+
98
+ jest.mock('react-i18next', () => ({
99
+ useTranslation: () => ({
100
+ t: (key: string, opts?: Record<string, unknown>) =>
101
+ String(key).replace(/\{\{(\w+)\}\}/g, (_m: string, n: string) =>
102
+ String(opts?.[n] ?? '')
103
+ ),
104
+ }),
105
+ }));
106
+
107
+ jest.mock('react-native-toast-message', () => ({
108
+ __esModule: true,
109
+ default: { show: jest.fn() },
110
+ }));
111
+
112
+ jest.mock('../icons/index', () => {
113
+ const icon = (name: string) => () => React.createElement(name);
114
+ return {
115
+ AudioLinesIcon: icon('AudioLinesIcon'),
116
+ BanIcon: icon('BanIcon'),
117
+ EllipsisIcon: icon('EllipsisIcon'),
118
+ HandIcon: icon('HandIcon'),
119
+ MicIcon: icon('MicIcon'),
120
+ MicOffIcon: icon('MicOffIcon'),
121
+ PinIcon: icon('PinIcon'),
122
+ WifiOffIcon: icon('WifiOffIcon'),
123
+ };
124
+ });
125
+
126
+ jest.mock('../ui/dropdown-menu', () => ({
127
+ DropdownMenu: (props: { children?: unknown }) =>
128
+ React.createElement('DropdownMenu', null, props.children),
129
+ DropdownMenuContent: (props: { children?: unknown }) =>
130
+ React.createElement('DropdownMenuContent', null, props.children),
131
+ DropdownMenuGroup: (props: { children?: unknown }) =>
132
+ React.createElement('DropdownMenuGroup', null, props.children),
133
+ DropdownMenuItem: (props: { children?: unknown }) =>
134
+ React.createElement('DropdownMenuItem', null, props.children),
135
+ DropdownMenuTrigger: (props: { children?: unknown }) =>
136
+ React.createElement('DropdownMenuTrigger', null, props.children),
137
+ }));
138
+
139
+ jest.mock('../ui/themed-text', () => ({
140
+ ThemedText: (props: { children?: unknown; className?: string }) =>
141
+ React.createElement(
142
+ 'ThemedText',
143
+ { className: props.className },
144
+ props.children
145
+ ),
146
+ }));
147
+
148
+ jest.mock('../ui/button', () => ({
149
+ Button: (props: { children?: unknown; accessibilityLabel?: string }) =>
150
+ React.createElement(
151
+ 'Button',
152
+ { accessibilityLabel: props.accessibilityLabel },
153
+ props.children
154
+ ),
155
+ }));
156
+
157
+ jest.mock('../../lib/utils', () => ({
158
+ cn: (...classes: Array<string | false | undefined>) =>
159
+ classes.filter(Boolean).join(' '),
160
+ }));
161
+
162
+ jest.mock('../profile/Avatar', () => ({
163
+ ProfileAvatar: () => React.createElement('ProfileAvatar'),
164
+ }));
165
+
166
+ jest.mock('./useParticipantConnection', () => ({
167
+ useConnectionLost: () => false,
168
+ }));
169
+
170
+ jest.mock('../../theme/useThemeOpacity', () => ({
171
+ useThemeOpacity: () => ({
172
+ background: () => ({ backgroundColor: 'rgba(0, 0, 0, 0.7)' }),
173
+ }),
174
+ }));
175
+
176
+ const { JamCallParticipant } = require('./JamCallParticipant');
177
+
178
+ // Minimal stand-in for the webrtc MediaStream surface the hook touches.
179
+ const makeMediaStream = (initialVideoTracks = 0) => {
180
+ const videoTracks: unknown[] = Array.from({ length: initialVideoTracks });
181
+ const listeners: Array<() => void> = [];
182
+ return {
183
+ getVideoTracks: () => videoTracks,
184
+ addEventListener: (_type: string, listener: () => void) =>
185
+ listeners.push(listener),
186
+ removeEventListener: (_type: string, listener: () => void) => {
187
+ const index = listeners.indexOf(listener);
188
+ if (index !== -1) listeners.splice(index, 1);
189
+ },
190
+ // Test helper, not part of the real MediaStream API.
191
+ addVideoTrack() {
192
+ videoTracks.push({});
193
+ listeners.forEach((listener) => listener());
194
+ },
195
+ };
196
+ };
197
+
198
+ type Stream = ReturnType<typeof makeMediaStream>;
199
+
200
+ const renderTile = (track: { mediaStream?: Stream }) => {
201
+ videoTrackMountCount = 0;
202
+ const publication = {
203
+ trackSid: 'track-1',
204
+ isMuted: false,
205
+ track: track as never,
206
+ };
207
+ const trackRef = {
208
+ participant: {
209
+ identity: 'remote',
210
+ isLocal: false,
211
+ isMicrophoneEnabled: true,
212
+ connectionQuality: 1, // ConnectionQuality.Good
213
+ },
214
+ publication,
215
+ } as never;
216
+ let tree!: ReactTestRenderer.ReactTestRenderer;
217
+ ReactTestRenderer.act(() => {
218
+ tree = ReactTestRenderer.create(
219
+ React.createElement(JamCallParticipant, {
220
+ trackRef,
221
+ size: 'size-full',
222
+ })
223
+ );
224
+ });
225
+ return {
226
+ tree,
227
+ mounts: () => videoTrackMountCount,
228
+ unmount() {
229
+ ReactTestRenderer.act(() => {
230
+ tree.unmount();
231
+ });
232
+ },
233
+ };
234
+ };
235
+
236
+ describe('JamCallParticipant camera tile wiring', () => {
237
+ it('remounts the VideoTrack when the stream gains its first video track, plus a bounded burst, then stops', () => {
238
+ jest.useFakeTimers();
239
+ try {
240
+ const stream = makeMediaStream(0);
241
+ const tile = renderTile({ mediaStream: stream });
242
+
243
+ // Two mounts at initial render: the tile renders once, then the #1903
244
+ // trackEpoch effect bumps once on mount, re-keying the VideoTrack.
245
+ expect(tile.mounts()).toBe(2);
246
+
247
+ // First-track transition: one remount (remountKey 0 → 1)...
248
+ ReactTestRenderer.act(() => {
249
+ stream.addVideoTrack();
250
+ });
251
+ expect(tile.mounts()).toBe(3);
252
+
253
+ // ...then the hook's bounded burst: one remount per 300 ms tick, so
254
+ // keep the commits separate by advancing one tick per act().
255
+ ReactTestRenderer.act(() => {
256
+ jest.advanceTimersByTime(300);
257
+ });
258
+ expect(tile.mounts()).toBe(4);
259
+ ReactTestRenderer.act(() => {
260
+ jest.advanceTimersByTime(300);
261
+ });
262
+ expect(tile.mounts()).toBe(5);
263
+ ReactTestRenderer.act(() => {
264
+ jest.advanceTimersByTime(300);
265
+ });
266
+ expect(tile.mounts()).toBe(6);
267
+
268
+ // The tight burst is bounded: the fourth tick (1200 ms) clears the
269
+ // interval, and nothing further remounts up to the tail's first tick
270
+ // (3200 ms) — advance just before it. The tail's remounts at 2 s
271
+ // intervals are covered by the hook's own suite
272
+ // (`useLateTrackMediaStream.test.ts`, "sparse backoff tail"); a camera
273
+ // publish mid-Jam does not renegotiate, so it stays within the burst
274
+ // window.
275
+ ReactTestRenderer.act(() => {
276
+ jest.advanceTimersByTime(1900);
277
+ });
278
+ expect(tile.mounts()).toBe(6);
279
+ tile.unmount();
280
+ } finally {
281
+ jest.useRealTimers();
282
+ }
283
+ });
284
+
285
+ it('does not burst when the stream already has its track at mount', () => {
286
+ jest.useFakeTimers();
287
+ try {
288
+ const tile = renderTile({ mediaStream: makeMediaStream(1) });
289
+
290
+ // The two initial mounts come from the trackEpoch mount bump only.
291
+ expect(tile.mounts()).toBe(2);
292
+
293
+ // No 0 → 1 transition ever happens: no late remounts at all.
294
+ ReactTestRenderer.act(() => {
295
+ jest.advanceTimersByTime(300 * 20);
296
+ });
297
+ expect(tile.mounts()).toBe(2);
298
+ tile.unmount();
299
+ } finally {
300
+ jest.useRealTimers();
301
+ }
302
+ });
303
+ });
@@ -42,6 +42,7 @@ import { cn } from '../../lib/utils';
42
42
  import { ProfileAvatar } from '../profile/Avatar';
43
43
  import { JamAnimatedEmoji } from './JamAnimatedEmoji';
44
44
  import { useConnectionLost } from './useParticipantConnection';
45
+ import { useLateTrackMediaStream } from './useLateTrackMediaStream';
45
46
  import { useThemeOpacity } from '../../theme/useThemeOpacity';
46
47
 
47
48
  export interface JamCallParticipantProps {
@@ -377,6 +378,11 @@ export const JamCallParticipant = ({
377
378
  useEffect(() => {
378
379
  bumpTrackEpoch();
379
380
  }, [cameraTrack]);
381
+ // A first mid-Jam camera publish has the same late-attach race as screen
382
+ // share: the one-shot `streamURL` attach can run before the stream carries
383
+ // its video track and the native view never retries — so the hook's
384
+ // remountKey re-runs it once the track arrives.
385
+ const { remountKey } = useLateTrackMediaStream(cameraTrack);
380
386
  const cameraOn =
381
387
  isTrackReference(trackRef) &&
382
388
  !!cameraTrack &&
@@ -401,7 +407,7 @@ export const JamCallParticipant = ({
401
407
  >
402
408
  {cameraOn ? (
403
409
  <VideoTrack
404
- key={`${trackRef.publication.trackSid}-${trackEpoch}`}
410
+ key={`${trackRef.publication.trackSid}-${trackEpoch}-${remountKey}`}
405
411
  trackRef={trackRef}
406
412
  style={StyleSheet.absoluteFill}
407
413
  objectFit="cover"
@@ -1,10 +1,11 @@
1
1
  /**
2
2
  * useLateTrackMediaStream: emits a changing `remountKey` when the track's
3
- * media stream gains its first video track (and a few times after, as a
4
- * bounded burst). That is what lets the screen-share VideoTrack remount — and
5
- * its native view re-run its one-shot `streamURL` attach — once a share that
6
- * started after we joined delivers its video track, including the first share
7
- * where `addtrack` fires before the layout is mounted.
3
+ * media stream gains its first video track (and on the bounded retry
4
+ * schedule after), so the screen-share VideoTrack's native view re-runs its
5
+ * one-shot `streamURL` attach until it can succeed. The non-obvious part: on
6
+ * the first share of a Jam the receiver renegotiates, so the native track
7
+ * registration can land seconds after the JS-side SDP apply — past the tight
8
+ * burst — which is why the schedule adds a sparse backoff tail.
8
9
  */
9
10
  import React, { act } from 'react';
10
11
  import TestRenderer from 'react-test-renderer';
@@ -22,11 +23,12 @@ const makeMediaStream = (initialVideoTracks = 0) => {
22
23
  const index = listeners.indexOf(listener);
23
24
  if (index !== -1) listeners.splice(index, 1);
24
25
  },
25
- // Test helper, not part of the real MediaStream API.
26
+ // Test helpers, not part of the real MediaStream API.
26
27
  addVideoTrack() {
27
28
  videoTracks.push({});
28
29
  listeners.forEach((listener) => listener());
29
30
  },
31
+ listenerCount: () => listeners.length,
30
32
  };
31
33
  };
32
34
 
@@ -136,9 +138,10 @@ describe('useLateTrackMediaStream', () => {
136
138
  });
137
139
  expect(hook.current.remountKey).toBe(4);
138
140
 
139
- // The burst is bounded: more time passes without further remounts.
141
+ // The burst is bounded at 3 remounts: just before the tail's first
142
+ // tick (2 s after the burst ends) nothing further has bumped.
140
143
  act(() => {
141
- jest.advanceTimersByTime(300 * 10);
144
+ jest.advanceTimersByTime(1900);
142
145
  });
143
146
  expect(hook.current.remountKey).toBe(4);
144
147
  hook.unmount();
@@ -147,23 +150,6 @@ describe('useLateTrackMediaStream', () => {
147
150
  }
148
151
  });
149
152
 
150
- it('does not emit the burst for a stream that already has its track', () => {
151
- jest.useFakeTimers();
152
- try {
153
- const hook = renderHook({ mediaStream: makeMediaStream(1) });
154
- expect(hook.current.remountKey).toBe(0);
155
-
156
- // No 0 → 1 transition ever happens, so no remount burst should fire.
157
- act(() => {
158
- jest.advanceTimersByTime(300 * 20);
159
- });
160
- expect(hook.current.remountKey).toBe(0);
161
- hook.unmount();
162
- } finally {
163
- jest.useRealTimers();
164
- }
165
- });
166
-
167
153
  it('follows the current stream when the track changes', () => {
168
154
  jest.useFakeTimers();
169
155
  try {
@@ -231,4 +217,132 @@ describe('useLateTrackMediaStream', () => {
231
217
  jest.useRealTimers();
232
218
  }
233
219
  });
220
+
221
+ it('ignores a late addtrack on a stream that was swapped out', () => {
222
+ jest.useFakeTimers();
223
+ try {
224
+ const first = makeMediaStream(0);
225
+ const hook = renderHook({ mediaStream: first });
226
+
227
+ act(() => {
228
+ first.addVideoTrack();
229
+ });
230
+ expect(hook.current.remountKey).toBe(1);
231
+
232
+ // Swap to a stream that does not carry a track yet: the stale-listener
233
+ // regression lives here. The effect cleanup resets prevCountRef to 0
234
+ // for the new stream, so a surviving addtrack listener from the old
235
+ // stream would pass its own 0 → 1 check against the old stream and arm
236
+ // a full burst + tail of spurious remounts.
237
+ const second = makeMediaStream(0);
238
+ hook.rerender({ mediaStream: second });
239
+ expect(hook.current.remountKey).toBe(0);
240
+
241
+ act(() => {
242
+ first.addVideoTrack();
243
+ });
244
+ expect(hook.current.remountKey).toBe(0);
245
+
246
+ // Nothing the old stream does can bump the key at any horizon: the
247
+ // cleanup must have removed its listener and cleared its timers.
248
+ act(() => {
249
+ jest.advanceTimersByTime(300 * 4 + 2000 * 5);
250
+ });
251
+ expect(hook.current.remountKey).toBe(0);
252
+ hook.unmount();
253
+ } finally {
254
+ jest.useRealTimers();
255
+ }
256
+ });
257
+
258
+ it('removes its addtrack listener when the hook unmounts', () => {
259
+ jest.useFakeTimers();
260
+ try {
261
+ const stream = makeMediaStream(0);
262
+ const hook = renderHook({ mediaStream: stream });
263
+ expect(stream.listenerCount()).toBe(1);
264
+
265
+ hook.unmount();
266
+ expect(stream.listenerCount()).toBe(0);
267
+
268
+ // The dead stream can no longer drive the hook: a late track after
269
+ // unmount produces neither a state update nor a remount schedule.
270
+ act(() => {
271
+ stream.addVideoTrack();
272
+ jest.advanceTimersByTime(300 * 4 + 2000 * 5);
273
+ });
274
+ } finally {
275
+ jest.useRealTimers();
276
+ }
277
+ });
278
+
279
+ it('emits a sparse backoff tail after the burst for a slow first share', () => {
280
+ jest.useFakeTimers();
281
+ try {
282
+ const stream = makeMediaStream(0);
283
+ const hook = renderHook({ mediaStream: stream });
284
+
285
+ act(() => {
286
+ stream.addVideoTrack();
287
+ });
288
+ expect(hook.current.remountKey).toBe(1);
289
+
290
+ // Tight burst: keys 2..4 at 300/600/900 ms; the 4th tick (1200 ms)
291
+ // clears the burst and starts the tail.
292
+ act(() => {
293
+ jest.advanceTimersByTime(300 * 4);
294
+ });
295
+ expect(hook.current.remountKey).toBe(4);
296
+
297
+ // The first share of a Jam adds a new m-line, so the receiver
298
+ // renegotiates and the native stream can register its video track
299
+ // seconds after the JS-side SDP apply — past the burst. The tail
300
+ // remounts again at 2 s, 4 s, 6 s and 8 s after the burst (keys 5..8).
301
+ act(() => {
302
+ jest.advanceTimersByTime(2000);
303
+ });
304
+ expect(hook.current.remountKey).toBe(5);
305
+ act(() => {
306
+ jest.advanceTimersByTime(2000);
307
+ });
308
+ expect(hook.current.remountKey).toBe(6);
309
+ act(() => {
310
+ jest.advanceTimersByTime(2000);
311
+ });
312
+ expect(hook.current.remountKey).toBe(7);
313
+ act(() => {
314
+ jest.advanceTimersByTime(2000);
315
+ });
316
+ expect(hook.current.remountKey).toBe(8);
317
+
318
+ // The schedule is finite: the tail's 5th tick (10 s after the burst)
319
+ // clears the interval without bumping, so a long-lived tile settles to
320
+ // a stable key — no endless remounts.
321
+ act(() => {
322
+ jest.advanceTimersByTime(30000);
323
+ });
324
+ expect(hook.current.remountKey).toBe(8);
325
+ hook.unmount();
326
+ } finally {
327
+ jest.useRealTimers();
328
+ }
329
+ });
330
+
331
+ it('does not run the retry schedule for a stream that already has its track', () => {
332
+ jest.useFakeTimers();
333
+ try {
334
+ const hook = renderHook({ mediaStream: makeMediaStream(1) });
335
+ expect(hook.current.remountKey).toBe(0);
336
+
337
+ // No 0 -> 1 transition ever happens: no burst and no tail at any
338
+ // horizon, so an already-attached share never remounts.
339
+ act(() => {
340
+ jest.advanceTimersByTime(30000);
341
+ });
342
+ expect(hook.current.remountKey).toBe(0);
343
+ hook.unmount();
344
+ } finally {
345
+ jest.useRealTimers();
346
+ }
347
+ });
234
348
  });
@@ -7,27 +7,49 @@ import type { MediaStream } from '@livekit/react-native-webrtc';
7
7
  const asWebRtcStream = (track?: Track): MediaStream | undefined =>
8
8
  track?.mediaStream as MediaStream | undefined;
9
9
 
10
- // A single remount is not enough for the first share of a call: the JS stream
11
- // reports its video track (SDP apply) a beat before the native stream has the
12
- // track registered (first frame / dynacast), so the native one-shot `streamURL`
13
- // lookup that a single remount triggers can still run empty. A short, bounded
14
- // burst of remounts after the first track appears gives the native attach a few
15
- // chances to land once the stream is ready. Each extra remount is a cheap
16
- // native view recreation, and the burst only happens on the rare late-share
17
- // path — an already-attached share never sees one.
10
+ // A single remount is not enough for a late share: the JS stream reports its
11
+ // video track (SDP apply) before the native stream has the track registered
12
+ // (first frame / dynacast), so the native one-shot `streamURL` lookup that a
13
+ // remount triggers can run empty. The retry schedule below gives the attach a
14
+ // sequence of bounded chances to land while the native stream settles. Each
15
+ // extra remount is a cheap native view recreation, and the whole schedule only
16
+ // ever runs on the rare late-share path — an already-attached share never
17
+ // sees any of it.
18
18
  //
19
- // The burst is bounded and self-clearing: `RETRY_BURST_ATTEMPTS = 4` ticks at
20
- // `RETRY_BURST_INTERVAL_MS = 300`; the first three ticks bump (300/600/900 ms
21
- // after the transition bump, ~1.2 s window), and the fourth (1200 ms) is
22
- // consumed by the interval-clear branch — so exactly 3 extra remounts land.
19
+ // The schedule has two parts:
20
+ //
21
+ // 1. A tight burst right after the first track appears: `RETRY_BURST_ATTEMPTS`
22
+ // = 4 ticks at `RETRY_BURST_INTERVAL_MS` = 300; the first three ticks bump
23
+ // (300/600/900 ms after the transition bump) and the fourth (1200 ms) is
24
+ // consumed by the interval-clear branch — so exactly 3 extra remounts land
25
+ // within ~1.2 s. This covers the warm-connection case (subsequent shares),
26
+ // where the native stream registers its track almost at the same time as
27
+ // the JS one.
28
+ //
29
+ // 2. A sparse tail: `RETRY_TAIL_ATTEMPTS` = 5 ticks at
30
+ // `RETRY_TAIL_INTERVAL_MS` = 2000 ms each — ticks 1–4 bump (remounts at
31
+ // 2 s, 4 s, 6 s and 8 s after the burst ends) and tick 5 (10 s) is
32
+ // consumed by the interval-clear branch, exactly as the burst's 4th tick
33
+ // clears it (the N constant counts ticks, the last one clears). The FIRST
34
+ // share in a Jam adds a new m-line, so the receiver renegotiates and
35
+ // re-establishes ICE before the native stream registers the video track —
36
+ // that can take several seconds, well past the ~1.2 s burst. The tail is
37
+ // the part that fixes that case (verified against the QA report: the
38
+ // initial share of a Jam stayed black while subsequent shares worked).
39
+ // After the tail's clear tick, the schedule is fully over and
40
+ // self-cleared: no further remounts and no live timers.
23
41
  const RETRY_BURST_ATTEMPTS = 4;
24
42
  const RETRY_BURST_INTERVAL_MS = 300;
43
+ const RETRY_TAIL_ATTEMPTS = 5;
44
+ const RETRY_TAIL_INTERVAL_MS = 2000;
25
45
 
26
46
  export interface LateTrackMediaStream {
27
47
  /**
28
- * Changes when the stream gains its first video track and again on a short
29
- * burst of retries immediately after, so the native view re-runs its
30
- * one-shot `streamURL` attach a few times while the native stream settles.
48
+ * Changes when the stream gains its first video track and again on the
49
+ * bounded retry schedule (tight burst, then sparse backoff tail) while the
50
+ * native stream settles, so the native view re-runs its one-shot `streamURL`
51
+ * attach until it can succeed — including the first share of a Jam, whose
52
+ * renegotiation delays the native track registration past the burst window.
31
53
  */
32
54
  remountKey: number;
33
55
  }
@@ -41,16 +63,18 @@ export interface LateTrackMediaStream {
41
63
  * that lookup runs before the stream carries its video track, iOS gives up (no
42
64
  * retry) and the tile stays black until we leave and rejoin. Re-keying the
43
65
  * `VideoTrack` with `remountKey` remounts the native view when the stream gains
44
- * its video track — and again a few times over the next second or so — so the
45
- * one-shot lookup re-runs until it can succeed.
66
+ * its video track — and again on the bounded retry schedule — so the one-shot
67
+ * lookup re-runs until it can succeed.
46
68
  *
47
69
  * The poll is not redundant with the `addtrack` listener: on the first share,
48
70
  * `addtrack` fires inside the native SDP-apply batch, before the room re-emits
49
71
  * the publication that mounts this layout — so the event can fire before the
50
72
  * listener exists.
51
73
  *
52
- * Camera tiles don't need this: they remount on the track object itself
53
- * (see `JamCallParticipant`), which is a different (renegotiation) case.
74
+ * Camera tiles use the same mechanism (see `JamCallParticipant`): a first
75
+ * mid-Jam camera publish has the same race — the JS track exists before the
76
+ * native stream carries its video track, so the one-shot attach needs the
77
+ * same late-track remount burst (#1808).
54
78
  */
55
79
  export const useLateTrackMediaStream = (
56
80
  track?: Track
@@ -70,6 +94,23 @@ export const useLateTrackMediaStream = (
70
94
 
71
95
  let pollTimer: ReturnType<typeof setInterval> | undefined;
72
96
  let burstTimer: ReturnType<typeof setInterval> | undefined;
97
+ let tailTimer: ReturnType<typeof setInterval> | undefined;
98
+
99
+ const startTail = () => {
100
+ // Sparse retries for the first share of a Jam: its m-line forces a
101
+ // receiver-side renegotiation, so the native stream can register its
102
+ // video track seconds after the JS-side SDP apply — past the tight
103
+ // burst. Remounts at 2 s, 4 s, 6 s and 8 s after the burst (ticks 1–4
104
+ // bump); the 5th tick (10 s) clears the interval and ends the schedule.
105
+ let extra = 0;
106
+ tailTimer = setInterval(() => {
107
+ if (++extra >= RETRY_TAIL_ATTEMPTS) {
108
+ clearInterval(tailTimer!);
109
+ return;
110
+ }
111
+ setRemountKey((key) => key + 1);
112
+ }, RETRY_TAIL_INTERVAL_MS);
113
+ };
73
114
 
74
115
  const startBurst = () => {
75
116
  // The first track just appeared: the view re-mounts once for it (key
@@ -81,6 +122,7 @@ export const useLateTrackMediaStream = (
81
122
  burstTimer = setInterval(() => {
82
123
  if (++extra >= RETRY_BURST_ATTEMPTS) {
83
124
  clearInterval(burstTimer!);
125
+ startTail();
84
126
  return;
85
127
  }
86
128
  setRemountKey((key) => key + 1);
@@ -114,6 +156,7 @@ export const useLateTrackMediaStream = (
114
156
  return () => {
115
157
  if (pollTimer) clearInterval(pollTimer);
116
158
  if (burstTimer) clearInterval(burstTimer);
159
+ if (tailTimer) clearInterval(tailTimer);
117
160
  mediaStream.removeEventListener('addtrack', refresh);
118
161
  prevCountRef.current = 0;
119
162
  };