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

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