@siteed/expo-audio-studio 2.12.3 → 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.
Files changed (33) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/android/build.gradle +11 -0
  3. package/android/src/main/AndroidManifest.xml +8 -0
  4. package/android/src/main/java/net/siteed/audiostream/AudioDeviceManager.kt +266 -42
  5. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +55 -1
  6. package/app.plugin.js +3 -1
  7. package/build/cjs/AudioDeviceManager.js +225 -40
  8. package/build/cjs/AudioDeviceManager.js.map +1 -1
  9. package/build/cjs/hooks/useAudioDevices.js +30 -5
  10. package/build/cjs/hooks/useAudioDevices.js.map +1 -1
  11. package/build/cjs/useAudioRecorder.js +52 -8
  12. package/build/cjs/useAudioRecorder.js.map +1 -1
  13. package/build/esm/AudioDeviceManager.js +225 -40
  14. package/build/esm/AudioDeviceManager.js.map +1 -1
  15. package/build/esm/hooks/useAudioDevices.js +31 -6
  16. package/build/esm/hooks/useAudioDevices.js.map +1 -1
  17. package/build/esm/useAudioRecorder.js +53 -9
  18. package/build/esm/useAudioRecorder.js.map +1 -1
  19. package/build/types/AudioDeviceManager.d.ts +78 -2
  20. package/build/types/AudioDeviceManager.d.ts.map +1 -1
  21. package/build/types/hooks/useAudioDevices.d.ts +1 -0
  22. package/build/types/hooks/useAudioDevices.d.ts.map +1 -1
  23. package/build/types/useAudioRecorder.d.ts.map +1 -1
  24. package/ios/AudioDeviceManager.swift +21 -9
  25. package/ios/ExpoAudioStreamModule.swift +33 -1
  26. package/package.json +8 -6
  27. package/plugin/build/index.cjs +194 -0
  28. package/plugin/build/index.d.cts +1 -0
  29. package/plugin/build/index.js +7 -6
  30. package/plugin/src/index.ts +8 -8
  31. package/src/AudioDeviceManager.ts +286 -59
  32. package/src/hooks/useAudioDevices.ts +39 -6
  33. package/src/useAudioRecorder.tsx +102 -9
@@ -40,48 +40,88 @@ function mapRawDeviceToAudioDevice(rawDevice) {
40
40
  }
41
41
  /**
42
42
  * Class that provides a cross-platform API for managing audio input devices
43
+ *
44
+ * EVENT API SPECIFICATION:
45
+ * ========================
46
+ *
47
+ * Device Events (deviceChangedEvent):
48
+ * {
49
+ * type: "deviceConnected" | "deviceDisconnected",
50
+ * deviceId: string
51
+ * }
52
+ *
53
+ * Recording Interruption Events (recordingInterruptedEvent):
54
+ * {
55
+ * reason: "userPaused" | "userResumed" | "audioFocusLoss" | "audioFocusGain" |
56
+ * "deviceFallback" | "deviceSwitchFailed" | "phoneCall" | "phoneCallEnded",
57
+ * isPaused: boolean,
58
+ * timestamp: number
59
+ * }
60
+ *
61
+ * NOTE: Device events use "type" field, interruption events use "reason" field.
62
+ * This is intentional to distinguish between different event categories.
43
63
  */
