@4players/odin
Version:
A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects
124 lines (123 loc) • 5.75 kB
JavaScript
import { PLUGIN } from '../api';
import { assert, generateCaptureParameters } from './helpers';
import { AudioInput } from '../api/media/audio-input';
import { VideoInput } from '../api/media/video-input';
/**
* The DeviceManager class provides a collection of static methods to interact with audio and video devices.
* It includes capabilities for listing available input/output devices and creating audio or video input instances.
*/
export class DeviceManager {
static async listAudioInputs() {
return this.listDevices('AudioCapture');
}
/**
* Retrieves a list of audio output devices available on the system.
*
* @return {Promise<Device[]>} A promise that resolves to an array of Device objects representing the audio output devices.
*/
static async listAudioOutputs() {
return this.listDevices('AudioPlayback');
}
/**
* Lists available devices, optionally filtered by the specified kind.
*
* @param {DeviceKinds} [kind] - The type of devices to filter. If undefined, all devices will be listed.
* @return {Promise<Device[]>} A promise that resolves to an array of devices matching the specified kind.
*/
static async listDevices(kind) {
assert(PLUGIN, 'Please init the Odin WebSDK with a plugin first.');
try {
return (await PLUGIN.enumerateDevices()).filter((device) => {
if (kind && device.type === kind) {
return true;
}
return typeof kind === 'undefined';
});
}
catch (err) {
throw new Error(`Could not fetch the userMedia\n${err}\n`);
}
}
/**
* Retrieves an input device by its name from the list of available audio input devices.
*
* @param {string} name - The name of the input device to search for.
* @return {Promise<Device | undefined>} A promise that resolves to the input device object if found, or undefined if no matching device is found.
*/
static async getInputDeviceByName(name) {
const devices = await this.listAudioInputs();
return devices.find((device) => device.name === name);
}
/**
* Retrieves an audio output device by its name.
*
* @param {string} name - The name of the desired audio output device.
* @return {Promise<Device | undefined>} A promise that resolves to the device object if a match is found, or undefined if no matching device is found.
*/
static async getOutputDeviceByName(name) {
const devices = await this.listAudioOutputs();
return devices.find((device) => device.name === name);
}
/**
* Retrieves an input device by its id from the list of available audio input devices.
*
* @param {string} id - The id of the input device to search for.
* @return {Promise<Device | undefined>} A promise that resolves to the input device object if found, or undefined if no matching device is found.
*/
static async getInputDevice(id) {
const devices = await this.listAudioInputs();
return devices.find((device) => device.id === id);
}
/**
* Retrieves an audio output device by its id.
*
* @param {string} id - The id of the desired audio output device.
* @return {Promise<Device | undefined>} A promise that resolves to the device object if a match is found, or undefined if no matching device is found.
*/
static async getOutputDevice(id) {
const devices = await this.listAudioOutputs();
return devices.find((device) => device.id === id);
}
/**
* Retrieves an audio device by its id.
*
* @param {string} id - The id of the desired audio device.
* @return {Promise<Device | undefined>} A promise that resolves to the device object if a match is found, or undefined if no matching device is found.
*/
static async getDevice(id) {
const devices = await this.listDevices();
return devices.find((device) => device.id === id);
}
/**
* Creates an audio input instance using the specified device parameters and input settings.
*
* @param {DeviceParameters} [device] - Optional parameters for selecting the audio capture device.
* @param {InputSettings} [settings] - Optional configuration settings for the audio input.
* @param {string} [customType] - Optional custom type for the audio input that's helping identify different AudioInputs.
* @return {Promise<AudioInput>} A promise that resolves to an instance of AudioInput.
*/
static async createAudioInput(device, settings, customType) {
assert(PLUGIN, "Can't create an AudioInput, no AudioPlugin initialized");
let parameters = generateCaptureParameters(device, settings);
if (customType) {
parameters = {
...parameters,
customType,
};
}
const audioCapture = await PLUGIN.createAudioCapture(parameters);
return new AudioInput(settings ?? {}, audioCapture);
}
/**
* Creates a new VideoInput instance from the given MediaStream and optional configuration.
*
* @param {MediaStream} ms - The MediaStream instance used to initialize the VideoInput.
* @param {VideoInputOptions} [options] - Optional configuration parameters for the VideoInput.
* @return {Promise<VideoInput>} A promise that resolves to the created VideoInput instance.
*/
static async createVideoInput(ms, options) {
assert(PLUGIN, "Can't create an VideoInput, no Plugin initialized");
const capture = await PLUGIN.createVideoCapture(ms, options);
return new VideoInput(capture);
}
}