@poolse/sdk 2.12.1 → 2.13.1

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.cjs CHANGED
@@ -1665,6 +1665,8 @@ var CallRoom = class {
1665
1665
  tokenProvider;
1666
1666
  startWithVideo;
1667
1667
  startMuted;
1668
+ aloneGraceMs;
1669
+ facing;
1668
1670
  room = null;
1669
1671
  status = "idle";
1670
1672
  selfId = null;
@@ -1672,9 +1674,22 @@ var CallRoom = class {
1672
1674
  // both hold the same call, and the second holder must not open a
1673
1675
  // second SFU connection or tear the first one down on leaving.
1674
1676
  holders = 0;
1677
+ /**
1678
+ * Whether anyone else has ever been on this call.
1679
+ *
1680
+ * The whole point of the alone check is to end a call the other side
1681
+ * has vanished from, and "vanished" is only meaningful once they were
1682
+ * here. Without this flag the caller who connects first — alone, by
1683
+ * definition, for as long as it takes the callee to join — would end
1684
+ * their own call before it ever started.
1685
+ */
1686
+ hasSeenPeer = false;
1687
+ /** Pending alone verdict, cleared the moment anyone shows up. */
1688
+ aloneTimer = null;
1675
1689
  statusListeners = /* @__PURE__ */ new Set();
1676
1690
  participantListeners = /* @__PURE__ */ new Set();
1677
1691
  errorListeners = /* @__PURE__ */ new Set();
1692
+ aloneListeners = /* @__PURE__ */ new Set();
1678
1693
  /** Bound once so `off()` in teardown removes the same references. */
1679
1694
  onRoomChange = () => this.emitParticipants();
1680
1695
  /**
@@ -1695,6 +1710,8 @@ var CallRoom = class {
1695
1710
  this.tokenProvider = opts.tokenProvider;
1696
1711
  this.startWithVideo = opts.video ?? false;
1697
1712
  this.startMuted = opts.muted ?? false;
1713
+ this.facing = opts.facing ?? "user";
1714
+ this.aloneGraceMs = opts.aloneGraceMs ?? 1500;
1698
1715
  }
1699
1716
  // ── observation ─────────────────────────────────────────────────────
1700
1717
  getStatus() {
@@ -1712,6 +1729,23 @@ var CallRoom = class {
1712
1729
  this.errorListeners.add(fn);
1713
1730
  return () => this.errorListeners.delete(fn);
1714
1731
  }
1732
+ /**
1733
+ * Everyone else has left and is not coming back.
1734
+ *
1735
+ * A call whose other participants hang up cleanly ends by signal:
1736
+ * `call:hangup` travels over poolse's own channel and the far side
1737
+ * closes its screen. This covers the case where no signal is ever
1738
+ * sent — the app was killed, the phone lost its network, the tab was
1739
+ * closed — where the only evidence is that the SFU roster emptied.
1740
+ *
1741
+ * Media is NOT torn down here. Whoever handles this owns hanging up,
1742
+ * because ending a call also means telling poolse about it, which is
1743
+ * a layer this class deliberately knows nothing about.
1744
+ */
1745
+ onAlone(fn) {
1746
+ this.aloneListeners.add(fn);
1747
+ return () => this.aloneListeners.delete(fn);
1748
+ }
1715
1749
  getParticipants() {
1716
1750
  if (!this.room) return [];
1717
1751
  const self = this.toParticipant(this.room.localParticipant, true);
@@ -1763,7 +1797,9 @@ var CallRoom = class {
1763
1797
  await room.connect(connection.url, connection.token);
1764
1798
  this.selfId = room.localParticipant.identity;
1765
1799
  await room.localParticipant.setMicrophoneEnabled(!this.startMuted);
1766
- if (this.startWithVideo) await room.localParticipant.setCameraEnabled(true);
1800
+ if (this.startWithVideo) {
1801
+ await room.localParticipant.setCameraEnabled(true, { facingMode: this.facing });
1802
+ }
1767
1803
  try {
1768
1804
  await room.startAudio?.();
1769
1805
  } catch {
@@ -1795,7 +1831,41 @@ var CallRoom = class {
1795
1831
  * renegotiates underneath.
1796
1832
  */
1797
1833
  async setCameraEnabled(enabled) {
1798
- await this.withLocal((local) => local.setCameraEnabled(enabled), "camera");
1834
+ await this.withLocal(
1835
+ (local) => enabled ? local.setCameraEnabled(true, { facingMode: this.facing }) : local.setCameraEnabled(false),
1836
+ "camera"
1837
+ );
1838
+ }
1839
+ /**
1840
+ * Swap between the front and rear cameras.
1841
+ *
1842
+ * `restartTrack` re-acquires the capture device under the *existing*
1843
+ * publication, so the far side's tile keeps rendering throughout.
1844
+ * Unpublishing and republishing instead would blank their view and
1845
+ * force a renegotiation for what is, to them, the same camera.
1846
+ *
1847
+ * No-ops when the camera is off — there is nothing to restart, and
1848
+ * the next `setCameraEnabled(true)` will open whichever lens is
1849
+ * selected anyway.
1850
+ */
1851
+ async switchCamera() {
1852
+ const room = this.room;
1853
+ if (!room) return;
1854
+ const source = this.livekit.Track.Source["Camera"] ?? "camera";
1855
+ const track = room.localParticipant.getTrackPublication(source)?.videoTrack;
1856
+ if (!track?.restartTrack) return;
1857
+ const next = this.facing === "user" ? "environment" : "user";
1858
+ try {
1859
+ await track.restartTrack({ facingMode: next });
1860
+ this.facing = next;
1861
+ this.emitParticipants();
1862
+ } catch (err) {
1863
+ this.emitError(new Error(`could not switch camera: ${String(err)}`));
1864
+ }
1865
+ }
1866
+ /** Which camera the local video track is capturing from. */
1867
+ getFacingMode() {
1868
+ return this.facing;
1799
1869
  }
1800
1870
  /** Mute or unmute the microphone. */
1801
1871
  async setMicrophoneEnabled(enabled) {
@@ -1903,6 +1973,8 @@ var CallRoom = class {
1903
1973
  this.room = null;
1904
1974
  this.selfId = null;
1905
1975
  this.holders = 0;
1976
+ this.hasSeenPeer = false;
1977
+ this.clearAloneTimer();
1906
1978
  for (const track of [...this.audioSinks.keys()]) this.stopAudio(track);
1907
1979
  if (room) {
1908
1980
  try {
@@ -1921,6 +1993,39 @@ var CallRoom = class {
1921
1993
  emitParticipants() {
1922
1994
  const participants = this.getParticipants();
1923
1995
  for (const fn of this.participantListeners) fn(participants);
1996
+ this.checkAlone();
1997
+ }
1998
+ /**
1999
+ * Decide whether this client is the last one standing.
2000
+ *
2001
+ * Runs on every roster change. Three things have to be true before a
2002
+ * call is declared over: it is actually connected, somebody else was
2003
+ * here at some point, and nobody is here now — and even then the
2004
+ * verdict waits out {@link CallRoomOptions.aloneGraceMs}, so a
2005
+ * simultaneous hang-up or a momentary SFU reconnect does not read as
2006
+ * abandonment.
2007
+ */
2008
+ checkAlone() {
2009
+ if (this.aloneGraceMs <= 0) return;
2010
+ const others = this.room?.remoteParticipants.size ?? 0;
2011
+ if (others > 0) {
2012
+ this.hasSeenPeer = true;
2013
+ this.clearAloneTimer();
2014
+ return;
2015
+ }
2016
+ if (!this.hasSeenPeer || this.status !== "connected") return;
2017
+ if (this.aloneTimer) return;
2018
+ this.aloneTimer = setTimeout(() => {
2019
+ this.aloneTimer = null;
2020
+ if ((this.room?.remoteParticipants.size ?? 0) > 0) return;
2021
+ if (this.status !== "connected") return;
2022
+ for (const fn of this.aloneListeners) fn();
2023
+ }, this.aloneGraceMs);
2024
+ }
2025
+ clearAloneTimer() {
2026
+ if (!this.aloneTimer) return;
2027
+ clearTimeout(this.aloneTimer);
2028
+ this.aloneTimer = null;
1924
2029
  }
1925
2030
  emitError(error) {
1926
2031
  for (const fn of this.errorListeners) fn(error);
@@ -2261,7 +2366,7 @@ var Poolse = class {
2261
2366
  };
2262
2367
 
2263
2368
  // src/version.ts
2264
- var version = "2.12.1";
2369
+ var version = "2.13.1";
2265
2370
 
2266
2371
  exports.ApiError = ApiError;
2267
2372
  exports.AttachmentHandle = AttachmentHandle;