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

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