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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @returns {Promise}
  103. */
  104. export function setAudioOutputDeviceId(
  105. newId: string = 'default',
  106. dispatch: Function): Promise<*> {
  107. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  108. .then(() =>
  109. dispatch(updateSettings({
  110. audioOutputDeviceId: newId
  111. })));
  112. }