您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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: Error
  145. * }}
  146. * @public
  147. */
  148. export function conferenceFailed(conference: Object, error: string) {
  149. return {
  150. type: CONFERENCE_FAILED,
  151. conference,
  152. // Make the error resemble an Error instance (to the extent that
  153. // jitsi-meet needs it).
  154. error: {
  155. name: error
  156. }
  157. };
  158. }
  159. /**
  160. * Signals that a specific conference has been joined.
  161. *
  162. * @param {JitsiConference} conference - The JitsiConference instance which was
  163. * joined by the local participant.
  164. * @returns {{
  165. * type: CONFERENCE_JOINED,
  166. * conference: JitsiConference
  167. * }}
  168. */
  169. export function conferenceJoined(conference: Object) {
  170. return {
  171. type: CONFERENCE_JOINED,
  172. conference
  173. };
  174. }
  175. /**
  176. * Signals that a specific conference has been left.
  177. *
  178. * @param {JitsiConference} conference - The JitsiConference instance which was
  179. * left by the local participant.
  180. * @returns {{
  181. * type: CONFERENCE_LEFT,
  182. * conference: JitsiConference
  183. * }}
  184. */
  185. export function conferenceLeft(conference: Object) {
  186. return {
  187. type: CONFERENCE_LEFT,
  188. conference
  189. };
  190. }
  191. /**
  192. * Attaches any pre-existing local media to the conference, before
  193. * the conference will be joined. Then signals the intention of the application
  194. * to have the local participant join a specific conference.
  195. *
  196. * @param {JitsiConference} conference - The JitsiConference instance the
  197. * local participant will (try to) join.
  198. * @returns {Function}
  199. */
  200. function _conferenceWillJoin(conference: Object) {
  201. return (dispatch: Dispatch<*>, getState: Function) => {
  202. const localTracks
  203. = getState()['features/base/tracks']
  204. .filter(t => t.local)
  205. .map(t => t.jitsiTrack);
  206. if (localTracks.length) {
  207. _addLocalTracksToConference(conference, localTracks);
  208. }
  209. dispatch({
  210. type: CONFERENCE_WILL_JOIN,
  211. conference
  212. });
  213. };
  214. }
  215. /**
  216. * Signals the intention of the application to have the local participant leave
  217. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  218. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  219. * lib-jitsi-meet and not the application.
  220. *
  221. * @param {JitsiConference} conference - The JitsiConference instance which will
  222. * be left by the local participant.
  223. * @returns {{
  224. * type: CONFERENCE_LEFT,
  225. * conference: JitsiConference
  226. * }}
  227. */
  228. export function conferenceWillLeave(conference: Object) {
  229. return {
  230. type: CONFERENCE_WILL_LEAVE,
  231. conference
  232. };
  233. }
  234. /**
  235. * Initializes a new conference.
  236. *
  237. * @returns {Function}
  238. */
  239. export function createConference() {
  240. return (dispatch: Function, getState: Function) => {
  241. const state = getState();
  242. const { connection, locationURL } = state['features/base/connection'];
  243. if (!connection) {
  244. throw new Error('Cannot create a conference without a connection!');
  245. }
  246. const { password, room } = state['features/base/conference'];
  247. if (!room) {
  248. throw new Error('Cannot join a conference without a room name!');
  249. }
  250. const conference
  251. = connection.initJitsiConference(
  252. // XXX Lib-jitsi-meet does not accept uppercase letters.
  253. room.toLowerCase(),
  254. state['features/base/config']);
  255. conference[JITSI_CONFERENCE_URL_KEY] = locationURL;
  256. dispatch(_conferenceWillJoin(conference));
  257. _addConferenceListeners(conference, dispatch);
  258. _setLocalParticipantData(conference, state);
  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. }