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

functions.js 11KB

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