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.

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