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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  2. import { setVideoMuted } from '../media';
  3. import {
  4. dominantSpeakerChanged,
  5. getLocalParticipant,
  6. participantConnectionStatusChanged,
  7. participantJoined,
  8. participantLeft,
  9. participantRoleChanged,
  10. participantUpdated
  11. } from '../participants';
  12. import { trackAdded, trackRemoved } from '../tracks';
  13. import {
  14. CONFERENCE_FAILED,
  15. CONFERENCE_JOINED,
  16. CONFERENCE_LEFT,
  17. CONFERENCE_WILL_JOIN,
  18. CONFERENCE_WILL_LEAVE,
  19. LOCK_STATE_CHANGED,
  20. SET_AUDIO_ONLY,
  21. _SET_AUDIO_ONLY_VIDEO_MUTED,
  22. SET_LASTN,
  23. SET_PASSWORD,
  24. SET_ROOM
  25. } from './actionTypes';
  26. import {
  27. AVATAR_ID_COMMAND,
  28. AVATAR_URL_COMMAND,
  29. EMAIL_COMMAND
  30. } from './constants';
  31. import { _addLocalTracksToConference } from './functions';
  32. import type { Dispatch } from 'redux';
  33. /**
  34. * Adds conference (event) listeners.
  35. *
  36. * @param {JitsiConference} conference - The JitsiConference instance.
  37. * @param {Dispatch} dispatch - The Redux dispatch function.
  38. * @private
  39. * @returns {void}
  40. */
  41. function _addConferenceListeners(conference, dispatch) {
  42. // Dispatches into features/base/conference follow:
  43. conference.on(
  44. JitsiConferenceEvents.CONFERENCE_FAILED,
  45. (...args) => dispatch(conferenceFailed(conference, ...args)));
  46. conference.on(
  47. JitsiConferenceEvents.CONFERENCE_JOINED,
  48. (...args) => dispatch(conferenceJoined(conference, ...args)));
  49. conference.on(
  50. JitsiConferenceEvents.CONFERENCE_LEFT,
  51. (...args) => dispatch(conferenceLeft(conference, ...args)));
  52. conference.on(
  53. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  54. (...args) => dispatch(_lockStateChanged(conference, ...args)));
  55. // Dispatches into features/base/tracks follow:
  56. conference.on(
  57. JitsiConferenceEvents.TRACK_ADDED,
  58. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  59. conference.on(
  60. JitsiConferenceEvents.TRACK_REMOVED,
  61. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  62. // Dispatches into features/base/participants follow:
  63. conference.on(
  64. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  65. (...args) => dispatch(dominantSpeakerChanged(...args)));
  66. conference.on(
  67. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  68. (...args) => dispatch(participantConnectionStatusChanged(...args)));
  69. conference.on(
  70. JitsiConferenceEvents.USER_JOINED,
  71. (id, user) => dispatch(participantJoined({
  72. id,
  73. name: user.getDisplayName(),
  74. role: user.getRole()
  75. })));
  76. conference.on(
  77. JitsiConferenceEvents.USER_LEFT,
  78. (...args) => dispatch(participantLeft(...args)));
  79. conference.on(
  80. JitsiConferenceEvents.USER_ROLE_CHANGED,
  81. (...args) => dispatch(participantRoleChanged(...args)));
  82. conference.addCommandListener(
  83. AVATAR_ID_COMMAND,
  84. (data, id) => dispatch(participantUpdated({
  85. id,
  86. avatarID: data.value
  87. })));
  88. conference.addCommandListener(
  89. AVATAR_URL_COMMAND,
  90. (data, id) => dispatch(participantUpdated({
  91. id,
  92. avatarURL: data.value
  93. })));
  94. conference.addCommandListener(
  95. EMAIL_COMMAND,
  96. (data, id) => dispatch(participantUpdated({
  97. id,
  98. email: data.value
  99. })));
  100. }
  101. /**
  102. * Sets the data for the local participant to the conference.
  103. *
  104. * @param {JitsiConference} conference - The JitsiConference instance.
  105. * @param {Object} state - The Redux state.
  106. * @returns {void}
  107. */
  108. function _setLocalParticipantData(conference, state) {
  109. const localParticipant
  110. = getLocalParticipant(state['features/base/participants']);
  111. conference.removeCommand(AVATAR_ID_COMMAND);
  112. conference.sendCommand(AVATAR_ID_COMMAND, {
  113. value: localParticipant.avatarID
  114. });
  115. }
  116. /**
  117. * Signals that a specific conference has failed.
  118. *
  119. * @param {JitsiConference} conference - The JitsiConference that has failed.
  120. * @param {string} error - The error describing/detailing the cause of the
  121. * failure.
  122. * @returns {{
  123. * type: CONFERENCE_FAILED,
  124. * conference: JitsiConference,
  125. * error: string
  126. * }}
  127. * @public
  128. */
  129. export function conferenceFailed(conference, error) {
  130. return {
  131. type: CONFERENCE_FAILED,
  132. conference,
  133. error
  134. };
  135. }
  136. /**
  137. * Attach any pre-existing local media to the conference once the conference has
  138. * been joined.
  139. *
  140. * @param {JitsiConference} conference - The JitsiConference instance which was
  141. * joined by the local participant.
  142. * @returns {Function}
  143. */
  144. export function conferenceJoined(conference) {
  145. return (dispatch, getState) => {
  146. const localTracks
  147. = getState()['features/base/tracks']
  148. .filter(t => t.local)
  149. .map(t => t.jitsiTrack);
  150. if (localTracks.length) {
  151. _addLocalTracksToConference(conference, localTracks);
  152. }
  153. dispatch({
  154. type: CONFERENCE_JOINED,
  155. conference
  156. });
  157. };
  158. }
  159. /**
  160. * Signals that a specific conference has been left.
  161. *
  162. * @param {JitsiConference} conference - The JitsiConference instance which was
  163. * left by the local participant.
  164. * @returns {{
  165. * type: CONFERENCE_LEFT,
  166. * conference: JitsiConference
  167. * }}
  168. */
  169. export function conferenceLeft(conference) {
  170. return {
  171. type: CONFERENCE_LEFT,
  172. conference
  173. };
  174. }
  175. /**
  176. * Signals the intention of the application to have the local participant join a
  177. * conference with a specific room (name). Similar in fashion
  178. * to CONFERENCE_JOINED.
  179. *
  180. * @param {string} room - The room (name) which identifies the conference the
  181. * local participant will (try to) join.
  182. * @returns {{
  183. * type: CONFERENCE_WILL_JOIN,
  184. * room: string
  185. * }}
  186. */
  187. function _conferenceWillJoin(room) {
  188. return {
  189. type: CONFERENCE_WILL_JOIN,
  190. room
  191. };
  192. }
  193. /**
  194. * Signals the intention of the application to have the local participant leave
  195. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  196. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  197. * lib-jitsi-meet and not the application.
  198. *
  199. * @param {JitsiConference} conference - The JitsiConference instance which will
  200. * be left by the local participant.
  201. * @returns {{
  202. * type: CONFERENCE_LEFT,
  203. * conference: JitsiConference
  204. * }}
  205. */
  206. export function conferenceWillLeave(conference) {
  207. return {
  208. type: CONFERENCE_WILL_LEAVE,
  209. conference
  210. };
  211. }
  212. /**
  213. * Initializes a new conference.
  214. *
  215. * @returns {Function}
  216. */
  217. export function createConference() {
  218. return (dispatch, getState) => {
  219. const state = getState();
  220. const connection = state['features/base/connection'].connection;
  221. if (!connection) {
  222. throw new Error('Cannot create conference without connection');
  223. }
  224. const { password, room } = state['features/base/conference'];
  225. if (typeof room === 'undefined' || room === '') {
  226. throw new Error('Cannot join conference without room name');
  227. }
  228. dispatch(_conferenceWillJoin(room));
  229. // TODO Take options from config.
  230. const conference
  231. = connection.initJitsiConference(
  232. // XXX Lib-jitsi-meet does not accept uppercase letters.
  233. room.toLowerCase(),
  234. {
  235. openSctp: true
  236. // FIXME I tested H.264 from iPhone 6S during a morning
  237. // standup but, unfortunately, the other participants who
  238. // happened to be running the Web app saw only black.
  239. //
  240. // preferH264: true
  241. });
  242. _addConferenceListeners(conference, dispatch);
  243. _setLocalParticipantData(conference, state);
  244. conference.join(password);
  245. };
  246. }
  247. /**
  248. * Signals that the lock state of a specific JitsiConference changed.
  249. *
  250. * @param {JitsiConference} conference - The JitsiConference which had its lock
  251. * state changed.
  252. * @param {boolean} locked - If the specified conference became locked, true;
  253. * otherwise, false.
  254. * @returns {{
  255. * type: LOCK_STATE_CHANGED,
  256. * conference: JitsiConference,
  257. * locked: boolean
  258. * }}
  259. */
  260. function _lockStateChanged(conference, locked) {
  261. return {
  262. type: LOCK_STATE_CHANGED,
  263. conference,
  264. locked
  265. };
  266. }
  267. /**
  268. * Sets the audio-only flag for the current JitsiConference.
  269. *
  270. * @param {boolean} audioOnly - True if the conference should be audio only;
  271. * false, otherwise.
  272. * @private
  273. * @returns {{
  274. * type: SET_AUDIO_ONLY,
  275. * audioOnly: boolean
  276. * }}
  277. */
  278. function _setAudioOnly(audioOnly) {
  279. return {
  280. type: SET_AUDIO_ONLY,
  281. audioOnly
  282. };
  283. }
  284. /**
  285. * Signals that the app should mute video because it's now in audio-only mode,
  286. * or unmute it because it no longer is. If video was already muted, nothing
  287. * will happen; otherwise, it will be muted. When audio-only mode is disabled,
  288. * the previous state will be restored.
  289. *
  290. * @param {boolean} muted - True if video should be muted; false, otherwise.
  291. * @protected
  292. * @returns {Function}
  293. */
  294. export function _setAudioOnlyVideoMuted(muted: boolean) {
  295. return (dispatch, getState) => {
  296. if (muted) {
  297. const { video } = getState()['features/base/media'];
  298. if (video.muted) {
  299. // Video is already muted, do nothing.
  300. return;
  301. }
  302. } else {
  303. const { audioOnlyVideoMuted }
  304. = getState()['features/base/conference'];
  305. if (!audioOnlyVideoMuted) {
  306. // We didn't mute video, do nothing.
  307. return;
  308. }
  309. }
  310. // Remember that local video was muted due to the audio-only mode
  311. // vs user's choice.
  312. dispatch({
  313. type: _SET_AUDIO_ONLY_VIDEO_MUTED,
  314. muted
  315. });
  316. dispatch(setVideoMuted(muted));
  317. };
  318. }
  319. /**
  320. * Sets the video channel's last N (value) of the current conference. A value of
  321. * undefined shall be used to reset it to the default value.
  322. *
  323. * @param {(number|undefined)} lastN - The last N value to be set.
  324. * @returns {Function}
  325. */
  326. export function setLastN(lastN: ?number) {
  327. return (dispatch: Dispatch<*>, getState: Function) => {
  328. if (typeof lastN === 'undefined') {
  329. const { config } = getState()['features/base/lib-jitsi-meet'];
  330. /* eslint-disable no-param-reassign */
  331. lastN = config.channelLastN;
  332. if (typeof lastN === 'undefined') {
  333. lastN = -1;
  334. }
  335. /* eslint-enable no-param-reassign */
  336. }
  337. dispatch({
  338. type: SET_LASTN,
  339. lastN
  340. });
  341. };
  342. }
  343. /**
  344. * Sets the password to join or lock a specific JitsiConference.
  345. *
  346. * @param {JitsiConference} conference - The JitsiConference which requires a
  347. * password to join or is to be locked with the specified password.
  348. * @param {Function} method - The JitsiConference method of password protection
  349. * such as join or lock.
  350. * @param {string} password - The password with which the specified conference
  351. * is to be joined or locked.
  352. * @returns {Function}
  353. */
  354. export function setPassword(conference, method, password) {
  355. return (dispatch, getState) => {
  356. switch (method) {
  357. case conference.join: {
  358. let state = getState()['features/base/conference'];
  359. // Make sure that the action will set a password for a conference
  360. // that the application wants joined.
  361. if (state.passwordRequired === conference) {
  362. dispatch({
  363. type: SET_PASSWORD,
  364. conference,
  365. method,
  366. password
  367. });
  368. // Join the conference with the newly-set password.
  369. // Make sure that the action did set the password.
  370. state = getState()['features/base/conference'];
  371. if (state.password === password
  372. && !state.passwordRequired
  373. // Make sure that the application still wants the
  374. // conference joined.
  375. && !state.conference) {
  376. method.call(conference, password);
  377. }
  378. }
  379. break;
  380. }
  381. case conference.lock: {
  382. const state = getState()['features/base/conference'];
  383. if (state.conference === conference) {
  384. return (
  385. method.call(conference, password)
  386. .then(() => dispatch({
  387. type: SET_PASSWORD,
  388. conference,
  389. method,
  390. password
  391. })));
  392. }
  393. return Promise.reject();
  394. }
  395. }
  396. };
  397. }
  398. /**
  399. * Sets (the name of) the room of the conference to be joined.
  400. *
  401. * @param {(string|undefined)} room - The name of the room of the conference to
  402. * be joined.
  403. * @returns {{
  404. * type: SET_ROOM,
  405. * room: string
  406. * }}
  407. */
  408. export function setRoom(room) {
  409. return {
  410. type: SET_ROOM,
  411. room
  412. };
  413. }
  414. /**
  415. * Toggles the audio-only flag for the current JitsiConference.
  416. *
  417. * @returns {Function}
  418. */
  419. export function toggleAudioOnly() {
  420. return (dispatch: Dispatch<*>, getState: Function) => {
  421. const { audioOnly } = getState()['features/base/conference'];
  422. return dispatch(_setAudioOnly(!audioOnly));
  423. };
  424. }