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

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