Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

functions.web.ts 12KB

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