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

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