您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 20KB

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