@reactor-team/js-sdk 2.11.2 → 2.12.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/dist/index.mjs CHANGED
@@ -91,7 +91,7 @@ import { z } from "zod";
91
91
  // package.json
92
92
  var package_default = {
93
93
  name: "@reactor-team/js-sdk",
94
- version: "2.11.2",
94
+ version: "2.12.0",
95
95
  description: "Reactor JavaScript frontend SDK \u2014 connect React and TypeScript apps to real-time AI video models on Reactor.",
96
96
  license: "Apache-2.0",
97
97
  repository: {
@@ -147,6 +147,7 @@ var package_default = {
147
147
  },
148
148
  packageManager: "pnpm@10.12.1",
149
149
  dependencies: {
150
+ awaitqueue: "^3.3.0",
150
151
  mp4box: "^2.3.0",
151
152
  "sdp-transform": "^3.0.0",
152
153
  zod: "^4.4.3"
@@ -165,6 +166,7 @@ var package_default = {
165
166
  "@roamhq/wrtc": "^0.8.0",
166
167
  "@types/node": "^25",
167
168
  "@types/react": "^19.2.15",
169
+ "@types/sdp-transform": "^2.15.0",
168
170
  "hls.js": "^1.6.0",
169
171
  prettier: "^3.6.2",
170
172
  tsup: "^8.5.0",
@@ -260,8 +262,12 @@ var WebRTCSdpOfferRequestSchema = z.object({
260
262
  client_info: ClientInfoSchema.optional(),
261
263
  track_mapping: z.array(TrackMappingEntrySchema)
262
264
  });
265
+ var WebRTCSdpOfferResponseSchema = z.object({
266
+ connection_id: z.number().optional()
267
+ });
263
268
  var WebRTCSdpAnswerResponseSchema = z.object({
264
- sdp_answer: z.string()
269
+ sdp_answer: z.string(),
270
+ connection_id: z.number().optional()
265
271
  });
266
272
  var IceCandidateSchema = z.object({
267
273
  candidate: z.string(),
@@ -789,6 +795,9 @@ var LocalCoordinatorClient = class extends CoordinatorClient {
789
795
  }
790
796
  };
791
797
 
798
+ // src/core/WebRTCTransportClient.ts
799
+ import { AwaitQueue } from "awaitqueue";
800
+
792
801
  // src/utils/sdp.ts
793
802
  import { parse, parsePayloads } from "sdp-transform";
794
803
  function sdpLineSeparator(sdp) {
@@ -839,13 +848,6 @@ function collectSanitizedCodecPayloadSet(media) {
839
848
  }
840
849
  return tracked.size > 0 ? tracked : null;
841
850
  }
842
- function mediaSectionNeedsSanitizedPtRelocate(media) {
843
- const pts = collectSanitizedCodecPayloadSet(media);
844
- if (!pts) {
845
- return false;
846
- }
847
- return [...pts].some((p) => p < DYNAMIC_PT_MIN || p > DYNAMIC_PT_MAX);
848
- }
849
851
  function codecRelocationPriority(enc) {
850
852
  const u = (enc != null ? enc : "").toUpperCase();
851
853
  if (/^(H265|HEVC|HEV1|HVC1)/.test(u)) return 0;
@@ -879,7 +881,7 @@ function computeCodecRemapForMedia(media, forbidden) {
879
881
  return remap;
880
882
  }
881
883
  const relocate = [...tracked].filter(
882
- (p) => p < DYNAMIC_PT_MIN || p > DYNAMIC_PT_MAX
884
+ (p) => p < DYNAMIC_PT_MIN || p > DYNAMIC_PT_MAX || forbidden.has(p)
883
885
  );
884
886
  if (relocate.length === 0) {
885
887
  return remap;
@@ -1116,7 +1118,7 @@ function stripTelephoneEventsPreserveOrder(sdp) {
1116
1118
  function transformOfferSdpCodecDynamicPts(sdp) {
1117
1119
  var _a;
1118
1120
  const session = parse(sdp);
1119
- if (!session.media.some(mediaSectionNeedsSanitizedPtRelocate)) {
1121
+ if (!session.media.some((m) => collectSanitizedCodecPayloadSet(m) !== null)) {
1120
1122
  return sdp;
1121
1123
  }
1122
1124
  const remaps = [];
@@ -1238,6 +1240,26 @@ function parseMessage(data) {
1238
1240
  }
1239
1241
  return data;
1240
1242
  }
1243
+ function complementDirection(direction) {
1244
+ if (direction === "sendonly") return "recvonly";
1245
+ if (direction === "recvonly") return "sendonly";
1246
+ return direction;
1247
+ }
1248
+ function replaceSdpDirectionForMid(sdp, mid, direction) {
1249
+ const [session, ...mediaSections] = sdp.split(/(?=\r\nm=)/);
1250
+ const updated = mediaSections.map((section) => {
1251
+ if (!section.includes(`\r
1252
+ a=mid:${mid}\r
1253
+ `)) return section;
1254
+ return section.replace(
1255
+ /\r\na=(sendonly|recvonly|sendrecv|inactive)\r\n/,
1256
+ `\r
1257
+ a=${direction}\r
1258
+ `
1259
+ );
1260
+ });
1261
+ return session + updated.join("");
1262
+ }
1241
1263
  function closePeerConnection(pc) {
1242
1264
  pc.close();
1243
1265
  }
@@ -1323,6 +1345,8 @@ var INITIAL_BACKOFF_MS = 200;
1323
1345
  var MAX_BACKOFF_MS = 15e3;
1324
1346
  var BACKOFF_MULTIPLIER = 2;
1325
1347
  var DEFAULT_MAX_POLL_ATTEMPTS = 6;
1348
+ var MIN_CONNECTION_ID = 1e3;
1349
+ var MAX_CONNECTION_ID = 9999;
1326
1350
  var ICE_CANDIDATE_BATCH_WINDOW_MS = 25;
1327
1351
  var WebRTCTransportClient = class {
1328
1352
  constructor(config) {
@@ -1330,8 +1354,14 @@ var WebRTCTransportClient = class {
1330
1354
  this.status = "disconnected";
1331
1355
  this.transceiverMap = /* @__PURE__ */ new Map();
1332
1356
  this.publishedTracks = /* @__PURE__ */ new Map();
1357
+ // Serializes all SDP renegotiations (pause/resume direction changes) and gates
1358
+ // them behind the connection-ready task. Reset on each prepare() call.
1359
+ this.peerConnectionQueue = new AwaitQueue();
1360
+ this.pendingControlRequests = /* @__PURE__ */ new Map();
1361
+ this.controlRequestCounter = 0;
1333
1362
  this.peerConnected = false;
1334
1363
  this.dataChannelOpen = false;
1364
+ this.controlChannelOpen = false;
1335
1365
  // Trickle ICE batching
1336
1366
  this.pendingIceCandidates = [];
1337
1367
  var _a, _b;
@@ -1436,11 +1466,34 @@ var WebRTCTransportClient = class {
1436
1466
  return iceServers;
1437
1467
  });
1438
1468
  }
1439
- sendSdpOffer(sdpOffer, trackMapping, method = "POST") {
1469
+ registerConnection() {
1470
+ return __async(this, null, function* () {
1471
+ console.debug("[WebRTCTransport] Registering connection...");
1472
+ const response = yield fetch(`${this.transportBaseUrl}/connections`, {
1473
+ method: "POST",
1474
+ headers: yield this.getHeaders(),
1475
+ signal: this.signal
1476
+ });
1477
+ yield this.checkVersionMismatch(response);
1478
+ if (response.status !== 201) {
1479
+ const errorText = yield response.text();
1480
+ throw new Error(
1481
+ `Failed to register connection: ${response.status} ${errorText}`
1482
+ );
1483
+ }
1484
+ const data = yield response.json();
1485
+ const connectionId = data.connection_id;
1486
+ console.debug(
1487
+ `[WebRTCTransport] Connection registered: id=${connectionId}`
1488
+ );
1489
+ return connectionId;
1490
+ });
1491
+ }
1492
+ sendSdpOffer(connectionId, sdpOffer, trackMapping, reconnect = false) {
1440
1493
  return __async(this, null, function* () {
1494
+ const method = reconnect ? "PUT" : "POST";
1441
1495
  console.debug(
1442
- `[WebRTCTransport] Sending SDP offer (${method}) for session:`,
1443
- this.sessionId
1496
+ `[WebRTCTransport] Sending SDP offer (${method}) connection=${connectionId}`
1444
1497
  );
1445
1498
  const requestBody = {
1446
1499
  sdp_offer: sdpOffer,
@@ -1450,17 +1503,30 @@ var WebRTCTransportClient = class {
1450
1503
  },
1451
1504
  track_mapping: trackMapping
1452
1505
  };
1453
- const response = yield fetch(`${this.transportBaseUrl}/sdp_params`, {
1454
- method,
1455
- headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1456
- "Content-Type": "application/json"
1457
- }),
1458
- body: JSON.stringify(requestBody),
1459
- signal: this.signal
1460
- });
1506
+ const response = yield fetch(
1507
+ `${this.transportBaseUrl}/connections/${connectionId}/sdp_params`,
1508
+ {
1509
+ method,
1510
+ headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1511
+ "Content-Type": "application/json"
1512
+ }),
1513
+ body: JSON.stringify(requestBody),
1514
+ signal: this.signal
1515
+ }
1516
+ );
1461
1517
  yield this.checkVersionMismatch(response);
1462
1518
  if (response.status !== 202) {
1463
1519
  const errorText = yield response.text();
1520
+ if (response.status === 404) {
1521
+ throw new Error(
1522
+ `[WebRTCTransport] Connection ${connectionId} not found or already closed (404). A supplied connectionId must reference a connection registered under this session (POST .../connections) that is still open. ${errorText}`
1523
+ );
1524
+ }
1525
+ if (response.status === 400) {
1526
+ throw new Error(
1527
+ `[WebRTCTransport] Connection ${connectionId} rejected (400): ${errorText}`
1528
+ );
1529
+ }
1464
1530
  throw new Error(
1465
1531
  `Failed to send SDP offer: ${response.status} ${errorText}`
1466
1532
  );
@@ -1470,6 +1536,12 @@ var WebRTCTransportClient = class {
1470
1536
  }
1471
1537
  sendIceCandidates(candidates, is_final) {
1472
1538
  return __async(this, null, function* () {
1539
+ if (this.connectionId === void 0) {
1540
+ console.debug(
1541
+ "[WebRTCTransport] ICE candidates dropped: no active connection"
1542
+ );
1543
+ return;
1544
+ }
1473
1545
  console.debug(
1474
1546
  `[WebRTCTransport] Sending ICE candidates (count=${candidates.length}, is_final=${is_final})`
1475
1547
  );
@@ -1481,14 +1553,17 @@ var WebRTCTransportClient = class {
1481
1553
  sdk_type: REACTOR_SDK_TYPE
1482
1554
  }
1483
1555
  };
1484
- const response = yield fetch(`${this.transportBaseUrl}/ice_candidates`, {
1485
- method: "POST",
1486
- headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1487
- "Content-Type": "application/json"
1488
- }),
1489
- body: JSON.stringify(requestBody),
1490
- signal: this.signal
1491
- });
1556
+ const response = yield fetch(
1557
+ `${this.transportBaseUrl}/connections/${this.connectionId}/ice_candidates`,
1558
+ {
1559
+ method: "POST",
1560
+ headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1561
+ "Content-Type": "application/json"
1562
+ }),
1563
+ body: JSON.stringify(requestBody),
1564
+ signal: this.signal
1565
+ }
1566
+ );
1492
1567
  yield this.checkVersionMismatch(response);
1493
1568
  if (response.status !== 202) {
1494
1569
  const errorText = yield response.text();
@@ -1497,7 +1572,6 @@ var WebRTCTransportClient = class {
1497
1572
  );
1498
1573
  }
1499
1574
  console.debug("[WebRTCTransport] ICE candidates accepted (202)");
1500
- return;
1501
1575
  });
1502
1576
  }
1503
1577
  /**
@@ -1532,9 +1606,10 @@ var WebRTCTransportClient = class {
1532
1606
  }
1533
1607
  this.pendingIceCandidates = [];
1534
1608
  }
1535
- pollSdpAnswer() {
1609
+ pollSdpAnswer(connectionId) {
1536
1610
  return __async(this, null, function* () {
1537
1611
  console.debug("[WebRTCTransport] Polling for SDP answer...");
1612
+ const pollUrl = `${this.transportBaseUrl}/connections/${connectionId}/sdp_params`;
1538
1613
  const pollStart = performance.now();
1539
1614
  let backoffMs = INITIAL_BACKOFF_MS;
1540
1615
  let attempt = 0;
@@ -1551,7 +1626,7 @@ var WebRTCTransportClient = class {
1551
1626
  console.debug(
1552
1627
  `[WebRTCTransport] SDP poll attempt ${attempt}/${this.maxPollAttempts}`
1553
1628
  );
1554
- const response = yield fetch(`${this.transportBaseUrl}/sdp_params`, {
1629
+ const response = yield fetch(pollUrl, {
1555
1630
  method: "GET",
1556
1631
  headers: yield this.getHeaders(),
1557
1632
  signal: this.signal
@@ -1606,16 +1681,34 @@ var WebRTCTransportClient = class {
1606
1681
  this.dataChannel.close();
1607
1682
  this.dataChannel = void 0;
1608
1683
  }
1684
+ if (this.controlChannel) {
1685
+ this.controlChannel.close();
1686
+ this.controlChannel = void 0;
1687
+ }
1609
1688
  if (this.peerConnection) {
1610
1689
  closePeerConnection(this.peerConnection);
1611
1690
  this.peerConnection = void 0;
1612
1691
  }
1613
1692
  this.peerConnected = false;
1614
1693
  this.dataChannelOpen = false;
1694
+ this.controlChannelOpen = false;
1695
+ this.peerConnectionQueue.stop();
1696
+ this.peerConnectionQueue = new AwaitQueue();
1697
+ this.peerConnectionQueue.push(
1698
+ () => new Promise((resolve) => {
1699
+ this.readyGateResolve = resolve;
1700
+ })
1701
+ ).catch(() => {
1702
+ });
1615
1703
  const iceServers = this.cachedIceServers ? yield this.cachedIceServers : yield this.fetchIceServers();
1616
1704
  this.cachedIceServers = void 0;
1617
1705
  this.peerConnection = createPeerConnection({ iceServers });
1618
1706
  this.setupPeerConnectionHandlers();
1707
+ this.controlChannel = createDataChannel(
1708
+ this.peerConnection,
1709
+ "control"
1710
+ );
1711
+ this.setupControlChannelHandlers();
1619
1712
  this.dataChannel = createDataChannel(this.peerConnection);
1620
1713
  this.setupDataChannelHandlers();
1621
1714
  this.transceiverMap.clear();
@@ -1638,20 +1731,34 @@ var WebRTCTransportClient = class {
1638
1731
  console.debug("[WebRTCTransport] SDP offer prepared");
1639
1732
  });
1640
1733
  }
1641
- connect(reconnect = false) {
1734
+ connect(reconnect = false, connectionId) {
1642
1735
  return __async(this, null, function* () {
1643
1736
  if (!this.pendingSdpOffer || !this.pendingTrackMapping) {
1644
1737
  throw new Error(
1645
1738
  "[WebRTCTransport] No prepared connection. Call prepare() first."
1646
1739
  );
1647
1740
  }
1648
- const method = reconnect ? "PUT" : "POST";
1649
1741
  const sdpOffer = this.pendingSdpOffer;
1650
1742
  const trackMapping = this.pendingTrackMapping;
1651
1743
  this.pendingSdpOffer = void 0;
1652
1744
  this.pendingTrackMapping = void 0;
1653
- yield this.sendSdpOffer(sdpOffer, trackMapping, method);
1654
- const answerResponse = yield this.pollSdpAnswer();
1745
+ if (connectionId !== void 0) {
1746
+ if (!Number.isInteger(connectionId) || connectionId <= MIN_CONNECTION_ID || connectionId > MAX_CONNECTION_ID) {
1747
+ throw new Error(
1748
+ `[WebRTCTransport] Invalid connectionId ${connectionId}: must be an integer in (${MIN_CONNECTION_ID}, ${MAX_CONNECTION_ID}]`
1749
+ );
1750
+ }
1751
+ this.connectionId = connectionId;
1752
+ } else if (!reconnect || this.connectionId === void 0) {
1753
+ this.connectionId = yield this.registerConnection();
1754
+ }
1755
+ yield this.sendSdpOffer(
1756
+ this.connectionId,
1757
+ sdpOffer,
1758
+ trackMapping,
1759
+ reconnect
1760
+ );
1761
+ const answerResponse = yield this.pollSdpAnswer(this.connectionId);
1655
1762
  this.iceStartTime = performance.now();
1656
1763
  yield setRemoteDescription(
1657
1764
  this.peerConnection,
@@ -1672,13 +1779,28 @@ var WebRTCTransportClient = class {
1672
1779
  this.dataChannel.close();
1673
1780
  this.dataChannel = void 0;
1674
1781
  }
1782
+ if (this.controlChannel) {
1783
+ this.controlChannel.close();
1784
+ this.controlChannel = void 0;
1785
+ }
1675
1786
  if (this.peerConnection) {
1676
1787
  closePeerConnection(this.peerConnection);
1677
1788
  this.peerConnection = void 0;
1678
1789
  }
1790
+ for (const [, pending] of this.pendingControlRequests) {
1791
+ clearTimeout(pending.timeout);
1792
+ pending.reject(
1793
+ new Error("[WebRTCTransport] Disconnected while waiting for response")
1794
+ );
1795
+ }
1796
+ this.pendingControlRequests.clear();
1797
+ this.readyGateResolve = void 0;
1798
+ this.peerConnectionQueue.stop();
1799
+ this.connectionId = void 0;
1679
1800
  this.transceiverMap.clear();
1680
1801
  this.peerConnected = false;
1681
1802
  this.dataChannelOpen = false;
1803
+ this.controlChannelOpen = false;
1682
1804
  this.resetTransportTimings();
1683
1805
  this.setStatus("disconnected");
1684
1806
  console.debug("[WebRTCTransport] Disconnected");
@@ -1741,6 +1863,119 @@ var WebRTCTransportClient = class {
1741
1863
  console.warn("[WebRTCTransport] Failed to send message:", error);
1742
1864
  }
1743
1865
  }
1866
+ sendControlMessage(command, data, reqId) {
1867
+ if (!this.controlChannel) {
1868
+ console.warn("[WebRTCTransport] Control channel not available");
1869
+ return;
1870
+ }
1871
+ try {
1872
+ if (this.controlChannel.readyState !== "open") {
1873
+ throw new Error(
1874
+ `Control channel not open: ${this.controlChannel.readyState}`
1875
+ );
1876
+ }
1877
+ this.controlChannel.send(
1878
+ JSON.stringify({ type: "notification", event: command, data })
1879
+ );
1880
+ } catch (error) {
1881
+ console.warn("[WebRTCTransport] Failed to send control message:", error);
1882
+ }
1883
+ }
1884
+ sendControlRequest(method, data, timeoutMs = 1e4) {
1885
+ return new Promise((resolve, reject) => {
1886
+ const requestId = `ctrl_${++this.controlRequestCounter}`;
1887
+ const timeout = setTimeout(() => {
1888
+ this.pendingControlRequests.delete(requestId);
1889
+ reject(new Error(`[WebRTCTransport] Request "${method}" timed out`));
1890
+ }, timeoutMs);
1891
+ this.pendingControlRequests.set(requestId, { resolve, reject, timeout });
1892
+ try {
1893
+ if (!this.controlChannel || this.controlChannel.readyState !== "open") {
1894
+ throw new Error("Control channel not available");
1895
+ }
1896
+ this.controlChannel.send(
1897
+ JSON.stringify({
1898
+ type: "request",
1899
+ method,
1900
+ request_id: requestId,
1901
+ data
1902
+ })
1903
+ );
1904
+ } catch (error) {
1905
+ clearTimeout(timeout);
1906
+ this.pendingControlRequests.delete(requestId);
1907
+ reject(error);
1908
+ }
1909
+ });
1910
+ }
1911
+ pauseTrack(name) {
1912
+ this.peerConnectionQueue.push(() => __async(this, null, function* () {
1913
+ var _a;
1914
+ const entry = this.transceiverMap.get(name);
1915
+ if ((_a = entry == null ? void 0 : entry.transceiver) == null ? void 0 : _a.mid) {
1916
+ entry.transceiver.direction = "inactive";
1917
+ yield this.applyDirectionLocally(entry.transceiver.mid, "inactive");
1918
+ }
1919
+ this.sendControlMessage("pause_track", { name });
1920
+ })).catch((e) => {
1921
+ console.warn("[WebRTCTransport] Failed to pause track:", e);
1922
+ });
1923
+ }
1924
+ resumeTrack(name) {
1925
+ this.peerConnectionQueue.push(() => __async(this, null, function* () {
1926
+ var _a;
1927
+ const entry = this.transceiverMap.get(name);
1928
+ if ((_a = entry == null ? void 0 : entry.transceiver) == null ? void 0 : _a.mid) {
1929
+ entry.transceiver.direction = entry.direction;
1930
+ yield this.applyDirectionLocally(
1931
+ entry.transceiver.mid,
1932
+ entry.direction
1933
+ );
1934
+ }
1935
+ this.sendControlMessage("resume_track", { name });
1936
+ })).catch((e) => {
1937
+ console.warn("[WebRTCTransport] Failed to resume track:", e);
1938
+ });
1939
+ }
1940
+ applyDirectionLocally(mid, localDirection) {
1941
+ return __async(this, null, function* () {
1942
+ var _a, _b;
1943
+ const pc = this.peerConnection;
1944
+ if (!pc) return;
1945
+ const tx = pc.getTransceivers().find((t) => t.mid === mid);
1946
+ if (tx && tx.currentDirection === localDirection) return;
1947
+ if (pc.signalingState !== "stable") return;
1948
+ const localSdp = (_a = pc.localDescription) == null ? void 0 : _a.sdp;
1949
+ const remoteSdp = (_b = pc.remoteDescription) == null ? void 0 : _b.sdp;
1950
+ if (!localSdp || !remoteSdp) return;
1951
+ const modifiedLocal = replaceSdpDirectionForMid(
1952
+ localSdp,
1953
+ mid,
1954
+ localDirection
1955
+ );
1956
+ const modifiedRemote = replaceSdpDirectionForMid(
1957
+ remoteSdp,
1958
+ mid,
1959
+ complementDirection(localDirection)
1960
+ );
1961
+ try {
1962
+ yield pc.setLocalDescription(
1963
+ new RTCSessionDescription({ type: "offer", sdp: modifiedLocal })
1964
+ );
1965
+ yield pc.setRemoteDescription(
1966
+ new RTCSessionDescription({ type: "answer", sdp: modifiedRemote })
1967
+ );
1968
+ } catch (e) {
1969
+ if (pc.signalingState === "have-local-offer") {
1970
+ try {
1971
+ yield pc.setLocalDescription({ type: "rollback" });
1972
+ } catch (e2) {
1973
+ }
1974
+ }
1975
+ throw e;
1976
+ }
1977
+ });
1978
+ }
1744
1979
  // ─────────────────────────────────────────────────────────────────────────
1745
1980
  // Track Publishing
1746
1981
  // ─────────────────────────────────────────────────────────────────────────
@@ -1767,6 +2002,7 @@ var WebRTCTransportClient = class {
1767
2002
  `[WebRTCTransport] Cannot publish track "${name}" - transceiver is recvonly`
1768
2003
  );
1769
2004
  }
2005
+ yield this.sendControlRequest("publish_track", { name });
1770
2006
  yield entry.transceiver.sender.replaceTrack(track);
1771
2007
  this.publishedTracks.set(name, track);
1772
2008
  console.debug(`[WebRTCTransport] Track "${name}" published successfully`);
@@ -1778,6 +2014,7 @@ var WebRTCTransportClient = class {
1778
2014
  if (!(entry == null ? void 0 : entry.transceiver) || !this.publishedTracks.has(name)) return;
1779
2015
  try {
1780
2016
  yield entry.transceiver.sender.replaceTrack(null);
2017
+ this.sendControlMessage("unpublish_track", { name });
1781
2018
  console.debug(
1782
2019
  `[WebRTCTransport] Track "${name}" unpublished successfully`
1783
2020
  );
@@ -1863,9 +2100,11 @@ var WebRTCTransportClient = class {
1863
2100
  // Internal Helpers
1864
2101
  // ─────────────────────────────────────────────────────────────────────────
1865
2102
  checkFullyConnected() {
1866
- if (this.peerConnected && this.dataChannelOpen) {
2103
+ if (this.peerConnected && this.dataChannelOpen && this.controlChannelOpen) {
1867
2104
  this.setStatus("connected");
1868
2105
  this.startStatsPolling();
2106
+ this.readyGateResolve();
2107
+ this.readyGateResolve = void 0;
1869
2108
  }
1870
2109
  }
1871
2110
  setStatus(newStatus) {
@@ -1985,6 +2224,46 @@ var WebRTCTransportClient = class {
1985
2224
  }
1986
2225
  };
1987
2226
  }
2227
+ setupControlChannelHandlers() {
2228
+ if (!this.controlChannel) return;
2229
+ this.controlChannel.onopen = () => {
2230
+ console.debug("[WebRTCTransport] Control channel open");
2231
+ this.controlChannelOpen = true;
2232
+ this.checkFullyConnected();
2233
+ };
2234
+ this.controlChannel.onclose = () => {
2235
+ console.debug("[WebRTCTransport] Control channel closed");
2236
+ this.controlChannelOpen = false;
2237
+ };
2238
+ this.controlChannel.onerror = (error) => {
2239
+ console.error("[WebRTCTransport] Control channel error:", error);
2240
+ };
2241
+ this.controlChannel.onmessage = (event) => {
2242
+ var _a, _b;
2243
+ const raw = parseMessage(event.data);
2244
+ if ((raw == null ? void 0 : raw.type) === "response") {
2245
+ const requestId = raw == null ? void 0 : raw.request_id;
2246
+ if (requestId) {
2247
+ const pending = this.pendingControlRequests.get(requestId);
2248
+ if (pending) {
2249
+ clearTimeout(pending.timeout);
2250
+ this.pendingControlRequests.delete(requestId);
2251
+ if (raw == null ? void 0 : raw.error) {
2252
+ pending.reject(
2253
+ new Error(
2254
+ `[WebRTCTransport] ${(_a = raw.method) != null ? _a : "request"} failed: ${(_b = raw.error.message) != null ? _b : "unknown error"}`
2255
+ )
2256
+ );
2257
+ } else {
2258
+ pending.resolve();
2259
+ }
2260
+ }
2261
+ }
2262
+ return;
2263
+ }
2264
+ console.debug("[WebRTCTransport] Received control message:", raw);
2265
+ };
2266
+ }
1988
2267
  };
1989
2268
 
1990
2269
  // src/core/Reactor.ts
@@ -2668,7 +2947,17 @@ var OptionsSchema = z3.object({
2668
2947
  var Reactor = class {
2669
2948
  constructor(options) {
2670
2949
  this.status = "disconnected";
2950
+ /**
2951
+ * True only when the active session was created by THIS connect flow
2952
+ * (via `createSession()`). False when the session was adopted via
2953
+ * `connect({ sessionId })`. We must never DELETE a session we did not
2954
+ * create — another client (or the backend that created it) owns its
2955
+ * lifecycle, so on disconnect/teardown/error an adopting client tears
2956
+ * down its own transport but leaves the session alive.
2957
+ */
2958
+ this.createdSession = false;
2671
2959
  this.tracks = [];
2960
+ this.autoResumeTracks = false;
2672
2961
  this.eventListeners = /* @__PURE__ */ new Map();
2673
2962
  const validatedOptions = OptionsSchema.parse(options);
2674
2963
  this.coordinatorUrl = validatedOptions.apiUrl;
@@ -2905,6 +3194,7 @@ var Reactor = class {
2905
3194
  "gpu",
2906
3195
  true
2907
3196
  );
3197
+ throw error;
2908
3198
  }
2909
3199
  });
2910
3200
  }
@@ -2924,6 +3214,14 @@ var Reactor = class {
2924
3214
  }
2925
3215
  });
