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

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