You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. } from './constants';
  10. /**
  11. * Attach a set of local tracks to a conference.
  12. *
  13. * @param {JitsiConference} conference - Conference instance.
  14. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  15. * @protected
  16. * @returns {Promise}
  17. */
  18. export function _addLocalTracksToConference(
  19. conference: { addTrack: Function, getLocalTracks: Function },
  20. localTracks: Array<Object>) {
  21. const conferenceLocalTracks = conference.getLocalTracks();
  22. const promises = [];
  23. for (const track of localTracks) {
  24. // XXX The library lib-jitsi-meet may be draconian, for example, when
  25. // adding one and the same video track multiple times.
  26. if (conferenceLocalTracks.indexOf(track) === -1) {
  27. promises.push(
  28. conference.addTrack(track).catch(err => {
  29. _reportError(
  30. 'Failed to add local track to conference',
  31. err);
  32. }));
  33. }
  34. }
  35. return Promise.all(promises);
  36. }
  37. /**
  38. * Returns the current {@code JitsiConference} which is joining or joined and is
  39. * not leaving. Please note the contrast with merely reading the
  40. * {@code conference} state of the feature base/conference which is not joining
  41. * but may be leaving already.
  42. *
  43. * @param {Function|Object} stateful - The redux store, state, or
  44. * {@code getState} function.
  45. * @returns {JitsiConference|undefined}
  46. */
  47. export function getCurrentConference(stateful: Function | Object) {
  48. const { conference, joining, leaving }
  49. = toState(stateful)['features/base/conference'];
  50. return (
  51. conference
  52. ? conference === leaving ? undefined : conference
  53. : joining);
  54. }
  55. /**
  56. * Handle an error thrown by the backend (i.e. lib-jitsi-meet) while
  57. * manipulating a conference participant (e.g. pin or select participant).
  58. *
  59. * @param {Error} err - The Error which was thrown by the backend while
  60. * manipulating a conference participant and which is to be handled.
  61. * @protected
  62. * @returns {void}
  63. */
  64. export function _handleParticipantError(err: { message: ?string }) {
  65. // XXX DataChannels are initialized at some later point when the conference
  66. // has multiple participants, but code that pins or selects a participant
  67. // might be executed before. So here we're swallowing a particular error.
  68. // TODO Lib-jitsi-meet should be fixed to not throw such an exception in
  69. // these scenarios.
  70. if (err.message !== 'Data channels support is disabled!') {
  71. throw err;
  72. }
  73. }
  74. /**
  75. * Determines whether a specific string is a valid room name.
  76. *
  77. * @param {(string|undefined)} room - The name of the conference room to check
  78. * for validity.
  79. * @returns {boolean} If the specified room name is valid, then true; otherwise,
  80. * false.
  81. */
  82. export function isRoomValid(room: ?string) {
  83. return typeof room === 'string' && room !== '';
  84. }
  85. /**
  86. * Remove a set of local tracks from a conference.
  87. *
  88. * @param {JitsiConference} conference - Conference instance.
  89. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  90. * @protected
  91. * @returns {Promise}
  92. */
  93. export function _removeLocalTracksFromConference(
  94. conference: { removeTrack: Function },
  95. localTracks: Array<Object>) {
  96. return Promise.all(localTracks.map(track =>
  97. conference.removeTrack(track)
  98. .catch(err => {
  99. // Local track might be already disposed by direct
  100. // JitsiTrack#dispose() call. So we should ignore this error
  101. // here.
  102. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  103. _reportError(
  104. 'Failed to remove local track from conference',
  105. err);
  106. }
  107. })
  108. ));
  109. }
  110. /**
  111. * Reports a specific Error with a specific error message. While the
  112. * implementation merely logs the specified msg and err via the console at the
  113. * time of this writing, the intention of the function is to abstract the
  114. * reporting of errors and facilitate elaborating on it in the future.
  115. *
  116. * @param {string} msg - The error message to report.
  117. * @param {Error} err - The Error to report.
  118. * @private
  119. * @returns {void}
  120. */
  121. function _reportError(msg, err) {
  122. // TODO This is a good point to call some global error handler when we have
  123. // one.
  124. console.error(msg, err);
  125. }
  126. /**
  127. * Sends a representation of the local participant such as her avatar (URL),
  128. * e-mail address, and display name to (the remote participants of) a specific
  129. * conference.
  130. *
  131. * @param {Function|Object} stateful - The redux store, state, or
  132. * {@code getState} function.
  133. * @param {JitsiConference} conference - The {@code JitsiConference} to which
  134. * the representation of the local participant is to be sent.
  135. * @returns {void}
  136. */
  137. export function sendLocalParticipant(
  138. stateful: Function | Object,
  139. conference: { sendCommand: Function, setDisplayName: Function }) {
  140. const { avatarID, avatarURL, email, name } = getLocalParticipant(stateful);
  141. avatarID && conference.sendCommand(AVATAR_ID_COMMAND, {
  142. value: avatarID
  143. });
  144. avatarURL && conference.sendCommand(AVATAR_URL_COMMAND, {
  145. value: avatarURL
  146. });
  147. email && conference.sendCommand(EMAIL_COMMAND, {
  148. value: email
  149. });
  150. conference.setDisplayName(name);
  151. }