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.

functions.web.ts 9.4KB

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