Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

functions.js 11KB

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