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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * @returns {string|undefined}
  41. */
  42. export function getDeviceIdByLabel(state: Object, label: string) {
  43. const types = [ 'audioInput', 'audioOutput', 'videoInput' ];
  44. for (const type of types) {
  45. const device
  46. = (state['features/base/devices'].availableDevices[type] || [])
  47. .find(d => d.label === label);
  48. if (device) {
  49. return device.deviceId;
  50. }
  51. }
  52. }
  53. /**
  54. * Returns the devices set in the URL.
  55. *
  56. * @param {Object} state - The redux state.
  57. * @returns {Object|undefined}
  58. */
  59. export function getDevicesFromURL(state: Object) {
  60. const urlParams
  61. = parseURLParams(state['features/base/connection'].locationURL);
  62. const audioOutput = urlParams['devices.audioOutput'];
  63. const videoInput = urlParams['devices.videoInput'];
  64. const audioInput = urlParams['devices.audioInput'];
  65. if (!audioOutput && !videoInput && !audioInput) {
  66. return undefined;
  67. }
  68. const devices = {};
  69. audioOutput && (devices.audioOutput = audioOutput);
  70. videoInput && (devices.videoInput = videoInput);
  71. audioInput && (devices.audioInput = audioInput);
  72. return devices;
  73. }
  74. /**
  75. * Converts an array of media devices into an object organized by device kind.
  76. *
  77. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  78. * @private
  79. * @returns {Object} An object with the media devices split by type. The keys
  80. * are device type and the values are arrays with devices matching the device
  81. * type.
  82. */
  83. export function groupDevicesByKind(devices: Object[]): Object {
  84. return {
  85. audioInput: devices.filter(device => device.kind === 'audioinput'),
  86. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  87. videoInput: devices.filter(device => device.kind === 'videoinput')
  88. };
  89. }
  90. /**
  91. * Set device id of the audio output device which is currently in use.
  92. * Empty string stands for default device.
  93. *
  94. * @param {string} newId - New audio output device id.
  95. * @param {Function} dispatch - The Redux dispatch function.
  96. * @returns {Promise}
  97. */
  98. export function setAudioOutputDeviceId(
  99. newId: string = 'default',
  100. dispatch: Function): Promise<*> {
  101. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  102. .then(() =>
  103. dispatch(updateSettings({
  104. audioOutputDeviceId: newId
  105. })));
  106. }