@4players/odin
Version:
A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects
128 lines (127 loc) • 5.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceManager = void 0;
const api_1 = require("../api");
const helpers_1 = require("./helpers");
const audio_input_1 = require("../api/media/audio-input");
const video_input_1 = require("../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.
*/
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) {
const plugin = await (0, api_1.ensurePlugin)();
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) {
const plugin = await (0, api_1.ensurePlugin)();
let parameters = (0, helpers_1.generateCaptureParameters)(device, settings);
if (customType) {
parameters = {
...parameters,
customType,
};
}
const audioCapture = await plugin.createAudioCapture(parameters);
return new audio_input_1.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 {CreateVideoCaptureParameters} [parameters] - Optional configuration parameters for the VideoInput.
* @return {Promise<VideoInput>} A promise that resolves to the created VideoInput instance.
*/
static async createVideoInput(ms, parameters) {
const plugin = await (0, api_1.ensurePlugin)();
const capture = await plugin.createVideoCapture(ms, parameters);
return new video_input_1.VideoInput(capture);
}
}
exports.DeviceManager = DeviceManager;