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.

DeviceSelectionPopup.js 9.2KB

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