@openpeepshq/rn-components 0.16.0 → 0.17.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.
@@ -187,7 +187,7 @@ const ScreenSharingLayout = ({
187
187
  const videoTrack = isTrackReference(screenShareTrack)
188
188
  ? screenShareTrack.publication.track
189
189
  : undefined;
190
- const mediaStreamEpoch = useLateTrackMediaStream(videoTrack);
190
+ const { remountKey } = useLateTrackMediaStream(videoTrack);
191
191
 
192
192
  return (
193
193
  <View className="size-full gap-2">
@@ -199,7 +199,7 @@ const ScreenSharingLayout = ({
199
199
  <View className="relative min-h-0 w-full flex-1 overflow-hidden rounded-xl border border-border">
200
200
  {videoTrack ? (
201
201
  <VideoTrack
202
- key={`${videoTrack.sid}-${mediaStreamEpoch}`}
202
+ key={`${videoTrack.sid}-${remountKey}`}
203
203
  trackRef={screenShareTrack as TrackReference}
204
204
  style={StyleSheet.absoluteFill}
205
205
  objectFit="contain"
@@ -1,11 +1,10 @@
1
1
  /**
2
- * useLateTrackMediaStream: reports how many video tracks the track's media
3
- * stream currently has, and re-reports each time that count rises — both on
4
- * `addtrack` events and while the post-mount poll is still running. That is
5
- * what lets the screen-share VideoTrack remount — and its native view
6
- * re-resolve — once a share that started after we joined delivers its video
7
- * track, including the first share where `addtrack` fires before the layout
8
- * is mounted.
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.
9
8
  */
10
9
  import React, { act } from 'react';
11
10
  import TestRenderer from 'react-test-renderer';
@@ -37,7 +36,9 @@ type TrackLike = { mediaStream?: Stream };
37
36
 
38
37
  const renderHook = (initial: TrackLike | undefined) => {
39
38
  let track = initial;
40
- const captured: number[] = [];
39
+ const captured: Array<
40
+ ReturnType<typeof useLateTrackMediaStream> | undefined
41
+ > = [];
41
42
  const Probe = () => {
42
43
  captured[0] = useLateTrackMediaStream(
43
44
  track as unknown as Parameters<typeof useLateTrackMediaStream>[0]
@@ -50,7 +51,7 @@ const renderHook = (initial: TrackLike | undefined) => {
50
51
  });
51
52
  return {
52
53
  get current() {
53
- return captured[0];
54
+ return captured[0]!;
54
55
  },
55
56
  rerender(next: TrackLike | undefined) {
56
57
  track = next;
@@ -67,27 +68,29 @@ const renderHook = (initial: TrackLike | undefined) => {
67
68
  };
68
69
 
69
70
  describe('useLateTrackMediaStream', () => {
70
- it('reports zero when there is no track or stream', () => {
71
+ it('stays at a zero key when there is no track or stream', () => {
71
72
  const hook = renderHook(undefined);
72
- expect(hook.current).toBe(0);
73
+ expect(hook.current.remountKey).toBe(0);
73
74
  hook.unmount();
74
75
  });
75
76
 
76
- it('reports the existing video track count on first render', () => {
77
+ it('does not remount when the stream already has its track at mount', () => {
77
78
  const hook = renderHook({ mediaStream: makeMediaStream(1) });
78
- expect(hook.current).toBe(1);
79
+ // The share is already attached: no first-track transition, so the key
80
+ // stays at 0 (no pointless remount of a working view).
81
+ expect(hook.current.remountKey).toBe(0);
79
82
  hook.unmount();
80
83
  });
81
84
 
82
- it('increments when the stream gains a video track late', () => {
85
+ it('bumps the key once when the stream gains its first video track late', () => {
83
86
  const stream = makeMediaStream(0);
84
87
  const hook = renderHook({ mediaStream: stream });
85
- expect(hook.current).toBe(0);
88
+ expect(hook.current.remountKey).toBe(0);
86
89
 
87
90
  act(() => {
88
91
  stream.addVideoTrack();
89
92
  });
90
- expect(hook.current).toBe(1);
93
+ expect(hook.current.remountKey).toBe(1);
91
94
  hook.unmount();
92
95
  });
93
96
 
@@ -96,7 +99,7 @@ describe('useLateTrackMediaStream', () => {
96
99
  try {
97
100
  const stream = makeMediaStream(0);
98
101
  const hook = renderHook({ mediaStream: stream });
99
- expect(hook.current).toBe(0);
102
+ expect(hook.current.remountKey).toBe(0);
100
103
 
101
104
  // Simulate the first-share race: the track is added while the layout is
102
105
  // not yet mounted, so no `addtrack` listener receives an event.
@@ -104,7 +107,57 @@ describe('useLateTrackMediaStream', () => {
104
107
  act(() => {
105
108
  jest.advanceTimersByTime(150);
106
109
  });
107
- expect(hook.current).toBe(1);
110
+ expect(hook.current.remountKey).toBe(1);
111
+ hook.unmount();
112
+ } finally {
113
+ jest.useRealTimers();
114
+ }
115
+ });
116
+
117
+ it('emits a bounded burst of remounts after the first track appears', () => {
118
+ jest.useFakeTimers();
119
+ try {
120
+ const stream = makeMediaStream(0);
121
+ const hook = renderHook({ mediaStream: stream });
122
+ expect(hook.current.remountKey).toBe(0);
123
+
124
+ // The addtrack callback fires synchronously; wrap in act() so state
125
+ // updates are flushed.
126
+ act(() => {
127
+ stream.addVideoTrack();
128
+ });
129
+ // The first-track transition bumps the key once (key 1).
130
+ expect(hook.current.remountKey).toBe(1);
131
+
132
+ // Then a short burst of extra remounts at 300 ms intervals (keys 2..4)
133
+ // while the native stream settles.
134
+ act(() => {
135
+ jest.advanceTimersByTime(300 * 4);
136
+ });
137
+ expect(hook.current.remountKey).toBe(4);
138
+
139
+ // The burst is bounded: more time passes without further remounts.
140
+ act(() => {
141
+ jest.advanceTimersByTime(300 * 10);
142
+ });
143
+ expect(hook.current.remountKey).toBe(4);
144
+ hook.unmount();
145
+ } finally {
146
+ jest.useRealTimers();
147
+ }
148
+ });
149
+
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);
108
161
  hook.unmount();
109
162
  } finally {
110
163
  jest.useRealTimers();
@@ -117,26 +170,62 @@ describe('useLateTrackMediaStream', () => {
117
170
  const first = makeMediaStream(0);
118
171
  const second = makeMediaStream(1);
119
172
  const hook = renderHook({ mediaStream: first });
120
- expect(hook.current).toBe(0);
173
+ expect(hook.current.remountKey).toBe(0);
121
174
 
122
175
  act(() => {
123
176
  first.addVideoTrack();
124
177
  });
125
- expect(hook.current).toBe(1);
178
+ expect(hook.current.remountKey).toBe(1);
126
179
 
127
180
  hook.rerender({ mediaStream: second });
128
- expect(hook.current).toBe(1);
181
+ // Switching to a stream that already has its track resets the epoch: no
182
+ // spurious remount burst for the already-attached second share.
183
+ expect(hook.current.remountKey).toBe(0);
129
184
 
130
- // The old stream no longer drives the value.
185
+ // The old stream no longer drives the key.
131
186
  act(() => {
132
187
  first.addVideoTrack();
133
188
  });
134
- expect(hook.current).toBe(1);
189
+ // Still no remount: the swap cleared the listener and the burst, so the
190
+ // stale stream's late track cannot bump the key.
191
+ expect(hook.current.remountKey).toBe(0);
192
+
193
+ act(() => {
194
+ second.addVideoTrack();
195
+ });
196
+ // A stream that already has a track gaining another is not a
197
+ // first-track transition: no spurious remount (pins justGotFirst to the
198
+ // 0 → 1 edge, not a plain count > prev).
199
+ expect(hook.current.remountKey).toBe(0);
200
+ hook.unmount();
201
+ } finally {
202
+ jest.useRealTimers();
203
+ }
204
+ });
205
+
206
+ it('resets the key when swapping to a stream whose track has not arrived yet', () => {
207
+ jest.useFakeTimers();
208
+ try {
209
+ const first = makeMediaStream(0);
210
+ const hook = renderHook({ mediaStream: first });
211
+
212
+ act(() => {
213
+ first.addVideoTrack();
214
+ });
215
+ expect(hook.current.remountKey).toBe(1);
216
+
217
+ // Swap to a stream whose track has not arrived yet: the epoch must
218
+ // reset so the new share's burst starts from 1 instead of carrying the
219
+ // first stream's key forward (reset-before-refresh ordering).
220
+ const second = makeMediaStream(0);
221
+ hook.rerender({ mediaStream: second });
222
+ expect(hook.current.remountKey).toBe(0);
135
223
 
224
+ // The new stream gains its track: the burst starts at key 1.
136
225
  act(() => {
137
226
  second.addVideoTrack();
138
227
  });
139
- expect(hook.current).toBe(2);
228
+ expect(hook.current.remountKey).toBe(1);
140
229
  hook.unmount();
141
230
  } finally {
142
231
  jest.useRealTimers();
@@ -1,4 +1,4 @@
1
- import { useEffect, useState } from 'react';
1
+ import { useEffect, useRef, useState } from 'react';
2
2
  import type { Track } from 'livekit-client';
3
3
  import type { MediaStream } from '@livekit/react-native-webrtc';
4
4
 
@@ -7,16 +7,42 @@ 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.
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.
23
+ const RETRY_BURST_ATTEMPTS = 4;
24
+ const RETRY_BURST_INTERVAL_MS = 300;
25
+
26
+ export interface LateTrackMediaStream {
27
+ /**
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.
31
+ */
32
+ remountKey: number;
33
+ }
34
+
10
35
  /**
11
- * The number of video tracks on the track's media stream, kept in sync with
12
- * `addtrack` events plus a short post-mount poll.
36
+ * Tracks the video-track count on a track's media stream and derives a
37
+ * remount key from it.
13
38
  *
14
39
  * A screen share that starts after we have joined is attached to the native
15
40
  * `RTCView` by a one-time `streamURL` lookup of the stream's video track. When
16
41
  * that lookup runs before the stream carries its video track, iOS gives up (no
17
42
  * retry) and the tile stays black until we leave and rejoin. Re-keying the
18
- * `VideoTrack` with this count remounts the native view once the stream gains
19
- * its video track, giving the lookup a second chance when it can succeed.
43
+ * `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.
20
46
  *
21
47
  * The poll is not redundant with the `addtrack` listener: on the first share,
22
48
  * `addtrack` fires inside the native SDP-apply batch, before the room re-emits
@@ -26,36 +52,72 @@ const asWebRtcStream = (track?: Track): MediaStream | undefined =>
26
52
  * Camera tiles don't need this: they remount on the track object itself
27
53
  * (see `JamCallParticipant`), which is a different (renegotiation) case.
28
54
  */
29
- export const useLateTrackMediaStream = (track?: Track) => {
55
+ export const useLateTrackMediaStream = (
56
+ track?: Track
57
+ ): LateTrackMediaStream => {
30
58
  const mediaStream = asWebRtcStream(track);
31
- const [videoTrackCount, setVideoTrackCount] = useState(
32
- () => asWebRtcStream(track)?.getVideoTracks().length ?? 0
33
- );
59
+ const [remountKey, setRemountKey] = useState(0);
60
+ // Previous video-track count on the current stream, so the 0 → 1 transition
61
+ // (first track arriving) is detected inside the effect.
62
+ const prevCountRef = useRef(0);
34
63
 
35
64
  useEffect(() => {
36
65
  if (!mediaStream) {
37
- setVideoTrackCount(0);
38
66
  return;
39
67
  }
40
- const refresh = () =>
41
- setVideoTrackCount(mediaStream.getVideoTracks().length);
42
- refresh();
43
- // 20 x 150 ms (~3 s) covers the first-share negotiation window; stop as
44
- // soon as the stream carries its track so an already-attached share
45
- // causes no further remounts.
46
- let attempts = 0;
47
- const timer = setInterval(() => {
48
- refresh();
49
- if (mediaStream.getVideoTracks().length > 0 || ++attempts >= 20) {
50
- clearInterval(timer);
68
+ setRemountKey(0);
69
+ prevCountRef.current = mediaStream.getVideoTracks().length;
70
+
71
+ let pollTimer: ReturnType<typeof setInterval> | undefined;
72
+ let burstTimer: ReturnType<typeof setInterval> | undefined;
73
+
74
+ const startBurst = () => {
75
+ // The first track just appeared: the view re-mounts once for it (key
76
+ // bumped by `refresh`'s `justGotFirst` branch, immediately before this
77
+ // call), then nudge a few extra remounts while the native stream
78
+ // catches up (it registers the track on its first frame, which can
79
+ // lag the JS-side SDP apply).
80
+ let extra = 0;
81
+ burstTimer = setInterval(() => {
82
+ if (++extra >= RETRY_BURST_ATTEMPTS) {
83
+ clearInterval(burstTimer!);
84
+ return;
85
+ }
86
+ setRemountKey((key) => key + 1);
87
+ }, RETRY_BURST_INTERVAL_MS);
88
+ };
89
+
90
+ const refresh = () => {
91
+ const count = mediaStream.getVideoTracks().length;
92
+ const justGotFirst = count > 0 && prevCountRef.current === 0;
93
+ prevCountRef.current = count;
94
+ if (justGotFirst) {
95
+ setRemountKey((key) => key + 1);
96
+ startBurst();
97
+ if (pollTimer) clearInterval(pollTimer);
51
98
  }
52
- }, 150);
99
+ };
100
+ refresh();
101
+ if (prevCountRef.current === 0) {
102
+ // 20 x 150 ms (~3 s) covers the first-share negotiation window; stop as
103
+ // soon as the stream carries its track so an already-attached share
104
+ // causes no further remounts.
105
+ let attempts = 0;
106
+ pollTimer = setInterval(() => {
107
+ refresh();
108
+ if (mediaStream.getVideoTracks().length > 0 || ++attempts >= 20) {
109
+ clearInterval(pollTimer!);
110
+ }
111
+ }, 150);
112
+ }
53
113
  mediaStream.addEventListener('addtrack', refresh);
54
114
  return () => {
55
- clearInterval(timer);
115
+ if (pollTimer) clearInterval(pollTimer);
116
+ if (burstTimer) clearInterval(burstTimer);
56
117
  mediaStream.removeEventListener('addtrack', refresh);
118
+ prevCountRef.current = 0;
57
119
  };
58
120
  }, [mediaStream]);
59
121
 
60
- return videoTrackCount;
122
+ return { remountKey };
61
123
  };
@@ -229,7 +229,7 @@ export const ResourceForm = ({
229
229
  className="items-center rounded-md border border-input px-3 py-2"
230
230
  >
231
231
  <ThemedText>
232
- {t('resources.form.addMedia', { defaultValue: 'Add files' })}
232
+ {t('resources.form.addMedia', { defaultValue: 'Add file' })}
233
233
  </ThemedText>
234
234
  </Pressable>
235
235
  ) : null}