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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  2. import { setAudioMuted, setVideoMuted } from '../media';
  3. import {
  4. dominantSpeakerChanged,
  5. getLocalParticipant,
  6. participantConnectionStatusChanged,
  7. participantJoined,
  8. participantLeft,
  9. participantRoleChanged,
  10. participantUpdated
  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_AUDIO_ONLY,
  21. SET_LARGE_VIDEO_HD_STATUS,
  22. SET_LASTN,
  23. SET_PASSWORD,
  24. SET_PASSWORD_FAILED,
  25. SET_ROOM
  26. } from './actionTypes';
  27. import {
  28. AVATAR_ID_COMMAND,
  29. AVATAR_URL_COMMAND,
  30. EMAIL_COMMAND,
  31. JITSI_CONFERENCE_URL_KEY
  32. } from './constants';
  33. import { _addLocalTracksToConference } from './functions';
  34. import type { Dispatch } from 'redux';
  35. /**
  36. * Adds conference (event) listeners.
  37. *
  38. * @param {JitsiConference} conference - The JitsiConference instance.
  39. * @param {Dispatch} dispatch - The Redux dispatch function.
  40. * @private
  41. * @returns {void}
  42. */
  43. function _addConferenceListeners(conference, dispatch) {
  44. // Dispatches into features/base/conference follow:
  45. conference.on(
  46. JitsiConferenceEvents.CONFERENCE_FAILED,
  47. (...args) => dispatch(conferenceFailed(conference, ...args)));
  48. conference.on(
  49. JitsiConferenceEvents.CONFERENCE_JOINED,
  50. (...args) => dispatch(conferenceJoined(conference, ...args)));
  51. conference.on(
  52. JitsiConferenceEvents.CONFERENCE_LEFT,
  53. (...args) => dispatch(conferenceLeft(conference, ...args)));
  54. conference.on(
  55. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  56. (...args) => dispatch(lockStateChanged(conference, ...args)));
  57. // Dispatches into features/base/media follow:
  58. // FIXME: This is needed because when Jicofo tells us to start muted
  59. // lib-jitsi-meet does the actual muting. Perhaps this should be refactored
  60. // so applications are hinted to start muted, but lib-jitsi-meet doesn't
  61. // take action.
  62. conference.on(
  63. JitsiConferenceEvents.STARTED_MUTED,
  64. () => {
  65. dispatch(setAudioMuted(Boolean(conference.startAudioMuted)));
  66. dispatch(setVideoMuted(Boolean(conference.startVideoMuted)));
  67. });
  68. // Dispatches into features/base/tracks follow:
  69. conference.on(
  70. JitsiConferenceEvents.TRACK_ADDED,
  71. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  72. conference.on(
  73. JitsiConferenceEvents.TRACK_REMOVED,
  74. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  75. // Dispatches into features/base/participants follow:
  76. conference.on(
  77. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  78. (...args) => dispatch(dominantSpeakerChanged(...args)));
  79. conference.on(
  80. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  81. (...args) => dispatch(participantConnectionStatusChanged(...args)));
  82. conference.on(
  83. JitsiConferenceEvents.USER_JOINED,
  84. (id, user) => dispatch(participantJoined({
  85. id,
  86. name: user.getDisplayName(),
  87. role: user.getRole()
  88. })));
  89. conference.on(
  90. JitsiConferenceEvents.USER_LEFT,
  91. (...args) => dispatch(participantLeft(...args)));
  92. conference.on(
  93. JitsiConferenceEvents.USER_ROLE_CHANGED,
  94. (...args) => dispatch(participantRoleChanged(...args)));
  95. conference.addCommandListener(
  96. AVATAR_ID_COMMAND,
  97. (data, id) => dispatch(participantUpdated({
  98. id,
  99. avatarID: data.value
  100. })));
  101. conference.addCommandListener(
  102. AVATAR_URL_COMMAND,
  103. (data, id) => dispatch(participantUpdated({
  104. id,
  105. avatarURL: data.value
  106. })));
  107. conference.addCommandListener(
  108. EMAIL_COMMAND,
  109. (data, id) => dispatch(participantUpdated({
  110. id,
  111. email: data.value
  112. })));
  113. }
  114. /**
  115. * Sets the data for the local participant to the conference.
  116. *
  117. * @param {JitsiConference} conference - The JitsiConference instance.
  118. * @param {Object} state - The Redux state.
  119. * @returns {void}
  120. */
  121. function _setLocalParticipantData(conference, state) {
  122. const { avatarID } = getLocalParticipant(state);
  123. conference.removeCommand(AVATAR_ID_COMMAND);
  124. conference.sendCommand(AVATAR_ID_COMMAND, {
  125. value: avatarID
  126. });
  127. }
  128. /**
  129. * Signals that a specific conference has failed.
  130. *
  131. * @param {JitsiConference} conference - The JitsiConference that has failed.
  132. * @param {string} error - The error describing/detailing the cause of the
  133. * failure.
  134. * @returns {{
  135. * type: CONFERENCE_FAILED,
  136. * conference: JitsiConference,
  137. * error: string
  138. * }}
  139. * @public
  140. */
  141. export function conferenceFailed(conference, error) {
  142. return {
  143. type: CONFERENCE_FAILED,
  144. conference,
  145. error
  146. };
  147. }
  148. /**
  149. * Attach any pre-existing local media to the conference once the conference has
  150. * been joined.
  151. *
  152. * @param {JitsiConference} conference - The JitsiConference instance which was
  153. * joined by the local participant.
  154. * @returns {Function}
  155. */
  156. export function conferenceJoined(conference) {
  157. return (dispatch, getState) => {
  158. const localTracks
  159. = getState()['features/base/tracks']
  160. .filter(t => t.local)
  161. .map(t => t.jitsiTrack);
  162. if (localTracks.length) {
  163. _addLocalTracksToConference(conference, localTracks);
  164. }
  165. dispatch({
  166. type: CONFERENCE_JOINED,
  167. conference
  168. });
  169. };
  170. }
  171. /**
  172. * Signals that a specific conference has been left.
  173. *
  174. * @param {JitsiConference} conference - The JitsiConference instance which was
  175. * left by the local participant.
  176. * @returns {{
  177. * type: CONFERENCE_LEFT,
  178. * conference: JitsiConference
  179. * }}
  180. */
  181. export function conferenceLeft(conference) {
  182. return {
  183. type: CONFERENCE_LEFT,
  184. conference
  185. };
  186. }
  187. /**
  188. * Signals the intention of the application to have the local participant join a
  189. * specific conference. Similar in fashion to {@code CONFERENCE_JOINED}.
  190. *
  191. * @param {JitsiConference} conference - The JitsiConference instance the
  192. * local participant will (try to) join.
  193. * @returns {{
  194. * type: CONFERENCE_WILL_JOIN,
  195. * conference: JitsiConference
  196. * }}
  197. */
  198. function _conferenceWillJoin(conference) {
  199. return {
  200. type: CONFERENCE_WILL_JOIN,
  201. conference
  202. };
  203. }
  204. /**
  205. * Signals the intention of the application to have the local participant leave
  206. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  207. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  208. * lib-jitsi-meet and not the application.
  209. *
  210. * @param {JitsiConference} conference - The JitsiConference instance which will
  211. * be left by the local participant.
  212. * @returns {{
  213. * type: CONFERENCE_LEFT,
  214. * conference: JitsiConference
  215. * }}
  216. */
  217. export function conferenceWillLeave(conference) {
  218. return {
  219. type: CONFERENCE_WILL_LEAVE,
  220. conference
  221. };
  222. }
  223. /**
  224. * Initializes a new conference.
  225. *
  226. * @returns {Function}
  227. */
  228. export function createConference() {
  229. return (dispatch, getState) => {
  230. const state = getState();
  231. const { connection, locationURL } = state['features/base/connection'];
  232. if (!connection) {
  233. throw new Error('Cannot create a conference without a connection!');
  234. }
  235. const { password, room } = state['features/base/conference'];
  236. if (!room) {
  237. throw new Error('Cannot join a conference without a room name!');
  238. }
  239. const conference
  240. = connection.initJitsiConference(
  241. // XXX Lib-jitsi-meet does not accept uppercase letters.
  242. room.toLowerCase(),
  243. state['features/base/config']);
  244. conference[JITSI_CONFERENCE_URL_KEY] = locationURL;
  245. dispatch(_conferenceWillJoin(conference));
  246. _addConferenceListeners(conference, dispatch);
  247. _setLocalParticipantData(conference, state);
  248. conference.join(password);
  249. };
  250. }
  251. /**
  252. * Signals that the lock state of a specific JitsiConference changed.
  253. *
  254. * @param {JitsiConference} conference - The JitsiConference which had its lock
  255. * state changed.
  256. * @param {boolean} locked - If the specified conference became locked, true;
  257. * otherwise, false.
  258. * @returns {{
  259. * type: LOCK_STATE_CHANGED,
  260. * conference: JitsiConference,
  261. * locked: boolean
  262. * }}
  263. */
  264. export function lockStateChanged(conference, locked) {
  265. return {
  266. type: LOCK_STATE_CHANGED,
  267. conference,
  268. locked
  269. };
  270. }
  271. /**
  272. * Sets the audio-only flag for the current JitsiConference.
  273. *
  274. * @param {boolean} audioOnly - True if the conference should be audio only;
  275. * false, otherwise.
  276. * @returns {{
  277. * type: SET_AUDIO_ONLY,
  278. * audioOnly: boolean
  279. * }}
  280. */
  281. export function setAudioOnly(audioOnly) {
  282. return {
  283. type: SET_AUDIO_ONLY,
  284. audioOnly
  285. };
  286. }
  287. /**
  288. * Action to set whether or not the currently displayed large video is in
  289. * high-definition.
  290. *
  291. * @param {boolean} isLargeVideoHD - True if the large video is high-definition.
  292. * @returns {{
  293. * type: SET_LARGE_VIDEO_HD_STATUS,
  294. * isLargeVideoHD: boolean
  295. * }}
  296. */
  297. export function setLargeVideoHDStatus(isLargeVideoHD) {
  298. return {
  299. type: SET_LARGE_VIDEO_HD_STATUS,
  300. isLargeVideoHD
  301. };
  302. }
  303. /**
  304. * Sets the video channel's last N (value) of the current conference. A value of
  305. * undefined shall be used to reset it to the default value.
  306. *
  307. * @param {(number|undefined)} lastN - The last N value to be set.
  308. * @returns {Function}
  309. */
  310. export function setLastN(lastN: ?number) {
  311. return (dispatch: Dispatch<*>, getState: Function) => {
  312. if (typeof lastN === 'undefined') {
  313. const config = getState()['features/base/config'];
  314. /* eslint-disable no-param-reassign */
  315. lastN = config.channelLastN;
  316. if (typeof lastN === 'undefined') {
  317. lastN = -1;
  318. }
  319. /* eslint-enable no-param-reassign */
  320. }
  321. dispatch({
  322. type: SET_LASTN,
  323. lastN
  324. });
  325. };
  326. }
  327. /**
  328. * Sets the password to join or lock a specific JitsiConference.
  329. *
  330. * @param {JitsiConference} conference - The JitsiConference which requires a
  331. * password to join or is to be locked with the specified password.
  332. * @param {Function} method - The JitsiConference method of password protection
  333. * such as join or lock.
  334. * @param {string} password - The password with which the specified conference
  335. * is to be joined or locked.
  336. * @returns {Function}
  337. */
  338. export function setPassword(conference, method, password) {
  339. return (dispatch, getState) => {
  340. switch (method) {
  341. case conference.join: {
  342. let state = getState()['features/base/conference'];
  343. // Make sure that the action will set a password for a conference
  344. // that the application wants joined.
  345. if (state.passwordRequired === conference) {
  346. dispatch({
  347. type: SET_PASSWORD,
  348. conference,
  349. method,
  350. password
  351. });
  352. // Join the conference with the newly-set password.
  353. // Make sure that the action did set the password.
  354. state = getState()['features/base/conference'];
  355. if (state.password === password
  356. && !state.passwordRequired
  357. // Make sure that the application still wants the
  358. // conference joined.
  359. && !state.conference) {
  360. method.call(conference, password);
  361. }
  362. }
  363. break;
  364. }
  365. case conference.lock: {
  366. const state = getState()['features/base/conference'];
  367. if (state.conference === conference) {
  368. return (
  369. method.call(conference, password)
  370. .then(() => dispatch({
  371. type: SET_PASSWORD,
  372. conference,
  373. method,
  374. password
  375. }))
  376. .catch(error => dispatch({
  377. type: SET_PASSWORD_FAILED,
  378. error
  379. }))
  380. );
  381. }
  382. return Promise.reject();
  383. }
  384. }
  385. };
  386. }
  387. /**
  388. * Sets (the name of) the room of the conference to be joined.
  389. *
  390. * @param {(string|undefined)} room - The name of the room of the conference to
  391. * be joined.
  392. * @returns {{
  393. * type: SET_ROOM,
  394. * room: string
  395. * }}
  396. */
  397. export function setRoom(room) {
  398. return {
  399. type: SET_ROOM,
  400. room
  401. };
  402. }
  403. /**
  404. * Toggles the audio-only flag for the current JitsiConference.
  405. *
  406. * @returns {Function}
  407. */
  408. export function toggleAudioOnly() {
  409. return (dispatch: Dispatch<*>, getState: Function) => {
  410. const { audioOnly } = getState()['features/base/conference'];
  411. return dispatch(setAudioOnly(!audioOnly));
  412. };
  413. }