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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. hasAudioPermission: JitsiMeetJS.mediaDevices
  57. .isDevicePermissionGranted.bind(null, 'audio'),
  58. hasVideoPermission: JitsiMeetJS.mediaDevices
  59. .isDevicePermissionGranted.bind(null, 'video'),
  60. hideAudioInputPreview: !JitsiMeetJS.isCollectingLocalStats(),
  61. hideAudioOutputSelect: true
  62. };
  63. this._initState();
  64. }
  65. /**
  66. * Sends event to Jitsi Meet to close the popup dialog.
  67. *
  68. * @returns {void}
  69. */
  70. close() {
  71. this._transport.sendEvent({
  72. type: 'devices-dialog',
  73. name: 'close'
  74. });
  75. }
  76. /**
  77. * Changes the properties of the react component and re-renders it.
  78. *
  79. * @param {Object} newProps - The new properties that will be assigned to
  80. * the current ones.
  81. * @returns {void}
  82. */
  83. _changeDialogProps(newProps) {
  84. this._dialogProps = {
  85. ...this._dialogProps,
  86. ...newProps
  87. };
  88. this._render();
  89. }
  90. /**
  91. * Returns Promise that resolves with result an list of available devices.
  92. *
  93. * @returns {Promise}
  94. */
  95. _getAvailableDevices() {
  96. return this._transport.sendRequest({
  97. type: 'devices',
  98. name: 'getAvailableDevices'
  99. }).catch(e => {
  100. logger.error(e);
  101. return {};
  102. });
  103. }
  104. /**
  105. * Returns Promise that resolves with current selected devices.
  106. *
  107. * @returns {Promise}
  108. */
  109. _getCurrentDevices() {
  110. return this._transport.sendRequest({
  111. type: 'devices',
  112. name: 'getCurrentDevices'
  113. }).catch(e => {
  114. logger.error(e);
  115. return {};
  116. });
  117. }
  118. /**
  119. * Initializes the state.
  120. *
  121. * @returns {void}
  122. */
  123. _initState() {
  124. return Promise.all([
  125. this._getAvailableDevices(),
  126. this._isDeviceListAvailable(),
  127. this._isDeviceChangeAvailable(),
  128. this._isDeviceChangeAvailable('output'),
  129. this._getCurrentDevices(),
  130. this._isMultipleAudioInputSupported()
  131. ]).then(([
  132. availableDevices,
  133. listAvailable,
  134. changeAvailable,
  135. changeOutputAvailable,
  136. currentDevices,
  137. multiAudioInputSupported
  138. ]) => {
  139. this._changeDialogProps({
  140. availableDevices,
  141. selectedAudioInputId: currentDevices.audioInput,
  142. selectedAudioOutputId: currentDevices.audioOutput,
  143. selectedVideoInputId: currentDevices.videoInput,
  144. disableAudioInputChange: !multiAudioInputSupported,
  145. disableDeviceChange: !listAvailable || !changeAvailable,
  146. hideAudioOutputSelect: !changeOutputAvailable
  147. });
  148. });
  149. }
  150. /**
  151. * Returns Promise that resolves with true if the device change is available
  152. * and with false if not.
  153. *
  154. * @param {string} [deviceType] - Values - 'output', 'input' or undefined.
  155. * Default - 'input'.
  156. * @returns {Promise}
  157. */
  158. _isDeviceChangeAvailable(deviceType) {
  159. return this._transport.sendRequest({
  160. deviceType,
  161. type: 'devices',
  162. name: 'isDeviceChangeAvailable'
  163. }).catch(e => {
  164. logger.error(e);
  165. return false;
  166. });
  167. }
  168. /**
  169. * Returns Promise that resolves with true if the device list is available
  170. * and with false if not.
  171. *
  172. * @returns {Promise}
  173. */
  174. _isDeviceListAvailable() {
  175. return this._transport.sendRequest({
  176. type: 'devices',
  177. name: 'isDeviceListAvailable'
  178. }).catch(e => {
  179. logger.error(e);
  180. return false;
  181. });
  182. }
  183. /**
  184. * Returns Promise that resolves with true if the device list is available
  185. * and with false if not.
  186. *
  187. * @returns {Promise}
  188. */
  189. _isMultipleAudioInputSupported() {
  190. return this._transport.sendRequest({
  191. type: 'devices',
  192. name: 'isMultipleAudioInputSupported'
  193. }).catch(e => {
  194. logger.error(e);
  195. return false;
  196. });
  197. }
  198. /**
  199. * Callback invoked to save changes to selected devices and close the
  200. * dialog.
  201. *
  202. * @param {Object} newSettings - The chosen device IDs.
  203. * @private
  204. * @returns {void}
  205. */
  206. _onSubmit(newSettings) {
  207. const promises = [];
  208. if (newSettings.selectedVideoInputId
  209. !== this._dialogProps.selectedVideoInputId) {
  210. promises.push(
  211. this._setVideoInputDevice(newSettings.selectedVideoInputId));
  212. }
  213. if (newSettings.selectedAudioInputId
  214. !== this._dialogProps.selectedAudioInputId) {
  215. promises.push(
  216. this._setAudioInputDevice(newSettings.selectedAudioInputId));
  217. }
  218. if (newSettings.selectedAudioOutputId
  219. !== this._dialogProps.selectedAudioOutputId) {
  220. promises.push(
  221. this._setAudioOutputDevice(newSettings.selectedAudioOutputId));
  222. }
  223. Promise.all(promises).then(this.close, this.close);
  224. }
  225. /**
  226. * Renders the React components for the popup page.
  227. *
  228. * @returns {void}
  229. */
  230. _render() {
  231. const onSubmit = this.close;
  232. ReactDOM.render(
  233. <I18nextProvider i18n = { this._i18next }>
  234. <AtlasKitThemeProvider mode = 'dark'>
  235. <DialogWithTabs
  236. closeDialog = { this.close }
  237. onSubmit = { onSubmit }
  238. tabs = { [ {
  239. component: DeviceSelection,
  240. label: 'settings.devices',
  241. props: this._dialogProps,
  242. submit: this._onSubmit
  243. } ] } />
  244. </AtlasKitThemeProvider>
  245. </I18nextProvider>,
  246. document.getElementById('react'));
  247. }
  248. /**
  249. * Sets the audio input device to the one with the id that is passed.
  250. *
  251. * @param {string} id - The id of the new device.
  252. * @returns {Promise}
  253. */
  254. _setAudioInputDevice(id) {
  255. return this._setDevice({
  256. id,
  257. kind: 'audioinput'
  258. });
  259. }
  260. /**
  261. * Sets the audio output device to the one with the id that is passed.
  262. *
  263. * @param {string} id - The id of the new device.
  264. * @returns {Promise}
  265. */
  266. _setAudioOutputDevice(id) {
  267. return this._setDevice({
  268. id,
  269. kind: 'audiooutput'
  270. });
  271. }
  272. /**
  273. * Sets the currently used device to the one that is passed.
  274. *
  275. * @param {Object} device - The new device to be used.
  276. * @returns {Promise}
  277. */
  278. _setDevice(device) {
  279. return this._transport.sendRequest({
  280. type: 'devices',
  281. name: 'setDevice',
  282. device
  283. });
  284. }
  285. /**
  286. * Sets the video input device to the one with the id that is passed.
  287. *
  288. * @param {string} id - The id of the new device.
  289. * @returns {Promise}
  290. */
  291. _setVideoInputDevice(id) {
  292. return this._setDevice({
  293. id,
  294. kind: 'videoinput'
  295. });
  296. }
  297. /**
  298. * Updates the available devices.
  299. *
  300. * @returns {void}
  301. */
  302. _updateAvailableDevices() {
  303. this._getAvailableDevices().then(devices =>
  304. this._changeDialogProps({ availableDevices: devices })
  305. );
  306. }
  307. }