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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 { getLocalTracks, 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. = getLocalTracks(getState()['features/base/tracks'])
  203. .map(t => t.jitsiTrack);
  204. if (localTracks.length) {
  205. _addLocalTracksToConference(conference, localTracks);
  206. }
  207. dispatch({
  208. type: CONFERENCE_WILL_JOIN,
  209. conference
  210. });
  211. };
  212. }
  213. /**
  214. * Signals the intention of the application to have the local participant leave
  215. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  216. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  217. * lib-jitsi-meet and not the application.
  218. *
  219. * @param {JitsiConference} conference - The JitsiConference instance which will
  220. * be left by the local participant.
  221. * @returns {{
  222. * type: CONFERENCE_LEFT,
  223. * conference: JitsiConference
  224. * }}
  225. */
  226. export function conferenceWillLeave(conference: Object) {
  227. return {
  228. type: CONFERENCE_WILL_LEAVE,
  229. conference
  230. };
  231. }
  232. /**
  233. * Initializes a new conference.
  234. *
  235. * @returns {Function}
  236. */
  237. export function createConference() {
  238. return (dispatch: Function, getState: Function) => {
  239. const state = getState();
  240. const { connection, locationURL } = state['features/base/connection'];
  241. if (!connection) {
  242. throw new Error('Cannot create a conference without a connection!');
  243. }
  244. const { password, room } = state['features/base/conference'];
  245. if (!room) {
  246. throw new Error('Cannot join a conference without a room name!');
  247. }
  248. const conference
  249. = connection.initJitsiConference(
  250. // XXX Lib-jitsi-meet does not accept uppercase letters.
  251. room.toLowerCase(), {
  252. ...state['features/base/config'],
  253. applicationName: getName()
  254. });
  255. conference[JITSI_CONFERENCE_URL_KEY] = locationURL;
  256. dispatch(_conferenceWillJoin(conference));
  257. _addConferenceListeners(conference, dispatch);
  258. sendLocalParticipant(state, conference);
  259. conference.join(password);
  260. };
  261. }
  262. /**
  263. * Will try to join the conference again in case it failed earlier with
  264. * {@link JitsiConferenceErrors.AUTHENTICATION_REQUIRED}. It means that Jicofo
  265. * did not allow to create new room from anonymous domain, but it can be tried
  266. * again later in case authenticated user created it in the meantime.
  267. *
  268. * @returns {Function}
  269. */
  270. export function checkIfCanJoin() {
  271. return (dispatch: Dispatch<*>, getState: Function) => {
  272. const { authRequired, password }
  273. = getState()['features/base/conference'];
  274. authRequired && authRequired.join(password);
  275. };
  276. }
  277. /**
  278. * Signals the data channel with the bridge has successfully opened.
  279. *
  280. * @returns {{
  281. * type: DATA_CHANNEL_OPENED
  282. * }}
  283. */
  284. export function dataChannelOpened() {
  285. return {
  286. type: DATA_CHANNEL_OPENED
  287. };
  288. }
  289. /**
  290. * Signals that the lock state of a specific JitsiConference changed.
  291. *
  292. * @param {JitsiConference} conference - The JitsiConference which had its lock
  293. * state changed.
  294. * @param {boolean} locked - If the specified conference became locked, true;
  295. * otherwise, false.
  296. * @returns {{
  297. * type: LOCK_STATE_CHANGED,
  298. * conference: JitsiConference,
  299. * locked: boolean
  300. * }}
  301. */
  302. export function lockStateChanged(conference: Object, locked: boolean) {
  303. return {
  304. type: LOCK_STATE_CHANGED,
  305. conference,
  306. locked
  307. };
  308. }
  309. /**
  310. * Sets whether or not peer2peer is currently enabled.
  311. *
  312. * @param {boolean} p2p - Whether or not peer2peer is currently active.
  313. * @returns {{
  314. * type: P2P_STATUS_CHANGED,
  315. * p2p: boolean
  316. * }}
  317. */
  318. export function p2pStatusChanged(p2p: boolean) {
  319. return {
  320. type: P2P_STATUS_CHANGED,
  321. p2p
  322. };
  323. }
  324. /**
  325. * Sets the audio-only flag for the current JitsiConference.
  326. *
  327. * @param {boolean} audioOnly - True if the conference should be audio only;
  328. * false, otherwise.
  329. * @returns {{
  330. * type: SET_AUDIO_ONLY,
  331. * audioOnly: boolean
  332. * }}
  333. */
  334. export function setAudioOnly(audioOnly: boolean) {
  335. return {
  336. type: SET_AUDIO_ONLY,
  337. audioOnly
  338. };
  339. }
  340. /**
  341. * Sets the video channel's last N (value) of the current conference. A value of
  342. * undefined shall be used to reset it to the default value.
  343. *
  344. * @param {(number|undefined)} lastN - The last N value to be set.
  345. * @returns {Function}
  346. */
  347. export function setLastN(lastN: ?number) {
  348. return (dispatch: Dispatch<*>, getState: Function) => {
  349. if (typeof lastN === 'undefined') {
  350. const config = getState()['features/base/config'];
  351. /* eslint-disable no-param-reassign */
  352. lastN = config.channelLastN;
  353. if (typeof lastN === 'undefined') {
  354. lastN = -1;
  355. }
  356. /* eslint-enable no-param-reassign */
  357. }
  358. dispatch({
  359. type: SET_LASTN,
  360. lastN
  361. });
  362. };
  363. }
  364. /**
  365. * Sets the password to join or lock a specific JitsiConference.
  366. *
  367. * @param {JitsiConference} conference - The JitsiConference which requires a
  368. * password to join or is to be locked with the specified password.
  369. * @param {Function} method - The JitsiConference method of password protection
  370. * such as join or lock.
  371. * @param {string} password - The password with which the specified conference
  372. * is to be joined or locked.
  373. * @returns {Function}
  374. */
  375. export function setPassword(
  376. conference: Object,
  377. method: Function,
  378. password: string) {
  379. return (dispatch: Dispatch<*>, getState: Function) => {
  380. switch (method) {
  381. case conference.join: {
  382. let state = getState()['features/base/conference'];
  383. // Make sure that the action will set a password for a conference
  384. // that the application wants joined.
  385. if (state.passwordRequired === conference) {
  386. dispatch({
  387. type: SET_PASSWORD,
  388. conference,
  389. method,
  390. password
  391. });
  392. // Join the conference with the newly-set password.
  393. // Make sure that the action did set the password.
  394. state = getState()['features/base/conference'];
  395. if (state.password === password
  396. && !state.passwordRequired
  397. // Make sure that the application still wants the
  398. // conference joined.
  399. && !state.conference) {
  400. method.call(conference, password);
  401. }
  402. }
  403. break;
  404. }
  405. case conference.lock: {
  406. const state = getState()['features/base/conference'];
  407. if (state.conference === conference) {
  408. return (
  409. method.call(conference, password)
  410. .then(() => dispatch({
  411. type: SET_PASSWORD,
  412. conference,
  413. method,
  414. password
  415. }))
  416. .catch(error => dispatch({
  417. type: SET_PASSWORD_FAILED,
  418. error
  419. }))
  420. );
  421. }
  422. return Promise.reject();
  423. }
  424. }
  425. };
  426. }
  427. /**
  428. * Sets the max frame height to receive from remote participant videos.
  429. *
  430. * @param {number} receiveVideoQuality - The max video resolution to receive.
  431. * @returns {{
  432. * type: SET_RECEIVE_VIDEO_QUALITY,
  433. * receiveVideoQuality: number
  434. * }}
  435. */
  436. export function setReceiveVideoQuality(receiveVideoQuality: number) {
  437. return {
  438. type: SET_RECEIVE_VIDEO_QUALITY,
  439. receiveVideoQuality
  440. };
  441. }
  442. /**
  443. * Sets (the name of) the room of the conference to be joined.
  444. *
  445. * @param {(string|undefined)} room - The name of the room of the conference to
  446. * be joined.
  447. * @returns {{
  448. * type: SET_ROOM,
  449. * room: string
  450. * }}
  451. */
  452. export function setRoom(room: ?string) {
  453. return {
  454. type: SET_ROOM,
  455. room
  456. };
  457. }
  458. /**
  459. * Toggles the audio-only flag for the current JitsiConference.
  460. *
  461. * @returns {Function}
  462. */
  463. export function toggleAudioOnly() {
  464. return (dispatch: Dispatch<*>, getState: Function) => {
  465. const { audioOnly } = getState()['features/base/conference'];
  466. return dispatch(setAudioOnly(!audioOnly));
  467. };
  468. }