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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // @flow
  2. import { JitsiTrackErrors } from '../lib-jitsi-meet';
  3. import { getLocalParticipant } from '../participants';
  4. import { toState } from '../redux';
  5. import {
  6. AVATAR_ID_COMMAND,
  7. AVATAR_URL_COMMAND,
  8. EMAIL_COMMAND,
  9. JITSI_CONFERENCE_URL_KEY,
  10. VIDEO_QUALITY_LEVELS
  11. } from './constants';
  12. const logger = require('jitsi-meet-logger').getLogger(__filename);
  13. /**
  14. * Attach a set of local tracks to a conference.
  15. *
  16. * @param {JitsiConference} conference - Conference instance.
  17. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  18. * @protected
  19. * @returns {Promise}
  20. */
  21. export function _addLocalTracksToConference(
  22. conference: { addTrack: Function, getLocalTracks: Function },
  23. localTracks: Array<Object>) {
  24. const conferenceLocalTracks = conference.getLocalTracks();
  25. const promises = [];
  26. for (const track of localTracks) {
  27. // XXX The library lib-jitsi-meet may be draconian, for example, when
  28. // adding one and the same video track multiple times.
  29. if (conferenceLocalTracks.indexOf(track) === -1) {
  30. promises.push(
  31. conference.addTrack(track).catch(err => {
  32. _reportError(
  33. 'Failed to add local track to conference',
  34. err);
  35. }));
  36. }
  37. }
  38. return Promise.all(promises);
  39. }
  40. /**
  41. * Evaluates a specific predicate for each {@link JitsiConference} known to the
  42. * redux state features/base/conference while it returns {@code true}.
  43. *
  44. * @param {Function | Object} stateful - The redux store, state, or
  45. * {@code getState} function.
  46. * @param {Function} predicate - The predicate to evaluate for each
  47. * {@code JitsiConference} know to the redux state features/base/conference
  48. * while it returns {@code true}.
  49. * @returns {boolean} If the specified {@code predicate} returned {@code true}
  50. * for all {@code JitsiConference} instances known to the redux state
  51. * features/base/conference.
  52. */
  53. export function forEachConference(
  54. stateful: Function | Object,
  55. predicate: (Object, URL) => boolean) {
  56. const state = toState(stateful)['features/base/conference'];
  57. for (const v of Object.values(state)) {
  58. // Does the value of the base/conference's property look like a
  59. // JitsiConference?
  60. if (v && typeof v === 'object') {
  61. // $FlowFixMe
  62. const url: URL = v[JITSI_CONFERENCE_URL_KEY];
  63. // XXX The Web version of Jitsi Meet does not utilize
  64. // JITSI_CONFERENCE_URL_KEY at the time of this writing. An
  65. // alternative is necessary then to recognize JitsiConference
  66. // instances and myUserId is as good as any other property.
  67. if ((url || typeof v.myUserId === 'function')
  68. && !predicate(v, url)) {
  69. return false;
  70. }
  71. }
  72. }
  73. return true;
  74. }
  75. /**
  76. * Returns the current {@code JitsiConference} which is joining or joined and is
  77. * not leaving. Please note the contrast with merely reading the
  78. * {@code conference} state of the feature base/conference which is not joining
  79. * but may be leaving already.
  80. *
  81. * @param {Function|Object} stateful - The redux store, state, or
  82. * {@code getState} function.
  83. * @returns {JitsiConference|undefined}
  84. */
  85. export function getCurrentConference(stateful: Function | Object) {
  86. const { conference, joining, leaving }
  87. = toState(stateful)['features/base/conference'];
  88. return (
  89. conference
  90. ? conference === leaving ? undefined : conference
  91. : joining);
  92. }
  93. /**
  94. * Finds the nearest match for the passed in {@link availableHeight} to am
  95. * enumerated value in {@code VIDEO_QUALITY_LEVELS}.
  96. *
  97. * @param {number} availableHeight - The height to which a matching video
  98. * quality level should be found.
  99. * @returns {number} The closest matching value from
  100. * {@code VIDEO_QUALITY_LEVELS}.
  101. */
  102. export function getNearestReceiverVideoQualityLevel(availableHeight: number) {
  103. const qualityLevels = [
  104. VIDEO_QUALITY_LEVELS.HIGH,
  105. VIDEO_QUALITY_LEVELS.STANDARD,
  106. VIDEO_QUALITY_LEVELS.LOW
  107. ];
  108. let selectedLevel = qualityLevels[0];
  109. for (let i = 1; i < qualityLevels.length; i++) {
  110. const previousValue = qualityLevels[i - 1];
  111. const currentValue = qualityLevels[i];
  112. const diffWithCurrent = Math.abs(availableHeight - currentValue);
  113. const diffWithPrevious = Math.abs(availableHeight - previousValue);
  114. if (diffWithCurrent < diffWithPrevious) {
  115. selectedLevel = currentValue;
  116. }
  117. }
  118. return selectedLevel;
  119. }
  120. /**
  121. * Handle an error thrown by the backend (i.e. lib-jitsi-meet) while
  122. * manipulating a conference participant (e.g. pin or select participant).
  123. *
  124. * @param {Error} err - The Error which was thrown by the backend while
  125. * manipulating a conference participant and which is to be handled.
  126. * @protected
  127. * @returns {void}
  128. */
  129. export function _handleParticipantError(err: { message: ?string }) {
  130. // XXX DataChannels are initialized at some later point when the conference
  131. // has multiple participants, but code that pins or selects a participant
  132. // might be executed before. So here we're swallowing a particular error.
  133. // TODO Lib-jitsi-meet should be fixed to not throw such an exception in
  134. // these scenarios.
  135. if (err.message !== 'Data channels support is disabled!') {
  136. throw err;
  137. }
  138. }
  139. /**
  140. * Determines whether a specific string is a valid room name.
  141. *
  142. * @param {(string|undefined)} room - The name of the conference room to check
  143. * for validity.
  144. * @returns {boolean} If the specified room name is valid, then true; otherwise,
  145. * false.
  146. */
  147. export function isRoomValid(room: ?string) {
  148. return typeof room === 'string' && room !== '';
  149. }
  150. /**
  151. * Remove a set of local tracks from a conference.
  152. *
  153. * @param {JitsiConference} conference - Conference instance.
  154. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  155. * @protected
  156. * @returns {Promise}
  157. */
  158. export function _removeLocalTracksFromConference(
  159. conference: { removeTrack: Function },
  160. localTracks: Array<Object>) {
  161. return Promise.all(localTracks.map(track =>
  162. conference.removeTrack(track)
  163. .catch(err => {
  164. // Local track might be already disposed by direct
  165. // JitsiTrack#dispose() call. So we should ignore this error
  166. // here.
  167. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  168. _reportError(
  169. 'Failed to remove local track from conference',
  170. err);
  171. }
  172. })
  173. ));
  174. }
  175. /**
  176. * Reports a specific Error with a specific error message. While the
  177. * implementation merely logs the specified msg and err via the console at the
  178. * time of this writing, the intention of the function is to abstract the
  179. * reporting of errors and facilitate elaborating on it in the future.
  180. *
  181. * @param {string} msg - The error message to report.
  182. * @param {Error} err - The Error to report.
  183. * @private
  184. * @returns {void}
  185. */
  186. function _reportError(msg, err) {
  187. // TODO This is a good point to call some global error handler when we have
  188. // one.
  189. logger.error(msg, err);
  190. }
  191. /**
  192. * Sends a representation of the local participant such as her avatar (URL),
  193. * e-mail address, and display name to (the remote participants of) a specific
  194. * conference.
  195. *
  196. * @param {Function|Object} stateful - The redux store, state, or
  197. * {@code getState} function.
  198. * @param {JitsiConference} conference - The {@code JitsiConference} to which
  199. * the representation of the local participant is to be sent.
  200. * @returns {void}
  201. */
  202. export function sendLocalParticipant(
  203. stateful: Function | Object,
  204. conference: {
  205. sendCommand: Function,
  206. setDisplayName: Function,
  207. setLocalParticipantProperty: Function }) {
  208. const {
  209. avatarID,
  210. avatarURL,
  211. email,
  212. features,
  213. name
  214. } = getLocalParticipant(stateful);
  215. avatarID && conference.sendCommand(AVATAR_ID_COMMAND, {
  216. value: avatarID
  217. });
  218. avatarURL && conference.sendCommand(AVATAR_URL_COMMAND, {
  219. value: avatarURL
  220. });
  221. email && conference.sendCommand(EMAIL_COMMAND, {
  222. value: email
  223. });
  224. if (features && features['screen-sharing'] === 'true') {
  225. conference.setLocalParticipantProperty('features_screen-sharing', true);
  226. }
  227. conference.setDisplayName(name);
  228. }