@poolse/sdk 2.12.0 → 2.13.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.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,16 +1674,44 @@ 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();
1695
+ /**
1696
+ * Hidden `<audio>` elements playing each remote audio track.
1697
+ *
1698
+ * livekit-client does NOT play remote audio for you on the web — that
1699
+ * is what `@livekit/components-react`'s `<RoomAudioRenderer />` exists
1700
+ * to do. Without this a call connects, the roster is right, video
1701
+ * flows, and nobody hears anything. The peer-to-peer mesh always did
1702
+ * this (see `webrtc-browser.ts`); the SFU path has to as well.
1703
+ *
1704
+ * Empty on React Native, where there is no DOM and LiveKit routes
1705
+ * audio through the native audio session instead.
1706
+ */
1707
+ audioSinks = /* @__PURE__ */ new Map();
1680
1708
  constructor(opts) {
1681
1709
  this.livekit = opts.livekit;
1682
1710
  this.tokenProvider = opts.tokenProvider;
1683
1711
  this.startWithVideo = opts.video ?? false;
1684
1712
  this.startMuted = opts.muted ?? false;
1713
+ this.facing = opts.facing ?? "user";
1714
+ this.aloneGraceMs = opts.aloneGraceMs ?? 1500;
1685
1715
  }
1686
1716
  // ── observation ─────────────────────────────────────────────────────
1687
1717
  getStatus() {
@@ -1699,6 +1729,23 @@ var CallRoom = class {
1699
1729
  this.errorListeners.add(fn);
1700
1730
  return () => this.errorListeners.delete(fn);
1701
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
+ }
1702
1749
  getParticipants() {
1703
1750
  if (!this.room) return [];
1704
1751
  const self = this.toParticipant(this.room.localParticipant, true);
@@ -1750,7 +1797,9 @@ var CallRoom = class {
1750
1797
  await room.connect(connection.url, connection.token);
1751
1798
  this.selfId = room.localParticipant.identity;
1752
1799
  await room.localParticipant.setMicrophoneEnabled(!this.startMuted);
1753
- if (this.startWithVideo) await room.localParticipant.setCameraEnabled(true);
1800
+ if (this.startWithVideo) {
1801
+ await room.localParticipant.setCameraEnabled(true, { facingMode: this.facing });
1802
+ }
1754
1803
  try {
1755
1804
  await room.startAudio?.();
1756
1805
  } catch {
@@ -1782,7 +1831,41 @@ var CallRoom = class {
1782
1831
  * renegotiates underneath.
1783
1832
  */
1784
1833
  async setCameraEnabled(enabled) {
1785
- 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;
1786
1869
  }
1787
1870
  /** Mute or unmute the microphone. */
1788
1871
  async setMicrophoneEnabled(enabled) {
@@ -1821,6 +1904,14 @@ var CallRoom = class {
1821
1904
  ]) {
1822
1905
  if (event) room.on(event, this.onRoomChange);
1823
1906
  }
1907
+ const subscribed = RoomEvent["TrackSubscribed"];
1908
+ if (subscribed) {
1909
+ room.on(subscribed, ((track) => this.playAudio(track)));
1910
+ }
1911
+ const unsubscribed = RoomEvent["TrackUnsubscribed"];
1912
+ if (unsubscribed) {
1913
+ room.on(unsubscribed, ((track) => this.stopAudio(track)));
1914
+ }
1824
1915
  const disconnected = RoomEvent["Disconnected"];
1825
1916
  if (disconnected) {
1826
1917
  room.on(disconnected, () => {
@@ -1829,6 +1920,38 @@ var CallRoom = class {
1829
1920
  });
1830
1921
  }
1831
1922
  }
1923
+ /**
1924
+ * Play a remote audio track through a hidden element.
1925
+ *
1926
+ * No-ops for video (the UI renders that) and on React Native, where
1927
+ * there is no `document` and the native audio session handles
1928
+ * playback.
1929
+ */
1930
+ playAudio(track) {
1931
+ if (typeof document === "undefined") return;
1932
+ if (track?.kind !== "audio" || typeof track.attach !== "function") return;
1933
+ if (this.audioSinks.has(track)) return;
1934
+ try {
1935
+ const element = track.attach();
1936
+ element.autoplay = true;
1937
+ element.style.display = "none";
1938
+ document.body.appendChild(element);
1939
+ this.audioSinks.set(track, element);
1940
+ } catch (err) {
1941
+ this.emitError(new Error(`could not play remote audio: ${String(err)}`));
1942
+ }
1943
+ }
1944
+ /** Tear a sink down, so a departed peer stops holding an element. */
1945
+ stopAudio(track) {
1946
+ const element = this.audioSinks.get(track);
1947
+ if (!element) return;
1948
+ this.audioSinks.delete(track);
1949
+ try {
1950
+ track.detach?.(element);
1951
+ } catch {
1952
+ }
1953
+ element.remove();
1954
+ }
1832
1955
  toParticipant(participant, isSelf) {
1833
1956
  const cameraSource = this.livekit.Track.Source["Camera"] ?? "camera";
1834
1957
  const publication = participant.getTrackPublication(cameraSource);
@@ -1850,6 +1973,9 @@ var CallRoom = class {
1850
1973
  this.room = null;
1851
1974
  this.selfId = null;
1852
1975
  this.holders = 0;
1976
+ this.hasSeenPeer = false;
1977
+ this.clearAloneTimer();
1978
+ for (const track of [...this.audioSinks.keys()]) this.stopAudio(track);
1853
1979
  if (room) {
1854
1980
  try {
1855
1981
  await room.disconnect();
@@ -1867,6 +1993,39 @@ var CallRoom = class {
1867
1993
  emitParticipants() {
1868
1994
  const participants = this.getParticipants();
1869
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;
1870
2029
  }
1871
2030
  emitError(error) {
1872
2031
  for (const fn of this.errorListeners) fn(error);
@@ -2207,7 +2366,7 @@ var Poolse = class {
2207
2366
  };
2208
2367
 
2209
2368
  // src/version.ts
2210
- var version = "2.12.0";
2369
+ var version = "2.13.0";
2211
2370
 
2212
2371
  exports.ApiError = ApiError;
2213
2372
  exports.AttachmentHandle = AttachmentHandle;