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

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