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

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