Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @flow
  2. import { parseURLParams } from '../config';
  3. import JitsiMeetJS from '../lib-jitsi-meet';
  4. import { updateSettings } from '../settings';
  5. declare var APP: Object;
  6. /**
  7. * Detects the use case when the labels are not available if the A/V permissions
  8. * are not yet granted.
  9. *
  10. * @param {Object} state - The redux state.
  11. * @returns {boolean} - True if the labels are already initialized and false
  12. * otherwise.
  13. */
  14. export function areDeviceLabelsInitialized(state: Object) {
  15. // TODO: Replace with something that doesn't use APP when the conference.js logic is reactified.
  16. if (APP.conference._localTracksInitialized) {
  17. return true;
  18. }
  19. for (const type of [ 'audioInput', 'audioOutput', 'videoInput' ]) {
  20. if ((state['features/base/devices'].availableDevices[type] || []).find(d => Boolean(d.label))) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. /**
  27. * Get device id of the audio output device which is currently in use.
  28. * Empty string stands for default device.
  29. *
  30. * @returns {string}
  31. */
  32. export function getAudioOutputDeviceId() {
  33. return JitsiMeetJS.mediaDevices.getAudioOutputDevice();
  34. }
  35. /**
  36. * Finds a device with a label that matches the passed label and returns its id.
  37. *
  38. * @param {Object} state - The redux state.
  39. * @param {string} label - The label.
  40. * @param {string} kind - The type of the device. One of "audioInput",
  41. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  42. * of the preceding types.
  43. * @returns {string|undefined}
  44. */
  45. export function getDeviceIdByLabel(state: Object, label: string, kind: string) {
  46. const webrtcKindToJitsiKindTranslator = {
  47. audioinput: 'audioInput',
  48. audiooutput: 'audioOutput',
  49. videoinput: 'videoInput'
  50. };
  51. const kindToSearch = webrtcKindToJitsiKindTranslator[kind] || kind;
  52. const device
  53. = (state['features/base/devices'].availableDevices[kindToSearch] || [])
  54. .find(d => d.label === label);
  55. if (device) {
  56. return device.deviceId;
  57. }
  58. }
  59. /**
  60. * Finds a device with a label that matches the passed id and returns its label.
  61. *
  62. * @param {Object} state - The redux state.
  63. * @param {string} id - The device id.
  64. * @param {string} kind - The type of the device. One of "audioInput",
  65. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  66. * of the preceding types.
  67. * @returns {string|undefined}
  68. */
  69. export function getDeviceLabelById(state: Object, id: string, kind: string) {
  70. const webrtcKindToJitsiKindTranslator = {
  71. audioinput: 'audioInput',
  72. audiooutput: 'audioOutput',
  73. videoinput: 'videoInput'
  74. };
  75. const kindToSearch = webrtcKindToJitsiKindTranslator[kind] || kind;
  76. const device
  77. = (state['features/base/devices'].availableDevices[kindToSearch] || [])
  78. .find(d => d.deviceId === id);
  79. if (device) {
  80. return device.label;
  81. }
  82. }
  83. /**
  84. * Returns the devices set in the URL.
  85. *
  86. * @param {Object} state - The redux state.
  87. * @returns {Object|undefined}
  88. */
  89. export function getDevicesFromURL(state: Object) {
  90. const urlParams
  91. = parseURLParams(state['features/base/connection'].locationURL);
  92. const audioOutput = urlParams['devices.audioOutput'];
  93. const videoInput = urlParams['devices.videoInput'];
  94. const audioInput = urlParams['devices.audioInput'];
  95. if (!audioOutput && !videoInput && !audioInput) {
  96. return undefined;
  97. }
  98. const devices = {};
  99. audioOutput && (devices.audioOutput = audioOutput);
  100. videoInput && (devices.videoInput = videoInput);
  101. audioInput && (devices.audioInput = audioInput);
  102. return devices;
  103. }
  104. /**
  105. * Converts an array of media devices into an object organized by device kind.
  106. *
  107. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  108. * @private
  109. * @returns {Object} An object with the media devices split by type. The keys
  110. * are device type and the values are arrays with devices matching the device
  111. * type.
  112. */
  113. export function groupDevicesByKind(devices: Object[]): Object {
  114. return {
  115. audioInput: devices.filter(device => device.kind === 'audioinput'),
  116. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  117. videoInput: devices.filter(device => device.kind === 'videoinput')
  118. };
  119. }
  120. /**
  121. * Set device id of the audio output device which is currently in use.
  122. * Empty string stands for default device.
  123. *
  124. * @param {string} newId - New audio output device id.
  125. * @param {Function} dispatch - The Redux dispatch function.
  126. * @param {boolean} userSelection - Whether this is a user selection update.
  127. * @param {?string} newLabel - New audio output device label to store.
  128. * @returns {Promise}
  129. */
  130. export function setAudioOutputDeviceId(
  131. newId: string = 'default',
  132. dispatch: Function,
  133. userSelection: boolean = false,
  134. newLabel: ?string): Promise<*> {
  135. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  136. .then(() => {
  137. const newSettings = {
  138. audioOutputDeviceId: newId,
  139. userSelectedAudioOutputDeviceId: undefined,
  140. userSelectedAudioOutputDeviceLabel: undefined
  141. };
  142. if (userSelection) {
  143. newSettings.userSelectedAudioOutputDeviceId = newId;
  144. newSettings.userSelectedAudioOutputDeviceLabel = newLabel;
  145. } else {
  146. // a flow workaround, I needed to add 'userSelectedAudioOutputDeviceId: undefined'
  147. delete newSettings.userSelectedAudioOutputDeviceId;
  148. delete newSettings.userSelectedAudioOutputDeviceLabel;
  149. }
  150. return dispatch(updateSettings(newSettings));
  151. });
  152. }