Source: lib/getDevices.js

var Janus = require('../externals/janus.nojquery');
var Promise = require('bluebird/js/release/bluebird');

/**
 * Gets the list of available media devices (Audio and Video) in the browser/app
 * @class
 * @classdesc Get Devices
 * @param  {object} config getUserMedia constraints
 * @return {Promise<ListDevices>}
 * @example
 * usecases.getDevices({audio: true, video: true})
 *     .then((devices) => {
 *         console.log(devices.videoDevices); // [{label: 'Camera Front', id: *****}]
 *         console.log(devices.audioDevices); // [{label: 'Microphone', id: *****}]
 *     })
 */
var getDevices = function(config) {
    if (config == null) config = { audio: true, video: true };
    return new Promise(function (resolve, reject) {
        Janus.listDevices(function(devices) {
            var audioDevices = [];
            var videoDevices = [];
            devices.forEach(function(device) {
                var label = device.label;
                if(label === null || label === undefined || label === "") label = "";
                if(device.kind === 'audioinput') {
                    if(label === "") label = 'Audio ' + (audioDevices.length + 1);
                    audioDevices.push({
                        label: label,
                        id: device.deviceId
                    });
                }
                if(device.kind === 'videoinput') {
                    if(label === "") label = 'Camera ' + (videoDevices.length + 1);
                    videoDevices.push({
                        label: label,
                        id: device.deviceId
                    });
                }
        	});
            resolve({
                audioDevices: audioDevices,
                videoDevices: videoDevices
            });
        }, config);
    });
};

exports.getDevices = getDevices;