Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 5.0KB

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