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

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