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

actions.js 20KB

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