Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. import logger from './logger';
  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
  147. || subject
  148. || callDisplayName
  149. || (callee && callee.name)
  150. || _.startCase(decodeURIComponent(room));
  151. }
  152. /**
  153. * Returns the current {@code JitsiConference} which is joining or joined and is
  154. * not leaving. Please note the contrast with merely reading the
  155. * {@code conference} state of the feature base/conference which is not joining
  156. * but may be leaving already.
  157. *
  158. * @param {Function|Object} stateful - The redux store, state, or
  159. * {@code getState} function.
  160. * @returns {JitsiConference|undefined}
  161. */
  162. export function getCurrentConference(stateful: Function | Object) {
  163. const { conference, joining, leaving }
  164. = toState(stateful)['features/base/conference'];
  165. return (
  166. conference
  167. ? conference === leaving ? undefined : conference
  168. : joining);
  169. }
  170. /**
  171. * Finds the nearest match for the passed in {@link availableHeight} to am
  172. * enumerated value in {@code VIDEO_QUALITY_LEVELS}.
  173. *
  174. * @param {number} availableHeight - The height to which a matching video
  175. * quality level should be found.
  176. * @returns {number} The closest matching value from
  177. * {@code VIDEO_QUALITY_LEVELS}.
  178. */
  179. export function getNearestReceiverVideoQualityLevel(availableHeight: number) {
  180. const qualityLevels = [
  181. VIDEO_QUALITY_LEVELS.HIGH,
  182. VIDEO_QUALITY_LEVELS.STANDARD,
  183. VIDEO_QUALITY_LEVELS.LOW
  184. ];
  185. let selectedLevel = qualityLevels[0];
  186. for (let i = 1; i < qualityLevels.length; i++) {
  187. const previousValue = qualityLevels[i - 1];
  188. const currentValue = qualityLevels[i];
  189. const diffWithCurrent = Math.abs(availableHeight - currentValue);
  190. const diffWithPrevious = Math.abs(availableHeight - previousValue);
  191. if (diffWithCurrent < diffWithPrevious) {
  192. selectedLevel = currentValue;
  193. }
  194. }
  195. return selectedLevel;
  196. }
  197. /**
  198. * Handle an error thrown by the backend (i.e. {@code lib-jitsi-meet}) while
  199. * manipulating a conference participant (e.g. Pin or select participant).
  200. *
  201. * @param {Error} err - The Error which was thrown by the backend while
  202. * manipulating a conference participant and which is to be handled.
  203. * @protected
  204. * @returns {void}
  205. */
  206. export function _handleParticipantError(err: { message: ?string }) {
  207. // XXX DataChannels are initialized at some later point when the conference
  208. // has multiple participants, but code that pins or selects a participant
  209. // might be executed before. So here we're swallowing a particular error.
  210. // TODO Lib-jitsi-meet should be fixed to not throw such an exception in
  211. // these scenarios.
  212. if (err.message !== 'Data channels support is disabled!') {
  213. throw err;
  214. }
  215. }
  216. /**
  217. * Determines whether a specific string is a valid room name.
  218. *
  219. * @param {(string|undefined)} room - The name of the conference room to check
  220. * for validity.
  221. * @returns {boolean} If the specified room name is valid, then true; otherwise,
  222. * false.
  223. */
  224. export function isRoomValid(room: ?string) {
  225. return typeof room === 'string' && room !== '';
  226. }
  227. /**
  228. * Remove a set of local tracks from a conference.
  229. *
  230. * @param {JitsiConference} conference - Conference instance.
  231. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  232. * @protected
  233. * @returns {Promise}
  234. */
  235. export function _removeLocalTracksFromConference(
  236. conference: { removeTrack: Function },
  237. localTracks: Array<Object>) {
  238. return Promise.all(localTracks.map(track =>
  239. conference.removeTrack(track)
  240. .catch(err => {
  241. // Local track might be already disposed by direct
  242. // JitsiTrack#dispose() call. So we should ignore this error
  243. // here.
  244. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  245. _reportError(
  246. 'Failed to remove local track from conference',
  247. err);
  248. }
  249. })
  250. ));
  251. }
  252. /**
  253. * Reports a specific Error with a specific error message. While the
  254. * implementation merely logs the specified msg and err via the console at the
  255. * time of this writing, the intention of the function is to abstract the
  256. * reporting of errors and facilitate elaborating on it in the future.
  257. *
  258. * @param {string} msg - The error message to report.
  259. * @param {Error} err - The Error to report.
  260. * @private
  261. * @returns {void}
  262. */
  263. function _reportError(msg, err) {
  264. // TODO This is a good point to call some global error handler when we have
  265. // one.
  266. logger.error(msg, err);
  267. }
  268. /**
  269. * Sends a representation of the local participant such as her avatar (URL),
  270. * e-mail address, and display name to (the remote participants of) a specific
  271. * conference.
  272. *
  273. * @param {Function|Object} stateful - The redux store, state, or
  274. * {@code getState} function.
  275. * @param {JitsiConference} conference - The {@code JitsiConference} to which
  276. * the representation of the local participant is to be sent.
  277. * @returns {void}
  278. */
  279. export function sendLocalParticipant(
  280. stateful: Function | Object,
  281. conference: {
  282. sendCommand: Function,
  283. setDisplayName: Function,
  284. setLocalParticipantProperty: Function }) {
  285. const {
  286. avatarID,
  287. avatarURL,
  288. email,
  289. features,
  290. name
  291. } = getLocalParticipant(stateful);
  292. avatarID && conference.sendCommand(AVATAR_ID_COMMAND, {
  293. value: avatarID
  294. });
  295. avatarURL && conference.sendCommand(AVATAR_URL_COMMAND, {
  296. value: avatarURL
  297. });
  298. email && conference.sendCommand(EMAIL_COMMAND, {
  299. value: email
  300. });
  301. if (features && features['screen-sharing'] === 'true') {
  302. conference.setLocalParticipantProperty('features_screen-sharing', true);
  303. }
  304. conference.setDisplayName(name);
  305. }