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

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