Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

functions.web.ts 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import { IReduxState, IStore } from '../../app/types';
  2. import JitsiMeetJS from '../lib-jitsi-meet';
  3. import { updateSettings } from '../settings/actions';
  4. import { ISettingsState } from '../settings/reducer';
  5. import { setNewAudioOutputDevice } from '../sounds/functions.web';
  6. import { parseURLParams } from '../util/parseURLParams';
  7. import { DEVICE_LABEL_PREFIXES_TO_IGNORE } from './constants';
  8. import logger from './logger';
  9. import { IDevicesState } from './types';
  10. export * from './functions.any';
  11. const webrtcKindToJitsiKindTranslator = {
  12. audioinput: 'audioInput',
  13. audiooutput: 'audioOutput',
  14. videoinput: 'videoInput'
  15. };
  16. /**
  17. * Detects the use case when the labels are not available if the A/V permissions
  18. * are not yet granted.
  19. *
  20. * @param {Object} state - The redux state.
  21. * @returns {boolean} - True if the labels are already initialized and false
  22. * otherwise.
  23. */
  24. export function areDeviceLabelsInitialized(state: IReduxState) {
  25. // TODO: Replace with something that doesn't use APP when the conference.js logic is reactified.
  26. if (APP.conference._localTracksInitialized) {
  27. return true;
  28. }
  29. for (const type of [ 'audioInput', 'audioOutput', 'videoInput' ]) {
  30. const availableDevices = state['features/base/devices'].availableDevices;
  31. if ((availableDevices[type as keyof typeof availableDevices] || []).find(d => Boolean(d.label))) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. /**
  38. * Get device id of the audio output device which is currently in use.
  39. * Empty string stands for default device.
  40. *
  41. * @returns {string}
  42. */
  43. export function getAudioOutputDeviceId() {
  44. return JitsiMeetJS.mediaDevices.getAudioOutputDevice();
  45. }
  46. /**
  47. * Finds the real device id of the default device of the given type.
  48. *
  49. * @param {Object} state - The redux state.
  50. * @param {*} kind - The type of the device. One of "audioInput",
  51. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  52. * of the preceding types.
  53. * @returns {string|undefined}
  54. */
  55. export function getDefaultDeviceId(state: IReduxState, kind: string) {
  56. const kindToSearch = webrtcKindToJitsiKindTranslator[kind as keyof typeof webrtcKindToJitsiKindTranslator] || kind;
  57. const availableDevices = state['features/base/devices'].availableDevices;
  58. const defaultDevice = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  59. .find(d => d.deviceId === 'default');
  60. // Find the device with a matching group id.
  61. const matchingDevice = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  62. .find(d => d.deviceId !== 'default' && d.groupId === defaultDevice?.groupId);
  63. if (matchingDevice) {
  64. return matchingDevice.deviceId;
  65. }
  66. }
  67. /**
  68. * Finds a device with a label that matches the passed label and returns its id.
  69. *
  70. * @param {Object} state - The redux state.
  71. * @param {string} label - The label.
  72. * @param {string} kind - The type of the device. One of "audioInput",
  73. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  74. * of the preceding types.
  75. * @returns {string|undefined}
  76. */
  77. export function getDeviceIdByLabel(state: IReduxState, label: string, kind: string) {
  78. const kindToSearch = webrtcKindToJitsiKindTranslator[kind as keyof typeof webrtcKindToJitsiKindTranslator] || kind;
  79. const availableDevices = state['features/base/devices'].availableDevices;
  80. const device
  81. = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  82. .find(d => d.label === label);
  83. if (device) {
  84. return device.deviceId;
  85. }
  86. }
  87. /**
  88. * Finds a device with a label that matches the passed id and returns its label.
  89. *
  90. * @param {Object} state - The redux state.
  91. * @param {string} id - The device id.
  92. * @param {string} kind - The type of the device. One of "audioInput",
  93. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  94. * of the preceding types.
  95. * @returns {string|undefined}
  96. */
  97. export function getDeviceLabelById(state: IReduxState, id: string, kind: string) {
  98. const kindToSearch = webrtcKindToJitsiKindTranslator[kind as keyof typeof webrtcKindToJitsiKindTranslator] || kind;
  99. const availableDevices = state['features/base/devices'].availableDevices;
  100. const device
  101. = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  102. .find(d => d.deviceId === id);
  103. if (device) {
  104. return device.label;
  105. }
  106. }
  107. /**
  108. * Returns the devices set in the URL.
  109. *
  110. * @param {Object} state - The redux state.
  111. * @returns {Object|undefined}
  112. */
  113. export function getDevicesFromURL(state: IReduxState) {
  114. const urlParams
  115. = parseURLParams(state['features/base/connection'].locationURL ?? '');
  116. const audioOutput = urlParams['devices.audioOutput'];
  117. const videoInput = urlParams['devices.videoInput'];
  118. const audioInput = urlParams['devices.audioInput'];
  119. if (!audioOutput && !videoInput && !audioInput) {
  120. return undefined;
  121. }
  122. const devices: IDevicesState['availableDevices'] = {};
  123. audioOutput && (devices.audioOutput = audioOutput);
  124. videoInput && (devices.videoInput = videoInput);
  125. audioInput && (devices.audioInput = audioInput);
  126. return devices;
  127. }
  128. /**
  129. * Converts an array of media devices into an object organized by device kind.
  130. *
  131. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  132. * @private
  133. * @returns {Object} An object with the media devices split by type. The keys
  134. * are device type and the values are arrays with devices matching the device
  135. * type.
  136. */
  137. export function groupDevicesByKind(devices: MediaDeviceInfo[]): IDevicesState['availableDevices'] {
  138. return {
  139. audioInput: devices.filter(device => device.kind === 'audioinput'),
  140. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  141. videoInput: devices.filter(device => device.kind === 'videoinput')
  142. };
  143. }
  144. /**
  145. * Filters audio devices from a list of MediaDeviceInfo objects.
  146. *
  147. * @param {Array<MediaDeviceInfo>} devices - Unfiltered media devices.
  148. * @private
  149. * @returns {Array<MediaDeviceInfo>} Filtered audio devices.
  150. */
  151. export function filterAudioDevices(devices: MediaDeviceInfo[]) {
  152. return devices.filter(device => device.kind === 'audioinput');
  153. }
  154. /**
  155. * Filters the devices that start with one of the prefixes from DEVICE_LABEL_PREFIXES_TO_IGNORE.
  156. *
  157. * @param {MediaDeviceInfo[]} devices - The devices to be filtered.
  158. * @returns {MediaDeviceInfo[]} - The filtered devices.
  159. */
  160. export function filterIgnoredDevices(devices: MediaDeviceInfo[] = []) {
  161. const ignoredDevices: MediaDeviceInfo[] = [];
  162. const filteredDevices = devices.filter(device => {
  163. if (!device.label) {
  164. return true;
  165. }
  166. if (DEVICE_LABEL_PREFIXES_TO_IGNORE.find(prefix => device.label?.startsWith(prefix))) {
  167. ignoredDevices.push(device);
  168. return false;
  169. }
  170. return true;
  171. });
  172. return {
  173. filteredDevices,
  174. ignoredDevices
  175. };
  176. }
  177. /**
  178. * Check if the passed device arrays are different.
  179. *
  180. * @param {MediaDeviceInfo[]} devices1 - Array with devices to be compared.
  181. * @param {MediaDeviceInfo[]} devices2 - Array with devices to be compared.
  182. * @returns {boolean} - True if the device arrays are different and false otherwise.
  183. */
  184. export function areDevicesDifferent(devices1: MediaDeviceInfo[] = [], devices2: MediaDeviceInfo[] = []) {
  185. if (devices1.length !== devices2.length) {
  186. return true;
  187. }
  188. for (let i = 0; i < devices1.length; i++) {
  189. const device1 = devices1[i];
  190. const found = devices2.find(({ deviceId, groupId, kind, label }) =>
  191. device1.deviceId === deviceId
  192. && device1.groupId === groupId
  193. && device1.kind === kind
  194. && device1.label === label
  195. );
  196. if (!found) {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. /**
  203. * Flattens the availableDevices from redux.
  204. *
  205. * @param {IDevicesState.availableDevices} devices - The available devices from redux.
  206. * @returns {MediaDeviceInfo[]} - The flattened array of devices.
  207. */
  208. export function flattenAvailableDevices(
  209. { audioInput = [], audioOutput = [], videoInput = [] }: IDevicesState['availableDevices']) {
  210. return audioInput.concat(audioOutput).concat(videoInput);
  211. }
  212. /**
  213. * We want to strip any device details that are not very user friendly, like usb ids put in brackets at the end.
  214. *
  215. * @param {string} label - Device label to format.
  216. *
  217. * @returns {string} - Formatted string.
  218. */
  219. export function formatDeviceLabel(label: string) {
  220. let formattedLabel = label;
  221. // Remove braked description at the end as it contains non user friendly strings i.e.
  222. // Microsoft® LifeCam HD-3000 (045e:0779:31dg:d1231)
  223. const ix = formattedLabel.lastIndexOf('(');
  224. if (ix !== -1) {
  225. formattedLabel = formattedLabel.substr(0, ix);
  226. }
  227. return formattedLabel;
  228. }
  229. /**
  230. * Returns a list of objects containing all the microphone device ids and labels.
  231. *
  232. * @param {Object} state - The state of the application.
  233. * @returns {Object[]}
  234. */
  235. export function getAudioInputDeviceData(state: IReduxState) {
  236. return state['features/base/devices'].availableDevices.audioInput?.map(
  237. ({ deviceId, label }) => {
  238. return {
  239. deviceId,
  240. label
  241. };
  242. });
  243. }
  244. /**
  245. * Returns a list of objectes containing all the output device ids and labels.
  246. *
  247. * @param {Object} state - The state of the application.
  248. * @returns {Object[]}
  249. */
  250. export function getAudioOutputDeviceData(state: IReduxState) {
  251. return state['features/base/devices'].availableDevices.audioOutput?.map(
  252. ({ deviceId, label }) => {
  253. return {
  254. deviceId,
  255. label
  256. };
  257. });
  258. }
  259. /**
  260. * Returns a list of all the camera device ids.
  261. *
  262. * @param {Object} state - The state of the application.
  263. * @returns {string[]}
  264. */
  265. export function getVideoDeviceIds(state: IReduxState) {
  266. return state['features/base/devices'].availableDevices.videoInput?.map(({ deviceId }) => deviceId);
  267. }
  268. /**
  269. * Converts an array of device info objects into string.
  270. *
  271. * @param {MediaDeviceInfo[]} devices - The devices.
  272. * @returns {string}
  273. */
  274. function devicesToStr(devices?: MediaDeviceInfo[]) {
  275. return devices?.map(device => `\t\t${device.label}[${device.deviceId}]`).join('\n');
  276. }
  277. /**
  278. * Logs an array of devices.
  279. *
  280. * @param {MediaDeviceInfo[]} devices - The array of devices.
  281. * @param {string} title - The title that will be printed in the log.
  282. * @returns {void}
  283. */
  284. export function logDevices(devices: MediaDeviceInfo[], title = '') {
  285. const deviceList = groupDevicesByKind(devices);
  286. const audioInputs = devicesToStr(deviceList.audioInput);
  287. const audioOutputs = devicesToStr(deviceList.audioOutput);
  288. const videoInputs = devicesToStr(deviceList.videoInput);
  289. logger.debug(`${title}:\n`
  290. + `audioInput:\n${audioInputs}\n`
  291. + `audioOutput:\n${audioOutputs}\n`
  292. + `videoInput:\n${videoInputs}`);
  293. }
  294. /**
  295. * Set device id of the audio output device which is currently in use.
  296. * Empty string stands for default device.
  297. *
  298. * @param {string} newId - New audio output device id.
  299. * @param {Function} dispatch - The Redux dispatch function.
  300. * @param {boolean} userSelection - Whether this is a user selection update.
  301. * @param {?string} newLabel - New audio output device label to store.
  302. * @returns {Promise}
  303. */
  304. export function setAudioOutputDeviceId(
  305. newId = 'default',
  306. dispatch: IStore['dispatch'],
  307. userSelection = false,
  308. newLabel?: string): Promise<any> {
  309. logger.debug(`setAudioOutputDevice: ${String(newLabel)}[${newId}]`);
  310. if (!JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  311. logger.warn('Adjusting audio output is not supported');
  312. return Promise.resolve();
  313. }
  314. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  315. .then(() => {
  316. dispatch(setNewAudioOutputDevice(newId));
  317. const newSettings: Partial<ISettingsState> = {
  318. audioOutputDeviceId: newId,
  319. userSelectedAudioOutputDeviceId: undefined,
  320. userSelectedAudioOutputDeviceLabel: undefined
  321. };
  322. if (userSelection) {
  323. newSettings.userSelectedAudioOutputDeviceId = newId;
  324. newSettings.userSelectedAudioOutputDeviceLabel = newLabel;
  325. } else {
  326. // a flow workaround, I needed to add 'userSelectedAudioOutputDeviceId: undefined'
  327. delete newSettings.userSelectedAudioOutputDeviceId;
  328. delete newSettings.userSelectedAudioOutputDeviceLabel;
  329. }
  330. return dispatch(updateSettings(newSettings));
  331. });
  332. }