Class

DeviceCheck

DeviceCheck(options)

Constructor

# new DeviceCheck(options)

DeviceCheck is a utility class for validating and testing audio and video devices in the browser. It can check device existence, verify device functionality, and create test media streams for user feedback. Uses centralized MediaDeviceManager for cross-browser compatibility.

Parameters:
Name Type Description
options Object

Configuration options

audioInputDevices Array

Array of available audio input devices (optional, will enumerate if not provided)

audioOutputDevices Array

Array of available audio output devices (optional, will enumerate if not provided)

videoInputDevices Array

Array of available video input devices (optional, will enumerate if not provided)

debug boolean

Enable debug logging

Example
const deviceCheck = new DeviceCheck({
  audioInputDevices: [...],  // optional
  audioOutputDevices: [...], // optional
  videoInputDevices: [...]   // optional
});

// Check all devices
const result = await deviceCheck.checkAll({
  audioInputDeviceId: 'mic-id',
  audioOutputDeviceId: 'speaker-id',
  videoInputDeviceId: 'camera-id'
});
if (result.valid) { ... }

Methods

# async _getUserMediaWithFallback(constraints) → {Promise.<MediaStream>}

Helper method to get media stream with device fallback for cross-browser compatibility. Uses centralized MediaDeviceManager for consistent behavior.

Parameters:
Name Type Description
constraints Object

Media constraints

  • The media stream
Promise.<MediaStream>

# async check(options) → {Promise.<Object>}

Check if the specified device IDs exist in the provided device lists.

Parameters:
Name Type Description
options Object
audioInputDeviceId String

ID of the audio input device

audioOutputDeviceId String

ID of the audio output device

videoInputDeviceId String

ID of the video input device

  • Object with validity and reason fields
Promise.<Object>
Example
// Quick existence check without functionality verification
const { valid, audioInputDevice, videoDevice } = await deviceCheck.check({
  audioInputDeviceId: 'mic123',
  videoInputDeviceId: 'camera789'
});

# async checkAll(options) → {Promise.<Object>}

Check the existence and functionality of all specified devices.

Parameters:
Name Type Description
options Object
audioInputDeviceId String

ID of the audio input device to check

audioOutputDeviceId String

ID of the audio output device to check

videoInputDeviceId String

ID of the video input device to check

  • Object with validity and reason fields
Promise.<Object>
Example
const result = await deviceCheck.checkAll({
  audioInputDeviceId: 'mic123',
  audioOutputDeviceId: 'speaker456',
  videoInputDeviceId: 'camera789'
});
if (!result.valid) {
  alert('Device check failed: ' + result.reason);
}

# async createAudioInputMediaStream(options) → {Promise.<MediaStream>}

Create a test audio input media stream from the specified device.

Parameters:
Name Type Description
options Object
audioInputDeviceId String

ID of the audio input device

  • The created audio media stream
Promise.<MediaStream>
Example
// Create stream for audio level visualization
const stream = await deviceCheck.createAudioInputMediaStream({
  audioInputDeviceId: 'mic123'
});
// Use stream for audio processing...
// When done:
deviceCheck.stopAudioInputMediaStream();

# async createVideoInputMediaStream(options) → {Promise.<MediaStream>}

Create a test video input media stream from the specified device and attach it to a video element.

Parameters:
Name Type Description
options Object
videoElementId String

ID of the video element to attach the stream to

videoInputDeviceId String

ID of the video input device

  • The created video media stream
Promise.<MediaStream>
Example
// Preview camera in a video element
const stream = await deviceCheck.createVideoInputMediaStream({
  videoElementId: 'preview-video',
  videoInputDeviceId: 'camera789'
});
// When done:
deviceCheck.stopVideoInputMediaStream();

# async enumerateDevices() → {Promise.<Array>}

Enumerate all available media devices (audio input, audio output, video input).

  • Array of device info objects (deviceId, kind, label)
Promise.<Array>
Example
const devices = await deviceCheck.enumerateDevices();
devices.forEach(d => console.log(d.kind, d.label));

# async getAudioInputDevices() → {Promise.<Array>}

Get all available audio input devices.

  • Array of audio input device info
