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.

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