您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 5.7KB

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