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

DeviceSelectionPopup.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* global JitsiMeetJS */
  2. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { I18nextProvider } from 'react-i18next';
  6. import {
  7. PostMessageTransportBackend,
  8. Transport
  9. } from '../../../../../modules/transport';
  10. import {
  11. getAvailableDevices,
  12. getCurrentDevices,
  13. isDeviceChangeAvailable,
  14. isDeviceListAvailable,
  15. isMultipleAudioInputSupported,
  16. setAudioInputDevice,
  17. setAudioOutputDevice,
  18. setVideoInputDevice
  19. } from '../../../../../modules/API/external';
  20. import { parseURLParams } from '../../../base/config';
  21. import { DialogWithTabs } from '../../../base/dialog';
  22. import { DeviceSelection } from '../../../device-selection';
  23. /**
  24. * Implements a class that renders the React components for the device selection
  25. * popup page and handles the communication between the components and Jitsi
  26. * Meet.
  27. */
  28. export default class DeviceSelectionPopup {
  29. /**
  30. * Initializes a new DeviceSelectionPopup instance.
  31. *
  32. * @param {Object} i18next - The i18next instance used for translation.
  33. */
  34. constructor(i18next) {
  35. this.close = this.close.bind(this);
  36. this._i18next = i18next;
  37. this._onSubmit = this._onSubmit.bind(this);
  38. const { scope } = parseURLParams(window.location);
  39. this._transport = new Transport({
  40. backend: new PostMessageTransportBackend({
  41. postisOptions: {
  42. scope,
  43. window: window.opener
  44. }
  45. })
  46. });
  47. this._transport.on('event', event => {
  48. if (event.name === 'deviceListChanged') {
  49. this._updateAvailableDevices();
  50. return true;
  51. }
  52. return false;
  53. });
  54. this._dialogProps = {
  55. availableDevices: {},
  56. selectedAudioInputId: '',
  57. selectedAudioOutputId: '',
  58. selectedVideoInputId: '',
  59. disableAudioInputChange: true,
  60. disableBlanketClickDismiss: true,
  61. disableDeviceChange: true,
  62. hideAudioInputPreview: !JitsiMeetJS.isCollectingLocalStats(),
  63. hideAudioOutputSelect: true
  64. };
  65. this._initState();
  66. }
  67. /**
  68. * Sends event to Jitsi Meet to close the popup dialog.
  69. *
  70. * @returns {void}
  71. */
  72. close() {
  73. this._transport.sendEvent({
  74. type: 'devices-dialog',
  75. name: 'close'
  76. });
  77. }
  78. /**
  79. * Changes the properties of the react component and re-renders it.
  80. *
  81. * @param {Object} newProps - The new properties that will be assigned to
  82. * the current ones.
  83. * @returns {void}
  84. */
  85. _changeDialogProps(newProps) {
  86. this._dialogProps = {
  87. ...this._dialogProps,
  88. ...newProps
  89. };
  90. this._render();
  91. }
  92. /**
  93. * Returns Promise that resolves with result an list of available devices.
  94. *
  95. * @returns {Promise}
  96. */
  97. _getAvailableDevices() {
  98. return getAvailableDevices(this._transport);
  99. }
  100. /**
  101. * Returns Promise that resolves with current selected devices.
  102. *
  103. * @returns {Promise}
  104. */
  105. _getCurrentDevices() {
  106. return getCurrentDevices(this._transport);
  107. }
  108. /**
  109. * Initializes the state.
  110. *
  111. * @returns {void}
  112. */
  113. _initState() {
  114. return Promise.all([
  115. this._getAvailableDevices(),
  116. this._isDeviceListAvailable(),
  117. this._isDeviceChangeAvailable(),
  118. this._isDeviceChangeAvailable('output'),
  119. this._getCurrentDevices(),
  120. this._isMultipleAudioInputSupported()
  121. ]).then(([
  122. availableDevices,
  123. listAvailable,
  124. changeAvailable,
  125. changeOutputAvailable,
  126. currentDevices,
  127. multiAudioInputSupported
  128. ]) => {
  129. this._changeDialogProps({
  130. availableDevices,
  131. selectedAudioInputId: currentDevices.audioInput,
  132. selectedAudioOutputId: currentDevices.audioOutput,
  133. selectedVideoInputId: currentDevices.videoInput,
  134. disableAudioInputChange: !multiAudioInputSupported,
  135. disableDeviceChange: !listAvailable || !changeAvailable,
  136. hideAudioOutputSelect: !changeOutputAvailable
  137. });
  138. });
  139. }
  140. /**
  141. * Returns Promise that resolves with true if the device change is available
  142. * and with false if not.
  143. *
  144. * @param {string} [deviceType] - Values - 'output', 'input' or undefined.
  145. * Default - 'input'.
  146. * @returns {Promise}
  147. */
  148. _isDeviceChangeAvailable(deviceType) {
  149. return isDeviceChangeAvailable(this._transport, deviceType);
  150. }
  151. /**
  152. * Returns Promise that resolves with true if the device list is available
  153. * and with false if not.
  154. *
  155. * @returns {Promise}
  156. */
  157. _isDeviceListAvailable() {
  158. return isDeviceListAvailable(this._transport);
  159. }
  160. /**
  161. * Returns Promise that resolves with true if the device list is available
  162. * and with false if not.
  163. *
  164. * @returns {Promise}
  165. */
  166. _isMultipleAudioInputSupported() {
  167. return isMultipleAudioInputSupported(this._transport);
  168. }
  169. /**
  170. * Callback invoked to save changes to selected devices and close the
  171. * dialog.
  172. *
  173. * @param {Object} newSettings - The chosen device IDs.
  174. * @private
  175. * @returns {void}
  176. */
  177. _onSubmit(newSettings) {
  178. const promises = [];
  179. if (newSettings.selectedVideoInputId
  180. !== this._dialogProps.selectedVideoInputId) {
  181. promises.push(
  182. this._setVideoInputDevice(newSettings.selectedVideoInputId));
  183. }
  184. if (newSettings.selectedAudioInputId
  185. !== this._dialogProps.selectedAudioInputId) {
  186. promises.push(
  187. this._setAudioInputDevice(newSettings.selectedAudioInputId));
  188. }
  189. if (newSettings.selectedAudioOutputId
  190. !== this._dialogProps.selectedAudioOutputId) {
  191. promises.push(
  192. this._setAudioOutputDevice(newSettings.selectedAudioOutputId));
  193. }
  194. Promise.all(promises).then(this.close, this.close);
  195. }
  196. /**
  197. * Renders the React components for the popup page.
  198. *
  199. * @returns {void}
  200. */
  201. _render() {
  202. const onSubmit = this.close;
  203. ReactDOM.render(
  204. <I18nextProvider i18n = { this._i18next }>
  205. <AtlasKitThemeProvider mode = 'dark'>
  206. <DialogWithTabs
  207. closeDialog = { this.close }
  208. cssClassName = 'settings-dialog'
  209. onSubmit = { onSubmit }
  210. tabs = { [ {
  211. component: DeviceSelection,
  212. label: 'settings.devices',
  213. props: this._dialogProps,
  214. submit: this._onSubmit
  215. } ] }
  216. titleKey = 'settings.title' />
  217. </AtlasKitThemeProvider>
  218. </I18nextProvider>,
  219. document.getElementById('react'));
  220. }
  221. /**
  222. * Sets the audio input device to the one with the id that is passed.
  223. *
  224. * @param {string} id - The id of the new device.
  225. * @returns {Promise}
  226. */
  227. _setAudioInputDevice(id) {
  228. return setAudioInputDevice(this._transport, id);
  229. }
  230. /**
  231. * Sets the audio output device to the one with the id that is passed.
  232. *
  233. * @param {string} id - The id of the new device.
  234. * @returns {Promise}
  235. */
  236. _setAudioOutputDevice(id) {
  237. return setAudioOutputDevice(this._transport, id);
  238. }
  239. /**
  240. * Sets the video input device to the one with the id that is passed.
  241. *
  242. * @param {string} id - The id of the new device.
  243. * @returns {Promise}
  244. */
  245. _setVideoInputDevice(id) {
  246. return setVideoInputDevice(this._transport, id);
  247. }
  248. /**
  249. * Updates the available devices.
  250. *
  251. * @returns {void}
  252. */
  253. _updateAvailableDevices() {
  254. this._getAvailableDevices().then(devices =>
  255. this._changeDialogProps({ availableDevices: devices })
  256. );
  257. }
  258. }