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

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