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

actions.js 15KB

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