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

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