選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

functions.js 11KB

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