Tutorial

DeviceCheck

DeviceCheck API Documentation

Overview

The DeviceCheck class is designed to validate audio and video devices to ensure they are functional and meet the requirements for real-time communication. It also provides methods to create test media streams for verification purposes.

Constructor

const deviceCheck = new DeviceCheck({
  audioInputDevices: [], // Array of audio input devices
  audioOutputDevices: [], // Array of audio output devices
  videoInputDevices: []  // Array of video input devices
});

Parameters

  • audioInputDevices (Array): List of available audio input devices.
  • audioOutputDevices (Array): List of available audio output devices.
  • videoInputDevices (Array): List of available video input devices.

Methods

setDontCheckAudioOutput(value)

Sets whether to skip checking audio output devices.

Parameters

  • value (Boolean): If true, audio output devices will not be checked.

Example

deviceCheck.setDontCheckAudioOutput(true);

checkAll(options)

Validates all specified devices and ensures they are functional.

Parameters

  • options (Object):
    • audioInputDeviceId (String): ID of the audio input device.
    • audioOutputDevicesId (String): ID of the audio output device.
    • videoInputDeviceId (String): ID of the video input device.

Returns

  • Object: Results of the validation.
    • valid (Boolean): Whether all devices are valid and functional.
    • reason (String): Reason for failure, if any.

Example

const results = await deviceCheck.checkAll({
  audioInputDeviceId: 'mic-id',
  audioOutputDevicesId: 'speaker-id',
  videoInputDeviceId: 'camera-id'
});

if (results.valid) {
  console.log('All devices are functional');
} else {
  console.error('Device check failed:', results.reason);
}

check(options)

Checks the existence of specified devices.

Parameters

  • options (Object):
    • audioInputDeviceId (String): ID of the audio input device.
    • audioOutputDevicesId (String): ID of the audio output device.
    • videoInputDeviceId (String): ID of the video input device.

Returns

  • Object: Results of the check.
    • valid (Boolean): Whether the devices exist.
    • reason (String): Reason for failure, if any.

Example

const results = await deviceCheck.check({
  audioInputDeviceId: 'mic-id',
  audioOutputDevicesId: 'speaker-id',
  videoInputDeviceId: 'camera-id'
});

if (results.valid) {
  console.log('Devices exist');
} else {
  console.error('Device check failed:', results.reason);
}

verifyAudioInputDevice(audioInputDeviceId)

Verifies the functionality of an audio input device by attempting to create a media stream.

Parameters

  • audioInputDeviceId (String): ID of the audio input device.

Returns

  • Boolean: Whether the device is functional.

Example

const isWorking = await deviceCheck.verifyAudioInputDevice('mic-id');
console.log('Audio input device working:', isWorking);

verifyVideoInputDevice(videoInputDeviceId)

Verifies the functionality of a video input device by attempting to create a media stream.

Parameters

  • videoInputDeviceId (String): ID of the video input device.

Returns

  • Boolean: Whether the device is functional.

Example

const isWorking = await deviceCheck.verifyVideoInputDevice('camera-id');
console.log('Video input device working:', isWorking);

verifyAudioOutputDevice(audioOutputDeviceId)

Verifies the functionality of an audio output device by attempting to set it as the sink for an audio element.

Parameters

  • audioOutputDeviceId (String): ID of the audio output device.

Returns

  • Boolean: Whether the device is functional.

Example

const isWorking = await deviceCheck.verifyAudioOutputDevice('speaker-id');
console.log('Audio output device working:', isWorking);

createAudioInputMediaStream(options)

Creates a media stream for the specified audio input device.

Parameters

  • options (Object):
    • audioInputDeviceId (String): ID of the audio input device.

Returns

  • MediaStream: The created media stream.

Example

const stream = await deviceCheck.createAudioInputMediaStream({
  audioInputDeviceId: 'mic-id'
});
console.log('Audio input stream created:', stream);

createVideoInputMediaStream(options)

Creates a media stream for the specified video input device and attaches it to a video element.

Parameters

  • options (Object):
    • videoElementId (String): ID of the video element.
    • videoInputDeviceId (String): ID of the video input device.

Returns

  • MediaStream: The created media stream.

Example

const stream = await deviceCheck.createVideoInputMediaStream({
  videoElementId: 'video-preview',
  videoInputDeviceId: 'camera-id'
});
console.log('Video input stream created:', stream);

stopAudioInputMediaStream()

Stops the currently active audio input media stream.

Example

deviceCheck.stopAudioInputMediaStream();
console.log('Audio input stream stopped');

stopVideoInputMediaStream()

Stops the currently active video input media stream.

Example

deviceCheck.stopVideoInputMediaStream();
console.log('Video input stream stopped');

Complete Example

async function testDevices() {
  const deviceCheck = new DeviceCheck({
    audioInputDevices: await navigator.mediaDevices.enumerateDevices(),
    audioOutputDevices: await navigator.mediaDevices.enumerateDevices(),
    videoInputDevices: await navigator.mediaDevices.enumerateDevices()
  });

  const results = await deviceCheck.checkAll({
    audioInputDeviceId: 'mic-id',
    audioOutputDevicesId: 'speaker-id',
    videoInputDeviceId: 'camera-id'
  });

  if (results.valid) {
    console.log('All devices are functional');
  } else {
    console.error('Device check failed:', results.reason);
  }
}

testDevices();