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

functions.js 10KB

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