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.

actions.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import JitsiMeetJS from '../lib-jitsi-meet';
  2. import {
  3. changeParticipantEmail,
  4. dominantSpeakerChanged,
  5. participantJoined,
  6. participantLeft,
  7. participantRoleChanged
  8. } from '../participants';
  9. import { trackAdded, trackRemoved } from '../tracks';
  10. import {
  11. CONFERENCE_JOINED,
  12. CONFERENCE_LEFT,
  13. CONFERENCE_WILL_LEAVE,
  14. SET_ROOM
  15. } from './actionTypes';
  16. import { EMAIL_COMMAND } from './constants';
  17. import { _addLocalTracksToConference } from './functions';
  18. import './middleware';
  19. import './reducer';
  20. /**
  21. * Adds conference (event) listeners.
  22. *
  23. * @param {JitsiConference} conference - The JitsiConference instance.
  24. * @param {Dispatch} dispatch - The Redux dispatch function.
  25. * @private
  26. * @returns {void}
  27. */
  28. function _addConferenceListeners(conference, dispatch) {
  29. const JitsiConferenceEvents = JitsiMeetJS.events.conference;
  30. conference.on(
  31. JitsiConferenceEvents.CONFERENCE_JOINED,
  32. (...args) => dispatch(_conferenceJoined(conference, ...args)));
  33. conference.on(
  34. JitsiConferenceEvents.CONFERENCE_LEFT,
  35. (...args) => dispatch(_conferenceLeft(conference, ...args)));
  36. conference.on(
  37. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  38. (...args) => dispatch(dominantSpeakerChanged(...args)));
  39. conference.on(
  40. JitsiConferenceEvents.TRACK_ADDED,
  41. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  42. conference.on(
  43. JitsiConferenceEvents.TRACK_REMOVED,
  44. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  45. conference.on(
  46. JitsiConferenceEvents.USER_JOINED,
  47. (id, user) => dispatch(participantJoined({
  48. id,
  49. name: user.getDisplayName(),
  50. role: user.getRole()
  51. })));
  52. conference.on(
  53. JitsiConferenceEvents.USER_LEFT,
  54. (...args) => dispatch(participantLeft(...args)));
  55. conference.on(
  56. JitsiConferenceEvents.USER_ROLE_CHANGED,
  57. (...args) => dispatch(participantRoleChanged(...args)));
  58. conference.addCommandListener(
  59. EMAIL_COMMAND,
  60. (data, id) => dispatch(changeParticipantEmail(id, data.value)));
  61. }
  62. /**
  63. * Attach any pre-existing local media to the conference once the conference has
  64. * been joined.
  65. *
  66. * @param {JitsiConference} conference - The JitsiConference instance which was
  67. * joined by the local participant.
  68. * @returns {Function}
  69. */
  70. function _conferenceJoined(conference) {
  71. return (dispatch, getState) => {
  72. const localTracks
  73. = getState()['features/base/tracks']
  74. .filter(t => t.local)
  75. .map(t => t.jitsiTrack);
  76. if (localTracks.length) {
  77. _addLocalTracksToConference(conference, localTracks);
  78. }
  79. dispatch({
  80. type: CONFERENCE_JOINED,
  81. conference
  82. });
  83. };
  84. }
  85. /**
  86. * Signals that a specific conference has been left.
  87. *
  88. * @param {JitsiConference} conference - The JitsiConference instance which was
  89. * left by the local participant.
  90. * @returns {{
  91. * type: CONFERENCE_LEFT,
  92. * conference: JitsiConference
  93. * }}
  94. */
  95. function _conferenceLeft(conference) {
  96. return {
  97. type: CONFERENCE_LEFT,
  98. conference
  99. };
  100. }
  101. /**
  102. * Signals the intention of the application to have the local participant leave
  103. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  104. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  105. * lib-jitsi-meet and not the application.
  106. *
  107. * @param {JitsiConference} conference - The JitsiConference instance which will
  108. * be left by the local participant.
  109. * @returns {{
  110. * type: CONFERENCE_LEFT,
  111. * conference: JitsiConference
  112. * }}
  113. */
  114. export function conferenceWillLeave(conference) {
  115. return {
  116. type: CONFERENCE_WILL_LEAVE,
  117. conference
  118. };
  119. }
  120. /**
  121. * Initializes a new conference.
  122. *
  123. * @returns {Function}
  124. */
  125. export function createConference() {
  126. return (dispatch, getState) => {
  127. const state = getState();
  128. const connection = state['features/base/connection'].connection;
  129. if (!connection) {
  130. throw new Error('Cannot create conference without connection');
  131. }
  132. const room = state['features/base/conference'].room;
  133. if (typeof room === 'undefined' || room === '') {
  134. throw new Error('Cannot join conference without room name');
  135. }
  136. // TODO Take options from config.
  137. const conference
  138. = connection.initJitsiConference(room, { openSctp: true });
  139. _addConferenceListeners(conference, dispatch);
  140. conference.join();
  141. };
  142. }
  143. /**
  144. * Sets (the name of) the room of the conference to be joined.
  145. *
  146. * @param {(string|undefined)} room - The name of the room of the conference to
  147. * be joined.
  148. * @returns {{
  149. * type: SET_ROOM,
  150. * room: string
  151. * }}
  152. */
  153. export function setRoom(room) {
  154. return {
  155. type: SET_ROOM,
  156. room
  157. };
  158. }