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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import { JitsiConferenceEvents } 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_FAILED,
  12. CONFERENCE_JOINED,
  13. CONFERENCE_LEFT,
  14. CONFERENCE_WILL_JOIN,
  15. CONFERENCE_WILL_LEAVE,
  16. LOCK_STATE_CHANGED,
  17. SET_PASSWORD,
  18. SET_ROOM
  19. } from './actionTypes';
  20. import { EMAIL_COMMAND } from './constants';
  21. import { _addLocalTracksToConference } from './functions';
  22. /**
  23. * Adds conference (event) listeners.
  24. *
  25. * @param {JitsiConference} conference - The JitsiConference instance.
  26. * @param {Dispatch} dispatch - The Redux dispatch function.
  27. * @private
  28. * @returns {void}
  29. */
  30. function _addConferenceListeners(conference, dispatch) {
  31. conference.on(
  32. JitsiConferenceEvents.CONFERENCE_FAILED,
  33. (...args) => dispatch(conferenceFailed(conference, ...args)));
  34. conference.on(
  35. JitsiConferenceEvents.CONFERENCE_JOINED,
  36. (...args) => dispatch(_conferenceJoined(conference, ...args)));
  37. conference.on(
  38. JitsiConferenceEvents.CONFERENCE_LEFT,
  39. (...args) => dispatch(conferenceLeft(conference, ...args)));
  40. conference.on(
  41. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  42. (...args) => dispatch(dominantSpeakerChanged(...args)));
  43. conference.on(
  44. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  45. (...args) => dispatch(_lockStateChanged(conference, ...args)));
  46. conference.on(
  47. JitsiConferenceEvents.TRACK_ADDED,
  48. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  49. conference.on(
  50. JitsiConferenceEvents.TRACK_REMOVED,
  51. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  52. conference.on(
  53. JitsiConferenceEvents.USER_JOINED,
  54. (id, user) => dispatch(participantJoined({
  55. id,
  56. name: user.getDisplayName(),
  57. role: user.getRole()
  58. })));
  59. conference.on(
  60. JitsiConferenceEvents.USER_LEFT,
  61. (...args) => dispatch(participantLeft(...args)));
  62. conference.on(
  63. JitsiConferenceEvents.USER_ROLE_CHANGED,
  64. (...args) => dispatch(participantRoleChanged(...args)));
  65. conference.addCommandListener(
  66. EMAIL_COMMAND,
  67. (data, id) => dispatch(changeParticipantEmail(id, data.value)));
  68. }
  69. /**
  70. * Signals that a specific conference has failed.
  71. *
  72. * @param {JitsiConference} conference - The JitsiConference that has failed.
  73. * @param {string} error - The error describing/detailing the cause of the
  74. * failure.
  75. * @returns {{
  76. * type: CONFERENCE_FAILED,
  77. * conference: JitsiConference,
  78. * error: string
  79. * }}
  80. * @public
  81. */
  82. export function conferenceFailed(conference, error) {
  83. return {
  84. type: CONFERENCE_FAILED,
  85. conference,
  86. error
  87. };
  88. }
  89. /**
  90. * Attach any pre-existing local media to the conference once the conference has
  91. * been joined.
  92. *
  93. * @param {JitsiConference} conference - The JitsiConference instance which was
  94. * joined by the local participant.
  95. * @returns {Function}
  96. */
  97. function _conferenceJoined(conference) {
  98. return (dispatch, getState) => {
  99. const localTracks
  100. = getState()['features/base/tracks']
  101. .filter(t => t.local)
  102. .map(t => t.jitsiTrack);
  103. if (localTracks.length) {
  104. _addLocalTracksToConference(conference, localTracks);
  105. }
  106. dispatch({
  107. type: CONFERENCE_JOINED,
  108. conference
  109. });
  110. };
  111. }
  112. /**
  113. * Signals that a specific conference has been left.
  114. *
  115. * @param {JitsiConference} conference - The JitsiConference instance which was
  116. * left by the local participant.
  117. * @returns {{
  118. * type: CONFERENCE_LEFT,
  119. * conference: JitsiConference
  120. * }}
  121. */
  122. export function conferenceLeft(conference) {
  123. return {
  124. type: CONFERENCE_LEFT,
  125. conference
  126. };
  127. }
  128. /**
  129. * Signals the intention of the application to have the local participant join a
  130. * conference with a specific room (name). Similar in fashion
  131. * to CONFERENCE_JOINED.
  132. *
  133. * @param {string} room - The room (name) which identifies the conference the
  134. * local participant will (try to) join.
  135. * @returns {{
  136. * type: CONFERENCE_WILL_JOIN,
  137. * room: string
  138. * }}
  139. */
  140. function _conferenceWillJoin(room) {
  141. return {
  142. type: CONFERENCE_WILL_JOIN,
  143. room
  144. };
  145. }
  146. /**
  147. * Signals the intention of the application to have the local participant leave
  148. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  149. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  150. * lib-jitsi-meet and not the application.
  151. *
  152. * @param {JitsiConference} conference - The JitsiConference instance which will
  153. * be left by the local participant.
  154. * @returns {{
  155. * type: CONFERENCE_LEFT,
  156. * conference: JitsiConference
  157. * }}
  158. */
  159. export function conferenceWillLeave(conference) {
  160. return {
  161. type: CONFERENCE_WILL_LEAVE,
  162. conference
  163. };
  164. }
  165. /**
  166. * Initializes a new conference.
  167. *
  168. * @returns {Function}
  169. */
  170. export function createConference() {
  171. return (dispatch, getState) => {
  172. const state = getState();
  173. const connection = state['features/base/connection'].connection;
  174. if (!connection) {
  175. throw new Error('Cannot create conference without connection');
  176. }
  177. const { password, room } = state['features/base/conference'];
  178. if (typeof room === 'undefined' || room === '') {
  179. throw new Error('Cannot join conference without room name');
  180. }
  181. dispatch(_conferenceWillJoin(room));
  182. // TODO Take options from config.
  183. const conference
  184. = connection.initJitsiConference(
  185. // XXX Lib-jitsi-meet does not accept uppercase letters.
  186. room.toLowerCase(),
  187. {
  188. openSctp: true
  189. // FIXME I tested H.264 from iPhone 6S during a morning
  190. // standup but, unfortunately, the other participants who
  191. // happened to be running the Web app saw only black.
  192. //
  193. // preferH264: true
  194. });
  195. _addConferenceListeners(conference, dispatch);
  196. conference.join(password);
  197. };
  198. }
  199. /**
  200. * Signals that the lock state of a specific JitsiConference changed.
  201. *
  202. * @param {JitsiConference} conference - The JitsiConference which had its lock
  203. * state changed.
  204. * @param {boolean} locked - If the specified conference became locked, true;
  205. * otherwise, false.
  206. * @returns {{
  207. * type: LOCK_STATE_CHANGED,
  208. * conference: JitsiConference,
  209. * locked: boolean
  210. * }}
  211. */
  212. function _lockStateChanged(conference, locked) {
  213. return {
  214. type: LOCK_STATE_CHANGED,
  215. conference,
  216. locked
  217. };
  218. }
  219. /**
  220. * Sets the password to join or lock a specific JitsiConference.
  221. *
  222. * @param {JitsiConference} conference - The JitsiConference which requires a
  223. * password to join or is to be locked with the specified password.
  224. * @param {Function} method - The JitsiConference method of password protection
  225. * such as join or lock.
  226. * @param {string} password - The password with which the specified conference
  227. * is to be joined or locked.
  228. * @returns {Function}
  229. */
  230. export function setPassword(conference, method, password) {
  231. return (dispatch, getState) => {
  232. switch (method) {
  233. case conference.join: {
  234. let state = getState()['features/base/conference'];
  235. // Make sure that the action will set a password for a conference
  236. // that the application wants joined.
  237. if (state.passwordRequired === conference) {
  238. dispatch({
  239. type: SET_PASSWORD,
  240. conference,
  241. method,
  242. password
  243. });
  244. // Join the conference with the newly-set password.
  245. // Make sure that the action did set the password.
  246. state = getState()['features/base/conference'];
  247. if (state.password === password
  248. && !state.passwordRequired
  249. // Make sure that the application still wants the
  250. // conference joined.
  251. && !state.conference) {
  252. method.call(conference, password);
  253. }
  254. }
  255. break;
  256. }
  257. case conference.lock: {
  258. const state = getState()['features/base/conference'];
  259. if (state.conference === conference) {
  260. return (
  261. method.call(conference, password)
  262. .then(() => dispatch({
  263. type: SET_PASSWORD,
  264. conference,
  265. method,
  266. password
  267. })));
  268. }
  269. return Promise.reject();
  270. }
  271. }
  272. };
  273. }
  274. /**
  275. * Sets (the name of) the room of the conference to be joined.
  276. *
  277. * @param {(string|undefined)} room - The name of the room of the conference to
  278. * be joined.
  279. * @returns {{
  280. * type: SET_ROOM,
  281. * room: string
  282. * }}
  283. */
  284. export function setRoom(room) {
  285. return {
  286. type: SET_ROOM,
  287. room
  288. };
  289. }