2020-05-20 10:57:03 +00:00
|
|
|
import { ReducerRegistry } from '../redux';
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
import {
|
2019-03-27 17:30:56 +00:00
|
|
|
ADD_PENDING_DEVICE_REQUEST,
|
|
|
|
REMOVE_PENDING_DEVICE_REQUESTS,
|
2017-03-29 17:43:30 +00:00
|
|
|
SET_AUDIO_INPUT_DEVICE,
|
|
|
|
SET_VIDEO_INPUT_DEVICE,
|
|
|
|
UPDATE_DEVICE_LIST
|
|
|
|
} from './actionTypes';
|
2019-03-25 11:33:41 +00:00
|
|
|
import { groupDevicesByKind } from './functions';
|
2020-03-06 13:56:46 +00:00
|
|
|
import logger from './logger';
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
const DEFAULT_STATE = {
|
2019-03-28 16:29:30 +00:00
|
|
|
availableDevices: {
|
2019-03-28 12:21:38 +00:00
|
|
|
audioInput: [],
|
|
|
|
audioOutput: [],
|
|
|
|
videoInput: []
|
|
|
|
},
|
2019-03-27 17:30:56 +00:00
|
|
|
pendingRequests: []
|
2017-03-29 17:43:30 +00:00
|
|
|
};
|
|
|
|
|
2020-03-06 13:56:46 +00:00
|
|
|
/**
|
|
|
|
* Logs the current device list.
|
|
|
|
*
|
|
|
|
* @param {Object} deviceList - Whatever is returned by {@link groupDevicesByKind}.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function logDeviceList(deviceList) {
|
|
|
|
const devicesToStr = list => list.map(device => `\t\t${device.label}[${device.deviceId}]`).join('\n');
|
|
|
|
const audioInputs = devicesToStr(deviceList.audioInput);
|
|
|
|
const audioOutputs = devicesToStr(deviceList.audioOutput);
|
|
|
|
const videoInputs = devicesToStr(deviceList.videoInput);
|
|
|
|
|
|
|
|
logger.debug('Device list updated:\n'
|
|
|
|
+ `audioInput:\n${audioInputs}\n`
|
|
|
|
+ `audioOutput:\n${audioOutputs}\n`
|
|
|
|
+ `videoInput:\n${videoInputs}`);
|
|
|
|
}
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* Listen for actions which changes the state of known and used devices.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state of the feature features/base/devices.
|
|
|
|
* @param {Object} action - Action object.
|
|
|
|
* @param {string} action.type - Type of action.
|
|
|
|
* @param {Array<MediaDeviceInfo>} action.devices - All available audio and
|
|
|
|
* video devices.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
ReducerRegistry.register(
|
|
|
|
'features/base/devices',
|
|
|
|
(state = DEFAULT_STATE, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case UPDATE_DEVICE_LIST: {
|
2019-03-25 11:33:41 +00:00
|
|
|
const deviceList = groupDevicesByKind(action.devices);
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2020-03-06 13:56:46 +00:00
|
|
|
logDeviceList(deviceList);
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
return {
|
2019-03-28 12:21:38 +00:00
|
|
|
...state,
|
2019-03-28 16:29:30 +00:00
|
|
|
availableDevices: deviceList
|
2017-03-29 17:43:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:30:56 +00:00
|
|
|
case ADD_PENDING_DEVICE_REQUEST:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
pendingRequests: [
|
|
|
|
...state.pendingRequests,
|
|
|
|
action.request
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
case REMOVE_PENDING_DEVICE_REQUESTS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
pendingRequests: [ ]
|
|
|
|
};
|
|
|
|
|
2020-03-06 13:56:46 +00:00
|
|
|
// TODO: Changing of current audio and video device id is currently handled outside of react/redux.
|
|
|
|
case SET_AUDIO_INPUT_DEVICE: {
|
|
|
|
logger.debug(`set audio input device: ${action.deviceId}`);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
case SET_VIDEO_INPUT_DEVICE: {
|
|
|
|
logger.debug(`set video input device: ${action.deviceId}`);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
2017-03-29 17:43:30 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|