Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 19KB

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