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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. MAX_DISPLAY_NAME_LENGTH,
  13. dominantSpeakerChanged,
  14. participantConnectionStatusChanged,
  15. participantJoined,
  16. participantLeft,
  17. participantRoleChanged,
  18. participantUpdated
  19. } from '../participants';
  20. import { getLocalTracks, trackAdded, trackRemoved } from '../tracks';
  21. import { getJitsiMeetGlobalNS } from '../util';
  22. import {
  23. CONFERENCE_FAILED,
  24. CONFERENCE_JOINED,
  25. CONFERENCE_LEFT,
  26. CONFERENCE_WILL_JOIN,
  27. CONFERENCE_WILL_LEAVE,
  28. DATA_CHANNEL_OPENED,
  29. LOCK_STATE_CHANGED,
  30. P2P_STATUS_CHANGED,
  31. SET_AUDIO_ONLY,
  32. SET_FOLLOW_ME,
  33. SET_LASTN,
  34. SET_PASSWORD,
  35. SET_PASSWORD_FAILED,
  36. SET_RECEIVE_VIDEO_QUALITY,
  37. SET_ROOM,
  38. SET_START_MUTED_POLICY
  39. } from './actionTypes';
  40. import {
  41. AVATAR_ID_COMMAND,
  42. AVATAR_URL_COMMAND,
  43. EMAIL_COMMAND,
  44. JITSI_CONFERENCE_URL_KEY
  45. } from './constants';
  46. import {
  47. _addLocalTracksToConference,
  48. sendLocalParticipant
  49. } from './functions';
  50. import type { Dispatch } from 'redux';
  51. const logger = require('jitsi-meet-logger').getLogger(__filename);
  52. declare var APP: Object;
  53. /**
  54. * Adds conference (event) listeners.
  55. *
  56. * @param {JitsiConference} conference - The JitsiConference instance.
  57. * @param {Dispatch} dispatch - The Redux dispatch function.
  58. * @private
  59. * @returns {void}
  60. */
  61. function _addConferenceListeners(conference, dispatch) {
  62. // Dispatches into features/base/conference follow:
  63. conference.on(
  64. JitsiConferenceEvents.CONFERENCE_FAILED,
  65. (...args) => dispatch(conferenceFailed(conference, ...args)));
  66. conference.on(
  67. JitsiConferenceEvents.CONFERENCE_JOINED,
  68. (...args) => dispatch(conferenceJoined(conference, ...args)));
  69. conference.on(
  70. JitsiConferenceEvents.CONFERENCE_LEFT,
  71. (...args) => dispatch(conferenceLeft(conference, ...args)));
  72. conference.on(
  73. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  74. (...args) => dispatch(lockStateChanged(conference, ...args)));
  75. // Dispatches into features/base/media follow:
  76. conference.on(
  77. JitsiConferenceEvents.STARTED_MUTED,
  78. () => {
  79. const audioMuted = Boolean(conference.startAudioMuted);
  80. const videoMuted = Boolean(conference.startVideoMuted);
  81. sendAnalyticsEvent(
  82. `${START_MUTED_SERVER_AUDIO_}.${
  83. audioMuted ? 'muted' : 'unmuted'}`);
  84. sendAnalyticsEvent(
  85. `${START_MUTED_SERVER_VIDEO_}.${
  86. videoMuted ? 'muted' : 'unmuted'}`);
  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 the lock state of a specific JitsiConference changed.
  324. *
  325. * @param {JitsiConference} conference - The JitsiConference which had its lock
  326. * state changed.
  327. * @param {boolean} locked - If the specified conference became locked, true;
  328. * otherwise, false.
  329. * @returns {{
  330. * type: LOCK_STATE_CHANGED,
  331. * conference: JitsiConference,
  332. * locked: boolean
  333. * }}
  334. */
  335. export function lockStateChanged(conference: Object, locked: boolean) {
  336. return {
  337. type: LOCK_STATE_CHANGED,
  338. conference,
  339. locked
  340. };
  341. }
  342. /**
  343. * Updates the known state of start muted policies.
  344. *
  345. * @param {boolean} audioMuted - Whether or not members will join the conference
  346. * as audio muted.
  347. * @param {boolean} videoMuted - Whether or not members will join the conference
  348. * as video muted.
  349. * @returns {{
  350. * type: SET_START_MUTED_POLICY,
  351. * startAudioMutedPolicy: boolean,
  352. * startVideoMutedPolicy: boolean
  353. * }}
  354. */
  355. export function onStartMutedPolicyChanged(
  356. audioMuted: boolean, videoMuted: boolean) {
  357. return {
  358. type: SET_START_MUTED_POLICY,
  359. startAudioMutedPolicy: audioMuted,
  360. startVideoMutedPolicy: videoMuted
  361. };
  362. }
  363. /**
  364. * Sets whether or not peer2peer is currently enabled.
  365. *
  366. * @param {boolean} p2p - Whether or not peer2peer is currently active.
  367. * @returns {{
  368. * type: P2P_STATUS_CHANGED,
  369. * p2p: boolean
  370. * }}
  371. */
  372. export function p2pStatusChanged(p2p: boolean) {
  373. return {
  374. type: P2P_STATUS_CHANGED,
  375. p2p
  376. };
  377. }
  378. /**
  379. * Sets the audio-only flag for the current JitsiConference.
  380. *
  381. * @param {boolean} audioOnly - True if the conference should be audio only;
  382. * false, otherwise.
  383. * @returns {{
  384. * type: SET_AUDIO_ONLY,
  385. * audioOnly: boolean
  386. * }}
  387. */
  388. export function setAudioOnly(audioOnly: boolean) {
  389. return {
  390. type: SET_AUDIO_ONLY,
  391. audioOnly
  392. };
  393. }
  394. /**
  395. * Enables or disables the Follow Me feature.
  396. *
  397. * @param {boolean} enabled - Whether or not Follow Me should be enabled.
  398. * @returns {{
  399. * type: SET_FOLLOW_ME,
  400. * enabled: boolean
  401. * }}
  402. */
  403. export function setFollowMe(enabled: boolean) {
  404. if (typeof APP !== 'undefined') {
  405. APP.UI.emitEvent(UIEvents.FOLLOW_ME_ENABLED, enabled);
  406. }
  407. return {
  408. type: SET_FOLLOW_ME,
  409. enabled
  410. };
  411. }
  412. /**
  413. * Sets the video channel's last N (value) of the current conference. A value of
  414. * undefined shall be used to reset it to the default value.
  415. *
  416. * @param {(number|undefined)} lastN - The last N value to be set.
  417. * @returns {Function}
  418. */
  419. export function setLastN(lastN: ?number) {
  420. return (dispatch: Dispatch<*>, getState: Function) => {
  421. if (typeof lastN === 'undefined') {
  422. const config = getState()['features/base/config'];
  423. /* eslint-disable no-param-reassign */
  424. lastN = config.channelLastN;
  425. if (typeof lastN === 'undefined') {
  426. lastN = -1;
  427. }
  428. /* eslint-enable no-param-reassign */
  429. }
  430. dispatch({
  431. type: SET_LASTN,
  432. lastN
  433. });
  434. };
  435. }
  436. /**
  437. * Sets the password to join or lock a specific JitsiConference.
  438. *
  439. * @param {JitsiConference} conference - The JitsiConference which requires a
  440. * password to join or is to be locked with the specified password.
  441. * @param {Function} method - The JitsiConference method of password protection
  442. * such as join or lock.
  443. * @param {string} password - The password with which the specified conference
  444. * is to be joined or locked.
  445. * @returns {Function}
  446. */
  447. export function setPassword(
  448. conference: Object,
  449. method: Function,
  450. password: string) {
  451. return (dispatch: Dispatch<*>, getState: Function): ?Promise<void> => {
  452. switch (method) {
  453. case conference.join: {
  454. let state = getState()['features/base/conference'];
  455. // Make sure that the action will set a password for a conference
  456. // that the application wants joined.
  457. if (state.passwordRequired === conference) {
  458. dispatch({
  459. type: SET_PASSWORD,
  460. conference,
  461. method,
  462. password
  463. });
  464. // Join the conference with the newly-set password.
  465. // Make sure that the action did set the password.
  466. state = getState()['features/base/conference'];
  467. if (state.password === password
  468. && !state.passwordRequired
  469. // Make sure that the application still wants the
  470. // conference joined.
  471. && !state.conference) {
  472. method.call(conference, password);
  473. }
  474. }
  475. break;
  476. }
  477. case conference.lock: {
  478. const state = getState()['features/base/conference'];
  479. if (state.conference === conference) {
  480. return (
  481. method.call(conference, password)
  482. .then(() => dispatch({
  483. type: SET_PASSWORD,
  484. conference,
  485. method,
  486. password
  487. }))
  488. .catch(error => dispatch({
  489. type: SET_PASSWORD_FAILED,
  490. error
  491. }))
  492. );
  493. }
  494. return Promise.reject();
  495. }
  496. }
  497. };
  498. }
  499. /**
  500. * Sets the max frame height to receive from remote participant videos.
  501. *
  502. * @param {number} receiveVideoQuality - The max video resolution to receive.
  503. * @returns {{
  504. * type: SET_RECEIVE_VIDEO_QUALITY,
  505. * receiveVideoQuality: number
  506. * }}
  507. */
  508. export function setReceiveVideoQuality(receiveVideoQuality: number) {
  509. return {
  510. type: SET_RECEIVE_VIDEO_QUALITY,
  511. receiveVideoQuality
  512. };
  513. }
  514. /**
  515. * Sets (the name of) the room of the conference to be joined.
  516. *
  517. * @param {(string|undefined)} room - The name of the room of the conference to
  518. * be joined.
  519. * @returns {{
  520. * type: SET_ROOM,
  521. * room: string
  522. * }}
  523. */
  524. export function setRoom(room: ?string) {
  525. return {
  526. type: SET_ROOM,
  527. room
  528. };
  529. }
  530. /**
  531. * Sets whether or not members should join audio and/or video muted.
  532. *
  533. * @param {boolean} startAudioMuted - Whether or not members will join the
  534. * conference as audio muted.
  535. * @param {boolean} startVideoMuted - Whether or not members will join the
  536. * conference as video muted.
  537. * @returns {Function}
  538. */
  539. export function setStartMutedPolicy(
  540. startAudioMuted: boolean, startVideoMuted: boolean) {
  541. return (dispatch: Dispatch<*>, getState: Function) => {
  542. const { conference } = getState()['features/base/conference'];
  543. conference.setStartMutedPolicy({
  544. audio: startAudioMuted,
  545. video: startVideoMuted
  546. });
  547. return dispatch(
  548. onStartMutedPolicyChanged(startAudioMuted, startVideoMuted));
  549. };
  550. }
  551. /**
  552. * Toggles the audio-only flag for the current JitsiConference.
  553. *
  554. * @returns {Function}
  555. */
  556. export function toggleAudioOnly() {
  557. return (dispatch: Dispatch<*>, getState: Function) => {
  558. const { audioOnly } = getState()['features/base/conference'];
  559. return dispatch(setAudioOnly(!audioOnly));
  560. };
  561. }