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

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