@signalwire/js 4.0.0-dev-20260911195739 → 4.0.0-dev-20260911205444

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.
@@ -9100,6 +9100,8 @@ const MEDIA_ACCESS_DENIAL_NAMES = [
9100
9100
  "SecurityError",
9101
9101
  "PermissionDeniedError"
9102
9102
  ];
9103
+ /** Error names browsers use when the capture hardware is already held exclusively. */
9104
+ const MEDIA_DEVICE_IN_USE_NAMES = ["NotReadableError", "TrackStartError"];
9103
9105
  /** Default polling interval for RTCPeerConnection.getStats() in milliseconds. */
9104
9106
  const DEFAULT_STATS_POLLING_INTERVAL_MS = 1e3;
9105
9107
  /** Number of initial samples used to build a baseline for spike detection. */
@@ -9438,6 +9440,10 @@ var MediaTrackError = class extends Error {
9438
9440
  function isMediaAccessDenial(originalError) {
9439
9441
  return originalError instanceof Error && MEDIA_ACCESS_DENIAL_NAMES.includes(originalError.name);
9440
9442
  }
9443
+ /** True when a `getUserMedia` rejection means the hardware is already held exclusively. */
9444
+ function isMediaDeviceInUse(originalError) {
9445
+ return originalError instanceof Error && MEDIA_DEVICE_IN_USE_NAMES.includes(originalError.name);
9446
+ }
9441
9447
  /**
9442
9448
  * Failure to acquire local media (camera, microphone, or screen capture)
9443
9449
  * via `getUserMedia`/`getDisplayMedia`.
@@ -16056,38 +16062,32 @@ var RTCPeerConnectionController = class extends Destroyable {
16056
16062
  this.negotiationNeeded$.next();
16057
16063
  };
16058
16064
  this.updateSelectedInputDevice = async (kind, deviceInfo) => {
16065
+ const { localStream } = this;
16066
+ if (!localStream) {
16067
+ logger$17.warn("[RTCPeerConnectionController] No local stream available to update input device.");
16068
+ return;
16069
+ }
16070
+ const currentTrack = localStream.getTracks().find((track) => track.kind === kind);
16071
+ if (!currentTrack) {
16072
+ logger$17.debug(`[RTCPeerConnectionController] No ${kind} track to switch.`);
16073
+ return;
16074
+ }
16075
+ if (!deviceInfo) {
16076
+ logger$17.debug(`[RTCPeerConnectionController] ${kind} input device selected: none`);
16077
+ this.stopTrackSender(kind);
16078
+ return;
16079
+ }
16080
+ const constraints = {
16081
+ ...currentTrack.getConstraints(),
16082
+ ...this.deviceController.deviceInfoToConstraints(deviceInfo)
16083
+ };
16059
16084
  try {
16060
- const { localStream } = this;
16061
- if (!localStream) {
16062
- logger$17.warn("[RTCPeerConnectionController] No local stream available to update input device.");
16063
- return;
16064
- }
16065
- logger$17.debug(`[RTCPeerConnectionController] Updating selected ${kind} input device:`, localStream.getTracks());
16066
- const track = localStream.getTracks().find((track$1) => track$1.kind === kind);
16067
- if (track) {
16068
- this.transceiverController?.stopTrackSender(kind);
16069
- this.localStreamController.removeTrack(track.id);
16070
- logger$17.debug(`[RTCPeerConnectionController] Stopped existing ${kind} track: ${track.id}`, localStream.getTracks());
16071
- if (!deviceInfo) {
16072
- logger$17.debug(`[RTCPeerConnectionController] ${kind} input device selected: none`);
16073
- return;
16074
- }
16075
- const streamTrack = (await this.getUserMedia({ [kind]: {
16076
- ...track.getConstraints(),
16077
- ...this.deviceController.deviceInfoToConstraints(deviceInfo)
16078
- } })).getTracks().find((t) => t.kind === kind);
16079
- if (streamTrack) {
16080
- logger$17.debug(`[RTCPeerConnectionController] Adding new ${kind} track: ${streamTrack.id}`);
16081
- this.localStreamController.addTrack(streamTrack);
16082
- await this.transceiverController?.replaceSenderTrack(kind, streamTrack);
16083
- logger$17.debug(`[RTCPeerConnectionController] Added new ${kind} track: ${streamTrack.id}`, this.localStream?.getTracks());
16084
- }
16085
- }
16086
- logger$17.debug(`[RTCPeerConnectionController] ${kind} input device selected:`, deviceInfo?.label);
16085
+ const newTrack = await this.acquireInputTrack(kind, constraints, deviceInfo, currentTrack);
16086
+ await this.attachInputTrack(kind, newTrack, currentTrack);
16087
+ logger$17.debug(`[RTCPeerConnectionController] ${kind} input device selected:`, deviceInfo.label, newTrack.id);
16087
16088
  } catch (error) {
16088
16089
  logger$17.error(`[RTCPeerConnectionController] Failed to select ${kind} input device:`, error);
16089
16090
  this._errors$.next(new MediaTrackError("updateSelectedInputDevice", kind, error));
16090
- throw error;
16091
16091
  }
16092
16092
  };
16093
16093
  this._isNegotiating$ = this.createBehaviorSubject(false);
@@ -16790,6 +16790,61 @@ var RTCPeerConnectionController = class extends Destroyable {
16790
16790
  this._localAudioPipeline.setInputTrack(newTrack);
16791
16791
  }
16792
16792
  /**
16793
+ * Capture the newly selected device, leaving the current capture running.
16794
+ *
16795
+ * A rejection must leave the current track sending, so nothing is released
16796
+ * until the replacement is in hand. The one exception is hardware that admits
16797
+ * a single opener — a phone's front and back cameras, typically — which
16798
+ * rejects the second capture until the first is closed.
16799
+ */
16800
+ async acquireInputTrack(kind, constraints, deviceInfo, currentTrack) {
16801
+ try {
16802
+ return await this.captureTrack(kind, constraints, deviceInfo.deviceId);
16803
+ } catch (error) {
16804
+ if (!isMediaDeviceInUse(error)) throw error;
16805
+ logger$17.warn(`[RTCPeerConnectionController] ${kind} device is held exclusively; releasing the current capture to retry:`, error);
16806
+ const previousDeviceId = currentTrack.getSettings().deviceId;
16807
+ this.stopTrackSender(kind);
16808
+ try {
16809
+ return await this.captureTrack(kind, constraints, deviceInfo.deviceId);
16810
+ } catch (retryError) {
16811
+ await this.restorePreviousInputTrack(kind, constraints, previousDeviceId, currentTrack);
16812
+ throw retryError;
16813
+ }
16814
+ }
16815
+ }
16816
+ async captureTrack(kind, constraints, deviceId) {
16817
+ const { stream, fallbackLevel } = await getUserMediaWithFallback({ getUserMedia: async (c) => this.getUserMedia(c) }, { [kind]: constraints }, kind, deviceId);
16818
+ const track = stream.getTracks().find((t) => t.kind === kind);
16819
+ if (!track) {
16820
+ stream.getTracks().forEach((t) => t.stop());
16821
+ throw new MediaTrackError("updateSelectedInputDevice", kind, /* @__PURE__ */ new Error("getUserMedia returned no track of the requested kind"));
16822
+ }
16823
+ if (fallbackLevel !== "exact") logger$17.warn(`[RTCPeerConnectionController] ${kind} device acquired at fallback level '${fallbackLevel}'; the capture may not be the requested device.`);
16824
+ return track;
16825
+ }
16826
+ /** Best-effort return to the device that was released for an exclusive retry. */
16827
+ async restorePreviousInputTrack(kind, constraints, previousDeviceId, releasedTrack) {
16828
+ try {
16829
+ const restored = await this.captureTrack(kind, constraints, previousDeviceId);
16830
+ await this.attachInputTrack(kind, restored, releasedTrack);
16831
+ } catch (error) {
16832
+ logger$17.error(`[RTCPeerConnectionController] Failed to restore the previous ${kind} device:`, error);
16833
+ }
16834
+ }
16835
+ async attachInputTrack(kind, newTrack, oldTrack) {
16836
+ const pipelineOwnsAudio = kind === "audio" && this._localAudioPipeline;
16837
+ if (!pipelineOwnsAudio) try {
16838
+ await this.transceiverController?.replaceSenderTrack(kind, newTrack);
16839
+ } catch (error) {
16840
+ newTrack.stop();
16841
+ throw error;
16842
+ }
16843
+ this.localStreamController.removeTrack(oldTrack.id);
16844
+ this.localStreamController.addTrack(newTrack);
16845
+ if (pipelineOwnsAudio) this._localAudioPipeline?.setInputTrack(newTrack);
16846
+ }
16847
+ /**
16793
16848
  * Return the lazily-created {@link LocalAudioPipeline}, constructing it on
16794
16849
  * first access. On creation the current audio sender's track is routed
16795
16850
  * through the pipeline (input → gain → analyser → destination) and the