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 5.2KB

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