@layercode/js-sdk 2.0.5 → 2.0.6

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.
@@ -6395,6 +6395,7 @@ class LayercodeClient {
6395
6395
  this.activeDeviceId = null;
6396
6396
  this.useSystemDefaultDevice = false;
6397
6397
  this.lastReportedDeviceId = null;
6398
+ this.lastKnownSystemDefaultDeviceKey = null;
6398
6399
  this.isMuted = false;
6399
6400
  // this.audioPauseTime = null;
6400
6401
  // Bind event handlers
@@ -6773,6 +6774,7 @@ class LayercodeClient {
6773
6774
  this.activeDeviceId = null;
6774
6775
  this.useSystemDefaultDevice = false;
6775
6776
  this.lastReportedDeviceId = null;
6777
+ this.lastKnownSystemDefaultDeviceKey = null;
6776
6778
  this.recorderStarted = false;
6777
6779
  this.readySent = false;
6778
6780
  // Clean up VAD if it exists
@@ -6893,6 +6895,8 @@ class LayercodeClient {
6893
6895
  try {
6894
6896
  const defaultDevice = devices.find((device) => device.default);
6895
6897
  const usingDefaultDevice = this.useSystemDefaultDevice;
6898
+ const previousDefaultDeviceKey = this.lastKnownSystemDefaultDeviceKey;
6899
+ const currentDefaultDeviceKey = this._getDeviceComparisonKey(defaultDevice);
6896
6900
  let shouldSwitch = !this.recorderStarted;
6897
6901
  if (!shouldSwitch) {
6898
6902
  if (usingDefaultDevice) {
@@ -6904,12 +6908,17 @@ class LayercodeClient {
6904
6908
  defaultDevice.deviceId !== this.activeDeviceId) {
6905
6909
  shouldSwitch = true;
6906
6910
  }
6911
+ else if ((previousDefaultDeviceKey && previousDefaultDeviceKey !== currentDefaultDeviceKey) ||
6912
+ (!previousDefaultDeviceKey && !currentDefaultDeviceKey && this.recorderStarted)) {
6913
+ shouldSwitch = true;
6914
+ }
6907
6915
  }
6908
6916
  else {
6909
6917
  const matchesRequestedDevice = devices.some((device) => device.deviceId === this.deviceId || device.deviceId === this.activeDeviceId);
6910
6918
  shouldSwitch = !matchesRequestedDevice;
6911
6919
  }
6912
6920
  }
6921
+ this.lastKnownSystemDefaultDeviceKey = currentDefaultDeviceKey;
6913
6922
  if (shouldSwitch) {
6914
6923
  console.debug('Selecting fallback audio input device');
6915
6924
  const fallbackDevice = defaultDevice || devices[0];
@@ -6927,6 +6936,24 @@ class LayercodeClient {
6927
6936
  }
6928
6937
  });
6929
6938
  }
6939
+ _getDeviceComparisonKey(device) {
6940
+ if (!device || typeof device !== 'object') {
6941
+ return null;
6942
+ }
6943
+ const deviceId = typeof device.deviceId === 'string' ? device.deviceId : '';
6944
+ if (deviceId && deviceId !== 'default') {
6945
+ return deviceId;
6946
+ }
6947
+ const groupId = typeof device.groupId === 'string' ? device.groupId : '';
6948
+ if (groupId) {
6949
+ return groupId;
6950
+ }
6951
+ const label = typeof device.label === 'string' ? device.label : '';
6952
+ if (label) {
6953
+ return label;
6954
+ }
6955
+ return null;
6956
+ }
6930
6957
  /**
6931
6958
  * Mutes the microphone to stop sending audio to the server
6932
6959
  * The connection and recording remain active for quick unmute