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.

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