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.

functions.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import {
  4. addPendingDeviceRequest,
  5. areDeviceLabelsInitialized,
  6. getAudioOutputDeviceId,
  7. getAvailableDevices,
  8. getDeviceIdByLabel,
  9. groupDevicesByKind,
  10. setAudioInputDevice,
  11. setAudioOutputDeviceId,
  12. setVideoInputDevice
  13. } from '../base/devices';
  14. import JitsiMeetJS from '../base/lib-jitsi-meet';
  15. import { toState } from '../base/redux';
  16. import {
  17. getUserSelectedCameraDeviceId,
  18. getUserSelectedMicDeviceId,
  19. getUserSelectedOutputDeviceId
  20. } from '../base/settings';
  21. /**
  22. * Returns the properties for the device selection dialog from Redux state.
  23. *
  24. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  25. * {@code getState} function to be used to retrieve the state.
  26. * @returns {Object} - The properties for the device selection dialog.
  27. */
  28. export function getDeviceSelectionDialogProps(stateful: Object | Function) {
  29. const state = toState(stateful);
  30. const settings = state['features/base/settings'];
  31. const { conference } = state['features/base/conference'];
  32. let disableAudioInputChange = !JitsiMeetJS.mediaDevices.isMultipleAudioInputSupported();
  33. let selectedAudioInputId = settings.micDeviceId;
  34. let selectedAudioOutputId = getAudioOutputDeviceId();
  35. let selectedVideoInputId = settings.cameraDeviceId;
  36. // audio input change will be a problem only when we are in a
  37. // conference and this is not supported, when we open device selection on
  38. // welcome page changing input devices will not be a problem
  39. // on welcome page we also show only what we have saved as user selected devices
  40. if (!conference) {
  41. disableAudioInputChange = false;
  42. selectedAudioInputId = getUserSelectedMicDeviceId(state);
  43. selectedAudioOutputId = getUserSelectedOutputDeviceId(state);
  44. selectedVideoInputId = getUserSelectedCameraDeviceId(state);
  45. }
  46. // we fill the device selection dialog with the devices that are currently
  47. // used or if none are currently used with what we have in settings(user selected)
  48. return {
  49. availableDevices: state['features/base/devices'].availableDevices,
  50. disableAudioInputChange,
  51. disableDeviceChange:
  52. !JitsiMeetJS.mediaDevices.isDeviceChangeAvailable(),
  53. hideAudioInputPreview:
  54. !JitsiMeetJS.isCollectingLocalStats(),
  55. hideAudioOutputSelect: !JitsiMeetJS.mediaDevices
  56. .isDeviceChangeAvailable('output'),
  57. selectedAudioInputId,
  58. selectedAudioOutputId,
  59. selectedVideoInputId
  60. };
  61. }
  62. /**
  63. * Processes device requests from external applications.
  64. *
  65. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  66. * @param {Function} getState - The redux function that gets/retrieves the redux
  67. * state.
  68. * @param {Object} request - The request to be processed.
  69. * @param {Function} responseCallback - The callback that will send the
  70. * response.
  71. * @returns {boolean} - True if the request has been processed and false otherwise.
  72. */
  73. export function processExternalDeviceRequest( // eslint-disable-line max-params
  74. dispatch: Dispatch<any>,
  75. getState: Function,
  76. request: Object,
  77. responseCallback: Function) {
  78. if (request.type !== 'devices') {
  79. return false;
  80. }
  81. const state = getState();
  82. const settings = state['features/base/settings'];
  83. let result = true;
  84. switch (request.name) {
  85. case 'isDeviceListAvailable':
  86. responseCallback(JitsiMeetJS.mediaDevices.isDeviceListAvailable());
  87. break;
  88. case 'isDeviceChangeAvailable':
  89. responseCallback(
  90. JitsiMeetJS.mediaDevices.isDeviceChangeAvailable(
  91. request.deviceType));
  92. break;
  93. case 'isMultipleAudioInputSupported':
  94. responseCallback(JitsiMeetJS.isMultipleAudioInputSupported());
  95. break;
  96. case 'getCurrentDevices':
  97. dispatch(getAvailableDevices()).then(devices => {
  98. if (areDeviceLabelsInitialized(state)) {
  99. const deviceDescriptions = {
  100. audioInput: undefined,
  101. audioOutput: undefined,
  102. videoInput: undefined
  103. };
  104. const currentlyUsedDeviceIds = new Set([
  105. getAudioOutputDeviceId(),
  106. settings.micDeviceId,
  107. settings.cameraDeviceId
  108. ]);
  109. devices.forEach(device => {
  110. const { deviceId, kind } = device;
  111. if (currentlyUsedDeviceIds.has(deviceId)) {
  112. switch (kind) {
  113. case 'audioinput':
  114. deviceDescriptions.audioInput = device;
  115. break;
  116. case 'audiooutput':
  117. deviceDescriptions.audioOutput = device;
  118. break;
  119. case 'videoinput':
  120. deviceDescriptions.videoInput = device;
  121. break;
  122. }
  123. }
  124. });
  125. responseCallback(deviceDescriptions);
  126. } else {
  127. // The labels are not available if the A/V permissions are
  128. // not yet granted.
  129. dispatch(addPendingDeviceRequest({
  130. type: 'devices',
  131. name: 'getCurrentDevices',
  132. responseCallback
  133. }));
  134. }
  135. });
  136. break;
  137. case 'getAvailableDevices':
  138. dispatch(getAvailableDevices()).then(devices => {
  139. if (areDeviceLabelsInitialized(state)) {
  140. responseCallback(groupDevicesByKind(devices));
  141. } else {
  142. // The labels are not available if the A/V permissions are
  143. // not yet granted.
  144. dispatch(addPendingDeviceRequest({
  145. type: 'devices',
  146. name: 'getAvailableDevices',
  147. responseCallback
  148. }));
  149. }
  150. });
  151. break;
  152. case 'setDevice': {
  153. const { device } = request;
  154. if (!areDeviceLabelsInitialized(state)) {
  155. dispatch(addPendingDeviceRequest({
  156. type: 'devices',
  157. name: 'setDevice',
  158. device,
  159. responseCallback
  160. }));
  161. return true;
  162. }
  163. const { label, id } = device;
  164. const deviceId = label
  165. ? getDeviceIdByLabel(state, device.label, device.kind)
  166. : id;
  167. if (deviceId) {
  168. switch (device.kind) {
  169. case 'audioinput': {
  170. dispatch(setAudioInputDevice(deviceId));
  171. break;
  172. }
  173. case 'audiooutput':
  174. setAudioOutputDeviceId(deviceId, dispatch);
  175. break;
  176. case 'videoinput':
  177. dispatch(setVideoInputDevice(deviceId));
  178. break;
  179. default:
  180. result = false;
  181. }
  182. } else {
  183. result = false;
  184. }
  185. responseCallback(result);
  186. break;
  187. }
  188. default:
  189. return false;
  190. }
  191. return true;
  192. }