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

functions.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // @flow
  2. import { JitsiTrackErrors } from '../lib-jitsi-meet';
  3. import { getLocalParticipant } from '../participants';
  4. import { toState } from '../redux';
  5. import {
  6. AVATAR_ID_COMMAND,
  7. AVATAR_URL_COMMAND,
  8. EMAIL_COMMAND,
  9. JITSI_CONFERENCE_URL_KEY
  10. } from './constants';
  11. const logger = require('jitsi-meet-logger').getLogger(__filename);
  12. /**
  13. * Attach a set of local tracks to a conference.
  14. *
  15. * @param {JitsiConference} conference - Conference instance.
  16. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  17. * @protected
  18. * @returns {Promise}
  19. */
  20. export function _addLocalTracksToConference(
  21. conference: { addTrack: Function, getLocalTracks: Function },
  22. localTracks: Array<Object>) {
  23. const conferenceLocalTracks = conference.getLocalTracks();
  24. const promises = [];
  25. for (const track of localTracks) {
  26. // XXX The library lib-jitsi-meet may be draconian, for example, when
  27. // adding one and the same video track multiple times.
  28. if (conferenceLocalTracks.indexOf(track) === -1) {
  29. promises.push(
  30. conference.addTrack(track).catch(err => {
  31. _reportError(
  32. 'Failed to add local track to conference',
  33. err);
  34. }));
  35. }
  36. }
  37. return Promise.all(promises);
  38. }
  39. /**
  40. * Evaluates a specific predicate for each {@link JitsiConference} known to the
  41. * redux state features/base/conference while it returns {@code true}.
  42. *
  43. * @param {Function | Object} stateful - The redux store, state, or
  44. * {@code getState} function.
  45. * @param {Function} predicate - The predicate to evaluate for each
  46. * {@code JitsiConference} know to the redux state features/base/conference
  47. * while it returns {@code true}.
  48. * @returns {boolean} If the specified {@code predicate} returned {@code true}
  49. * for all {@code JitsiConference} instances known to the redux state
  50. * features/base/conference.
  51. */
  52. export function forEachConference(
  53. stateful: Function | Object,
  54. predicate: (Object, URL) => boolean) {
  55. const state = toState(stateful)['features/base/conference'];
  56. for (const v of Object.values(state)) {
  57. // Does the value of the base/conference's property look like a
  58. // JitsiConference?
  59. if (v && typeof v === 'object') {
  60. // $FlowFixMe
  61. const url: URL = v[JITSI_CONFERENCE_URL_KEY];
  62. // XXX The Web version of Jitsi Meet does not utilize
  63. // JITSI_CONFERENCE_URL_KEY at the time of this writing. An
  64. // alternative is necessary then to recognize JitsiConference
  65. // instances and myUserId is as good as any other property.
  66. if ((url || typeof v.myUserId === 'function')
  67. && !predicate(v, url)) {
  68. return false;
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74. /**
  75. * Returns the current {@code JitsiConference} which is joining or joined and is
  76. * not leaving. Please note the contrast with merely reading the
  77. * {@code conference} state of the feature base/conference which is not joining
  78. * but may be leaving already.
  79. *
  80. * @param {Function|Object} stateful - The redux store, state, or
  81. * {@code getState} function.
  82. * @returns {JitsiConference|undefined}
  83. */
  84. export function getCurrentConference(stateful: Function | Object) {
  85. const { conference, joining, leaving }
  86. = toState(stateful)['features/base/conference'];
  87. return (
  88. conference
  89. ? conference === leaving ? undefined : conference
  90. : joining);
  91. }
  92. /**
  93. * Handle an error thrown by the backend (i.e. lib-jitsi-meet) while
  94. * manipulating a conference participant (e.g. pin or select participant).
  95. *
  96. * @param {Error} err - The Error which was thrown by the backend while
  97. * manipulating a conference participant and which is to be handled.
  98. * @protected
  99. * @returns {void}
  100. */
  101. export function _handleParticipantError(err: { message: ?string }) {
  102. // XXX DataChannels are initialized at some later point when the conference
  103. // has multiple participants, but code that pins or selects a participant
  104. // might be executed before. So here we're swallowing a particular error.
  105. // TODO Lib-jitsi-meet should be fixed to not throw such an exception in
  106. // these scenarios.
  107. if (err.message !== 'Data channels support is disabled!') {
  108. throw err;
  109. }
  110. }
  111. /**
  112. * Determines whether a specific string is a valid room name.
  113. *
  114. * @param {(string|undefined)} room - The name of the conference room to check
  115. * for validity.
  116. * @returns {boolean} If the specified room name is valid, then true; otherwise,
  117. * false.
  118. */
  119. export function isRoomValid(room: ?string) {
  120. return typeof room === 'string' && room !== '';
  121. }
  122. /**
  123. * Remove a set of local tracks from a conference.
  124. *
  125. * @param {JitsiConference} conference - Conference instance.
  126. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  127. * @protected
  128. * @returns {Promise}
  129. */
  130. export function _removeLocalTracksFromConference(
  131. conference: { removeTrack: Function },
  132. localTracks: Array<Object>) {
  133. return Promise.all(localTracks.map(track =>
  134. conference.removeTrack(track)
  135. .catch(err => {
  136. // Local track might be already disposed by direct
  137. // JitsiTrack#dispose() call. So we should ignore this error
  138. // here.
  139. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  140. _reportError(
  141. 'Failed to remove local track from conference',
  142. err);
  143. }
  144. })
  145. ));
  146. }
  147. /**
  148. * Reports a specific Error with a specific error message. While the
  149. * implementation merely logs the specified msg and err via the console at the
  150. * time of this writing, the intention of the function is to abstract the
  151. * reporting of errors and facilitate elaborating on it in the future.
  152. *
  153. * @param {string} msg - The error message to report.
  154. * @param {Error} err - The Error to report.
  155. * @private
  156. * @returns {void}
  157. */
  158. function _reportError(msg, err) {
  159. // TODO This is a good point to call some global error handler when we have
  160. // one.
  161. logger.error(msg, err);
  162. }
  163. /**
  164. * Sends a representation of the local participant such as her avatar (URL),
  165. * e-mail address, and display name to (the remote participants of) a specific
  166. * conference.
  167. *
  168. * @param {Function|Object} stateful - The redux store, state, or
  169. * {@code getState} function.
  170. * @param {JitsiConference} conference - The {@code JitsiConference} to which
  171. * the representation of the local participant is to be sent.
  172. * @returns {void}
  173. */
  174. export function sendLocalParticipant(
  175. stateful: Function | Object,
  176. conference: {
  177. sendCommand: Function,
  178. setDisplayName: Function,
  179. setLocalParticipantProperty: Function }) {
  180. const {
  181. avatarID,
  182. avatarURL,
  183. email,
  184. features,
  185. name
  186. } = getLocalParticipant(stateful);
  187. avatarID && conference.sendCommand(AVATAR_ID_COMMAND, {
  188. value: avatarID
  189. });
  190. avatarURL && conference.sendCommand(AVATAR_URL_COMMAND, {
  191. value: avatarURL
  192. });
  193. email && conference.sendCommand(EMAIL_COMMAND, {
  194. value: email
  195. });
  196. if (features && features['screen-sharing'] === 'true') {
  197. conference.setLocalParticipantProperty('features_screen-sharing', true);
  198. }
  199. conference.setDisplayName(name);
  200. }