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.

actions.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import JitsiMeetJS from '../lib-jitsi-meet';
  2. import { updateSettings } from '../settings';
  3. import {
  4. ADD_PENDING_DEVICE_REQUEST,
  5. REMOVE_PENDING_DEVICE_REQUESTS,
  6. SET_AUDIO_INPUT_DEVICE,
  7. SET_VIDEO_INPUT_DEVICE,
  8. UPDATE_DEVICE_LIST
  9. } from './actionTypes';
  10. import {
  11. areDeviceLabelsInitialized,
  12. getDeviceIdByLabel,
  13. getDevicesFromURL
  14. } from './functions';
  15. /**
  16. * Adds a pending device request.
  17. *
  18. * @param {Object} request - The request to be added.
  19. * @returns {{
  20. * type: ADD_PENDING_DEVICE_REQUEST,
  21. * request: Object
  22. * }}
  23. */
  24. export function addPendingDeviceRequest(request) {
  25. return {
  26. type: ADD_PENDING_DEVICE_REQUEST,
  27. request
  28. };
  29. }
  30. /**
  31. * Configures the initial A/V devices before the conference has started.
  32. *
  33. * @returns {Function}
  34. */
  35. export function configureInitialDevices() {
  36. return (dispatch, getState) => new Promise(resolve => {
  37. const deviceLabels = getDevicesFromURL(getState());
  38. if (deviceLabels) {
  39. dispatch(getAvailableDevices()).then(() => {
  40. const state = getState();
  41. if (!areDeviceLabelsInitialized(state)) {
  42. // The labels are not available if the A/V permissions are
  43. // not yet granted.
  44. Object.keys(deviceLabels).forEach(key => {
  45. dispatch(addPendingDeviceRequest({
  46. type: 'devices',
  47. name: 'setDevice',
  48. device: {
  49. kind: key.toLowerCase(),
  50. label: deviceLabels[key]
  51. },
  52. // eslint-disable-next-line no-empty-function
  53. responseCallback() {}
  54. }));
  55. });
  56. resolve();
  57. return;
  58. }
  59. const newSettings = {};
  60. const devicesKeysToSettingsKeys = {
  61. audioInput: 'micDeviceId',
  62. audioOutput: 'audioOutputDeviceId',
  63. videoInput: 'cameraDeviceId'
  64. };
  65. Object.keys(deviceLabels).forEach(key => {
  66. const label = deviceLabels[key];
  67. const deviceId = getDeviceIdByLabel(state, label);
  68. if (deviceId) {
  69. newSettings[devicesKeysToSettingsKeys[key]] = deviceId;
  70. }
  71. });
  72. dispatch(updateSettings(newSettings));
  73. resolve();
  74. });
  75. } else {
  76. resolve();
  77. }
  78. });
  79. }
  80. /**
  81. * Queries for connected A/V input and output devices and updates the redux
  82. * state of known devices.
  83. *
  84. * @returns {Function}
  85. */
  86. export function getAvailableDevices() {
  87. return dispatch => new Promise(resolve => {
  88. const { mediaDevices } = JitsiMeetJS;
  89. if (mediaDevices.isDeviceListAvailable()
  90. && mediaDevices.isDeviceChangeAvailable()) {
  91. mediaDevices.enumerateDevices(devices => {
  92. dispatch(updateDeviceList(devices));
  93. resolve(devices);
  94. });
  95. } else {
  96. resolve([]);
  97. }
  98. });
  99. }
  100. /**
  101. * Remove all pending device requests.
  102. *
  103. * @returns {{
  104. * type: REMOVE_PENDING_DEVICE_REQUESTS
  105. * }}
  106. */
  107. export function removePendingDeviceRequests() {
  108. return {
  109. type: REMOVE_PENDING_DEVICE_REQUESTS
  110. };
  111. }
  112. /**
  113. * Signals to update the currently used audio input device.
  114. *
  115. * @param {string} deviceId - The id of the new audio input device.
  116. * @returns {{
  117. * type: SET_AUDIO_INPUT_DEVICE,
  118. * deviceId: string
  119. * }}
  120. */
  121. export function setAudioInputDevice(deviceId) {
  122. return {
  123. type: SET_AUDIO_INPUT_DEVICE,
  124. deviceId
  125. };
  126. }
  127. /**
  128. * Signals to update the currently used video input device.
  129. *
  130. * @param {string} deviceId - The id of the new video input device.
  131. * @returns {{
  132. * type: SET_VIDEO_INPUT_DEVICE,
  133. * deviceId: string
  134. * }}
  135. */
  136. export function setVideoInputDevice(deviceId) {
  137. return {
  138. type: SET_VIDEO_INPUT_DEVICE,
  139. deviceId
  140. };
  141. }
  142. /**
  143. * Signals to update the list of known audio and video devices.
  144. *
  145. * @param {Array<MediaDeviceInfo>} devices - All known available audio input,
  146. * audio output, and video input devices.
  147. * @returns {{
  148. * type: UPDATE_DEVICE_LIST,
  149. * devices: Array<MediaDeviceInfo>
  150. * }}
  151. */
  152. export function updateDeviceList(devices) {
  153. return {
  154. type: UPDATE_DEVICE_LIST,
  155. devices
  156. };
  157. }