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

actions.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 { avatarID } = getLocalParticipant(state);
  112. conference.removeCommand(AVATAR_ID_COMMAND);
  113. conference.sendCommand(AVATAR_ID_COMMAND, {
  114. value: avatarID
  115. });
  116. }
  117. /**
  118. * Signals that a specific conference has failed.
  119. *
  120. * @param {JitsiConference} conference - The JitsiConference that has failed.
  121. * @param {string} error - The error describing/detailing the cause of the
  122. * failure.
  123. * @returns {{
  124. * type: CONFERENCE_FAILED,
  125. * conference: JitsiConference,
  126. * error: string
  127. * }}
  128. * @public
  129. */
  130. export function conferenceFailed(conference, error) {
  131. return {
  132. type: CONFERENCE_FAILED,
  133. conference,
  134. error
  135. };
  136. }
  137. /**
  138. * Attach any pre-existing local media to the conference once the conference has
  139. * been joined.
  140. *
  141. * @param {JitsiConference} conference - The JitsiConference instance which was
  142. * joined by the local participant.
  143. * @returns {Function}
  144. */
  145. export function conferenceJoined(conference) {
  146. return (dispatch, getState) => {
  147. const localTracks
  148. = getState()['features/base/tracks']
  149. .filter(t => t.local)
  150. .map(t => t.jitsiTrack);
  151. if (localTracks.length) {
  152. _addLocalTracksToConference(conference, localTracks);
  153. }
  154. dispatch({
  155. type: CONFERENCE_JOINED,
  156. conference
  157. });
  158. };
  159. }
  160. /**
  161. * Signals that a specific conference has been left.
  162. *
  163. * @param {JitsiConference} conference - The JitsiConference instance which was
  164. * left by the local participant.
  165. * @returns {{
  166. * type: CONFERENCE_LEFT,
  167. * conference: JitsiConference
  168. * }}
  169. */
  170. export function conferenceLeft(conference) {
  171. return {
  172. type: CONFERENCE_LEFT,
  173. conference
  174. };
  175. }
  176. /**
  177. * Signals the intention of the application to have the local participant join a
  178. * conference with a specific room (name). Similar in fashion
  179. * to CONFERENCE_JOINED.
  180. *
  181. * @param {string} room - The room (name) which identifies the conference the
  182. * local participant will (try to) join.
  183. * @returns {{
  184. * type: CONFERENCE_WILL_JOIN,
  185. * room: string
  186. * }}
  187. */
  188. function _conferenceWillJoin(room) {
  189. return {
  190. type: CONFERENCE_WILL_JOIN,
  191. room
  192. };
  193. }
  194. /**
  195. * Signals the intention of the application to have the local participant leave
  196. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  197. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  198. * lib-jitsi-meet and not the application.
  199. *
  200. * @param {JitsiConference} conference - The JitsiConference instance which will
  201. * be left by the local participant.
  202. * @returns {{
  203. * type: CONFERENCE_LEFT,
  204. * conference: JitsiConference
  205. * }}
  206. */
  207. export function conferenceWillLeave(conference) {
  208. return {
  209. type: CONFERENCE_WILL_LEAVE,
  210. conference
  211. };
  212. }
  213. /**
  214. * Initializes a new conference.
  215. *
  216. * @returns {Function}
  217. */
  218. export function createConference() {
  219. return (dispatch, getState) => {
  220. const state = getState();
  221. const connection = state['features/base/connection'].connection;
  222. if (!connection) {
  223. throw new Error('Cannot create conference without connection');
  224. }
  225. const { password, room } = state['features/base/conference'];
  226. if (typeof room === 'undefined' || room === '') {
  227. throw new Error('Cannot join conference without room name');
  228. }
  229. dispatch(_conferenceWillJoin(room));
  230. const conference
  231. = connection.initJitsiConference(
  232. // XXX Lib-jitsi-meet does not accept uppercase letters.
  233. room.toLowerCase(),
  234. state['features/base/config']);
  235. _addConferenceListeners(conference, dispatch);
  236. _setLocalParticipantData(conference, state);
  237. conference.join(password);
  238. };
  239. }
  240. /**
  241. * Signals that the lock state of a specific JitsiConference changed.
  242. *
  243. * @param {JitsiConference} conference - The JitsiConference which had its lock
  244. * state changed.
  245. * @param {boolean} locked - If the specified conference became locked, true;
  246. * otherwise, false.
  247. * @returns {{
  248. * type: LOCK_STATE_CHANGED,
  249. * conference: JitsiConference,
  250. * locked: boolean
  251. * }}
  252. */
  253. export function lockStateChanged(conference, locked) {
  254. return {
  255. type: LOCK_STATE_CHANGED,
  256. conference,
  257. locked
  258. };
  259. }
  260. /**
  261. * Sets the audio-only flag for the current JitsiConference.
  262. *
  263. * @param {boolean} audioOnly - True if the conference should be audio only;
  264. * false, otherwise.
  265. * @private
  266. * @returns {{
  267. * type: SET_AUDIO_ONLY,
  268. * audioOnly: boolean
  269. * }}
  270. */
  271. function _setAudioOnly(audioOnly) {
  272. return {
  273. type: SET_AUDIO_ONLY,
  274. audioOnly
  275. };
  276. }
  277. /**
  278. * Signals that the app should mute video because it's now in audio-only mode,
  279. * or unmute it because it no longer is. If video was already muted, nothing
  280. * will happen; otherwise, it will be muted. When audio-only mode is disabled,
  281. * the previous state will be restored.
  282. *
  283. * @param {boolean} muted - True if video should be muted; false, otherwise.
  284. * @protected
  285. * @returns {Function}
  286. */
  287. export function _setAudioOnlyVideoMuted(muted: boolean) {
  288. return (dispatch, getState) => {
  289. if (muted) {
  290. const { video } = getState()['features/base/media'];
  291. if (video.muted) {
  292. // Video is already muted, do nothing.
  293. return;
  294. }
  295. } else {
  296. const { audioOnlyVideoMuted }
  297. = getState()['features/base/conference'];
  298. if (!audioOnlyVideoMuted) {
  299. // We didn't mute video, do nothing.
  300. return;
  301. }
  302. }
  303. // Remember that local video was muted due to the audio-only mode
  304. // vs user's choice.
  305. dispatch({
  306. type: _SET_AUDIO_ONLY_VIDEO_MUTED,
  307. muted
  308. });
  309. dispatch(setVideoMuted(muted));
  310. };
  311. }
  312. /**
  313. * Action to set whether or not the currently displayed large video is in
  314. * high-definition.
  315. *
  316. * @param {boolean} isLargeVideoHD - True if the large video is high-definition.
  317. * @returns {{
  318. * type: SET_LARGE_VIDEO_HD_STATUS,
  319. * isLargeVideoHD: boolean
  320. * }}
  321. */
  322. export function setLargeVideoHDStatus(isLargeVideoHD) {
  323. return {
  324. type: SET_LARGE_VIDEO_HD_STATUS,
  325. isLargeVideoHD
  326. };
  327. }
  328. /**
  329. * Sets the video channel's last N (value) of the current conference. A value of
  330. * undefined shall be used to reset it to the default value.
  331. *
  332. * @param {(number|undefined)} lastN - The last N value to be set.
  333. * @returns {Function}
  334. */
  335. export function setLastN(lastN: ?number) {
  336. return (dispatch: Dispatch<*>, getState: Function) => {
  337. if (typeof lastN === 'undefined') {
  338. const config = getState()['features/base/config'];
  339. /* eslint-disable no-param-reassign */
  340. lastN = config.channelLastN;
  341. if (typeof lastN === 'undefined') {
  342. lastN = -1;
  343. }
  344. /* eslint-enable no-param-reassign */
  345. }
  346. dispatch({
  347. type: SET_LASTN,
  348. lastN
  349. });
  350. };
  351. }
  352. /**
  353. * Sets the password to join or lock a specific JitsiConference.
  354. *
  355. * @param {JitsiConference} conference - The JitsiConference which requires a
  356. * password to join or is to be locked with the specified password.
  357. * @param {Function} method - The JitsiConference method of password protection
  358. * such as join or lock.
  359. * @param {string} password - The password with which the specified conference
  360. * is to be joined or locked.
  361. * @returns {Function}
  362. */
  363. export function setPassword(conference, method, password) {
  364. return (dispatch, getState) => {
  365. switch (method) {
  366. case conference.join: {
  367. let state = getState()['features/base/conference'];
  368. // Make sure that the action will set a password for a conference
  369. // that the application wants joined.
  370. if (state.passwordRequired === conference) {
  371. dispatch({
  372. type: SET_PASSWORD,
  373. conference,
  374. method,
  375. password
  376. });
  377. // Join the conference with the newly-set password.
  378. // Make sure that the action did set the password.
  379. state = getState()['features/base/conference'];
  380. if (state.password === password
  381. && !state.passwordRequired
  382. // Make sure that the application still wants the
  383. // conference joined.
  384. && !state.conference) {
  385. method.call(conference, password);
  386. }
  387. }
  388. break;
  389. }
  390. case conference.lock: {
  391. const state = getState()['features/base/conference'];
  392. if (state.conference === conference) {
  393. return (
  394. method.call(conference, password)
  395. .then(() => dispatch({
  396. type: SET_PASSWORD,
  397. conference,
  398. method,
  399. password
  400. }))
  401. .catch(error => dispatch({
  402. type: SET_PASSWORD_FAILED,
  403. error
  404. }))
  405. );
  406. }
  407. return Promise.reject();
  408. }
  409. }
  410. };
  411. }
  412. /**
  413. * Sets (the name of) the room of the conference to be joined.
  414. *
  415. * @param {(string|undefined)} room - The name of the room of the conference to
  416. * be joined.
  417. * @returns {{
  418. * type: SET_ROOM,
  419. * room: string
  420. * }}
  421. */
  422. export function setRoom(room) {
  423. return {
  424. type: SET_ROOM,
  425. room
  426. };
  427. }
  428. /**
  429. * Toggles the audio-only flag for the current JitsiConference.
  430. *
  431. * @returns {Function}
  432. */
  433. export function toggleAudioOnly() {
  434. return (dispatch: Dispatch<*>, getState: Function) => {
  435. const { audioOnly } = getState()['features/base/conference'];
  436. return dispatch(_setAudioOnly(!audioOnly));
  437. };
  438. }