Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

actions.js 19KB

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