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.0KB

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