44
64
  export class AudioDeviceManager {
45
65
  eventEmitter;
46
66
  currentDeviceId = null;
47
67
  availableDevices = [];
48
68
  deviceChangeListeners = new Set();
49
- deviceListeners = new Set();
69
+ webDeviceChangeHandler;
50
70
  lastRefreshTime = 0;
51
71
  refreshInProgress = false;
52
72
  refreshDebounceMs = 500; // Minimum 500ms between refreshes
53
73
  logger;
74
+ // Track temporarily disconnected devices
75
+ temporarilyDisconnectedDevices = new Set();
76
+ disconnectionTimeouts = new Map();
77
+ DISCONNECTION_TIMEOUT_MS = 5000; // 5 seconds
54
78
  constructor(options) {
55
79
  this.eventEmitter = new EventEmitter(ExpoAudioStreamModule);
56
80
  this.logger = options?.logger;
57
- // Listen for device change events from native modules if not on web
58
- if (Platform.OS !== 'web') {
59
- // Store the last event type to avoid duplicates
60
- let lastEventType = null;
61
- let lastEventTime = 0;
62
- this.eventEmitter.addListener('deviceChangedEvent', (event) => {
63
- // Skip processing duplicate events that occur too close together
64
- const now = Date.now();
65
- const isSimilarEvent = lastEventType === event.type &&
66
- now - lastEventTime < this.refreshDebounceMs;
67
- if (isSimilarEvent) {
68
- this.logger?.debug(`Skipping similar device event (${event.type}) received too soon`);
69
- return;
70
- }
71
- // Update the last event tracking
72
- lastEventType = event.type;
73
- lastEventTime = now;
74
- // Only refresh on meaningful events
75
- if (event.type === 'deviceConnected' ||
76
- event.type === 'deviceDisconnected' ||
77
- event.type === 'routeChanged') {
78
- this.logger?.debug(`Processing device event: ${event.type}`);
79
- // Refresh devices and notify listeners regardless of the direct return value
80
- this.refreshDevices();
81
- }
82
- });
81
+ // Set up device event listeners for all platforms immediately
82
+ this.setupDeviceEventListeners();
83
+ }
84
+ /**
85
+ * Set up device event listeners for the current platform
86
+ */
87
+ setupDeviceEventListeners() {
88
+ if (Platform.OS === 'web') {
89
+ this.setupWebDeviceChangeListener();
90
+ }
91
+ else {
92
+ this.setupNativeDeviceEventListener();
83
93
  }
84
94
  }
95
+ /**
96
+ * Set up native device event listener for iOS/Android
97
+ */
98
+ setupNativeDeviceEventListener() {
99
+ // Store the last event type to avoid duplicates
100
+ let lastEventType = null;
101
+ let lastEventTime = 0;
102
+ this.eventEmitter.addListener('deviceChangedEvent', (event) => {
103
+ // Skip processing duplicate events that occur too close together
104
+ const now = Date.now();
105
+ const isSimilarEvent = lastEventType === event.type &&
106
+ now - lastEventTime < this.refreshDebounceMs;
107
+ if (isSimilarEvent) {
108
+ this.logger?.debug(`Skipping similar device event (${event.type}) received too soon`);
109
+ return;
110
+ }
111
+ // Update the last event tracking
112
+ lastEventType = event.type;
113
+ lastEventTime = now;
114
+ // Only refresh on meaningful events
115
+ if (event.type === 'deviceConnected' ||
116
+ event.type === 'deviceDisconnected' ||
117
+ event.type === 'routeChanged') {
118
+ this.logger?.debug(`Processing device event: ${event.type}`);
119
+ // Force refresh for device events to ensure fresh data
120
+ this.forceRefreshDevices();
121
+ }
122
+ });
123
+ this.logger?.debug('Native device event listener set up');
124
+ }
85
125
  /**
86
126
  * Initialize the device manager with a logger
87
127
  * @param logger A logger instance that implements the ConsoleLike interface
@@ -98,6 +138,29 @@ export class AudioDeviceManager {
98
138
  setLogger(logger) {
99
139
  this.logger = logger;
100
140
  }
141
+ /**
142
+ * Initialize or reinitialize device detection
143
+ * Useful for restarting device detection if initial setup failed
144
+ */
145
+ initializeDeviceDetection() {
146
+ this.logger?.debug('Initializing device detection...');
147
+ // Clean up existing listeners first
148
+ if (Platform.OS === 'web' && this.webDeviceChangeHandler) {
149
+ if (typeof navigator !== 'undefined' && navigator.mediaDevices) {
150
+ navigator.mediaDevices.removeEventListener('devicechange', this.webDeviceChangeHandler);
151
+ }
152
+ this.webDeviceChangeHandler = undefined;
153
+ }
154
+ // Re-setup device event listeners
155
+ this.setupDeviceEventListeners();
156
+ }
157
+ /**
158
+ * Get the current logger instance
159
+ * @returns The logger instance or undefined if not set
160
+ */
161
+ getLogger() {
162
+ return this.logger;
163
+ }
101
164
  /**
102
165
  * Get all available audio input devices
103
166
  * @param options Optional settings to force refresh the device list. Can include a refresh flag.
@@ -239,6 +302,122 @@ export class AudioDeviceManager {
239
302
  this.deviceChangeListeners.delete(listener);
240
303
  };
241
304
  }
305
+ /**
306
+ * Mark a device as temporarily disconnected (for UI filtering)
307
+ * @param deviceId The ID of the device that was disconnected
308
+ * @param notify Whether to notify listeners immediately (default: true)
309
+ */
310
+ markDeviceAsDisconnected(deviceId, notify = true) {
311
+ this.logger?.debug(`Marking device ${deviceId} as temporarily disconnected`);
312
+ // Clear any existing timeout for this device
313
+ const existingTimeout = this.disconnectionTimeouts.get(deviceId);
314
+ if (existingTimeout) {
315
+ clearTimeout(existingTimeout);
316
+ }
317
+ // Add to disconnected set
318
+ this.temporarilyDisconnectedDevices.add(deviceId);
319
+ // Set timeout to remove from disconnected set
320
+ const timeout = setTimeout(() => {
321
+ this.logger?.debug(`Reconnection timeout expired for device ${deviceId}`);
322
+ this.temporarilyDisconnectedDevices.delete(deviceId);
323
+ this.disconnectionTimeouts.delete(deviceId);
324
+ // Refresh devices to show the device again if it's still available
325
+ this.forceRefreshDevices();
326
+ }, this.DISCONNECTION_TIMEOUT_MS);
327
+ this.disconnectionTimeouts.set(deviceId, timeout);
328
+ // Only notify listeners if requested
329
+ if (notify) {
330
+ this.notifyListeners();
331
+ }
332
+ }
333
+ /**
334
+ * Mark a device as reconnected (remove from disconnected set)
335
+ * @param deviceId The ID of the device that was reconnected
336
+ */
337
+ markDeviceAsReconnected(deviceId) {
338
+ this.logger?.debug(`Marking device ${deviceId} as reconnected`);
339
+ // Clear timeout and remove from disconnected set
340
+ const timeout = this.disconnectionTimeouts.get(deviceId);
341
+ if (timeout) {
342
+ clearTimeout(timeout);
343
+ this.disconnectionTimeouts.delete(deviceId);
344
+ }
345
+ this.temporarilyDisconnectedDevices.delete(deviceId);
346
+ // Notify listeners with updated device list
347
+ this.notifyListeners();
348
+ }
349
+ /**
350
+ * Get filtered device list (excluding temporarily disconnected devices)
351
+ * @returns Array of available devices excluding temporarily disconnected ones
352
+ */
353
+ getFilteredDevices() {
354
+ if (this.temporarilyDisconnectedDevices.size === 0) {
355
+ return [...this.availableDevices];
356
+ }
357
+ const filtered = this.availableDevices.filter((device) => !this.temporarilyDisconnectedDevices.has(device.id));
358
+ this.logger?.debug(`Filtered ${this.availableDevices.length - filtered.length} temporarily disconnected devices. ` +
359
+ `Showing ${filtered.length} devices.`);
360
+ return filtered;
361
+ }
362
+ /**
363
+ * Get the raw device list (including temporarily disconnected devices)
364
+ * @returns Array of all available devices from native layer
365
+ */
366
+ getRawDevices() {
367
+ return [...this.availableDevices];
368
+ }
369
+ /**
370
+ * Get the IDs of temporarily disconnected devices
371
+ * @returns Set of device IDs that are temporarily hidden from UI
372
+ */
373
+ getTemporarilyDisconnectedDeviceIds() {
374
+ return new Set(this.temporarilyDisconnectedDevices);
375
+ }
376
+ /**
377
+ * Clean up timeouts and listeners (useful for testing or cleanup)
378
+ */
379
+ cleanup() {
380
+ // Clear all disconnection timeouts
381
+ this.disconnectionTimeouts.forEach((timeout) => clearTimeout(timeout));
382
+ this.disconnectionTimeouts.clear();
383
+ this.temporarilyDisconnectedDevices.clear();
384
+ // Clear device change listeners
385
+ this.deviceChangeListeners.clear();
386
+ // Clean up web device listener
387
+ if (Platform.OS === 'web' && this.webDeviceChangeHandler) {
388
+ if (typeof navigator !== 'undefined' && navigator.mediaDevices) {
389
+ navigator.mediaDevices.removeEventListener('devicechange', this.webDeviceChangeHandler);
390
+ }
391
+ this.webDeviceChangeHandler = undefined;
392
+ }
393
+ this.logger?.debug('AudioDeviceManager cleanup completed');
394
+ }
395
+ /**
396
+ * Force refresh devices without debouncing (for device events)
397
+ * @returns Promise resolving to the updated device list (AudioDevice[])
398
+ */
399
+ async forceRefreshDevices() {
400
+ this.logger?.debug('Force refreshing devices (bypassing debounce)...');
401
+ this.refreshInProgress = true;
402
+ try {
403
+ // Force fetch the latest devices from native layer
404
+ const devices = await this.getAvailableDevices({ refresh: true });
405
+ // Update internal state
406
+ this.availableDevices = devices;
407
+ // Notify listeners with fresh data
408
+ this.notifyListeners();
409
+ this.lastRefreshTime = Date.now();
410
+ return devices;
411
+ }
412
+ catch (error) {
413
+ this.logger?.error('Error during forceRefreshDevices:', error);
414
+ return this.availableDevices;
415
+ }
416
+ finally {
417
+ this.refreshInProgress = false;
418
+ this.logger?.debug('Force refresh finished.');
419
+ }
420
+ }
242
421
  /**
243
422
  * Refresh the list of available devices with debouncing and notify listeners.
244
423
  * @returns Promise resolving to the updated device list (AudioDevice[])
@@ -325,7 +504,6 @@ export class AudioDeviceManager {
325
504
  if (finalDevices.length === 0) {
326
505
  finalDevices = [DEFAULT_DEVICE];
327
506
  }
328
- this.setupWebDeviceChangeListener();
329
507
  this.availableDevices = finalDevices; // Update internal state
330
508
  return finalDevices;
331
509
  }
@@ -364,18 +542,24 @@ export class AudioDeviceManager {
364
542
  setupWebDeviceChangeListener() {
365
543
  if (typeof navigator === 'undefined' ||
366
544
  !navigator.mediaDevices ||
367
- this.deviceListeners.size > 0 // Avoid adding multiple listeners
545
+ this.webDeviceChangeHandler // Avoid adding multiple listeners
368
546
  ) {
547
+ this.logger?.debug('Web device change listener not available or already set up');
369
548
  return;
370
549
  }
371
- const handleDeviceChange = () => {
372
- this.logger?.debug('Web device change detected.');
373
- // Refresh devices on change
374
- this.refreshDevices();
375
- };
376
- navigator.mediaDevices.addEventListener('devicechange', handleDeviceChange);
377
- this.deviceListeners.add(handleDeviceChange);
378
- this.logger?.debug('Web device change listener added.');
550
+ try {
551
+ this.webDeviceChangeHandler = () => {
552
+ this.logger?.debug('Web device change detected, refreshing device list');
553
+ // Force refresh to get immediate updates
554
+ this.forceRefreshDevices();
555
+ };
556
+ navigator.mediaDevices.addEventListener('devicechange', this.webDeviceChangeHandler);
557
+ this.logger?.debug('Web device change listener successfully set up');
558
+ }
559
+ catch (error) {
560
+ this.logger?.warn('Failed to set up web device change listener:', error);
561
+ this.webDeviceChangeHandler = undefined;
562
+ }
379
563
  }
380
564
  /**
381
565
  * Check if the current browser is Safari or iOS WebKit
@@ -474,9 +658,10 @@ export class AudioDeviceManager {
474
658
  * Notify all registered listeners about device changes.
475
659
  */
476
660
  notifyListeners() {
477
- // Pass a copy of the current devices array to listeners
478
- const devicesCopy = [...this.availableDevices];
479
- this.logger?.debug(`Notifying ${this.deviceChangeListeners.size} listeners with ${devicesCopy.length} devices.`);
661
+ // Pass a copy of the filtered devices array to listeners
662
+ const devicesCopy = this.getFilteredDevices();
663
+ this.logger?.debug(`Notifying ${this.deviceChangeListeners.size} listeners with ${devicesCopy.length} devices ` +
664
+ `(${this.temporarilyDisconnectedDevices.size} temporarily hidden)`);
480
665
  this.deviceChangeListeners.forEach((listener) => {
481
666
  try {
482
667
  listener(devicesCopy);
@@ -1 +1 @@
1
- {"version":3,"file":"AudioDeviceManager.js","sourceRoot":"","sources":["../../src/AudioDeviceManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEvC,OAAO,EAGH,2BAA2B,GAE9B,MAAM,yBAAyB,CAAA;AAChC,OAAO,qBAAqB,MAAM,yBAAyB,C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W,CAAC,CAAA;YACzB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAA;YACjE,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AAED,2CAA2C;AAC3C,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,kBAAkB,EAAE,CAAA;AAE1D,OAAO,EAAE,2BAA2B,EAAE,CAAA","sourcesContent":["import { EventEmitter } from 'expo-modules-core'\nimport { Platform } from 'react-native'\n\nimport {\n AudioDevice,\n AudioDeviceCapabilities,\n DeviceDisconnectionBehavior,\n ConsoleLike,\n} from './ExpoAudioStream.types'\nimport ExpoAudioStreamModule from './ExpoAudioStreamModule'\n\n// Default device fallback for web and unsupported platforms\nconst DEFAULT_DEVICE: AudioDevice = {\n id: 'default',\n name: 'Default Microphone',\n type: 'builtin_mic',\n isDefault: true,\n isAvailable: true,\n capabilities: {\n sampleRates: [16000, 44100, 48000],\n channelCounts: [1, 2],\n bitDepths: [16, 24, 32],\n hasEchoCancellation: true,\n hasNoiseSuppression: true,\n hasAutomaticGainControl: true,\n },\n}\n\n// Helper function to map raw object to AudioDevice interface\n// This handles potential inconsistencies from the native module\nfunction mapRawDeviceToAudioDevice(rawDevice: any): AudioDevice {\n const capabilities = rawDevice.capabilities || {}\n return {\n id: rawDevice.id || 'unknown',\n name: rawDevice.name || 'Unknown Device',\n type: rawDevice.type || 'unknown',\n isDefault: rawDevice.isDefault || false,\n isAvailable:\n rawDevice.isAvailable !== undefined ? rawDevice.isAvailable : true, // Default to true if undefined\n capabilities: {\n sampleRates: capabilities.sampleRates || [16000, 44100, 48000], // Provide defaults\n channelCounts: capabilities.channelCounts || [1, 2],\n bitDepths: capabilities.bitDepths || [16, 24, 32],\n hasEchoCancellation: capabilities.hasEchoCancellation,\n hasNoiseSuppression: capabilities.hasNoiseSuppression,\n hasAutomaticGainControl: capabilities.hasAutomaticGainControl,\n },\n }\n}\n\n/**\n * Class that provides a cross-platform API for managing audio input devices\n */\nexport class AudioDeviceManager {\n private eventEmitter: InstanceType<typeof EventEmitter>\n private currentDeviceId: string | null = null\n private availableDevices: AudioDevice[] = []\n private deviceChangeListeners: Set<(devices: AudioDevice[]) => void> =\n new Set()\n private deviceListeners: Set<() => void> = new Set()\n private lastRefreshTime: number = 0\n private refreshInProgress: boolean = false\n private refreshDebounceMs: number = 500 // Minimum 500ms between refreshes\n private logger?: ConsoleLike\n\n constructor(options?: { logger?: ConsoleLike }) {\n this.eventEmitter = new EventEmitter(ExpoAudioStreamModule)\n this.logger = options?.logger\n\n // Listen for device change events from native modules if not on web\n if (Platform.OS !== 'web') {\n // Store the last event type to avoid duplicates\n let lastEventType: string | null = null\n let lastEventTime = 0\n\n this.eventEmitter.addListener(\n 'deviceChangedEvent',\n (event: any) => {\n // Skip processing duplicate events that occur too close together\n const now = Date.now()\n const isSimilarEvent =\n lastEventType === event.type &&\n now - lastEventTime < this.refreshDebounceMs\n\n if (isSimilarEvent) {\n this.logger?.debug(\n `Skipping similar device event (${event.type}) received too soon`\n )\n return\n }\n\n // Update the last event tracking\n lastEventType = event.type\n lastEventTime = now\n\n // Only refresh on meaningful events\n if (\n event.type === 'deviceConnected' ||\n event.type === 'deviceDisconnected' ||\n event.type === 'routeChanged'\n ) {\n this.logger?.debug(\n `Processing device event: ${event.type}`\n )\n // Refresh devices and notify listeners regardless of the direct return value\n this.refreshDevices()\n }\n }\n )\n }\n }\n\n /**\n * Initialize the device manager with a logger\n * @param logger A logger instance that implements the ConsoleLike interface\n * @returns The manager instance for chaining\n */\n initWithLogger(logger: ConsoleLike): AudioDeviceManager {\n this.setLogger(logger)\n return this\n }\n\n /**\n * Set the logger instance\n * @param logger A logger instance that implements the ConsoleLike interface\n */\n setLogger(logger: ConsoleLike) {\n this.logger = logger\n }\n\n /**\n * Get all available audio input devices\n * @param options Optional settings to force refresh the device list. Can include a refresh flag.\n * @returns Promise resolving to an array of audio devices conforming to AudioDevice interface\n */\n async getAvailableDevices(options?: {\n refresh?: boolean\n }): Promise<AudioDevice[]> {\n try {\n if (Platform.OS === 'web') {\n this.availableDevices = await this.getWebAudioDevices()\n } else if (ExpoAudioStreamModule.getAvailableInputDevices) {\n // Expecting an array of raw device objects from native\n const rawDevices: any[] =\n await ExpoAudioStreamModule.getAvailableInputDevices(\n options\n )\n // Map raw objects to the AudioDevice interface\n this.availableDevices = rawDevices.map(\n mapRawDeviceToAudioDevice\n )\n } else {\n // Fallback for unsupported platforms\n this.availableDevices = [DEFAULT_DEVICE]\n }\n return this.availableDevices\n } catch (error) {\n this.logger?.error('Failed to get available devices:', error)\n this.availableDevices = [DEFAULT_DEVICE] // Ensure state is updated on error\n return this.availableDevices\n }\n }\n\n /**\n * Get the currently selected audio input device\n * @returns Promise resolving to the current device (conforming to AudioDevice) or null\n */\n async getCurrentDevice(): Promise<AudioDevice | null> {\n try {\n if (Platform.OS === 'web') {\n if (!this.currentDeviceId) {\n // On web, return the typed default device if nothing is selected\n return DEFAULT_DEVICE\n }\n // Refresh web devices to ensure the current one is up-to-date\n const webDevices = await this.getWebAudioDevices()\n return (\n webDevices.find((d) => d.id === this.currentDeviceId) ||\n DEFAULT_DEVICE // Fallback to default if current ID not found\n )\n } else if (ExpoAudioStreamModule.getCurrentInputDevice) {\n // Expecting a single raw device object or null from native\n const rawDevice: any | null =\n await ExpoAudioStreamModule.getCurrentInputDevice()\n // Map to AudioDevice interface if not null\n return rawDevice ? mapRawDeviceToAudioDevice(rawDevice) : null\n } else {\n // Fallback for unsupported platforms\n return DEFAULT_DEVICE\n }\n } catch (error) {\n this.logger?.error('Failed to get current device:', error)\n return DEFAULT_DEVICE // Return default on error\n }\n }\n\n /**\n * Select a specific audio input device for recording\n * @param deviceId The ID of the device to select\n * @returns Promise resolving to a boolean indicating success\n */\n async selectDevice(deviceId: string): Promise<boolean> {\n try {\n let success = false\n if (Platform.OS === 'web') {\n // Check if the device exists before setting it\n const devices = await this.getWebAudioDevices()\n if (devices.some((d) => d.id === deviceId)) {\n this.currentDeviceId = deviceId\n success = true\n } else {\n this.logger?.warn(\n `Web: Device with ID ${deviceId} not found.`\n )\n success = false\n }\n } else if (ExpoAudioStreamModule.selectInputDevice) {\n success =\n await ExpoAudioStreamModule.selectInputDevice(deviceId)\n if (success) {\n this.currentDeviceId = deviceId\n }\n }\n // Refresh devices after selection attempt to update state\n await this.refreshDevices()\n return success\n } catch (error) {\n this.logger?.error('Failed to select device:', error)\n await this.refreshDevices() // Refresh even on error\n return false\n }\n }\n\n /**\n * Reset to the default audio input device\n * @returns Promise resolving to a boolean indicating success\n */\n async resetToDefaultDevice(): Promise<boolean> {\n try {\n let success = false\n if (Platform.OS === 'web') {\n this.currentDeviceId = 'default'\n success = true\n } else if (ExpoAudioStreamModule.resetToDefaultDevice) {\n success = await ExpoAudioStreamModule.resetToDefaultDevice()\n if (success) {\n this.currentDeviceId = null\n }\n }\n // Refresh devices after reset attempt\n await this.refreshDevices()\n return success\n } catch (error) {\n this.logger?.error('Failed to reset to default device:', error)\n await this.refreshDevices() // Refresh even on error\n return false\n }\n }\n\n /**\n * Register a listener for device changes\n * @param listener Function to call when devices change (receives AudioDevice[])\n * @returns Function to remove the listener\n */\n addDeviceChangeListener(\n listener: (devices: AudioDevice[]) => void\n ): () => void {\n this.deviceChangeListeners.add(listener)\n\n // Immediately call listener with current devices if available\n if (this.availableDevices.length > 0) {\n listener([...this.availableDevices])\n }\n\n // Return a function to remove the listener\n return () => {\n this.deviceChangeListeners.delete(listener)\n }\n }\n\n /**\n * Refresh the list of available devices with debouncing and notify listeners.\n * @returns Promise resolving to the updated device list (AudioDevice[])\n */\n async refreshDevices(): Promise<AudioDevice[]> {\n const now = Date.now()\n\n if (this.refreshInProgress) {\n this.logger?.debug('Refresh already in progress, skipping')\n return this.availableDevices\n }\n\n // Always allow refresh if forced by native event or longer than 2s debounce\n const timeSinceLastRefresh = now - this.lastRefreshTime\n const shouldDebounce =\n timeSinceLastRefresh < this.refreshDebounceMs &&\n timeSinceLastRefresh < 2000\n\n if (shouldDebounce) {\n this.logger?.debug(\n `Refresh debounced, skipping (last refresh was ${timeSinceLastRefresh}ms ago)`\n )\n return this.availableDevices\n }\n\n this.logger?.debug('Refreshing devices...')\n this.refreshInProgress = true\n try {\n // Fetch the latest devices; getAvailableDevices handles mapping now\n const devices = await this.getAvailableDevices({ refresh: true })\n // availableDevices state is updated within getAvailableDevices\n this.notifyListeners() // Notify listeners with the updated list\n this.lastRefreshTime = Date.now()\n return devices // Return the fetched & mapped list\n } catch (error) {\n this.logger?.error('Error during refreshDevices:', error)\n return this.availableDevices // Return potentially stale list on error\n } finally {\n this.refreshInProgress = false\n this.logger?.debug('Refresh finished.')\n }\n }\n\n /**\n * Get audio input devices using the Web Audio API\n * @returns Promise resolving to an array of AudioDevice objects\n */\n private async getWebAudioDevices(): Promise<AudioDevice[]> {\n if (\n typeof navigator === 'undefined' ||\n !navigator.mediaDevices ||\n !navigator.mediaDevices.enumerateDevices\n ) {\n return [DEFAULT_DEVICE]\n }\n\n try {\n const permissionStatus = await this.checkMicrophonePermission()\n\n if (permissionStatus === 'denied') {\n return [\n {\n ...DEFAULT_DEVICE,\n name: 'Microphone Access Denied',\n isAvailable: false,\n },\n ]\n }\n\n if (permissionStatus !== 'granted') {\n try {\n // Requesting stream often reveals device labels\n await navigator.mediaDevices.getUserMedia({ audio: true })\n } catch (error) {\n this.logger?.warn(\n 'Microphone permission request failed:',\n error\n )\n return [\n {\n ...DEFAULT_DEVICE,\n name: 'Microphone Access Required',\n isAvailable: false,\n },\n ]\n }\n }\n\n const devices = await navigator.mediaDevices.enumerateDevices()\n const audioInputDevices = devices\n .filter((device) => device.kind === 'audioinput')\n .map((device) => this.mapWebDeviceToAudioDevice(device))\n\n const hasUnlabeledDevices = audioInputDevices.some(\n (device) =>\n !device.name || device.name.startsWith('Microphone ')\n )\n\n let finalDevices = audioInputDevices\n if (hasUnlabeledDevices && this.isSafariOrIOS()) {\n finalDevices = this.enhanceDevicesForSafari(audioInputDevices)\n }\n\n if (finalDevices.length === 0) {\n finalDevices = [DEFAULT_DEVICE]\n }\n\n this.setupWebDeviceChangeListener()\n this.availableDevices = finalDevices // Update internal state\n return finalDevices\n } catch (error) {\n this.logger?.error('Failed to enumerate web audio devices:', error)\n this.availableDevices = [DEFAULT_DEVICE] // Update state on error\n return this.availableDevices\n }\n }\n\n /**\n * Check the current microphone permission status\n * @returns Permission state ('prompt', 'granted', or 'denied')\n */\n private async checkMicrophonePermission(): Promise<PermissionState> {\n if (!navigator.permissions || !navigator.permissions.query) {\n return 'prompt'\n }\n try {\n const permissionStatus = await navigator.permissions.query({\n name: 'microphone' as PermissionName,\n })\n permissionStatus.onchange = () => {\n // Refresh devices when permission changes\n this.refreshDevices()\n }\n return permissionStatus.state\n } catch (error) {\n this.logger?.warn('Permission query not supported:', error)\n return 'prompt'\n }\n }\n\n /**\n * Setup listener for device changes in web environment\n */\n private setupWebDeviceChangeListener() {\n if (\n typeof navigator === 'undefined' ||\n !navigator.mediaDevices ||\n this.deviceListeners.size > 0 // Avoid adding multiple listeners\n ) {\n return\n }\n\n const handleDeviceChange = () => {\n this.logger?.debug('Web device change detected.')\n // Refresh devices on change\n this.refreshDevices()\n }\n\n navigator.mediaDevices.addEventListener(\n 'devicechange',\n handleDeviceChange\n )\n this.deviceListeners.add(handleDeviceChange)\n this.logger?.debug('Web device change listener added.')\n }\n\n /**\n * Check if the current browser is Safari or iOS WebKit\n */\n private isSafariOrIOS(): boolean {\n if (typeof navigator === 'undefined') return false\n const ua = navigator.userAgent\n return (\n /^((?!chrome|android).)*safari/i.test(ua) ||\n /iPad|iPhone|iPod/.test(ua) ||\n (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)\n )\n }\n\n /**\n * Create enhanced device information for Safari and privacy-restricted browsers\n * @param devices Array of AudioDevice objects, potentially unlabeled\n * @returns Array of enhanced AudioDevice objects\n */\n private enhanceDevicesForSafari(devices: AudioDevice[]): AudioDevice[] {\n const defaultDevice = devices.find((d) => d.isDefault)\n\n if (devices.length <= 1) {\n // Return a typed default device\n return [\n {\n id: defaultDevice?.id || 'default',\n name: 'Microphone (Browser Managed)',\n type: 'builtin_mic',\n isDefault: true,\n isAvailable: true,\n capabilities:\n defaultDevice?.capabilities ||\n DEFAULT_DEVICE.capabilities,\n },\n ]\n }\n\n // Provide more descriptive names for unlabeled devices\n return devices.map((device, index) => {\n if (!device.name || device.name.startsWith('Microphone ')) {\n const deviceTypes = [\n 'Built-in Microphone',\n 'External Microphone',\n 'Headset Microphone',\n ]\n const typeName = deviceTypes[index % deviceTypes.length]\n return {\n ...device,\n name: device.isDefault ? `${typeName} (Default)` : typeName,\n }\n }\n return device\n })\n }\n\n /**\n * Map a Web MediaDeviceInfo to our AudioDevice format\n * @param device The MediaDeviceInfo object from the browser\n * @returns An object conforming to the AudioDevice interface\n */\n private mapWebDeviceToAudioDevice(device: MediaDeviceInfo): AudioDevice {\n const isDefault = device.deviceId === 'default'\n const deviceType = this.inferDeviceType(device.label || '')\n\n // Provide reasonable default capabilities for web devices\n const defaultWebCapabilities: AudioDeviceCapabilities = {\n sampleRates: [16000, 44100, 48000],\n channelCounts: [1, 2],\n bitDepths: [16, 32], // Web Audio uses float32, common PCM might be 16/32\n hasEchoCancellation: true, // Often handled by browser\n hasNoiseSuppression: true, // Often handled by browser\n hasAutomaticGainControl: true, // Often handled by browser\n }\n\n return {\n id: device.deviceId,\n name:\n device.label || `Microphone ${device.deviceId.substring(0, 8)}`,\n type: deviceType,\n isDefault,\n isAvailable: true, // Assume available if enumerated\n capabilities: defaultWebCapabilities,\n }\n }\n\n /**\n * Try to infer the device type from its name\n * @param deviceName The label of the device\n * @returns A string representing the inferred device type\n */\n private inferDeviceType(deviceName: string): string {\n const name = deviceName.toLowerCase()\n if (name.includes('bluetooth') || name.includes('airpods'))\n return 'bluetooth'\n if (name.includes('usb')) return 'usb'\n if (name.includes('headphone') || name.includes('headset')) {\n return name.includes('wired') ? 'wired_headset' : 'wired_headphones'\n }\n if (name.includes('speaker')) return 'speaker'\n return 'builtin_mic' // Default assumption\n }\n\n /**\n * Notify all registered listeners about device changes.\n */\n private notifyListeners(): void {\n // Pass a copy of the current devices array to listeners\n const devicesCopy = [...this.availableDevices]\n this.logger?.debug(\n `Notifying ${this.deviceChangeListeners.size} listeners with ${devicesCopy.length} devices.`\n )\n this.deviceChangeListeners.forEach((listener) => {\n try {\n listener(devicesCopy)\n } catch (error) {\n this.logger?.error('Error in device change listener:', error)\n }\n })\n }\n}\n\n// Create and export the singleton instance\nexport const audioDeviceManager = new AudioDeviceManager()\n\nexport { DeviceDisconnectionBehavior }\n"]}
1
+ {"version":3,"file":"AudioDeviceManager.js","sourceRoot":"","sources":["../../src/AudioDeviceManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEvC,OAAO,EAGH,2BAA2B,GAE9B,MAAM,yBAAyB,CAAA;AAChC,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAE3D,4DAA4D;AAC5D,MAAM,cAAc,GAAgB;IAChC,EAAE,EAAE,SAAS;IACb,IAAI,EAAE,oBAAoB;IAC1B,IAAI,EAAE,aAAa;IACnB,SAAS,EAAE,IAAI;IACf,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE;QACV,WAAW,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC;QAClC,aAAa,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;QACrB,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;QACvB,mBAAmB,EAAE,IAAI;QACzB,mBAAmB,EAAE,IAAI;QACzB,uBAAuB,EAAE,IAAI;KAChC;CACJ,CAAA;AAED,6DAA6D;AAC7D,gEAAgE;AAChE,SAAS,yBAAyB,CAAC,SAAc;IAC7C,MAAM,YAAY,GAAG,SA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C,OAAO,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,kBAAkB,CAAA;QACxE,CAAC;QACD,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC;YAAE,OAAO,SAAS,CAAA;QAC9C,OAAO,aAAa,CAAA,CAAC,qBAAqB;IAC9C,CAAC;IAED;;OAEG;IACH,eAAe;QACX,yDAAyD;QACzD,MAAM,WAAW,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAA;QAE7C,IAAI,CAAC,MAAM,EAAE,KAAK,CACd,aAAa,IAAI,CAAC,qBAAqB,CAAC,IAAI,mBAAmB,WAAW,CAAC,MAAM,WAAW;YACxF,IAAI,IAAI,CAAC,8BAA8B,CAAC,IAAI,sBAAsB,CACzE,CAAA;QAED,IAAI,CAAC,qBAAqB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YAC5C,IAAI,CAAC;gBACD,QAAQ,CAAC,WAAW,CAAC,CAAA;YACzB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAA;YACjE,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AAED,2CAA2C;AAC3C,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,kBAAkB,EAAE,CAAA;AAE1D,OAAO,EAAE,2BAA2B,EAAE,CAAA","sourcesContent":["import { EventEmitter } from 'expo-modules-core'\nimport { Platform } from 'react-native'\n\nimport {\n AudioDevice,\n AudioDeviceCapabilities,\n DeviceDisconnectionBehavior,\n ConsoleLike,\n} from './ExpoAudioStream.types'\nimport ExpoAudioStreamModule from './ExpoAudioStreamModule'\n\n// Default device fallback for web and unsupported platforms\nconst DEFAULT_DEVICE: AudioDevice = {\n id: 'default',\n name: 'Default Microphone',\n type: 'builtin_mic',\n isDefault: true,\n isAvailable: true,\n capabilities: {\n sampleRates: [16000, 44100, 48000],\n channelCounts: [1, 2],\n bitDepths: [16, 24, 32],\n hasEchoCancellation: true,\n hasNoiseSuppression: true,\n hasAutomaticGainControl: true,\n },\n}\n\n// Helper function to map raw object to AudioDevice interface\n// This handles potential inconsistencies from the native module\nfunction mapRawDeviceToAudioDevice(rawDevice: any): AudioDevice {\n const capabilities = rawDevice.capabilities || {}\n return {\n id: rawDevice.id || 'unknown',\n name: rawDevice.name || 'Unknown Device',\n type: rawDevice.type || 'unknown',\n isDefault: rawDevice.isDefault || false,\n isAvailable:\n rawDevice.isAvailable !== undefined ? rawDevice.isAvailable : true, // Default to true if undefined\n capabilities: {\n sampleRates: capabilities.sampleRates || [16000, 44100, 48000], // Provide defaults\n channelCounts: capabilities.channelCounts || [1, 2],\n bitDepths: capabilities.bitDepths || [16, 24, 32],\n hasEchoCancellation: capabilities.hasEchoCancellation,\n hasNoiseSuppression: capabilities.hasNoiseSuppression,\n hasAutomaticGainControl: capabilities.hasAutomaticGainControl,\n },\n }\n}\n\n/**\n * Class that provides a cross-platform API for managing audio input devices\n *\n * EVENT API SPECIFICATION:\n * ========================\n *\n * Device Events (deviceChangedEvent):\n * {\n * type: \"deviceConnected\" | \"deviceDisconnected\",\n * deviceId: string\n * }\n *\n * Recording Interruption Events (recordingInterruptedEvent):\n * {\n * reason: \"userPaused\" | \"userResumed\" | \"audioFocusLoss\" | \"audioFocusGain\" |\n * \"deviceFallback\" | \"deviceSwitchFailed\" | \"phoneCall\" | \"phoneCallEnded\",\n * isPaused: boolean,\n * timestamp: number\n * }\n *\n * NOTE: Device events use \"type\" field, interruption events use \"reason\" field.\n * This is intentional to distinguish between different event categories.\n */\nexport class AudioDeviceManager {\n private eventEmitter: InstanceType<typeof EventEmitter>\n private currentDeviceId: string | null = null\n private availableDevices: AudioDevice[] = []\n private deviceChangeListeners: Set<(devices: AudioDevice[]) => void> =\n new Set()\n private webDeviceChangeHandler?: () => void\n private lastRefreshTime: number = 0\n private refreshInProgress: boolean = false\n private refreshDebounceMs: number = 500 // Minimum 500ms between refreshes\n private logger?: ConsoleLike\n\n // Track temporarily disconnected devices\n private temporarilyDisconnectedDevices: Set<string> = new Set()\n private disconnectionTimeouts: Map<string, NodeJS.Timeout> = new Map()\n private readonly DISCONNECTION_TIMEOUT_MS = 5000 // 5 seconds\n\n constructor(options?: { logger?: ConsoleLike }) {\n this.eventEmitter = new EventEmitter(ExpoAudioStreamModule)\n this.logger = options?.logger\n\n // Set up device event listeners for all platforms immediately\n this.setupDeviceEventListeners()\n }\n\n /**\n * Set up device event listeners for the current platform\n */\n private setupDeviceEventListeners(): void {\n if (Platform.OS === 'web') {\n this.setupWebDeviceChangeListener()\n } else {\n this.setupNativeDeviceEventListener()\n }\n }\n\n /**\n * Set up native device event listener for iOS/Android\n */\n private setupNativeDeviceEventListener(): void {\n // Store the last event type to avoid duplicates\n let lastEventType: string | null = null\n let lastEventTime = 0\n\n this.eventEmitter.addListener('deviceChangedEvent', (event: any) => {\n // Skip processing duplicate events that occur too close together\n const now = Date.now()\n const isSimilarEvent =\n lastEventType === event.type &&\n now - lastEventTime < this.refreshDebounceMs\n\n if (isSimilarEvent) {\n this.logger?.debug(\n `Skipping similar device event (${event.type}) received too soon`\n )\n return\n }\n\n // Update the last event tracking\n lastEventType = event.type\n lastEventTime = now\n\n // Only refresh on meaningful events\n if (\n event.type === 'deviceConnected' ||\n event.type === 'deviceDisconnected' ||\n event.type === 'routeChanged'\n ) {\n this.logger?.debug(`Processing device event: ${event.type}`)\n // Force refresh for device events to ensure fresh data\n this.forceRefreshDevices()\n }\n })\n this.logger?.debug('Native device event listener set up')\n }\n\n /**\n * Initialize the device manager with a logger\n * @param logger A logger instance that implements the ConsoleLike interface\n * @returns The manager instance for chaining\n */\n initWithLogger(logger: ConsoleLike): AudioDeviceManager {\n this.setLogger(logger)\n return this\n }\n\n /**\n * Set the logger instance\n * @param logger A logger instance that implements the ConsoleLike interface\n */\n setLogger(logger: ConsoleLike) {\n this.logger = logger\n }\n\n /**\n * Initialize or reinitialize device detection\n * Useful for restarting device detection if initial setup failed\n */\n initializeDeviceDetection(): void {\n this.logger?.debug('Initializing device detection...')\n\n // Clean up existing listeners first\n if (Platform.OS === 'web' && this.webDeviceChangeHandler) {\n if (typeof navigator !== 'undefined' && navigator.mediaDevices) {\n navigator.mediaDevices.removeEventListener(\n 'devicechange',\n this.webDeviceChangeHandler\n )\n }\n this.webDeviceChangeHandler = undefined\n }\n\n // Re-setup device event listeners\n this.setupDeviceEventListeners()\n }\n\n /**\n * Get the current logger instance\n * @returns The logger instance or undefined if not set\n */\n getLogger(): ConsoleLike | undefined {\n return this.logger\n }\n\n /**\n * Get all available audio input devices\n * @param options Optional settings to force refresh the device list. Can include a refresh flag.\n * @returns Promise resolving to an array of audio devices conforming to AudioDevice interface\n */\n async getAvailableDevices(options?: {\n refresh?: boolean\n }): Promise<AudioDevice[]> {\n try {\n if (Platform.OS === 'web') {\n this.availableDevices = await this.getWebAudioDevices()\n } else if (ExpoAudioStreamModule.getAvailableInputDevices) {\n // Expecting an array of raw device objects from native\n const rawDevices: any[] =\n await ExpoAudioStreamModule.getAvailableInputDevices(\n options\n )\n // Map raw objects to the AudioDevice interface\n this.availableDevices = rawDevices.map(\n mapRawDeviceToAudioDevice\n )\n } else {\n // Fallback for unsupported platforms\n this.availableDevices = [DEFAULT_DEVICE]\n }\n return this.availableDevices\n } catch (error) {\n this.logger?.error('Failed to get available devices:', error)\n this.availableDevices = [DEFAULT_DEVICE] // Ensure state is updated on error\n return this.availableDevices\n }\n }\n\n /**\n * Get the currently selected audio input device\n * @returns Promise resolving to the current device (conforming to AudioDevice) or null\n */\n async getCurrentDevice(): Promise<AudioDevice | null> {\n try {\n if (Platform.OS === 'web') {\n if (!this.currentDeviceId) {\n // On web, return the typed default device if nothing is selected\n return DEFAULT_DEVICE\n }\n // Refresh web devices to ensure the current one is up-to-date\n const webDevices = await this.getWebAudioDevices()\n return (\n webDevices.find((d) => d.id === this.currentDeviceId) ||\n DEFAULT_DEVICE // Fallback to default if current ID not found\n )\n } else if (ExpoAudioStreamModule.getCurrentInputDevice) {\n // Expecting a single raw device object or null from native\n const rawDevice: any | null =\n await ExpoAudioStreamModule.getCurrentInputDevice()\n // Map to AudioDevice interface if not null\n return rawDevice ? mapRawDeviceToAudioDevice(rawDevice) : null\n } else {\n // Fallback for unsupported platforms\n return DEFAULT_DEVICE\n }\n } catch (error) {\n this.logger?.error('Failed to get current device:', error)\n return DEFAULT_DEVICE // Return default on error\n }\n }\n\n /**\n * Select a specific audio input device for recording\n * @param deviceId The ID of the device to select\n * @returns Promise resolving to a boolean indicating success\n */\n async selectDevice(deviceId: string): Promise<boolean> {\n try {\n let success = false\n if (Platform.OS === 'web') {\n // Check if the device exists before setting it\n const devices = await this.getWebAudioDevices()\n if (devices.some((d) => d.id === deviceId)) {\n this.currentDeviceId = deviceId\n success = true\n } else {\n this.logger?.warn(\n `Web: Device with ID ${deviceId} not found.`\n )\n success = false\n }\n } else if (ExpoAudioStreamModule.selectInputDevice) {\n success =\n await ExpoAudioStreamModule.selectInputDevice(deviceId)\n if (success) {\n this.currentDeviceId = deviceId\n }\n }\n // Refresh devices after selection attempt to update state\n await this.refreshDevices()\n return success\n } catch (error) {\n this.logger?.error('Failed to select device:', error)\n await this.refreshDevices() // Refresh even on error\n return false\n }\n }\n\n /**\n * Reset to the default audio input device\n * @returns Promise resolving to a boolean indicating success\n */\n async resetToDefaultDevice(): Promise<boolean> {\n try {\n let success = false\n if (Platform.OS === 'web') {\n this.currentDeviceId = 'default'\n success = true\n } else if (ExpoAudioStreamModule.resetToDefaultDevice) {\n success = await ExpoAudioStreamModule.resetToDefaultDevice()\n if (success) {\n this.currentDeviceId = null\n }\n }\n // Refresh devices after reset attempt\n await this.refreshDevices()\n return success\n } catch (error) {\n this.logger?.error('Failed to reset to default device:', error)\n await this.refreshDevices() // Refresh even on error\n return false\n }\n }\n\n /**\n * Register a listener for device changes\n * @param listener Function to call when devices change (receives AudioDevice[])\n * @returns Function to remove the listener\n */\n addDeviceChangeListener(\n listener: (devices: AudioDevice[]) => void\n ): () => void {\n this.deviceChangeListeners.add(listener)\n\n // Immediately call listener with current devices if available\n if (this.availableDevices.length > 0) {\n listener([...this.availableDevices])\n }\n\n // Return a function to remove the listener\n return () => {\n this.deviceChangeListeners.delete(listener)\n }\n }\n\n /**\n * Mark a device as temporarily disconnected (for UI filtering)\n * @param deviceId The ID of the device that was disconnected\n * @param notify Whether to notify listeners immediately (default: true)\n */\n markDeviceAsDisconnected(deviceId: string, notify: boolean = true): void {\n this.logger?.debug(\n `Marking device ${deviceId} as temporarily disconnected`\n )\n\n // Clear any existing timeout for this device\n const existingTimeout = this.disconnectionTimeouts.get(deviceId)\n if (existingTimeout) {\n clearTimeout(existingTimeout)\n }\n\n // Add to disconnected set\n this.temporarilyDisconnectedDevices.add(deviceId)\n\n // Set timeout to remove from disconnected set\n const timeout = setTimeout(() => {\n this.logger?.debug(\n `Reconnection timeout expired for device ${deviceId}`\n )\n this.temporarilyDisconnectedDevices.delete(deviceId)\n this.disconnectionTimeouts.delete(deviceId)\n // Refresh devices to show the device again if it's still available\n this.forceRefreshDevices()\n }, this.DISCONNECTION_TIMEOUT_MS)\n\n this.disconnectionTimeouts.set(deviceId, timeout)\n\n // Only notify listeners if requested\n if (notify) {\n this.notifyListeners()\n }\n }\n\n /**\n * Mark a device as reconnected (remove from disconnected set)\n * @param deviceId The ID of the device that was reconnected\n */\n markDeviceAsReconnected(deviceId: string): void {\n this.logger?.debug(`Marking device ${deviceId} as reconnected`)\n\n // Clear timeout and remove from disconnected set\n const timeout = this.disconnectionTimeouts.get(deviceId)\n if (timeout) {\n clearTimeout(timeout)\n this.disconnectionTimeouts.delete(deviceId)\n }\n\n this.temporarilyDisconnectedDevices.delete(deviceId)\n\n // Notify listeners with updated device list\n this.notifyListeners()\n }\n\n /**\n * Get filtered device list (excluding temporarily disconnected devices)\n * @returns Array of available devices excluding temporarily disconnected ones\n */\n private getFilteredDevices(): AudioDevice[] {\n if (this.temporarilyDisconnectedDevices.size === 0) {\n return [...this.availableDevices]\n }\n\n const filtered = this.availableDevices.filter(\n (device) => !this.temporarilyDisconnectedDevices.has(device.id)\n )\n\n this.logger?.debug(\n `Filtered ${this.availableDevices.length - filtered.length} temporarily disconnected devices. ` +\n `Showing ${filtered.length} devices.`\n )\n\n return filtered\n }\n\n /**\n * Get the raw device list (including temporarily disconnected devices)\n * @returns Array of all available devices from native layer\n */\n getRawDevices(): AudioDevice[] {\n return [...this.availableDevices]\n }\n\n /**\n * Get the IDs of temporarily disconnected devices\n * @returns Set of device IDs that are temporarily hidden from UI\n */\n getTemporarilyDisconnectedDeviceIds(): ReadonlySet<string> {\n return new Set(this.temporarilyDisconnectedDevices)\n }\n\n /**\n * Clean up timeouts and listeners (useful for testing or cleanup)\n */\n cleanup(): void {\n // Clear all disconnection timeouts\n this.disconnectionTimeouts.forEach((timeout) => clearTimeout(timeout))\n this.disconnectionTimeouts.clear()\n this.temporarilyDisconnectedDevices.clear()\n\n // Clear device change listeners\n this.deviceChangeListeners.clear()\n\n // Clean up web device listener\n if (Platform.OS === 'web' && this.webDeviceChangeHandler) {\n if (typeof navigator !== 'undefined' && navigator.mediaDevices) {\n navigator.mediaDevices.removeEventListener(\n 'devicechange',\n this.webDeviceChangeHandler\n )\n }\n this.webDeviceChangeHandler = undefined\n }\n\n this.logger?.debug('AudioDeviceManager cleanup completed')\n }\n\n /**\n * Force refresh devices without debouncing (for device events)\n * @returns Promise resolving to the updated device list (AudioDevice[])\n */\n async forceRefreshDevices(): Promise<AudioDevice[]> {\n this.logger?.debug('Force refreshing devices (bypassing debounce)...')\n this.refreshInProgress = true\n try {\n // Force fetch the latest devices from native layer\n const devices = await this.getAvailableDevices({ refresh: true })\n // Update internal state\n this.availableDevices = devices\n // Notify listeners with fresh data\n this.notifyListeners()\n this.lastRefreshTime = Date.now()\n return devices\n } catch (error) {\n this.logger?.error('Error during forceRefreshDevices:', error)\n return this.availableDevices\n } finally {\n this.refreshInProgress = false\n this.logger?.debug('Force refresh finished.')\n }\n }\n\n /**\n * Refresh the list of available devices with debouncing and notify listeners.\n * @returns Promise resolving to the updated device list (AudioDevice[])\n */\n async refreshDevices(): Promise<AudioDevice[]> {\n const now = Date.now()\n\n if (this.refreshInProgress) {\n this.logger?.debug('Refresh already in progress, skipping')\n return this.availableDevices\n }\n\n // Always allow refresh if forced by native event or longer than 2s debounce\n const timeSinceLastRefresh = now - this.lastRefreshTime\n const shouldDebounce =\n timeSinceLastRefresh < this.refreshDebounceMs &&\n timeSinceLastRefresh < 2000\n\n if (shouldDebounce) {\n this.logger?.debug(\n `Refresh debounced, skipping (last refresh was ${timeSinceLastRefresh}ms ago)`\n )\n return this.availableDevices\n }\n\n this.logger?.debug('Refreshing devices...')\n this.refreshInProgress = true\n try {\n // Fetch the latest devices; getAvailableDevices handles mapping now\n const devices = await this.getAvailableDevices({ refresh: true })\n // availableDevices state is updated within getAvailableDevices\n this.notifyListeners() // Notify listeners with the updated list\n this.lastRefreshTime = Date.now()\n return devices // Return the fetched & mapped list\n } catch (error) {\n this.logger?.error('Error during refreshDevices:', error)\n return this.availableDevices // Return potentially stale list on error\n } finally {\n this.refreshInProgress = false\n this.logger?.debug('Refresh finished.')\n }\n }\n\n /**\n * Get audio input devices using the Web Audio API\n * @returns Promise resolving to an array of AudioDevice objects\n */\n private async getWebAudioDevices(): Promise<AudioDevice[]> {\n if (\n typeof navigator === 'undefined' ||\n !navigator.mediaDevices ||\n !navigator.mediaDevices.enumerateDevices\n ) {\n return [DEFAULT_DEVICE]\n }\n\n try {\n const permissionStatus = await this.checkMicrophonePermission()\n\n if (permissionStatus === 'denied') {\n return [\n {\n ...DEFAULT_DEVICE,\n name: 'Microphone Access Denied',\n isAvailable: false,\n },\n ]\n }\n\n if (permissionStatus !== 'granted') {\n try {\n // Requesting stream often reveals device labels\n await navigator.mediaDevices.getUserMedia({ audio: true })\n } catch (error) {\n this.logger?.warn(\n 'Microphone permission request failed:',\n error\n )\n return [\n {\n ...DEFAULT_DEVICE,\n name: 'Microphone Access Required',\n isAvailable: false,\n },\n ]\n }\n }\n\n const devices = await navigator.mediaDevices.enumerateDevices()\n const audioInputDevices = devices\n .filter((device) => device.kind === 'audioinput')\n .map((device) => this.mapWebDeviceToAudioDevice(device))\n\n const hasUnlabeledDevices = audioInputDevices.some(\n (device) =>\n !device.name || device.name.startsWith('Microphone ')\n )\n\n let finalDevices = audioInputDevices\n if (hasUnlabeledDevices && this.isSafariOrIOS()) {\n finalDevices = this.enhanceDevicesForSafari(audioInputDevices)\n }\n\n if (finalDevices.length === 0) {\n finalDevices = [DEFAULT_DEVICE]\n }\n\n this.availableDevices = finalDevices // Update internal state\n return finalDevices\n } catch (error) {\n this.logger?.error('Failed to enumerate web audio devices:', error)\n this.availableDevices = [DEFAULT_DEVICE] // Update state on error\n return this.availableDevices\n }\n }\n\n /**\n * Check the current microphone permission status\n * @returns Permission state ('prompt', 'granted', or 'denied')\n */\n private async checkMicrophonePermission(): Promise<PermissionState> {\n if (!navigator.permissions || !navigator.permissions.query) {\n return 'prompt'\n }\n try {\n const permissionStatus = await navigator.permissions.query({\n name: 'microphone' as PermissionName,\n })\n permissionStatus.onchange = () => {\n // Refresh devices when permission changes\n this.refreshDevices()\n }\n return permissionStatus.state\n } catch (error) {\n this.logger?.warn('Permission query not supported:', error)\n return 'prompt'\n }\n }\n\n /**\n * Setup listener for device changes in web environment\n */\n private setupWebDeviceChangeListener(): void {\n if (\n typeof navigator === 'undefined' ||\n !navigator.mediaDevices ||\n this.webDeviceChangeHandler // Avoid adding multiple listeners\n ) {\n this.logger?.debug(\n 'Web device change listener not available or already set up'\n )\n return\n }\n\n try {\n this.webDeviceChangeHandler = () => {\n this.logger?.debug(\n 'Web device change detected, refreshing device list'\n )\n // Force refresh to get immediate updates\n this.forceRefreshDevices()\n }\n\n navigator.mediaDevices.addEventListener(\n 'devicechange',\n this.webDeviceChangeHandler\n )\n this.logger?.debug('Web device change listener successfully set up')\n } catch (error) {\n this.logger?.warn(\n 'Failed to set up web device change listener:',\n error\n )\n this.webDeviceChangeHandler = undefined\n }\n }\n\n /**\n * Check if the current browser is Safari or iOS WebKit\n */\n private isSafariOrIOS(): boolean {\n if (typeof navigator === 'undefined') return false\n const ua = navigator.userAgent\n return (\n /^((?!chrome|android).)*safari/i.test(ua) ||\n /iPad|iPhone|iPod/.test(ua) ||\n (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)\n )\n }\n\n /**\n * Create enhanced device information for Safari and privacy-restricted browsers\n * @param devices Array of AudioDevice objects, potentially unlabeled\n * @returns Array of enhanced AudioDevice objects\n */\n private enhanceDevicesForSafari(devices: AudioDevice[]): AudioDevice[] {\n const defaultDevice = devices.find((d) => d.isDefault)\n\n if (devices.length <= 1) {\n // Return a typed default device\n return [\n {\n id: defaultDevice?.id || 'default',\n name: 'Microphone (Browser Managed)',\n type: 'builtin_mic',\n isDefault: true,\n isAvailable: true,\n capabilities:\n defaultDevice?.capabilities ||\n DEFAULT_DEVICE.capabilities,\n },\n ]\n }\n\n // Provide more descriptive names for unlabeled devices\n return devices.map((device, index) => {\n if (!device.name || device.name.startsWith('Microphone ')) {\n const deviceTypes = [\n 'Built-in Microphone',\n 'External Microphone',\n 'Headset Microphone',\n ]\n const typeName = deviceTypes[index % deviceTypes.length]\n return {\n ...device,\n name: device.isDefault ? `${typeName} (Default)` : typeName,\n }\n }\n return device\n })\n }\n\n /**\n * Map a Web MediaDeviceInfo to our AudioDevice format\n * @param device The MediaDeviceInfo object from the browser\n * @returns An object conforming to the AudioDevice interface\n */\n private mapWebDeviceToAudioDevice(device: MediaDeviceInfo): AudioDevice {\n const isDefault = device.deviceId === 'default'\n const deviceType = this.inferDeviceType(device.label || '')\n\n // Provide reasonable default capabilities for web devices\n const defaultWebCapabilities: AudioDeviceCapabilities = {\n sampleRates: [16000, 44100, 48000],\n channelCounts: [1, 2],\n bitDepths: [16, 32], // Web Audio uses float32, common PCM might be 16/32\n hasEchoCancellation: true, // Often handled by browser\n hasNoiseSuppression: true, // Often handled by browser\n hasAutomaticGainControl: true, // Often handled by browser\n }\n\n return {\n id: device.deviceId,\n name:\n device.label || `Microphone ${device.deviceId.substring(0, 8)}`,\n type: deviceType,\n isDefault,\n isAvailable: true, // Assume available if enumerated\n capabilities: defaultWebCapabilities,\n }\n }\n\n /**\n * Try to infer the device type from its name\n * @param deviceName The label of the device\n * @returns A string representing the inferred device type\n */\n private inferDeviceType(deviceName: string): string {\n const name = deviceName.toLowerCase()\n if (name.includes('bluetooth') || name.includes('airpods'))\n return 'bluetooth'\n if (name.includes('usb')) return 'usb'\n if (name.includes('headphone') || name.includes('headset')) {\n return name.includes('wired') ? 'wired_headset' : 'wired_headphones'\n }\n if (name.includes('speaker')) return 'speaker'\n return 'builtin_mic' // Default assumption\n }\n\n /**\n * Notify all registered listeners about device changes.\n */\n notifyListeners(): void {\n // Pass a copy of the filtered devices array to listeners\n const devicesCopy = this.getFilteredDevices()\n\n this.logger?.debug(\n `Notifying ${this.deviceChangeListeners.size} listeners with ${devicesCopy.length} devices ` +\n `(${this.temporarilyDisconnectedDevices.size} temporarily hidden)`\n )\n\n this.deviceChangeListeners.forEach((listener) => {\n try {\n listener(devicesCopy)\n } catch (error) {\n this.logger?.error('Error in device change listener:', error)\n }\n })\n }\n}\n\n// Create and export the singleton instance\nexport const audioDeviceManager = new AudioDeviceManager()\n\nexport { DeviceDisconnectionBehavior }\n"]}
@@ -1,4 +1,4 @@
1
- import { useCallback, useEffect, useState } from 'react';
1
+ import { useCallback, useEffect, useState, useId } from 'react';
2
2
  import { audioDeviceManager } from '../AudioDeviceManager';
3
3
  /**
4
4
  * React hook for managing audio input devices
@@ -8,6 +8,8 @@ export function useAudioDevices() {
8
8
  const [currentDevice, setCurrentDevice] = useState(null);
9
9
  const [loading, setLoading] = useState(true);
10
10
  const [error, setError] = useState(null);
11
+ // Generate unique instance ID for debugging
12
+ const instanceId = useId().replace(/:/g, '').slice(0, 5);
11
13
  // Load devices on mount
12
14
  useEffect(() => {
13
15
  let isMounted = true;
@@ -25,7 +27,9 @@ export function useAudioDevices() {
25
27
  setCurrentDevice(device);
26
28
  }
27
29
  catch (err) {
28
- console.error('Failed to load audio devices:', err);
30
+ audioDeviceManager
31
+ .getLogger()
32
+ ?.error('Failed to load audio devices:', err);
29
33
  if (isMounted)
30
34
  setError(err instanceof Error
31
35
  ? err
@@ -39,16 +43,23 @@ export function useAudioDevices() {
39
43
  loadDevices();
40
44
  // Set up device change listener
41
45
  const removeListener = audioDeviceManager.addDeviceChangeListener((updatedDevices) => {
46
+ audioDeviceManager
47
+ .getLogger()
48
+ ?.debug(`🎛️ useAudioDevices [${instanceId}] received device change. Count: ${updatedDevices.length}`);
42
49
  if (isMounted) {
43
50
  setDevices(updatedDevices);
44
51
  // If our current device is no longer available, update it
45
52
  if (currentDevice &&
46
53
  !updatedDevices.some((d) => d.id === currentDevice.id)) {
54
+ audioDeviceManager
55
+ .getLogger()
56
+ ?.debug(`🎛️ useAudioDevices [${instanceId}] Current device ${currentDevice.id} no longer available, updating`);
47
57
  audioDeviceManager
48
58
  .getCurrentDevice()
49
59
  .then((newDevice) => {
50
- if (isMounted)
60
+ if (isMounted) {
51
61
  setCurrentDevice(newDevice);
62
+ }
52
63
  });
53
64
  }
54
65
  }
@@ -76,7 +87,9 @@ export function useAudioDevices() {
76
87
  return success;
77
88
  }
78
89
  catch (err) {
79
- console.error('Failed to select audio device:', err);
90
+ audioDeviceManager
91
+ .getLogger()
92
+ ?.error('Failed to select audio device:', err);
80
93
  setError(err instanceof Error
81
94
  ? err
82
95
  : new Error('Failed to select audio device'));
@@ -103,7 +116,9 @@ export function useAudioDevices() {
103
116
  return success;
104
117
  }
105
118
  catch (err) {
106
- console.error('Failed to reset to default audio device:', err);
119
+ audioDeviceManager
120
+ .getLogger()
121
+ ?.error('Failed to reset to default audio device:', err);
107
122
  setError(err instanceof Error
108
123
  ? err
109
124
  : new Error('Failed to reset to default audio device'));
@@ -128,7 +143,9 @@ export function useAudioDevices() {
128
143
  return updatedDevices;
129
144
  }
130
145
  catch (err) {
131
- console.error('Failed to refresh audio devices:', err);
146
+ audioDeviceManager
147
+ .getLogger()
148
+ ?.error('Failed to refresh audio devices:', err);
132
149
  setError(err instanceof Error
133
150
  ? err
134
151
  : new Error('Failed to refresh audio devices'));
@@ -138,6 +155,13 @@ export function useAudioDevices() {
138
155
  setLoading(false);
139
156
  }
140
157
  }, []);
158
+ /**
159
+ * Initialize device detection
160
+ * Useful for restarting device detection if it failed initially
161
+ */
162
+ const initializeDeviceDetection = useCallback(() => {
163
+ audioDeviceManager.initializeDeviceDetection();
164
+ }, []);
141
165
  return {
142
166
  devices,
143
167
  currentDevice,
@@ -146,6 +170,7 @@ export function useAudioDevices() {
146
170
  selectDevice,
147
171
  resetToDefaultDevice,
148
172
  refreshDevices,
173
+ initializeDeviceDetection,
149
174
  };
150
175
  }
151
176
  //# sourceMappingURL=useAudioDevices.js.map