Promise.<Array>
Example
const mics = await deviceCheck.getAudioInputDevices();
console.log('Available microphones:', mics.map(m => m.label));

# async getAudioOutputDevices() → {Promise.<Array>}

Get all available audio output devices.

  • Array of audio output device info
Promise.<Array>
Example
const speakers = await deviceCheck.getAudioOutputDevices();
console.log('Available speakers:', speakers.map(s => s.label));

# async getDefaultAudioInputDeviceId(options) → {Promise.<(String|undefined)>}

Get the default audio input device ID, optionally using a localStorage key.

Parameters:
Name Type Description
options Object
localStorageKey String

Key to retrieve the saved device ID from localStorage

  • The default or saved audio input device ID
Promise.<(String|undefined)>
Example
const micId = await deviceCheck.getDefaultAudioInputDeviceId({
  localStorageKey: 'preferredMic'
});

# async getDefaultAudioOutputDeviceId(options) → {Promise.<(String|undefined)>}

Get the default audio output device ID, optionally using a localStorage key.

Parameters:
Name Type Description
options Object
localStorageKey String

Key to retrieve the saved device ID from localStorage

  • The default or saved audio output device ID
Promise.<(String|undefined)>
Example
const speakerId = await deviceCheck.getDefaultAudioOutputDeviceId({
  localStorageKey: 'preferredSpeaker'
});

# async getDefaultVideoDeviceId(options) → {Promise.<(String|undefined)>}

Get the default video input device ID, optionally using a localStorage key.

Parameters:
Name Type Description
options Object
localStorageKey String

Key to retrieve the saved device ID from localStorage

  • The default or saved video input device ID
Promise.<(String|undefined)>
Example
const cameraId = await deviceCheck.getDefaultVideoDeviceId({
  localStorageKey: 'preferredCamera'
});

# async getVideoDevices() → {Promise.<Array>}

Get all available video input devices.

  • Array of video input device info
Promise.<Array>
Example
const cameras = await deviceCheck.getVideoDevices();
console.log('Available cameras:', cameras.map(c => c.label));

# setDontCheckAudioOutput(value)

Set whether to skip checking audio output devices (for browsers without setSinkId support).

Parameters:
Name Type Description
value Boolean

If true, audio output devices will not be checked.

Example
// Skip audio output check for Safari
if (!window.HTMLMediaElement.prototype.setSinkId) {
  deviceCheck.setDontCheckAudioOutput(true);
}

# stopAudioInputMediaStream()

Stop the currently active audio input media stream.

Example
deviceCheck.stopAudioInputMediaStream();

# stopVideoInputMediaStream()

Stop the currently active video input media stream.

Example
deviceCheck.stopVideoInputMediaStream();

# async verifyAudioInputDevice(audioInputDeviceId) → {Promise.<Boolean>}

Verify that the audio input device is functional by attempting to create a media stream.

Parameters:
Name Type Description
audioInputDeviceId String

ID of the audio input device

  • True if the device is functional, otherwise throws an error
Promise.<Boolean>
Example
try {
  const working = await deviceCheck.verifyAudioInputDevice('mic123');
  console.log('Microphone working:', working);
} catch (err) {
  console.error('Microphone error:', err.message);
}

# async verifyAudioOutputDevice(audioOutputDeviceId) → {Promise.<Boolean>}

Verify that the audio output device is functional by setting it as the sink for an audio element.

Parameters:
Name Type Description
audioOutputDeviceId String

ID of the audio output device

  • True if the device is functional, otherwise throws an error
Promise.<Boolean>
Example
try {
  const working = await deviceCheck.verifyAudioOutputDevice('speaker456');
  console.log('Speaker working:', working);
} catch (err) {
  console.error('Speaker error:', err.message);
}

# async verifyVideoInputDevice(videoInputDeviceId) → {Promise.<Boolean>}

Verify that the video input device is functional by attempting to create a media stream.

Parameters:
Name Type Description
videoInputDeviceId String

ID of the video input device

  • True if the device is functional, otherwise throws an error
Promise.<Boolean>
Example
try {
  const working = await deviceCheck.verifyVideoInputDevice('camera789');
  console.log('Camera working:', working);
} catch (err) {
  console.error('Camera error:', err.message);
}