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 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { ReducerRegistry } from '../redux';
  2. import {
  3. ADD_PENDING_DEVICE_REQUEST,
  4. REMOVE_PENDING_DEVICE_REQUESTS,
  5. SET_AUDIO_INPUT_DEVICE,
  6. SET_VIDEO_INPUT_DEVICE,
  7. UPDATE_DEVICE_LIST
  8. } from './actionTypes';
  9. import { groupDevicesByKind } from './functions';
  10. import logger from './logger';
  11. const DEFAULT_STATE = {
  12. availableDevices: {
  13. audioInput: [],
  14. audioOutput: [],
  15. videoInput: []
  16. },
  17. pendingRequests: []
  18. };
  19. /**
  20. * Logs the current device list.
  21. *
  22. * @param {Object} deviceList - Whatever is returned by {@link groupDevicesByKind}.
  23. * @returns {string}
  24. */
  25. function logDeviceList(deviceList) {
  26. const devicesToStr = list => list.map(device => `\t\t${device.label}[${device.deviceId}]`).join('\n');
  27. const audioInputs = devicesToStr(deviceList.audioInput);
  28. const audioOutputs = devicesToStr(deviceList.audioOutput);
  29. const videoInputs = devicesToStr(deviceList.videoInput);
  30. logger.debug('Device list updated:\n'
  31. + `audioInput:\n${audioInputs}\n`
  32. + `audioOutput:\n${audioOutputs}\n`
  33. + `videoInput:\n${videoInputs}`);
  34. }
  35. /**
  36. * Listen for actions which changes the state of known and used devices.
  37. *
  38. * @param {Object} state - The Redux state of the feature features/base/devices.
  39. * @param {Object} action - Action object.
  40. * @param {string} action.type - Type of action.
  41. * @param {Array<MediaDeviceInfo>} action.devices - All available audio and
  42. * video devices.
  43. * @returns {Object}
  44. */
  45. ReducerRegistry.register(
  46. 'features/base/devices',
  47. (state = DEFAULT_STATE, action) => {
  48. switch (action.type) {
  49. case UPDATE_DEVICE_LIST: {
  50. const deviceList = groupDevicesByKind(action.devices);
  51. logDeviceList(deviceList);
  52. return {
  53. ...state,
  54. availableDevices: deviceList
  55. };
  56. }
  57. case ADD_PENDING_DEVICE_REQUEST:
  58. return {
  59. ...state,
  60. pendingRequests: [
  61. ...state.pendingRequests,
  62. action.request
  63. ]
  64. };
  65. case REMOVE_PENDING_DEVICE_REQUESTS:
  66. return {
  67. ...state,
  68. pendingRequests: [ ]
  69. };
  70. // TODO: Changing of current audio and video device id is currently handled outside of react/redux.
  71. case SET_AUDIO_INPUT_DEVICE: {
  72. logger.debug(`set audio input device: ${action.deviceId}`);
  73. return state;
  74. }
  75. case SET_VIDEO_INPUT_DEVICE: {
  76. logger.debug(`set video input device: ${action.deviceId}`);
  77. return state;
  78. }
  79. default:
  80. return state;
  81. }
  82. });