@zapiack/react-native 0.1.0 → 0.2.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 +5 -0
- package/README.md +90 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/zapiack/reactnative/ZapiackPushModule.kt +3 -0
- package/dist/calls.d.mts +198 -0
- package/dist/calls.d.ts +198 -0
- package/dist/calls.js +793 -0
- package/dist/calls.mjs +406 -0
- package/dist/chunk-CCLICX3L.mjs +371 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +389 -6
- package/dist/index.mjs +21 -2
- package/dist/realtime-BAcafCSr.d.mts +173 -0
- package/dist/realtime-BAcafCSr.d.ts +173 -0
- package/ios/Zapiack/Api.swift +6 -2
- package/ios/Zapiack/LayoutModel.swift +128 -0
- package/ios/Zapiack/LayoutView.swift +206 -0
- package/ios/Zapiack/Models.swift +1 -1
- package/ios/Zapiack/PushClient.swift +9 -1
- package/ios/Zapiack/ZapiackLiveActivityWidget.swift +170 -0
- package/ios/Zapiack/ZapiackLiveAttributes.swift +78 -0
- package/ios/Zapiack/ZapiackPush.swift +27 -0
- package/ios/ZapiackPushModule.m +1 -0
- package/ios/ZapiackPushModule.swift +5 -0
- package/package.json +32 -5
package/dist/calls.mjs
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ZapiackRealtime,
|
|
3
|
+
ZapiackRealtimeError
|
|
4
|
+
} from "./chunk-CCLICX3L.mjs";
|
|
5
|
+
|
|
6
|
+
// src/calls.ts
|
|
7
|
+
import { Device } from "mediasoup-client";
|
|
8
|
+
import { MediaStream, mediaDevices, registerGlobals } from "react-native-webrtc";
|
|
9
|
+
var ZapiackCall = class {
|
|
10
|
+
constructor(options, env = {}) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.env = env;
|
|
13
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
14
|
+
this.sendT = null;
|
|
15
|
+
this.recvT = null;
|
|
16
|
+
this.producers = /* @__PURE__ */ new Map();
|
|
17
|
+
/** Everything being received, by the sender's track id. */
|
|
18
|
+
this.consumers = /* @__PURE__ */ new Map();
|
|
19
|
+
this.peers = /* @__PURE__ */ new Map();
|
|
20
|
+
this.joinedReply = null;
|
|
21
|
+
this.roomChannel = null;
|
|
22
|
+
this.stopRoom = null;
|
|
23
|
+
this.stopState = null;
|
|
24
|
+
this._state = "idle";
|
|
25
|
+
/** Tracks being fetched right now, so two events for the same track do not make two consumers. */
|
|
26
|
+
this.fetching = /* @__PURE__ */ new Set();
|
|
27
|
+
this.wasConnected = false;
|
|
28
|
+
this.resyncing = false;
|
|
29
|
+
this.resyncTimer = null;
|
|
30
|
+
this.speaker = false;
|
|
31
|
+
/** A transport that went `disconnected` gets a few seconds to recover by itself before its path is restarted. */
|
|
32
|
+
this.lapse = /* @__PURE__ */ new Map();
|
|
33
|
+
this.facing = options?.camera ?? "user";
|
|
34
|
+
if (typeof options?.getToken !== "function") throw new ZapiackRealtimeError("getToken is required: a function that asks your server for a call token.");
|
|
35
|
+
this.rt = env.realtime ?? new ZapiackRealtime({ getToken: options.getToken, url: options.url }, env.realtimeEnv);
|
|
36
|
+
if (!env.device && typeof globalThis.RTCPeerConnection === "undefined") registerGlobals();
|
|
37
|
+
this.device = env.device ? env.device() : new Device({ handlerName: "ReactNative106" });
|
|
38
|
+
}
|
|
39
|
+
get state() {
|
|
40
|
+
return this._state;
|
|
41
|
+
}
|
|
42
|
+
/** What this person may do in the room: `listener` can only receive. */
|
|
43
|
+
get role() {
|
|
44
|
+
return this.joinedReply?.role ?? null;
|
|
45
|
+
}
|
|
46
|
+
get roomId() {
|
|
47
|
+
return this.joinedReply?.roomId ?? null;
|
|
48
|
+
}
|
|
49
|
+
/** Who else is in the room now. */
|
|
50
|
+
get members() {
|
|
51
|
+
return [...this.peers.values()];
|
|
52
|
+
}
|
|
53
|
+
/** The tracks this person is sending now. */
|
|
54
|
+
get sending() {
|
|
55
|
+
return [...this.producers.keys()];
|
|
56
|
+
}
|
|
57
|
+
on(event, fn) {
|
|
58
|
+
let set = this.listeners.get(event);
|
|
59
|
+
if (!set) this.listeners.set(event, set = /* @__PURE__ */ new Set());
|
|
60
|
+
set.add(fn);
|
|
61
|
+
return () => set.delete(fn);
|
|
62
|
+
}
|
|
63
|
+
emit(event, data) {
|
|
64
|
+
for (const fn of [...this.listeners.get(event) ?? []]) {
|
|
65
|
+
try {
|
|
66
|
+
fn(data);
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Connects, joins the room the token is for, and starts receiving what the others are sending. */
|
|
72
|
+
async join() {
|
|
73
|
+
if (this._state === "joined") return { role: this.joinedReply.role, roomId: this.joinedReply.roomId, members: this.members };
|
|
74
|
+
this._state = "joining";
|
|
75
|
+
try {
|
|
76
|
+
await this.rt.connect();
|
|
77
|
+
const reply = await this.rt.request("media.join", {});
|
|
78
|
+
this.joinedReply = reply;
|
|
79
|
+
if (!this.device.loaded) await this.device.load({ routerRtpCapabilities: reply.routerRtpCapabilities });
|
|
80
|
+
this.roomChannel = `call:${reply.roomId}`;
|
|
81
|
+
this.stopRoom = this.rt.subscribe(this.roomChannel, (m) => this.onRoomEvent(m));
|
|
82
|
+
this.stopState = this.rt.onState((s) => void this.onLine(s));
|
|
83
|
+
this.wasConnected = true;
|
|
84
|
+
this.options.audioSession?.start({ media: "video" });
|
|
85
|
+
if (this.speaker) this.options.audioSession?.setSpeaker(true);
|
|
86
|
+
const fresh = await this.rt.request("media.join", {});
|
|
87
|
+
await this.reconcile(fresh.peers);
|
|
88
|
+
this._state = "joined";
|
|
89
|
+
this.startResync();
|
|
90
|
+
return { role: reply.role, roomId: reply.roomId, members: this.members };
|
|
91
|
+
} catch (e) {
|
|
92
|
+
await this.teardown();
|
|
93
|
+
this._state = "closed";
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** Turns the microphone on and sends it. Pass your own track to send something else. */
|
|
98
|
+
async enableMicrophone(track) {
|
|
99
|
+
const t = track ?? (await this.userMedia({ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } })).getAudioTracks()[0];
|
|
100
|
+
if (!t) throw new ZapiackRealtimeError("No microphone was found.");
|
|
101
|
+
await this.send("audio", t);
|
|
102
|
+
return t;
|
|
103
|
+
}
|
|
104
|
+
/** Turns the camera on and sends it. Opens the camera set by the `camera` option, front by default. */
|
|
105
|
+
async enableCamera(track) {
|
|
106
|
+
const t = track ?? (await this.userMedia({ video: { facingMode: this.facing, width: { ideal: 1280 }, height: { ideal: 720 }, frameRate: { ideal: 30 } } })).getVideoTracks()[0];
|
|
107
|
+
if (!t) throw new ZapiackRealtimeError("No camera was found.");
|
|
108
|
+
await this.send("video", t);
|
|
109
|
+
return t;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Shares the phone's screen. Android needs a foreground service for screen capture and iOS needs a broadcast extension: see
|
|
113
|
+
* react-native-webrtc's screen sharing guide. Stops by itself when the person stops sharing.
|
|
114
|
+
*/
|
|
115
|
+
async shareScreen(track) {
|
|
116
|
+
const t = track ?? (await this.displayMedia()).getVideoTracks()[0];
|
|
117
|
+
if (!t) throw new ZapiackRealtimeError("No screen was chosen.");
|
|
118
|
+
t.addEventListener("ended", () => void this.stop("screen"));
|
|
119
|
+
await this.send("screen", t);
|
|
120
|
+
return t;
|
|
121
|
+
}
|
|
122
|
+
/** Mutes or unmutes what is being sent. Nothing is sent while muted, and the others are told. */
|
|
123
|
+
async setMuted(source, muted) {
|
|
124
|
+
const p = this.producers.get(source);
|
|
125
|
+
if (!p) return;
|
|
126
|
+
if (muted) p.pause();
|
|
127
|
+
else p.resume();
|
|
128
|
+
await this.rt.request(muted ? "media.pauseProducer" : "media.resumeProducer", { producerId: p.id });
|
|
129
|
+
}
|
|
130
|
+
/** Flips between the front and back camera while the camera is on, without interrupting what is being sent. */
|
|
131
|
+
switchCamera() {
|
|
132
|
+
const p = this.producers.get("video");
|
|
133
|
+
if (!p?.track) throw new ZapiackRealtimeError("The camera is not on.");
|
|
134
|
+
p.track._switchCamera();
|
|
135
|
+
this.facing = this.facing === "user" ? "environment" : "user";
|
|
136
|
+
}
|
|
137
|
+
/** Plays sound through the loudspeaker (true) or the earpiece (false). Needs an `audioSession` option; remembered across a reconnect. */
|
|
138
|
+
setSpeaker(on) {
|
|
139
|
+
this.speaker = on;
|
|
140
|
+
this.options.audioSession?.setSpeaker(on);
|
|
141
|
+
}
|
|
142
|
+
isMuted(source) {
|
|
143
|
+
return this.producers.get(source)?.paused ?? false;
|
|
144
|
+
}
|
|
145
|
+
/** Stops sending a track and lets go of the device (the camera light goes off). */
|
|
146
|
+
async stop(source) {
|
|
147
|
+
const p = this.producers.get(source);
|
|
148
|
+
if (!p) return;
|
|
149
|
+
this.producers.delete(source);
|
|
150
|
+
const track = p.track;
|
|
151
|
+
p.close();
|
|
152
|
+
track?.stop();
|
|
153
|
+
await this.rt.request("media.closeProducer", { producerId: p.id }).catch(() => void 0);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* What is arriving now, one entry per track being received: how many bytes and packets, how many were lost, and the jitter in
|
|
157
|
+
* seconds. For a quality indicator, or to see that media is really flowing.
|
|
158
|
+
*/
|
|
159
|
+
async stats() {
|
|
160
|
+
const out = [];
|
|
161
|
+
for (const [, c] of this.consumers) {
|
|
162
|
+
const report = await c.consumer.getStats();
|
|
163
|
+
let bytes = 0, packets = 0, lost = 0, jitter = 0;
|
|
164
|
+
report.forEach((r) => {
|
|
165
|
+
if (r["type"] !== "inbound-rtp") return;
|
|
166
|
+
bytes += Number(r["bytesReceived"] ?? 0);
|
|
167
|
+
packets += Number(r["packetsReceived"] ?? 0);
|
|
168
|
+
lost += Number(r["packetsLost"] ?? 0);
|
|
169
|
+
jitter = Math.max(jitter, Number(r["jitter"] ?? 0));
|
|
170
|
+
});
|
|
171
|
+
out.push({ identity: c.identity, source: c.source, bytesReceived: bytes, packetsReceived: packets, packetsLost: lost, jitter });
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
/** Leaves the room and lets go of the camera, the microphone and the connection. */
|
|
176
|
+
async leave() {
|
|
177
|
+
if (this._state === "closed" || this._state === "idle") return;
|
|
178
|
+
await this.rt.request("media.leave", {}).catch(() => void 0);
|
|
179
|
+
await this.teardown();
|
|
180
|
+
this._state = "closed";
|
|
181
|
+
}
|
|
182
|
+
// ───────────────────────────── sending
|
|
183
|
+
userMedia(c) {
|
|
184
|
+
const fn = this.env.getUserMedia ?? ((x) => mediaDevices.getUserMedia(x));
|
|
185
|
+
return fn(c);
|
|
186
|
+
}
|
|
187
|
+
displayMedia() {
|
|
188
|
+
const fn = this.env.getDisplayMedia ?? (() => mediaDevices.getDisplayMedia({}));
|
|
189
|
+
return fn();
|
|
190
|
+
}
|
|
191
|
+
async transport(direction) {
|
|
192
|
+
const held = direction === "send" ? this.sendT : this.recvT;
|
|
193
|
+
if (held && !held.closed) return held;
|
|
194
|
+
const info = await this.rt.request("media.createTransport", { direction });
|
|
195
|
+
const opts = { ...info, iceServers: this.joinedReply?.iceServers ?? [], iceTransportPolicy: this.options.forceTcp ? "relay" : "all" };
|
|
196
|
+
const t = direction === "send" ? this.device.createSendTransport(opts) : this.device.createRecvTransport(opts);
|
|
197
|
+
t.on("connect", ({ dtlsParameters }, ok, fail) => {
|
|
198
|
+
this.rt.request("media.connectTransport", { transportId: t.id, dtlsParameters }).then(() => ok(), fail);
|
|
199
|
+
});
|
|
200
|
+
if (direction === "send") {
|
|
201
|
+
t.on("produce", ({ kind, rtpParameters, appData }, ok, fail) => {
|
|
202
|
+
const source = appData.source ?? kind;
|
|
203
|
+
this.rt.request("media.produce", { transportId: t.id, kind: source, rtpParameters }).then((r) => ok({ id: r.id }), fail);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
t.on("connectionstatechange", (s) => {
|
|
207
|
+
const timers = { set: this.env.setTimeout ?? ((fn, ms) => setTimeout(fn, ms)), clear: this.env.clearTimeout ?? ((id) => clearTimeout(id)) };
|
|
208
|
+
const pending = this.lapse.get(t.id);
|
|
209
|
+
if (pending !== void 0) {
|
|
210
|
+
timers.clear(pending);
|
|
211
|
+
this.lapse.delete(t.id);
|
|
212
|
+
}
|
|
213
|
+
if (s === "failed") void this.restartIce(t);
|
|
214
|
+
else if (s === "disconnected") {
|
|
215
|
+
this.lapse.set(t.id, timers.set(() => {
|
|
216
|
+
this.lapse.delete(t.id);
|
|
217
|
+
if (!t.closed) void this.restartIce(t);
|
|
218
|
+
}, 4e3));
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
if (direction === "send") this.sendT = t;
|
|
222
|
+
else this.recvT = t;
|
|
223
|
+
return t;
|
|
224
|
+
}
|
|
225
|
+
async restartIce(t) {
|
|
226
|
+
try {
|
|
227
|
+
const { iceParameters } = await this.rt.request("media.restartIce", { transportId: t.id });
|
|
228
|
+
await t.restartIce({ iceParameters });
|
|
229
|
+
} catch (e) {
|
|
230
|
+
this.emit("error", e instanceof ZapiackRealtimeError ? e : new ZapiackRealtimeError(String(e)));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async send(source, track) {
|
|
234
|
+
if (this._state !== "joined") throw new ZapiackRealtimeError("Join the call first.");
|
|
235
|
+
if (this.joinedReply?.role === "listener") throw new ZapiackRealtimeError("Your role in this call can only listen.", 0, "forbidden");
|
|
236
|
+
if (this.producers.has(source)) await this.stop(source);
|
|
237
|
+
const t = await this.transport("send");
|
|
238
|
+
const encodings = source === "audio" ? [{ maxBitrate: 64e3 }] : source === "screen" ? [{ maxBitrate: 25e5 }] : [{ maxBitrate: 12e5 }];
|
|
239
|
+
const producer = await t.produce({ track, encodings, appData: { source }, ...source === "audio" ? { codecOptions: { opusDtx: true, opusFec: true } } : {} });
|
|
240
|
+
producer.on("trackended", () => void this.stop(source));
|
|
241
|
+
this.producers.set(source, producer);
|
|
242
|
+
}
|
|
243
|
+
// ───────────────────────────── receiving
|
|
244
|
+
async reconcile(peers) {
|
|
245
|
+
const here = new Set(peers.map((p) => p.identity));
|
|
246
|
+
for (const p of peers) {
|
|
247
|
+
if (!this.peers.has(p.identity)) {
|
|
248
|
+
this.peers.set(p.identity, { identity: p.identity, role: p.role });
|
|
249
|
+
this.emit("peerJoined", { identity: p.identity, role: p.role });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (const id of [...this.peers.keys()]) {
|
|
253
|
+
if (!here.has(id)) {
|
|
254
|
+
this.peers.delete(id);
|
|
255
|
+
this.dropFrom(id);
|
|
256
|
+
this.emit("peerLeft", { identity: id });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const live = new Set(peers.flatMap((p) => p.producers.map((x) => x.id)));
|
|
260
|
+
for (const [producerId, c] of [...this.consumers]) {
|
|
261
|
+
if (!live.has(producerId)) this.endTrack(producerId, c.identity, c.source);
|
|
262
|
+
}
|
|
263
|
+
for (const p of peers) for (const pr of p.producers) await this.consume(pr.id, p.identity, pr.kind, pr.paused);
|
|
264
|
+
}
|
|
265
|
+
async consume(producerId, identity, kind, paused = false) {
|
|
266
|
+
if (this.consumers.has(producerId) || this.fetching.has(producerId)) return;
|
|
267
|
+
this.fetching.add(producerId);
|
|
268
|
+
try {
|
|
269
|
+
const t = await this.transport("recv");
|
|
270
|
+
const r = await this.rt.request("media.consume", { producerId, rtpCapabilities: this.device.rtpCapabilities });
|
|
271
|
+
const consumer = await t.consume({ id: r.id, producerId: r.producerId, kind: r.kind, rtpParameters: r.rtpParameters, appData: { source: r.source } });
|
|
272
|
+
this.consumers.set(producerId, { consumer, identity: r.identity, source: r.source });
|
|
273
|
+
await this.rt.request("media.resumeConsumer", { consumerId: consumer.id });
|
|
274
|
+
const stream = this.env.newStream ? this.env.newStream([consumer.track]) : new MediaStream([consumer.track]);
|
|
275
|
+
this.emit("track", { identity: r.identity, source: r.source, track: consumer.track, stream, producerId });
|
|
276
|
+
if (paused) this.emit("paused", { identity: r.identity, source: r.source, producerId, paused: true });
|
|
277
|
+
consumer.on("transportclose", () => this.endTrack(producerId, r.identity, r.source));
|
|
278
|
+
} catch (e) {
|
|
279
|
+
this.emit("error", e instanceof ZapiackRealtimeError ? e : new ZapiackRealtimeError(`Could not play ${identity}'s ${kind}: ${e instanceof Error ? e.message : String(e)}`));
|
|
280
|
+
} finally {
|
|
281
|
+
this.fetching.delete(producerId);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
endTrack(producerId, identity, source) {
|
|
285
|
+
const c = this.consumers.get(producerId);
|
|
286
|
+
if (!c) return;
|
|
287
|
+
this.consumers.delete(producerId);
|
|
288
|
+
c.consumer.close();
|
|
289
|
+
this.emit("trackEnded", { identity, source, producerId });
|
|
290
|
+
}
|
|
291
|
+
dropFrom(identity) {
|
|
292
|
+
for (const [producerId, c] of [...this.consumers]) if (c.identity === identity) this.endTrack(producerId, c.identity, c.source);
|
|
293
|
+
}
|
|
294
|
+
onRoomEvent(m) {
|
|
295
|
+
const d = m.data ?? {};
|
|
296
|
+
switch (m.event) {
|
|
297
|
+
case "peerJoined":
|
|
298
|
+
if (d["identity"] && d["identity"] !== this.identity() && !this.peers.has(d["identity"])) {
|
|
299
|
+
this.peers.set(d["identity"], { identity: d["identity"], role: d["role"] });
|
|
300
|
+
this.emit("peerJoined", { identity: d["identity"], role: d["role"] });
|
|
301
|
+
}
|
|
302
|
+
return;
|
|
303
|
+
case "peerLeft":
|
|
304
|
+
if (this.peers.delete(d["identity"])) {
|
|
305
|
+
this.dropFrom(d["identity"]);
|
|
306
|
+
this.emit("peerLeft", { identity: d["identity"] });
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
309
|
+
case "newProducer":
|
|
310
|
+
if (d["identity"] !== this.identity()) void this.consume(d["producerId"], d["identity"], d["kind"]);
|
|
311
|
+
return;
|
|
312
|
+
case "producerClosed": {
|
|
313
|
+
const c = this.consumers.get(d["producerId"]);
|
|
314
|
+
if (c) this.endTrack(d["producerId"], c.identity, c.source);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
case "producerPaused": {
|
|
318
|
+
const c = this.consumers.get(d["producerId"]);
|
|
319
|
+
if (c) this.emit("paused", { identity: c.identity, source: c.source, producerId: d["producerId"], paused: d["paused"] === true });
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
case "roomEnded":
|
|
323
|
+
case "roomEnding":
|
|
324
|
+
void this.teardown().then(() => {
|
|
325
|
+
this._state = "closed";
|
|
326
|
+
this.emit("ended", { reason: String(d["reason"] ?? "ended") });
|
|
327
|
+
});
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/** Who this person is in the room, as the server knows them: events about this name are about us. */
|
|
332
|
+
identity() {
|
|
333
|
+
return this.joinedReply?.identity ?? null;
|
|
334
|
+
}
|
|
335
|
+
// ───────────────────────────── the line coming and going
|
|
336
|
+
/**
|
|
337
|
+
* Room events travel over best-effort publish and subscribe, so a client does not rely on every one arriving: now and then it asks the
|
|
338
|
+
* server who is in the room and what they are sending, and catches up on anything it missed.
|
|
339
|
+
*/
|
|
340
|
+
startResync() {
|
|
341
|
+
const every = this.options.resyncMs ?? 3e4;
|
|
342
|
+
if (this.resyncTimer || every <= 0) return;
|
|
343
|
+
this.resyncTimer = setInterval(() => void this.resync(), every);
|
|
344
|
+
}
|
|
345
|
+
async resync() {
|
|
346
|
+
if (this._state !== "joined" || this.resyncing) return;
|
|
347
|
+
this.resyncing = true;
|
|
348
|
+
try {
|
|
349
|
+
const fresh = await this.rt.request("media.join", {});
|
|
350
|
+
if (this._state === "joined") await this.reconcile(fresh.peers);
|
|
351
|
+
} catch {
|
|
352
|
+
} finally {
|
|
353
|
+
this.resyncing = false;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** The socket dropped and came back: take the place in the room again and see what changed while away. */
|
|
357
|
+
async onLine(s) {
|
|
358
|
+
if (s === "reconnecting" && this._state === "joined") this._state = "reconnecting";
|
|
359
|
+
if (s === "closed" && this._state !== "closed" && this._state !== "idle") {
|
|
360
|
+
await this.teardown();
|
|
361
|
+
this._state = "closed";
|
|
362
|
+
this.emit("ended", { reason: "disconnected" });
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (s !== "connected" || !this.wasConnected || this._state !== "reconnecting" || this.resyncing) return;
|
|
366
|
+
this.resyncing = true;
|
|
367
|
+
try {
|
|
368
|
+
const fresh = await this.rt.request("media.join", {});
|
|
369
|
+
this._state = "joined";
|
|
370
|
+
for (const t of [this.sendT, this.recvT]) if (t && !t.closed) await this.restartIce(t);
|
|
371
|
+
await this.reconcile(fresh.peers);
|
|
372
|
+
} catch (e) {
|
|
373
|
+
await this.teardown();
|
|
374
|
+
this._state = "closed";
|
|
375
|
+
this.emit("ended", { reason: e instanceof ZapiackRealtimeError && e.reason ? e.reason : "rejoin_failed" });
|
|
376
|
+
} finally {
|
|
377
|
+
this.resyncing = false;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async teardown() {
|
|
381
|
+
if (this.resyncTimer) clearInterval(this.resyncTimer);
|
|
382
|
+
this.resyncTimer = null;
|
|
383
|
+
const clear = this.env.clearTimeout ?? ((id) => clearTimeout(id));
|
|
384
|
+
for (const id of this.lapse.values()) clear(id);
|
|
385
|
+
this.lapse.clear();
|
|
386
|
+
this.options.audioSession?.stop();
|
|
387
|
+
this.stopState?.();
|
|
388
|
+
this.stopRoom?.();
|
|
389
|
+
this.stopState = this.stopRoom = null;
|
|
390
|
+
for (const p of this.producers.values()) {
|
|
391
|
+
p.track?.stop();
|
|
392
|
+
p.close();
|
|
393
|
+
}
|
|
394
|
+
this.producers.clear();
|
|
395
|
+
for (const c of this.consumers.values()) c.consumer.close();
|
|
396
|
+
this.consumers.clear();
|
|
397
|
+
this.sendT?.close();
|
|
398
|
+
this.recvT?.close();
|
|
399
|
+
this.sendT = this.recvT = null;
|
|
400
|
+
this.peers.clear();
|
|
401
|
+
this.rt.disconnect();
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
export {
|
|
405
|
+
ZapiackCall
|
|
406
|
+
};
|