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

functions.web.ts 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 logger from './logger';
  8. import { IDevicesState } from './types';
  9. export * from './functions.any';
  10. const webrtcKindToJitsiKindTranslator = {
  11. audioinput: 'audioInput',
  12. audiooutput: 'audioOutput',
  13. videoinput: 'videoInput'
  14. };
  15. /**
  16. * Detects the use case when the labels are not available if the A/V permissions
  17. * are not yet granted.
  18. *
  19. * @param {Object} state - The redux state.
  20. * @returns {boolean} - True if the labels are already initialized and false
  21. * otherwise.
  22. */
  23. export function areDeviceLabelsInitialized(state: IReduxState) {
  24. // TODO: Replace with something that doesn't use APP when the conference.js logic is reactified.
  25. if (APP.conference._localTracksInitialized) {
  26. return true;
  27. }
  28. for (const type of [ 'audioInput', 'audioOutput', 'videoInput' ]) {
  29. const availableDevices = state['features/base/devices'].availableDevices;
  30. if ((availableDevices[type as keyof typeof availableDevices] || []).find(d => Boolean(d.label))) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. /**
  37. * Get device id of the audio output device which is currently in use.
  38. * Empty string stands for default device.
  39. *
  40. * @returns {string}
  41. */
  42. export function getAudioOutputDeviceId() {
  43. return JitsiMeetJS.mediaDevices.getAudioOutputDevice();
  44. }
  45. /**
  46. * Finds the real device id of the default device of the given type.
  47. *
  48. * @param {Object} state - The redux state.
  49. * @param {*} kind - The type of the device. One of "audioInput",
  50. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  51. * of the preceding types.
  52. * @returns {string|undefined}
  53. */
  54. export function getDefaultDeviceId(state: IReduxState, kind: string) {
  55. const kindToSearch = webrtcKindToJitsiKindTranslator[kind as keyof typeof webrtcKindToJitsiKindTranslator] || kind;
  56. const availableDevices = state['features/base/devices'].availableDevices;
  57. const defaultDevice = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  58. .find(d => d.deviceId === 'default');
  59. // Find the device with a matching group id.
  60. const matchingDevice = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  61. .find(d => d.deviceId !== 'default' && d.groupId === defaultDevice?.groupId);
  62. if (matchingDevice) {
  63. return matchingDevice.deviceId;
  64. }
  65. }
  66. /**
  67. * Finds a device with a label that matches the passed label and returns its id.
  68. *
  69. * @param {Object} state - The redux state.
  70. * @param {string} label - The label.
  71. * @param {string} kind - The type of the device. One of "audioInput",
  72. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  73. * of the preceding types.
  74. * @returns {string|undefined}
  75. */
  76. export function getDeviceIdByLabel(state: IReduxState, label: string, kind: string) {
  77. const kindToSearch = webrtcKindToJitsiKindTranslator[kind as keyof typeof webrtcKindToJitsiKindTranslator] || kind;
  78. const availableDevices = state['features/base/devices'].availableDevices;
  79. const device
  80. = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  81. .find(d => d.label === label);
  82. if (device) {
  83. return device.deviceId;
  84. }
  85. }
  86. /**
  87. * Finds a device with a label that matches the passed id and returns its label.
  88. *
  89. * @param {Object} state - The redux state.
  90. * @param {string} id - The device id.
  91. * @param {string} kind - The type of the device. One of "audioInput",
  92. * "audioOutput", and "videoInput". Also supported is all lowercase versions
  93. * of the preceding types.
  94. * @returns {string|undefined}
  95. */
  96. export function getDeviceLabelById(state: IReduxState, id: string, kind: string) {
  97. const kindToSearch = webrtcKindToJitsiKindTranslator[kind as keyof typeof webrtcKindToJitsiKindTranslator] || kind;
  98. const availableDevices = state['features/base/devices'].availableDevices;
  99. const device
  100. = (availableDevices[kindToSearch as keyof typeof availableDevices] || [])
  101. .find(d => d.deviceId === id);
  102. if (device) {
  103. return device.label;
  104. }
  105. }
  106. /**
  107. * Returns the devices set in the URL.
  108. *
  109. * @param {Object} state - The redux state.
  110. * @returns {Object|undefined}
  111. */
  112. export function getDevicesFromURL(state: IReduxState) {
  113. const urlParams
  114. = parseURLParams(state['features/base/connection'].locationURL ?? '');
  115. const audioOutput = urlParams['devices.audioOutput'];
  116. const videoInput = urlParams['devices.videoInput'];
  117. const audioInput = urlParams['devices.audioInput'];
  118. if (!audioOutput && !videoInput && !audioInput) {
  119. return undefined;
  120. }
  121. const devices: IDevicesState['availableDevices'] = {};
  122. audioOutput && (devices.audioOutput = audioOutput);
  123. videoInput && (devices.videoInput = videoInput);
  124. audioInput && (devices.audioInput = audioInput);
  125. return devices;
  126. }
  127. /**
  128. * Converts an array of media devices into an object organized by device kind.
  129. *
  130. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  131. * @private
  132. * @returns {Object} An object with the media devices split by type. The keys
  133. * are device type and the values are arrays with devices matching the device
  134. * type.
  135. */
  136. export function groupDevicesByKind(devices: MediaDeviceInfo[]): IDevicesState['availableDevices'] {
  137. return {
  138. audioInput: devices.filter(device => device.kind === 'audioinput'),
  139. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  140. videoInput: devices.filter(device => device.kind === 'videoinput')
  141. };
  142. }
  143. /**
  144. * Filters audio devices from a list of MediaDeviceInfo objects.
  145. *
  146. * @param {Array<MediaDeviceInfo>} devices - Unfiltered media devices.
  147. * @private
  148. * @returns {Array<MediaDeviceInfo>} Filtered audio devices.
  149. */
  150. export function filterAudioDevices(devices: MediaDeviceInfo[]) {
  151. return devices.filter(device => device.kind === 'audioinput');
  152. }
  153. /**
  154. * We want to strip any device details that are not very user friendly, like usb ids put in brackets at the end.
  155. *
  156. * @param {string} label - Device label to format.
  157. *
  158. * @returns {string} - Formatted string.
  159. */
  160. export function formatDeviceLabel(label: string) {
  161. let formattedLabel = label;
  162. // Remove braked description at the end as it contains non user friendly strings i.e.
  163. // Microsoft® LifeCam HD-3000 (045e:0779:31dg:d1231)
  164. const ix = formattedLabel.lastIndexOf('(');
  165. if (ix !== -1) {
  166. formattedLabel = formattedLabel.substr(0, ix);
  167. }
  168. return formattedLabel;
  169. }
  170. /**
  171. * Returns a list of objects containing all the microphone device ids and labels.
  172. *
  173. * @param {Object} state - The state of the application.
  174. * @returns {Object[]}
  175. */
  176. export function getAudioInputDeviceData(state: IReduxState) {
  177. return state['features/base/devices'].availableDevices.audioInput?.map(
  178. ({ deviceId, label }) => {
  179. return {
  180. deviceId,
  181. label
  182. };
  183. });
  184. }
  185. /**
  186. * Returns a list of objectes containing all the output device ids and labels.
  187. *
  188. * @param {Object} state - The state of the application.
  189. * @returns {Object[]}
  190. */
  191. export function getAudioOutputDeviceData(state: IReduxState) {
  192. return state['features/base/devices'].availableDevices.audioOutput?.map(
  193. ({ deviceId, label }) => {
  194. return {
  195. deviceId,
  196. label
  197. };
  198. });
  199. }
  200. /**
  201. * Returns a list of all the camera device ids.
  202. *
  203. * @param {Object} state - The state of the application.
  204. * @returns {string[]}
  205. */
  206. export function getVideoDeviceIds(state: IReduxState) {
  207. return state['features/base/devices'].availableDevices.videoInput?.map(({ deviceId }) => deviceId);
  208. }
  209. /**
  210. * Set device id of the audio output device which is currently in use.
  211. * Empty string stands for default device.
  212. *
  213. * @param {string} newId - New audio output device id.
  214. * @param {Function} dispatch - The Redux dispatch function.
  215. * @param {boolean} userSelection - Whether this is a user selection update.
  216. * @param {?string} newLabel - New audio output device label to store.
  217. * @returns {Promise}
  218. */
  219. export function setAudioOutputDeviceId(
  220. newId = 'default',
  221. dispatch: IStore['dispatch'],
  222. userSelection = false,
  223. newLabel?: string): Promise<any> {
  224. logger.debug(`setAudioOutputDevice: ${String(newLabel)}[${newId}]`);
  225. if (!JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  226. logger.warn('Adjusting audio output is not supported');
  227. return Promise.resolve();
  228. }
  229. return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
  230. .then(() => {
  231. dispatch(setNewAudioOutputDevice(newId));
  232. const newSettings: Partial<ISettingsState> = {
  233. audioOutputDeviceId: newId,
  234. userSelectedAudioOutputDeviceId: undefined,
  235. userSelectedAudioOutputDeviceLabel: undefined
  236. };
  237. if (userSelection) {
  238. newSettings.userSelectedAudioOutputDeviceId = newId;
  239. newSettings.userSelectedAudioOutputDeviceLabel = newLabel;
  240. } else {
  241. // a flow workaround, I needed to add 'userSelectedAudioOutputDeviceId: undefined'
  242. delete newSettings.userSelectedAudioOutputDeviceId;
  243. delete newSettings.userSelectedAudioOutputDeviceLabel;
  244. }
  245. return dispatch(updateSettings(newSettings));
  246. });
  247. }