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

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