您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.web.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 the devices that start with one of the prefixes from DEVICE_LABEL_PREFIXES_TO_IGNORE.
  146. *
  147. * @param {MediaDeviceInfo[]} devices - The devices to be filtered.
  148. * @returns {MediaDeviceInfo[]} - The filtered devices.
  149. */
  150. export function filterIgnoredDevices(devices: MediaDeviceInfo[] = []) {
  151. const ignoredDevices: MediaDeviceInfo[] = [];
  152. const filteredDevices = devices.filter(device => {
  153. if (!device.label) {
  154. return true;
  155. }
  156. if (DEVICE_LABEL_PREFIXES_TO_IGNORE.find(prefix => device.label?.startsWith(prefix))) {
  157. ignoredDevices.push(device);
  158. return false;
  159. }
  160. return true;
  161. });
  162. return {
  163. filteredDevices,
  164. ignoredDevices
  165. };
  166. }
  167. /**
  168. * Check if the passed device arrays are different.
  169. *
  170. * @param {MediaDeviceInfo[]} devices1 - Array with devices to be compared.
  171. * @param {MediaDeviceInfo[]} devices2 - Array with devices to be compared.
  172. * @returns {boolean} - True if the device arrays are different and false otherwise.
  173. */
  174. export function areDevicesDifferent(devices1: MediaDeviceInfo[] = [], devices2: MediaDeviceInfo[] = []) {
  175. if (devices1.length !== devices2.length) {
  176. return true;
  177. }
  178. for (let i = 0; i < devices1.length; i++) {
  179. const device1 = devices1[i];
  180. const found = devices2.find(({ deviceId, groupId, kind, label }) =>
  181. device1.deviceId === deviceId
  182. && device1.groupId === groupId
  183. && device1.kind === kind
  184. && device1.label === label
  185. );
  186. if (!found) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Flattens the availableDevices from redux.
  194. *
  195. * @param {IDevicesState.availableDevices} devices - The available devices from redux.
  196. * @returns {MediaDeviceInfo[]} - The flattened array of devices.
  197. */
  198. export function flattenAvailableDevices(
  199. { audioInput = [], audioOutput = [], videoInput = [] }: IDevicesState['availableDevices']) {
  200. return audioInput.concat(audioOutput).concat(videoInput);
  201. }
  202. /**
  203. * We want to strip any device details that are not very user friendly, like usb ids put in brackets at the end.
  204. *
  205. * @param {string} label - Device label to format.
  206. *
  207. * @returns {string} - Formatted string.
  208. */
  209. export function formatDeviceLabel(label: string) {
  210. let formattedLabel = label;
  211. // Remove braked description at the end as it contains non user friendly strings i.e.
  212. // Microsoft® LifeCam HD-3000 (045e:0779:31dg:d1231)
  213. const ix = formattedLabel.lastIndexOf('(');
  214. if (ix !== -1) {
  215. formattedLabel = formattedLabel.substr(0, ix);
  216. }
  217. return formattedLabel;
  218. }
  219. /**
  220. * Returns a list of objects containing all the microphone device ids and labels.
  221. *
  222. * @param {Object} state - The state of the application.
  223. * @returns {Object[]}
  224. */
  225. export function getAudioInputDeviceData(state: IReduxState) {
  226. return state['features/base/devices'].availableDevices.audioInput?.map(
  227. ({ deviceId, label }) => {
  228. return {
  229. deviceId,
  230. label
  231. };
  232. });
  233. }
  234. /**
  235. * Returns a list of objectes containing all the output device ids and labels.
  236. *
  237. * @param {Object} state - The state of the application.
  238. * @returns {Object[]}
  239. */
  240. export function getAudioOutputDeviceData(state: IReduxState) {
  241. return state['features/base/devices'].availableDevices.audioOutput?.map(
  242. ({ deviceId, label }) => {
  243. return {
  244. deviceId,
  245. label
  246. };
  247. });
  248. }
  249. /**
  250. * Returns a list of all the camera device ids.
  251. *
  252. * @param {Object} state - The state of the application.
  253. * @returns {string[]}
  254. */
  255. export function getVideoDeviceIds(state: IReduxState) {
  256. return state['features/base/devices'].availableDevices.videoInput?.map(({ deviceId }) => deviceId);
  257. }
  258. /**
  259. * Converts an array of device info objects into string.
  260. *
  261. * @param {MediaDeviceInfo[]} devices - The devices.
  262. * @returns {string}
  263. */
  264. function devicesToStr(devices?: MediaDeviceInfo[]) {
  265. return devices?.map(device => `\t\t${device.label}[${device.deviceId}]`).join('\n');
  266. }
  267. /**
  268. * Logs an array of devices.
  269. *
  270. * @param {MediaDeviceInfo[]} devices - The array of devices.
  271. * @param {string} title - The title that will be printed in the log.
  272. * @returns {void}
  273. */
  274. export function logDevices(devices: MediaDeviceInfo[], title = '') {
  275. const deviceList = groupDevicesByKind(devices);
  276. const audioInputs = devicesToStr(deviceList.audioInput);
  277. const audioOutputs = devicesToStr(deviceList.audioOutput);
  278. const videoInputs = devicesToStr(deviceList.videoInput);
  279. logger.debug(`${title}:\n`
  280. + `audioInput:\n${audioInputs}\n`
  281. + `audioOutput:\n${audioOutputs}\n`
  282. + `videoInput:\n${videoInputs}`);
  283. }
  284. /**
  285. * Set device id of the audio output device which is currently in use.
  286. * Empty string stands for default device.
  287. *
  288. * @param {string} newId - New audio output device id.
  289. * @param {Function} dispatch - The Redux dispatch function.
  290. * @param {boolean} userSelection - Whether this is a user selection update.
  291. * @param {?string} newLabel - New audio output device label to store.
  292. * @returns {Promise}
  293. */
  294. export function setAudioOutputDeviceId(
  295. newId = 'default',
  296. dispatch: IStore['dispatch'],
  297. userSelection = false,
  298. newLabel?: string): Promise<any> {
  299. logger.debug(`setAudioOutputDevice: ${String(newLabel)}[${newId}]`);
  300. if (!JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  301. logger.warn('Adjusting audio output is not supported');
  302. return Promise.resolve();
  303. }
  304. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  305. .then(() => {
  306. dispatch(setNewAudioOutputDevice(newId));
  307. const newSettings: Partial<ISettingsState> = {
  308. audioOutputDeviceId: newId,
  309. userSelectedAudioOutputDeviceId: undefined,
  310. userSelectedAudioOutputDeviceLabel: undefined
  311. };
  312. if (userSelection) {
  313. newSettings.userSelectedAudioOutputDeviceId = newId;
  314. newSettings.userSelectedAudioOutputDeviceLabel = newLabel;
  315. } else {
  316. // a flow workaround, I needed to add 'userSelectedAudioOutputDeviceId: undefined'
  317. delete newSettings.userSelectedAudioOutputDeviceId;
  318. delete newSettings.userSelectedAudioOutputDeviceLabel;
  319. }
  320. return dispatch(updateSettings(newSettings));
  321. });
  322. }