Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 9.4KB

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