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

functions.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Returns the devices set in the URL.
  61. *
  62. * @param {Object} state - The redux state.
  63. * @returns {Object|undefined}
  64. */
  65. export function getDevicesFromURL(state: Object) {
  66. const urlParams
  67. = parseURLParams(state['features/base/connection'].locationURL);
  68. const audioOutput = urlParams['devices.audioOutput'];
  69. const videoInput = urlParams['devices.videoInput'];
  70. const audioInput = urlParams['devices.audioInput'];
  71. if (!audioOutput && !videoInput && !audioInput) {
  72. return undefined;
  73. }
  74. const devices = {};
  75. audioOutput && (devices.audioOutput = audioOutput);
  76. videoInput && (devices.videoInput = videoInput);
  77. audioInput && (devices.audioInput = audioInput);
  78. return devices;
  79. }
  80. /**
  81. * Converts an array of media devices into an object organized by device kind.
  82. *
  83. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  84. * @private
  85. * @returns {Object} An object with the media devices split by type. The keys
  86. * are device type and the values are arrays with devices matching the device
  87. * type.
  88. */
  89. export function groupDevicesByKind(devices: Object[]): Object {
  90. return {
  91. audioInput: devices.filter(device => device.kind === 'audioinput'),
  92. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  93. videoInput: devices.filter(device => device.kind === 'videoinput')
  94. };
  95. }
  96. /**
  97. * Set device id of the audio output device which is currently in use.
  98. * Empty string stands for default device.
  99. *
  100. * @param {string} newId - New audio output device id.
  101. * @param {Function} dispatch - The Redux dispatch function.
  102. * @param {boolean} userSelection - Whether this is a user selection update.
  103. * @returns {Promise}
  104. */
  105. export function setAudioOutputDeviceId(
  106. newId: string = 'default',
  107. dispatch: Function,
  108. userSelection: boolean = false): Promise<*> {
  109. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  110. .then(() => {
  111. const newSettings = {
  112. audioOutputDeviceId: newId,
  113. userSelectedAudioOutputDeviceId: undefined
  114. };
  115. if (userSelection) {
  116. newSettings.userSelectedAudioOutputDeviceId = newId;
  117. } else {
  118. // a flow workaround, I needed to add 'userSelectedAudioOutputDeviceId: undefined'
  119. delete newSettings.userSelectedAudioOutputDeviceId;
  120. }
  121. return dispatch(updateSettings(newSettings));
  122. });
  123. }