Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 14KB

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