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.js 8.2KB

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