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

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