2926
3216
  }
3217
+ pauseTrack(name) {
3218
+ var _a;
3219
+ (_a = this.transportClient) == null ? void 0 : _a.pauseTrack(name);
3220
+ }
3221
+ resumeTrack(name) {
3222
+ var _a;
3223
+ (_a = this.transportClient) == null ? void 0 : _a.resumeTrack(name);
3224
+ }
2927
3225
  /**
2928
3226
  * Reconnects to an existing session with a fresh transport.
2929
3227
  */
@@ -2982,6 +3280,7 @@ var Reactor = class {
2982
3280
  */
2983
3281
  connect(jwtToken, options) {
2984
3282
  return __async(this, null, function* () {
3283
+ var _a;
2985
3284
  console.debug("[Reactor] Connecting, status:", this.status);
2986
3285
  if (jwtToken == void 0 && !this.local) {
2987
3286
  throw new Error("No authentication provided and not in local mode");
@@ -3005,12 +3304,14 @@ var Reactor = class {
3005
3304
  if (options == null ? void 0 : options.sessionId) {
3006
3305
  yield this.coordinatorClient.adoptSession(options.sessionId);
3007
3306
  sessionId = options.sessionId;
3307
+ this.createdSession = false;
3008
3308
  console.debug("[Reactor] Attaching to existing session:", sessionId);
3009
3309
  } else {
3010
3310
  const tSession = performance.now();
3011
3311
  const initialResponse = yield this.coordinatorClient.createSession();
3012
3312
  sessionCreationMs = performance.now() - tSession;
3013
3313
  sessionId = initialResponse.session_id;
3314
+ this.createdSession = true;
3014
3315
  console.debug(
3015
3316
  "[Reactor] Session created:",
3016
3317
  sessionId,
@@ -3030,19 +3331,22 @@ var Reactor = class {
3030
3331
  this.setupTransportHandlers();
3031
3332
  let sessionPollingMs;
3032
3333
  let transportConnectingMs;
3334
+ this.autoResumeTracks = (_a = options == null ? void 0 : options.autoResumeTracks) != null ? _a : true;
3033
3335
  if (this.presetTracks) {
3336
+ this.tracks = this.presetTracks;
3034
3337
  const tParallel = performance.now();
3035
3338
  const [sessionResponse] = yield Promise.all([
3036
3339
  this.coordinatorClient.pollSessionReady(),
3037
- this.transportClient.prepare(this.presetTracks)
3340
+ this.transportClient.prepare(this.tracks)
3038
3341
  ]);
3039
3342
  sessionPollingMs = performance.now() - tParallel;
3040
3343
  this.sessionResponse = sessionResponse;
3041
- this.capabilities = sessionResponse.capabilities;
3042
- this.tracks = this.presetTracks;
3344
+ this.capabilities = __spreadProps(__spreadValues({}, sessionResponse.capabilities), {
3345
+ tracks: this.tracks
3346
+ });
3043
3347
  this.emit("capabilitiesReceived", this.capabilities);
3044
3348
  const tConnect = performance.now();
3045
- yield this.transportClient.connect();
3349
+ yield this.transportClient.connect(false, options == null ? void 0 : options.connectionId);
3046
3350
  transportConnectingMs = performance.now() - tConnect;
3047
3351
  } else {
3048
3352
  this.transportClient.warmup();
@@ -3050,8 +3354,10 @@ var Reactor = class {
3050
3354
  const sessionResponse = yield this.coordinatorClient.pollSessionReady();
3051
3355
  sessionPollingMs = performance.now() - tPoll;
3052
3356
  this.sessionResponse = sessionResponse;
3053
- this.capabilities = sessionResponse.capabilities;
3054
3357
  this.tracks = sessionResponse.capabilities.tracks;
3358
+ this.capabilities = __spreadProps(__spreadValues({}, sessionResponse.capabilities), {
3359
+ tracks: this.tracks
3360
+ });
3055
3361
  this.emit("capabilitiesReceived", this.capabilities);
3056
3362
  const protocol = sessionResponse.selected_transport.protocol;
3057
3363
  if (protocol !== "webrtc") {
@@ -3059,7 +3365,7 @@ var Reactor = class {
3059
3365
  }
3060
3366
  const tTransport = performance.now();
3061
3367
  yield this.transportClient.prepare(this.tracks);
3062
- yield this.transportClient.connect();
3368
+ yield this.transportClient.connect(false, options == null ? void 0 : options.connectionId);
3063
3369
  transportConnectingMs = performance.now() - tTransport;
3064
3370
  }
3065
3371
  console.debug("[Reactor] Session ready, tracks:", this.tracks.length);
@@ -3109,6 +3415,13 @@ var Reactor = class {
3109
3415
  switch (status) {
3110
3416
  case "connected":
3111
3417
  this.finalizeConnectionTimings();
3418
+ if (this.autoResumeTracks) {
3419
+ for (const track of this.tracks) {
3420
+ if (track.direction === "recvonly") {
3421
+ this.resumeTrack(track.name);
3422
+ }
3423
+ }
3424
+ }
3112
3425
  this.setStatus("ready");
3113
3426
  break;
3114
3427
  case "disconnected":
@@ -3130,6 +3443,13 @@ var Reactor = class {
3130
3443
  (name, track, stream) => {
3131
3444
  if (this.transportClient !== client) return;
3132
3445
  this.emit("trackReceived", name, track, stream);
3446
+ if (this.autoResumeTracks) {
3447
+ for (const track2 of this.tracks) {
3448
+ if (track2.direction === "recvonly") {
3449
+ this.resumeTrack(track2.name);
3450
+ }
3451
+ }
3452
+ }
3133
3453
  }
3134
3454
  );
3135
3455
  client.on("statsUpdate", (stats) => {
@@ -3152,10 +3472,16 @@ var Reactor = class {
3152
3472
  (_a = this.coordinatorClient) == null ? void 0 : _a.abort();
3153
3473
  (_b = this.transportClient) == null ? void 0 : _b.abort();
3154
3474
  if (this.coordinatorClient && !recoverable) {
3155
- try {
3156
- yield this.coordinatorClient.terminateSession();
3157
- } catch (error) {
3158
- console.error("[Reactor] Error terminating session:", error);
3475
+ if (this.createdSession) {
3476
+ try {
3477
+ yield this.coordinatorClient.terminateSession();
3478
+ } catch (error) {
3479
+ console.error("[Reactor] Error terminating session:", error);
3480
+ }
3481
+ } else {
3482
+ console.debug(
3483
+ "[Reactor] Adopted session (not the creator) \u2014 skipping DELETE, leaving it alive"
3484
+ );
3159
3485
  }
3160
3486
  this.coordinatorClient = void 0;
3161
3487
  }
@@ -3177,6 +3503,7 @@ var Reactor = class {
3177
3503
  this.capabilities = void 0;
3178
3504
  this.tracks = [];
3179
3505
  this.sessionResponse = void 0;
3506
+ this.createdSession = false;
3180
3507
  }
3181
3508
  });
3182
3509
  }
@@ -3286,7 +3613,8 @@ var defaultInitState = {
3286
3613
  lastError: void 0,
3287
3614
  sessionExpiration: void 0,
3288
3615
  jwtToken: void 0,
3289
- sessionId: void 0
3616
+ sessionId: void 0,
3617
+ connectOptions: void 0
3290
3618
  };
3291
3619
  var initReactorStore = (props) => {
3292
3620
  return __spreadValues(__spreadValues({}, defaultInitState), props);
@@ -3342,6 +3670,7 @@ var createReactorStore = (initProps, publicState = defaultInitState) => {
3342
3670
  });
3343
3671
  return __spreadProps(__spreadValues({}, publicState), {
3344
3672
  jwtToken: initProps.jwtToken,
3673
+ connectOptions: initProps.connectOptions,
3345
3674
  internal: { reactor },
3346
3675
  // actions
3347
3676
  onMessage: (handler) => {
@@ -3370,9 +3699,10 @@ var createReactorStore = (initProps, publicState = defaultInitState) => {
3370
3699
  if (jwtToken === void 0) {
3371
3700
  jwtToken = get().jwtToken;
3372
3701
  }
3702
+ const resolvedOptions = __spreadValues(__spreadValues({}, get().connectOptions), options);
3373
3703
  console.debug("[ReactorStore] Connect called.");
3374
3704
  try {
3375
- yield get().internal.reactor.connect(jwtToken, options);
3705
+ yield get().internal.reactor.connect(jwtToken, resolvedOptions);
3376
3706
  console.debug("[ReactorStore] Connect completed successfully");
3377
3707
  } catch (error) {
3378
3708
  console.error("[ReactorStore] Connect failed:", error);
@@ -3480,30 +3810,31 @@ function ReactorProvider(_a) {
3480
3810
  const storeRef = useRef(void 0);
3481
3811
  const firstRender = useRef(true);
3482
3812
  const [_storeVersion, setStoreVersion] = useState(0);
3813
+ const _a2 = connectOptions != null ? connectOptions : {}, { autoConnect = false } = _a2, pollingOptions = __objRest(_a2, ["autoConnect"]);
3483
3814
  if (storeRef.current === void 0) {
3484
3815
  console.debug("[ReactorProvider] Creating new reactor store");
3485
3816
  storeRef.current = createReactorStore(
3486
3817
  initReactorStore(__spreadProps(__spreadValues({}, props), {
3487
- jwtToken: jwtSource
3818
+ jwtToken: jwtSource,
3819
+ connectOptions: pollingOptions
3488
3820
  }))
3489
3821
  );
3490
3822
  console.debug("[ReactorProvider] Reactor store created successfully");
3491
3823
  }
3492
- const _a2 = connectOptions != null ? connectOptions : {}, { autoConnect = false } = _a2, pollingOptions = __objRest(_a2, ["autoConnect"]);
3493
3824
  const { apiUrl, modelName, local } = props;
3494
3825
  const maxAttempts = pollingOptions.maxAttempts;
3826
+ const { autoResumeTracks } = pollingOptions;
3827
+ useEffect(() => {
3828
+ var _a3;
3829
+ (_a3 = storeRef.current) == null ? void 0 : _a3.setState({ connectOptions: pollingOptions });
3830
+ }, [autoResumeTracks, maxAttempts]);
3495
3831
  useEffect(() => {
3496
3832
  const handleBeforeUnload = () => {
3497
3833
  var _a3;
3498
- console.debug(
3499
- "[ReactorProvider] Page unloading, performing non-recoverable disconnect"
3500
- );
3501
3834
  (_a3 = storeRef.current) == null ? void 0 : _a3.getState().internal.reactor.disconnect(false);
3502
3835
  };
3503
3836
  window.addEventListener("beforeunload", handleBeforeUnload);
3504
- return () => {
3505
- window.removeEventListener("beforeunload", handleBeforeUnload);
3506
- };
3837
+ return () => window.removeEventListener("beforeunload", handleBeforeUnload);
3507
3838
  }, []);
3508
3839
  useEffect(() => {
3509
3840
  if (firstRender.current) {
@@ -3546,7 +3877,8 @@ function ReactorProvider(_a) {
3546
3877
  apiUrl,
3547
3878
  modelName,
3548
3879
  local,
3549
- jwtToken: jwtSource
3880
+ jwtToken: jwtSource,
3881
+ connectOptions: pollingOptions
3550
3882
  })
3551
3883
  );
3552
3884
  const current = storeRef.current;
@@ -3672,26 +4004,39 @@ function ReactorView({
3672
4004
  hasVideoTrack: !!videoMediaTrack,
3673
4005
  hasAudioTrack: !!audioMediaTrack
3674
4006
  });
3675
- if (videoRef.current && mediaStream) {
3676
- console.debug("[ReactorView] Attaching media stream to element");
4007
+ const el = videoRef.current;
4008
+ if (!el || !mediaStream) {
4009
+ console.debug("[ReactorView] No tracks or element to attach");
4010
+ return;
4011
+ }
4012
+ const attach = (reset) => {
3677
4013
  try {
3678
- videoRef.current.srcObject = mediaStream;
3679
- videoRef.current.play().catch((e) => {
4014
+ if (reset) el.srcObject = null;
4015
+ el.srcObject = mediaStream;
4016
+ el.play().catch((e) => {
3680
4017
  console.warn("[ReactorView] Auto-play failed:", e);
3681
4018
  });
3682
- console.debug("[ReactorView] Media stream attached successfully");
3683
4019
  } catch (error) {
3684
4020
  console.error("[ReactorView] Failed to attach media stream:", error);
3685
4021
  }
3686
- return () => {
3687
- console.debug("[ReactorView] Detaching media stream from element");
3688
- if (videoRef.current) {
3689
- videoRef.current.srcObject = null;
3690
- }
3691
- };
3692
- } else {
3693
- console.debug("[ReactorView] No tracks or element to attach");
3694
- }
4022
+ };
4023
+ console.debug("[ReactorView] Attaching media stream to element");
4024
+ attach(false);
4025
+ const onUnmute = () => {
4026
+ console.debug(
4027
+ "[ReactorView] Track unmuted \u2014 re-attaching to render incoming media"
4028
+ );
4029
+ attach(true);
4030
+ };
4031
+ const tracks = mediaStream.getTracks();
4032
+ for (const t of tracks) t.addEventListener("unmute", onUnmute);
4033
+ return () => {
4034
+ console.debug("[ReactorView] Detaching media stream from element");
4035
+ for (const t of tracks) t.removeEventListener("unmute", onUnmute);
4036
+ if (videoRef.current) {
4037
+ videoRef.current.srcObject = null;
4038
+ }
4039
+ };
3695
4040
  }, [mediaStream]);
3696
4041
  return /* @__PURE__ */ jsx2(
3697
4042
  "div",