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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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, getState) => {
  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. * Signals the data channel with the bridge has successfully opened.
  259. *
  260. * @returns {{
  261. * type: DATA_CHANNEL_OPENED
  262. * }}
  263. */
  264. export function dataChannelOpened() {
  265. return {
  266. type: DATA_CHANNEL_OPENED
  267. };
  268. }
  269. /**
  270. * Signals that the lock state of a specific JitsiConference changed.
  271. *
  272. * @param {JitsiConference} conference - The JitsiConference which had its lock
  273. * state changed.
  274. * @param {boolean} locked - If the specified conference became locked, true;
  275. * otherwise, false.
  276. * @returns {{
  277. * type: LOCK_STATE_CHANGED,
  278. * conference: JitsiConference,
  279. * locked: boolean
  280. * }}
  281. */
  282. export function lockStateChanged(conference, locked) {
  283. return {
  284. type: LOCK_STATE_CHANGED,
  285. conference,
  286. locked
  287. };
  288. }
  289. /**
  290. * Sets whether or not peer2peer is currently enabled.
  291. *
  292. * @param {boolean} p2p - Whether or not peer2peer is currently active.
  293. * @returns {{
  294. * type: P2P_STATUS_CHANGED,
  295. * p2p: boolean
  296. * }}
  297. */
  298. export function p2pStatusChanged(p2p) {
  299. return {
  300. type: P2P_STATUS_CHANGED,
  301. p2p
  302. };
  303. }
  304. /**
  305. * Sets the audio-only flag for the current JitsiConference.
  306. *
  307. * @param {boolean} audioOnly - True if the conference should be audio only;
  308. * false, otherwise.
  309. * @returns {{
  310. * type: SET_AUDIO_ONLY,
  311. * audioOnly: boolean
  312. * }}
  313. */
  314. export function setAudioOnly(audioOnly) {
  315. return {
  316. type: SET_AUDIO_ONLY,
  317. audioOnly
  318. };
  319. }
  320. /**
  321. * Sets the video channel's last N (value) of the current conference. A value of
  322. * undefined shall be used to reset it to the default value.
  323. *
  324. * @param {(number|undefined)} lastN - The last N value to be set.
  325. * @returns {Function}
  326. */
  327. export function setLastN(lastN: ?number) {
  328. return (dispatch: Dispatch<*>, getState: Function) => {
  329. if (typeof lastN === 'undefined') {
  330. const config = getState()['features/base/config'];
  331. /* eslint-disable no-param-reassign */
  332. lastN = config.channelLastN;
  333. if (typeof lastN === 'undefined') {
  334. lastN = -1;
  335. }
  336. /* eslint-enable no-param-reassign */
  337. }
  338. dispatch({
  339. type: SET_LASTN,
  340. lastN
  341. });
  342. };
  343. }
  344. /**
  345. * Sets the password to join or lock a specific JitsiConference.
  346. *
  347. * @param {JitsiConference} conference - The JitsiConference which requires a
  348. * password to join or is to be locked with the specified password.
  349. * @param {Function} method - The JitsiConference method of password protection
  350. * such as join or lock.
  351. * @param {string} password - The password with which the specified conference
  352. * is to be joined or locked.
  353. * @returns {Function}
  354. */
  355. export function setPassword(conference, method, password) {
  356. return (dispatch, getState) => {
  357. switch (method) {
  358. case conference.join: {
  359. let state = getState()['features/base/conference'];
  360. // Make sure that the action will set a password for a conference
  361. // that the application wants joined.
  362. if (state.passwordRequired === conference) {
  363. dispatch({
  364. type: SET_PASSWORD,
  365. conference,
  366. method,
  367. password
  368. });
  369. // Join the conference with the newly-set password.
  370. // Make sure that the action did set the password.
  371. state = getState()['features/base/conference'];
  372. if (state.password === password
  373. && !state.passwordRequired
  374. // Make sure that the application still wants the
  375. // conference joined.
  376. && !state.conference) {
  377. method.call(conference, password);
  378. }
  379. }
  380. break;
  381. }
  382. case conference.lock: {
  383. const state = getState()['features/base/conference'];
  384. if (state.conference === conference) {
  385. return (
  386. method.call(conference, password)
  387. .then(() => dispatch({
  388. type: SET_PASSWORD,
  389. conference,
  390. method,
  391. password
  392. }))
  393. .catch(error => dispatch({
  394. type: SET_PASSWORD_FAILED,
  395. error
  396. }))
  397. );
  398. }
  399. return Promise.reject();
  400. }
  401. }
  402. };
  403. }
  404. /**
  405. * Sets the max frame height to receive from remote participant videos.
  406. *
  407. * @param {number} receiveVideoQuality - The max video resolution to receive.
  408. * @returns {{
  409. * type: SET_RECEIVE_VIDEO_QUALITY,
  410. * receiveVideoQuality: number
  411. * }}
  412. */
  413. export function setReceiveVideoQuality(receiveVideoQuality) {
  414. return {
  415. type: SET_RECEIVE_VIDEO_QUALITY,
  416. receiveVideoQuality
  417. };
  418. }
  419. /**
  420. * Sets (the name of) the room of the conference to be joined.
  421. *
  422. * @param {(string|undefined)} room - The name of the room of the conference to
  423. * be joined.
  424. * @returns {{
  425. * type: SET_ROOM,
  426. * room: string
  427. * }}
  428. */
  429. export function setRoom(room) {
  430. return {
  431. type: SET_ROOM,
  432. room
  433. };
  434. }
  435. /**
  436. * Toggles the audio-only flag for the current JitsiConference.
  437. *
  438. * @returns {Function}
  439. */
  440. export function toggleAudioOnly() {
  441. return (dispatch: Dispatch<*>, getState: Function) => {
  442. const { audioOnly } = getState()['features/base/conference'];
  443. return dispatch(setAudioOnly(!audioOnly));
  444. };
  445. }