You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  2. import {
  3. dominantSpeakerChanged,
  4. getLocalParticipant,
  5. participantJoined,
  6. participantLeft,
  7. participantRoleChanged,
  8. participantUpdated
  9. } from '../participants';
  10. import { trackAdded, trackRemoved } from '../tracks';
  11. import {
  12. CONFERENCE_FAILED,
  13. CONFERENCE_JOINED,
  14. CONFERENCE_LEFT,
  15. CONFERENCE_WILL_JOIN,
  16. CONFERENCE_WILL_LEAVE,
  17. LOCK_STATE_CHANGED,
  18. SET_PASSWORD,
  19. SET_ROOM
  20. } from './actionTypes';
  21. import {
  22. AVATAR_ID_COMMAND,
  23. AVATAR_URL_COMMAND,
  24. EMAIL_COMMAND
  25. } from './constants';
  26. import { _addLocalTracksToConference } from './functions';
  27. /**
  28. * Adds conference (event) listeners.
  29. *
  30. * @param {JitsiConference} conference - The JitsiConference instance.
  31. * @param {Dispatch} dispatch - The Redux dispatch function.
  32. * @private
  33. * @returns {void}
  34. */
  35. function _addConferenceListeners(conference, dispatch) {
  36. conference.on(
  37. JitsiConferenceEvents.CONFERENCE_FAILED,
  38. (...args) => dispatch(conferenceFailed(conference, ...args)));
  39. conference.on(
  40. JitsiConferenceEvents.CONFERENCE_JOINED,
  41. (...args) => dispatch(conferenceJoined(conference, ...args)));
  42. conference.on(
  43. JitsiConferenceEvents.CONFERENCE_LEFT,
  44. (...args) => dispatch(conferenceLeft(conference, ...args)));
  45. conference.on(
  46. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  47. (...args) => dispatch(dominantSpeakerChanged(...args)));
  48. conference.on(
  49. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  50. (...args) => dispatch(_lockStateChanged(conference, ...args)));
  51. conference.on(
  52. JitsiConferenceEvents.TRACK_ADDED,
  53. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  54. conference.on(
  55. JitsiConferenceEvents.TRACK_REMOVED,
  56. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  57. conference.on(
  58. JitsiConferenceEvents.USER_JOINED,
  59. (id, user) => dispatch(participantJoined({
  60. id,
  61. name: user.getDisplayName(),
  62. role: user.getRole()
  63. })));
  64. conference.on(
  65. JitsiConferenceEvents.USER_LEFT,
  66. (...args) => dispatch(participantLeft(...args)));
  67. conference.on(
  68. JitsiConferenceEvents.USER_ROLE_CHANGED,
  69. (...args) => dispatch(participantRoleChanged(...args)));
  70. conference.addCommandListener(
  71. AVATAR_ID_COMMAND,
  72. (data, id) => dispatch(participantUpdated({
  73. id,
  74. avatarID: data.value
  75. })));
  76. conference.addCommandListener(
  77. AVATAR_URL_COMMAND,
  78. (data, id) => dispatch(participantUpdated({
  79. id,
  80. avatarURL: data.value
  81. })));
  82. conference.addCommandListener(
  83. EMAIL_COMMAND,
  84. (data, id) => dispatch(participantUpdated({
  85. id,
  86. email: data.value
  87. })));
  88. }
  89. /**
  90. * Sets the data for the local participant to the conference.
  91. *
  92. * @param {JitsiConference} conference - The JitsiConference instance.
  93. * @param {Object} state - The Redux state.
  94. * @returns {void}
  95. */
  96. function _setLocalParticipantData(conference, state) {
  97. const localParticipant
  98. = getLocalParticipant(state['features/base/participants']);
  99. conference.removeCommand(AVATAR_ID_COMMAND);
  100. conference.sendCommand(AVATAR_ID_COMMAND, {
  101. value: localParticipant.avatarID
  102. });
  103. }
  104. /**
  105. * Signals that a specific conference has failed.
  106. *
  107. * @param {JitsiConference} conference - The JitsiConference that has failed.
  108. * @param {string} error - The error describing/detailing the cause of the
  109. * failure.
  110. * @returns {{
  111. * type: CONFERENCE_FAILED,
  112. * conference: JitsiConference,
  113. * error: string
  114. * }}
  115. * @public
  116. */
  117. export function conferenceFailed(conference, error) {
  118. return {
  119. type: CONFERENCE_FAILED,
  120. conference,
  121. error
  122. };
  123. }
  124. /**
  125. * Attach any pre-existing local media to the conference once the conference has
  126. * been joined.
  127. *
  128. * @param {JitsiConference} conference - The JitsiConference instance which was
  129. * joined by the local participant.
  130. * @returns {Function}
  131. */
  132. export function conferenceJoined(conference) {
  133. return (dispatch, getState) => {
  134. const localTracks
  135. = getState()['features/base/tracks']
  136. .filter(t => t.local)
  137. .map(t => t.jitsiTrack);
  138. if (localTracks.length) {
  139. _addLocalTracksToConference(conference, localTracks);
  140. }
  141. dispatch({
  142. type: CONFERENCE_JOINED,
  143. conference
  144. });
  145. };
  146. }
  147. /**
  148. * Signals that a specific conference has been left.
  149. *
  150. * @param {JitsiConference} conference - The JitsiConference instance which was
  151. * left by the local participant.
  152. * @returns {{
  153. * type: CONFERENCE_LEFT,
  154. * conference: JitsiConference
  155. * }}
  156. */
  157. export function conferenceLeft(conference) {
  158. return {
  159. type: CONFERENCE_LEFT,
  160. conference
  161. };
  162. }
  163. /**
  164. * Signals the intention of the application to have the local participant join a
  165. * conference with a specific room (name). Similar in fashion
  166. * to CONFERENCE_JOINED.
  167. *
  168. * @param {string} room - The room (name) which identifies the conference the
  169. * local participant will (try to) join.
  170. * @returns {{
  171. * type: CONFERENCE_WILL_JOIN,
  172. * room: string
  173. * }}
  174. */
  175. function _conferenceWillJoin(room) {
  176. return {
  177. type: CONFERENCE_WILL_JOIN,
  178. room
  179. };
  180. }
  181. /**
  182. * Signals the intention of the application to have the local participant leave
  183. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  184. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  185. * lib-jitsi-meet and not the application.
  186. *
  187. * @param {JitsiConference} conference - The JitsiConference instance which will
  188. * be left by the local participant.
  189. * @returns {{
  190. * type: CONFERENCE_LEFT,
  191. * conference: JitsiConference
  192. * }}
  193. */
  194. export function conferenceWillLeave(conference) {
  195. return {
  196. type: CONFERENCE_WILL_LEAVE,
  197. conference
  198. };
  199. }
  200. /**
  201. * Initializes a new conference.
  202. *
  203. * @returns {Function}
  204. */
  205. export function createConference() {
  206. return (dispatch, getState) => {
  207. const state = getState();
  208. const connection = state['features/base/connection'].connection;
  209. if (!connection) {
  210. throw new Error('Cannot create conference without connection');
  211. }
  212. const { password, room } = state['features/base/conference'];
  213. if (typeof room === 'undefined' || room === '') {
  214. throw new Error('Cannot join conference without room name');
  215. }
  216. dispatch(_conferenceWillJoin(room));
  217. // TODO Take options from config.
  218. const conference
  219. = connection.initJitsiConference(
  220. // XXX Lib-jitsi-meet does not accept uppercase letters.
  221. room.toLowerCase(),
  222. {
  223. openSctp: true
  224. // FIXME I tested H.264 from iPhone 6S during a morning
  225. // standup but, unfortunately, the other participants who
  226. // happened to be running the Web app saw only black.
  227. //
  228. // preferH264: true
  229. });
  230. _addConferenceListeners(conference, dispatch);
  231. _setLocalParticipantData(conference, state);
  232. conference.join(password);
  233. };
  234. }
  235. /**
  236. * Signals that the lock state of a specific JitsiConference changed.
  237. *
  238. * @param {JitsiConference} conference - The JitsiConference which had its lock
  239. * state changed.
  240. * @param {boolean} locked - If the specified conference became locked, true;
  241. * otherwise, false.
  242. * @returns {{
  243. * type: LOCK_STATE_CHANGED,
  244. * conference: JitsiConference,
  245. * locked: boolean
  246. * }}
  247. */
  248. function _lockStateChanged(conference, locked) {
  249. return {
  250. type: LOCK_STATE_CHANGED,
  251. conference,
  252. locked
  253. };
  254. }
  255. /**
  256. * Sets the password to join or lock a specific JitsiConference.
  257. *
  258. * @param {JitsiConference} conference - The JitsiConference which requires a
  259. * password to join or is to be locked with the specified password.
  260. * @param {Function} method - The JitsiConference method of password protection
  261. * such as join or lock.
  262. * @param {string} password - The password with which the specified conference
  263. * is to be joined or locked.
  264. * @returns {Function}
  265. */
  266. export function setPassword(conference, method, password) {
  267. return (dispatch, getState) => {
  268. switch (method) {
  269. case conference.join: {
  270. let state = getState()['features/base/conference'];
  271. // Make sure that the action will set a password for a conference
  272. // that the application wants joined.
  273. if (state.passwordRequired === conference) {
  274. dispatch({
  275. type: SET_PASSWORD,
  276. conference,
  277. method,
  278. password
  279. });
  280. // Join the conference with the newly-set password.
  281. // Make sure that the action did set the password.
  282. state = getState()['features/base/conference'];
  283. if (state.password === password
  284. && !state.passwordRequired
  285. // Make sure that the application still wants the
  286. // conference joined.
  287. && !state.conference) {
  288. method.call(conference, password);
  289. }
  290. }
  291. break;
  292. }
  293. case conference.lock: {
  294. const state = getState()['features/base/conference'];
  295. if (state.conference === conference) {
  296. return (
  297. method.call(conference, password)
  298. .then(() => dispatch({
  299. type: SET_PASSWORD,
  300. conference,
  301. method,
  302. password
  303. })));
  304. }
  305. return Promise.reject();
  306. }
  307. }
  308. };
  309. }
  310. /**
  311. * Sets (the name of) the room of the conference to be joined.
  312. *
  313. * @param {(string|undefined)} room - The name of the room of the conference to
  314. * be joined.
  315. * @returns {{
  316. * type: SET_ROOM,
  317. * room: string
  318. * }}
  319. */
  320. export function setRoom(room) {
  321. return {
  322. type: SET_ROOM,
  323. room
  324. };
  325. }