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.

actions.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { API_ID } from '../../../modules/API/constants';
  2. import {
  3. PostMessageTransportBackend,
  4. Transport
  5. } from '../../../modules/transport';
  6. import { createDeviceChangedEvent, sendAnalytics } from '../analytics';
  7. import {
  8. setAudioInputDevice,
  9. setAudioOutputDeviceId,
  10. setVideoInputDevice
  11. } from '../base/devices';
  12. import { i18next } from '../base/i18n';
  13. import { updateSettings } from '../base/settings';
  14. import { SET_DEVICE_SELECTION_POPUP_DATA } from './actionTypes';
  15. import { getDeviceSelectionDialogProps, processExternalDeviceRequest } from './functions';
  16. const logger = require('jitsi-meet-logger').getLogger(__filename);
  17. /**
  18. * Opens a popup window with the device selection dialog in it.
  19. *
  20. * @returns {Function}
  21. */
  22. export function openDeviceSelectionPopup() {
  23. return (dispatch, getState) => {
  24. const { popupDialogData } = getState()['features/device-selection'];
  25. if (popupDialogData) {
  26. popupDialogData.popup.focus();
  27. return;
  28. }
  29. // API_ID will always be defined because the iframe api is enabled
  30. const scope = `dialog_${API_ID}`;
  31. const url = `${
  32. window.location.origin}/static/deviceSelectionPopup.html#scope=${
  33. encodeURIComponent(JSON.stringify(scope))}`;
  34. const popup
  35. = window.open(
  36. url,
  37. 'device-selection-popup',
  38. 'toolbar=no,scrollbars=no,resizable=no,width=720,height=458');
  39. popup.addEventListener('DOMContentLoaded', () => {
  40. popup.init(i18next);
  41. });
  42. const transport = new Transport({
  43. backend: new PostMessageTransportBackend({
  44. postisOptions: {
  45. scope,
  46. window: popup
  47. }
  48. })
  49. });
  50. transport.on('request',
  51. processExternalDeviceRequest.bind(undefined, dispatch, getState));
  52. transport.on('event', event => {
  53. if (event.type === 'devices-dialog' && event.name === 'close') {
  54. popup.close();
  55. transport.dispose();
  56. dispatch(_setDeviceSelectionPopupData());
  57. return true;
  58. }
  59. return false;
  60. });
  61. dispatch(_setDeviceSelectionPopupData({
  62. popup,
  63. transport
  64. }));
  65. };
  66. }
  67. /**
  68. * Sets information about device selection popup in the store.
  69. *
  70. * @param {Object} popupDialogData - Information about the popup.
  71. * @param {Object} popupDialog.popup - The popup object returned from
  72. * window.open.
  73. * @param {Object} popupDialogData.transport - The transport instance used for
  74. * communication with the popup window.
  75. * @returns {{
  76. * type: SET_DEVICE_SELECTION_POPUP_DATA,
  77. * popupDialogData: Object
  78. * }}
  79. */
  80. function _setDeviceSelectionPopupData(popupDialogData) {
  81. return {
  82. type: SET_DEVICE_SELECTION_POPUP_DATA,
  83. popupDialogData
  84. };
  85. }
  86. /**
  87. * Submits the settings related to device selection.
  88. *
  89. * @param {Object} newState - The new settings.
  90. * @returns {Function}
  91. */
  92. export function submitDeviceSelectionTab(newState) {
  93. return (dispatch, getState) => {
  94. const currentState = getDeviceSelectionDialogProps(getState());
  95. if (newState.selectedVideoInputId
  96. && newState.selectedVideoInputId
  97. !== currentState.selectedVideoInputId) {
  98. dispatch(updateSettings({
  99. cameraDeviceId: newState.selectedVideoInputId
  100. }));
  101. dispatch(
  102. setVideoInputDevice(newState.selectedVideoInputId));
  103. }
  104. if (newState.selectedAudioInputId
  105. && newState.selectedAudioInputId
  106. !== currentState.selectedAudioInputId) {
  107. dispatch(updateSettings({
  108. micDeviceId: newState.selectedAudioInputId
  109. }));
  110. dispatch(
  111. setAudioInputDevice(newState.selectedAudioInputId));
  112. }
  113. if (newState.selectedAudioOutputId
  114. && newState.selectedAudioOutputId
  115. !== currentState.selectedAudioOutputId) {
  116. sendAnalytics(createDeviceChangedEvent('audio', 'output'));
  117. setAudioOutputDeviceId(
  118. newState.selectedAudioOutputId,
  119. dispatch)
  120. .then(() => logger.log('changed audio output device'))
  121. .catch(err => {
  122. logger.warn(
  123. 'Failed to change audio output device.',
  124. 'Default or previously set audio output device will',
  125. ' be used instead.',
  126. err);
  127. });
  128. }
  129. };
  130. }