You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reducer.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {
  2. SET_AUDIO_INPUT_DEVICE,
  3. SET_VIDEO_INPUT_DEVICE,
  4. UPDATE_DEVICE_LIST
  5. } from './actionTypes';
  6. import { ReducerRegistry } from '../redux';
  7. const DEFAULT_STATE = {
  8. audioInput: [],
  9. audioOutput: [],
  10. videoInput: []
  11. };
  12. /**
  13. * Listen for actions which changes the state of known and used devices.
  14. *
  15. * @param {Object} state - The Redux state of the feature features/base/devices.
  16. * @param {Object} action - Action object.
  17. * @param {string} action.type - Type of action.
  18. * @param {Array<MediaDeviceInfo>} action.devices - All available audio and
  19. * video devices.
  20. * @returns {Object}
  21. */
  22. ReducerRegistry.register(
  23. 'features/base/devices',
  24. (state = DEFAULT_STATE, action) => {
  25. switch (action.type) {
  26. case UPDATE_DEVICE_LIST: {
  27. const deviceList = _groupDevicesByKind(action.devices);
  28. return {
  29. ...deviceList
  30. };
  31. }
  32. // TODO: Changing of current audio and video device id is currently
  33. // handled outside of react/redux. Fall through to default logic for
  34. // now.
  35. case SET_AUDIO_INPUT_DEVICE:
  36. case SET_VIDEO_INPUT_DEVICE:
  37. default:
  38. return state;
  39. }
  40. });
  41. /**
  42. * Converts an array of media devices into an object organized by device kind.
  43. *
  44. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  45. * @private
  46. * @returns {Object} An object with the media devices split by type. The keys
  47. * are device type and the values are arrays with devices matching the device
  48. * type.
  49. */
  50. function _groupDevicesByKind(devices) {
  51. return {
  52. audioInput: devices.filter(device => device.kind === 'audioinput'),
  53. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  54. videoInput: devices.filter(device => device.kind === 'videoinput')
  55. };
  56. }