Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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