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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // @flow
  2. import { sendAnalyticsEvent } from '../../analytics';
  3. import { getName } from '../../app';
  4. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  5. import { setAudioMuted, setVideoMuted } from '../media';
  6. import {
  7. dominantSpeakerChanged,
  8. participantConnectionStatusChanged,
  9. participantJoined,
  10. participantLeft,
  11. participantRoleChanged,
  12. participantUpdated
  13. } from '../participants';
  14. import { trackAdded, trackRemoved } from '../tracks';
  15. import {
  16. CONFERENCE_FAILED,
  17. CONFERENCE_JOINED,
  18. CONFERENCE_LEFT,
  19. CONFERENCE_WILL_JOIN,
  20. CONFERENCE_WILL_LEAVE,
  21. DATA_CHANNEL_OPENED,
  22. LOCK_STATE_CHANGED,
  23. P2P_STATUS_CHANGED,
  24. SET_AUDIO_ONLY,
  25. SET_LASTN,
  26. SET_PASSWORD,
  27. SET_PASSWORD_FAILED,
  28. SET_RECEIVE_VIDEO_QUALITY,
  29. SET_ROOM
  30. } from './actionTypes';
  31. import {
  32. AVATAR_ID_COMMAND,
  33. AVATAR_URL_COMMAND,
  34. EMAIL_COMMAND,
  35. JITSI_CONFERENCE_URL_KEY
  36. } from './constants';
  37. import {
  38. _addLocalTracksToConference,
  39. sendLocalParticipant
  40. } from './functions';
  41. import type { Dispatch } from 'redux';
  42. const logger = require('jitsi-meet-logger').getLogger(__filename);
  43. /**
  44. * Adds conference (event) listeners.
  45. *
  46. * @param {JitsiConference} conference - The JitsiConference instance.
  47. * @param {Dispatch} dispatch - The Redux dispatch function.
  48. * @private
  49. * @returns {void}
  50. */
  51. function _addConferenceListeners(conference, dispatch) {
  52. // Dispatches into features/base/conference follow:
  53. conference.on(
  54. JitsiConferenceEvents.CONFERENCE_FAILED,
  55. (...args) => dispatch(conferenceFailed(conference, ...args)));
  56. conference.on(
  57. JitsiConferenceEvents.CONFERENCE_JOINED,
  58. (...args) => dispatch(conferenceJoined(conference, ...args)));
  59. conference.on(
  60. JitsiConferenceEvents.CONFERENCE_LEFT,
  61. (...args) => dispatch(conferenceLeft(conference, ...args)));
  62. conference.on(
  63. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  64. (...args) => dispatch(lockStateChanged(conference, ...args)));
  65. // Dispatches into features/base/media follow:
  66. conference.on(
  67. JitsiConferenceEvents.STARTED_MUTED,
  68. () => {
  69. const audioMuted = Boolean(conference.startAudioMuted);
  70. const videoMuted = Boolean(conference.startVideoMuted);
  71. sendAnalyticsEvent(
  72. `startmuted.server.audio.${audioMuted ? 'muted' : 'unmuted'}`);
  73. sendAnalyticsEvent(
  74. `startmuted.server.video.${videoMuted ? 'muted' : 'unmuted'}`);
  75. logger.log(`Start muted: ${audioMuted ? 'audio, ' : ''}${
  76. videoMuted ? 'video' : ''}`);
  77. // XXX Jicofo tells lib-jitsi-meet to start with audio and/or video
  78. // muted i.e. Jicofo expresses an intent. Lib-jitsi-meet has turned
  79. // Jicofo's intent into reality by actually muting the respective
  80. // tracks. The reality is expressed in base/tracks already so what
  81. // is left is to express Jicofo's intent in base/media.
  82. // TODO Maybe the app needs to learn about Jicofo's intent and
  83. // transfer that intent to lib-jitsi-meet instead of lib-jitsi-meet
  84. // acting on Jicofo's intent without the app's knowledge.
  85. dispatch(setAudioMuted(audioMuted));
  86. dispatch(setVideoMuted(videoMuted));
  87. });
  88. // Dispatches into features/base/tracks follow:
  89. conference.on(
  90. JitsiConferenceEvents.TRACK_ADDED,
  91. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  92. conference.on(
  93. JitsiConferenceEvents.TRACK_REMOVED,
  94. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  95. // Dispatches into features/base/participants follow:
  96. conference.on(
  97. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  98. (...args) => dispatch(dominantSpeakerChanged(...args)));
  99. conference.on(
  100. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  101. (...args) => dispatch(participantConnectionStatusChanged(...args)));
  102. conference.on(
  103. JitsiConferenceEvents.USER_JOINED,
  104. (id, user) => dispatch(participantJoined({
  105. id,
  106. name: user.getDisplayName(),
  107. role: user.getRole()
  108. })));
  109. conference.on(
  110. JitsiConferenceEvents.USER_LEFT,
  111. (...args) => dispatch(participantLeft(...args)));
  112. conference.on(
  113. JitsiConferenceEvents.USER_ROLE_CHANGED,
  114. (...args) => dispatch(participantRoleChanged(...args)));
  115. conference.addCommandListener(
  116. AVATAR_ID_COMMAND,
  117. (data, id) => dispatch(participantUpdated({
  118. id,
  119. avatarID: data.value
  120. })));
  121. conference.addCommandListener(
  122. AVATAR_URL_COMMAND,
  123. (data, id) => dispatch(participantUpdated({
  124. id,
  125. avatarURL: data.value
  126. })));
  127. conference.addCommandListener(
  128. EMAIL_COMMAND,
  129. (data, id) => dispatch(participantUpdated({
  130. id,
  131. email: data.value
  132. })));
  133. }
  134. /**
  135. * Signals that a specific conference has failed.
  136. *
  137. * @param {JitsiConference} conference - The JitsiConference that has failed.
  138. * @param {string} error - The error describing/detailing the cause of the
  139. * failure.
  140. * @returns {{
  141. * type: CONFERENCE_FAILED,
  142. * conference: JitsiConference,
  143. * error: Error
  144. * }}
  145. * @public
  146. */
  147. export function conferenceFailed(conference: Object, error: string) {
  148. return {
  149. type: CONFERENCE_FAILED,
  150. conference,
  151. // Make the error resemble an Error instance (to the extent that
  152. // jitsi-meet needs it).
  153. error: {
  154. name: error
  155. }
  156. };
  157. }
  158. /**
  159. * Signals that a specific conference has been joined.
  160. *
  161. * @param {JitsiConference} conference - The JitsiConference instance which was
  162. * joined by the local participant.
  163. * @returns {{
  164. * type: CONFERENCE_JOINED,
  165. * conference: JitsiConference
  166. * }}
  167. */
  168. export function conferenceJoined(conference: Object) {
  169. return {
  170. type: CONFERENCE_JOINED,
  171. conference
  172. };
  173. }
  174. /**
  175. * Signals that a specific conference has been left.
  176. *
  177. * @param {JitsiConference} conference - The JitsiConference instance which was
  178. * left by the local participant.
  179. * @returns {{
  180. * type: CONFERENCE_LEFT,
  181. * conference: JitsiConference
  182. * }}
  183. */
  184. export function conferenceLeft(conference: Object) {
  185. return {
  186. type: CONFERENCE_LEFT,
  187. conference
  188. };
  189. }
  190. /**
  191. * Attaches any pre-existing local media to the conference, before
  192. * the conference will be joined. Then signals the intention of the application
  193. * to have the local participant join a specific conference.
  194. *
  195. * @param {JitsiConference} conference - The JitsiConference instance the
  196. * local participant will (try to) join.
  197. * @returns {Function}
  198. */
  199. function _conferenceWillJoin(conference: Object) {
  200. return (dispatch: Dispatch<*>, getState: Function) => {
  201. const localTracks
  202. = getState()['features/base/tracks']
  203. .filter(t => t.local)
  204. .map(t => t.jitsiTrack);
  205. if (localTracks.length) {
  206. _addLocalTracksToConference(conference, localTracks);
  207. }
  208. dispatch({
  209. type: CONFERENCE_WILL_JOIN,
  210. conference
  211. });
  212. };
  213. }
  214. /**
  215. * Signals the intention of the application to have the local participant leave
  216. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  217. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  218. * lib-jitsi-meet and not the application.
  219. *
  220. * @param {JitsiConference} conference - The JitsiConference instance which will
  221. * be left by the local participant.
  222. * @returns {{
  223. * type: CONFERENCE_LEFT,
  224. * conference: JitsiConference
  225. * }}
  226. */
  227. export function conferenceWillLeave(conference: Object) {
  228. return {
  229. type: CONFERENCE_WILL_LEAVE,
  230. conference
  231. };
  232. }
  233. /**
  234. * Initializes a new conference.
  235. *
  236. * @returns {Function}
  237. */
  238. export function createConference() {
  239. return (dispatch: Function, getState: Function) => {
  240. const state = getState();
  241. const { connection, locationURL } = state['features/base/connection'];
  242. if (!connection) {
  243. throw new Error('Cannot create a conference without a connection!');
  244. }
  245. const { password, room } = state['features/base/conference'];
  246. if (!room) {
  247. throw new Error('Cannot join a conference without a room name!');
  248. }
  249. const conference
  250. = connection.initJitsiConference(
  251. // XXX Lib-jitsi-meet does not accept uppercase letters.
  252. room.toLowerCase(), {
  253. ...state['features/base/config'],
  254. applicationName: getName()
  255. });
  256. conference[JITSI_CONFERENCE_URL_KEY] = locationURL;
  257. dispatch(_conferenceWillJoin(conference));
  258. _addConferenceListeners(conference, dispatch);
  259. sendLocalParticipant(state, conference);
  260. conference.join(password);
  261. };
  262. }
  263. /**
  264. * Will try to join the conference again in case it failed earlier with
  265. * {@link JitsiConferenceErrors.AUTHENTICATION_REQUIRED}. It means that Jicofo
  266. * did not allow to create new room from anonymous domain, but it can be tried
  267. * again later in case authenticated user created it in the meantime.
  268. *
  269. * @returns {Function}
  270. */
  271. export function checkIfCanJoin() {
  272. return (dispatch: Dispatch<*>, getState: Function) => {
  273. const { authRequired, password }
  274. = getState()['features/base/conference'];
  275. authRequired && authRequired.join(password);
  276. };
  277. }
  278. /**
  279. * Signals the data channel with the bridge has successfully opened.
  280. *
  281. * @returns {{
  282. * type: DATA_CHANNEL_OPENED
  283. * }}
  284. */
  285. export function dataChannelOpened() {
  286. return {
  287. type: DATA_CHANNEL_OPENED
  288. };
  289. }
  290. /**
  291. * Signals that the lock state of a specific JitsiConference changed.
  292. *
  293. * @param {JitsiConference} conference - The JitsiConference which had its lock
  294. * state changed.
  295. * @param {boolean} locked - If the specified conference became locked, true;
  296. * otherwise, false.
  297. * @returns {{
  298. * type: LOCK_STATE_CHANGED,
  299. * conference: JitsiConference,
  300. * locked: boolean
  301. * }}
  302. */
  303. export function lockStateChanged(conference: Object, locked: boolean) {
  304. return {
  305. type: LOCK_STATE_CHANGED,
  306. conference,
  307. locked
  308. };
  309. }
  310. /**
  311. * Sets whether or not peer2peer is currently enabled.
  312. *
  313. * @param {boolean} p2p - Whether or not peer2peer is currently active.
  314. * @returns {{
  315. * type: P2P_STATUS_CHANGED,
  316. * p2p: boolean
  317. * }}
  318. */
  319. export function p2pStatusChanged(p2p: boolean) {
  320. return {
  321. type: P2P_STATUS_CHANGED,
  322. p2p
  323. };
  324. }
  325. /**
  326. * Sets the audio-only flag for the current JitsiConference.
  327. *
  328. * @param {boolean} audioOnly - True if the conference should be audio only;
  329. * false, otherwise.
  330. * @returns {{
  331. * type: SET_AUDIO_ONLY,
  332. * audioOnly: boolean
  333. * }}
  334. */
  335. export function setAudioOnly(audioOnly: boolean) {
  336. return {
  337. type: SET_AUDIO_ONLY,
  338. audioOnly
  339. };
  340. }
  341. /**
  342. * Sets the video channel's last N (value) of the current conference. A value of
  343. * undefined shall be used to reset it to the default value.
  344. *
  345. * @param {(number|undefined)} lastN - The last N value to be set.
  346. * @returns {Function}
  347. */
  348. export function setLastN(lastN: ?number) {
  349. return (dispatch: Dispatch<*>, getState: Function) => {
  350. if (typeof lastN === 'undefined') {
  351. const config = getState()['features/base/config'];
  352. /* eslint-disable no-param-reassign */
  353. lastN = config.channelLastN;
  354. if (typeof lastN === 'undefined') {
  355. lastN = -1;
  356. }
  357. /* eslint-enable no-param-reassign */
  358. }
  359. dispatch({
  360. type: SET_LASTN,
  361. lastN
  362. });
  363. };
  364. }
  365. /**
  366. * Sets the password to join or lock a specific JitsiConference.
  367. *
  368. * @param {JitsiConference} conference - The JitsiConference which requires a
  369. * password to join or is to be locked with the specified password.
  370. * @param {Function} method - The JitsiConference method of password protection
  371. * such as join or lock.
  372. * @param {string} password - The password with which the specified conference
  373. * is to be joined or locked.
  374. * @returns {Function}
  375. */
  376. export function setPassword(
  377. conference: Object,
  378. method: Function,
  379. password: string) {
  380. return (dispatch: Dispatch<*>, getState: Function) => {
  381. switch (method) {
  382. case conference.join: {
  383. let state = getState()['features/base/conference'];
  384. // Make sure that the action will set a password for a conference
  385. // that the application wants joined.
  386. if (state.passwordRequired === conference) {
  387. dispatch({
  388. type: SET_PASSWORD,
  389. conference,
  390. method,
  391. password
  392. });
  393. // Join the conference with the newly-set password.
  394. // Make sure that the action did set the password.
  395. state = getState()['features/base/conference'];
  396. if (state.password === password
  397. && !state.passwordRequired
  398. // Make sure that the application still wants the
  399. // conference joined.
  400. && !state.conference) {
  401. method.call(conference, password);
  402. }
  403. }
  404. break;
  405. }
  406. case conference.lock: {
  407. const state = getState()['features/base/conference'];
  408. if (state.conference === conference) {
  409. return (
  410. method.call(conference, password)
  411. .then(() => dispatch({
  412. type: SET_PASSWORD,
  413. conference,
  414. method,
  415. password
  416. }))
  417. .catch(error => dispatch({
  418. type: SET_PASSWORD_FAILED,
  419. error
  420. }))
  421. );
  422. }
  423. return Promise.reject();
  424. }
  425. }
  426. };
  427. }
  428. /**
  429. * Sets the max frame height to receive from remote participant videos.
  430. *
  431. * @param {number} receiveVideoQuality - The max video resolution to receive.
  432. * @returns {{
  433. * type: SET_RECEIVE_VIDEO_QUALITY,
  434. * receiveVideoQuality: number
  435. * }}
  436. */
  437. export function setReceiveVideoQuality(receiveVideoQuality: number) {
  438. return {
  439. type: SET_RECEIVE_VIDEO_QUALITY,
  440. receiveVideoQuality
  441. };
  442. }
  443. /**
  444. * Sets (the name of) the room of the conference to be joined.
  445. *
  446. * @param {(string|undefined)} room - The name of the room of the conference to
  447. * be joined.
  448. * @returns {{
  449. * type: SET_ROOM,
  450. * room: string
  451. * }}
  452. */
  453. export function setRoom(room: ?string) {
  454. return {
  455. type: SET_ROOM,
  456. room
  457. };
  458. }
  459. /**
  460. * Toggles the audio-only flag for the current JitsiConference.
  461. *
  462. * @returns {Function}
  463. */
  464. export function toggleAudioOnly() {
  465. return (dispatch: Dispatch<*>, getState: Function) => {
  466. const { audioOnly } = getState()['features/base/conference'];
  467. return dispatch(setAudioOnly(!audioOnly));
  468. };
  469. }