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.

LargeVideoManager.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* global $, APP */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import Avatar from "../avatar/Avatar";
  4. import {createDeferred} from '../../util/helpers';
  5. import UIEvents from "../../../service/UI/UIEvents";
  6. import UIUtil from "../util/UIUtil";
  7. import {VideoContainer, VIDEO_CONTAINER_TYPE} from "./VideoContainer";
  8. import AudioLevels from "../audio_levels/AudioLevels";
  9. /**
  10. * Manager for all Large containers.
  11. */
  12. export default class LargeVideoManager {
  13. constructor (emitter) {
  14. /**
  15. * The map of <tt>LargeContainer</tt>s where the key is the video
  16. * container type.
  17. * @type {Object.<string, LargeContainer>}
  18. */
  19. this.containers = {};
  20. this.eventEmitter = emitter;
  21. this.state = VIDEO_CONTAINER_TYPE;
  22. // FIXME: We are passing resizeContainer as parameter which is calling
  23. // Container.resize. Probably there's better way to implement this.
  24. this.videoContainer = new VideoContainer(
  25. () => this.resizeContainer(VIDEO_CONTAINER_TYPE), emitter);
  26. this.addContainer(VIDEO_CONTAINER_TYPE, this.videoContainer);
  27. // use the same video container to handle desktop tracks
  28. this.addContainer("desktop", this.videoContainer);
  29. this.width = 0;
  30. this.height = 0;
  31. this.$container = $('#largeVideoContainer');
  32. this.$container.css({
  33. display: 'inline-block'
  34. });
  35. this.$container.hover(
  36. e => this.onHoverIn(e),
  37. e => this.onHoverOut(e)
  38. );
  39. }
  40. onHoverIn (e) {
  41. if (!this.state) {
  42. return;
  43. }
  44. let container = this.getContainer(this.state);
  45. container.onHoverIn(e);
  46. }
  47. onHoverOut (e) {
  48. if (!this.state) {
  49. return;
  50. }
  51. let container = this.getContainer(this.state);
  52. container.onHoverOut(e);
  53. }
  54. /**
  55. * Called when the media connection has been interrupted.
  56. */
  57. onVideoInterrupted () {
  58. this.enableLocalConnectionProblemFilter(true);
  59. this._setLocalConnectionMessage("connection.RECONNECTING");
  60. // Show the message only if the video is currently being displayed
  61. this.showLocalConnectionMessage(this.state === VIDEO_CONTAINER_TYPE);
  62. }
  63. /**
  64. * Called when the media connection has been restored.
  65. */
  66. onVideoRestored () {
  67. this.enableLocalConnectionProblemFilter(false);
  68. this.showLocalConnectionMessage(false);
  69. }
  70. get id () {
  71. let container = this.getContainer(this.state);
  72. return container.id;
  73. }
  74. scheduleLargeVideoUpdate () {
  75. if (this.updateInProcess || !this.newStreamData) {
  76. return;
  77. }
  78. this.updateInProcess = true;
  79. // Include hide()/fadeOut only if we're switching between users
  80. const isUserSwitch = this.newStreamData.id != this.id;
  81. const container = this.getContainer(this.state);
  82. const preUpdate = isUserSwitch ? container.hide() : Promise.resolve();
  83. preUpdate.then(() => {
  84. const { id, stream, videoType, resolve } = this.newStreamData;
  85. this.newStreamData = null;
  86. logger.info("hover in %s", id);
  87. this.state = videoType;
  88. const container = this.getContainer(this.state);
  89. container.setStream(stream, videoType);
  90. // change the avatar url on large
  91. this.updateAvatar(Avatar.getAvatarUrl(id));
  92. // FIXME that does not really make sense, because the videoType
  93. // (camera or desktop) is a completely different thing than
  94. // the video container type (Etherpad, SharedVideo, VideoContainer).
  95. // ----------------------------------------------------------------
  96. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  97. // its stream whether exist and is muted to set isVideoMuted
  98. // in rest of the cases it is false
  99. let showAvatar
  100. = (videoType === VIDEO_CONTAINER_TYPE)
  101. && (!stream || stream.isMuted());
  102. // If the user's connection is disrupted then the avatar will be
  103. // displayed in case we have no video image cached. That is if
  104. // there was a user switch(image is lost on stream detach) or if
  105. // the video was not rendered, before the connection has failed.
  106. const isHavingConnectivityIssues
  107. = APP.conference.isParticipantConnectionActive(id) === false;
  108. if (videoType === VIDEO_CONTAINER_TYPE
  109. && isHavingConnectivityIssues
  110. && (isUserSwitch || !container.wasVideoRendered)) {
  111. showAvatar = true;
  112. }
  113. let promise;
  114. // do not show stream if video is muted
  115. // but we still should show watermark
  116. if (showAvatar) {
  117. this.showWatermark(true);
  118. // If the intention of this switch is to show the avatar
  119. // we need to make sure that the video is hidden
  120. promise = container.hide();
  121. } else {
  122. promise = container.show();
  123. }
  124. // show the avatar on large if needed
  125. container.showAvatar(showAvatar);
  126. // Clean up audio level after previous speaker.
  127. if (showAvatar) {
  128. this.updateLargeVideoAudioLevel(0);
  129. }
  130. // Make sure no notification about remote failure is shown as
  131. // its UI conflicts with the one for local connection interrupted.
  132. const isConnected = APP.conference.isConnectionInterrupted()
  133. || !isHavingConnectivityIssues;
  134. this.updateParticipantConnStatusIndication(
  135. id,
  136. isConnected,
  137. (isHavingConnectivityIssues)
  138. ? "connection.USER_CONNECTION_INTERRUPTED"
  139. : "connection.LOW_BANDWIDTH");
  140. // resolve updateLargeVideo promise after everything is done
  141. promise.then(resolve);
  142. return promise;
  143. }).then(() => {
  144. // after everything is done check again if there are any pending
  145. // new streams.
  146. this.updateInProcess = false;
  147. this.eventEmitter.emit(UIEvents.LARGE_VIDEO_ID_CHANGED, this.id);
  148. this.scheduleLargeVideoUpdate();
  149. });
  150. }
  151. /**
  152. * Shows/hides notification about participant's connectivity issues to be
  153. * shown on the large video area.
  154. *
  155. * @param {string} id the id of remote participant(MUC nickname)
  156. * @param {boolean} isConnected true if the connection is active or false
  157. * when the user is having connectivity issues.
  158. * @param {string} messageKey the i18n key of the message
  159. *
  160. * @private
  161. */
  162. updateParticipantConnStatusIndication (id, isConnected, messageKey) {
  163. // Apply grey filter on the large video
  164. this.videoContainer.showRemoteConnectionProblemIndicator(!isConnected);
  165. if (isConnected) {
  166. // Hide the message
  167. this.showRemoteConnectionMessage(false);
  168. } else {
  169. // Get user's display name
  170. let displayName
  171. = APP.conference.getParticipantDisplayName(id);
  172. this._setRemoteConnectionMessage(
  173. messageKey,
  174. { displayName: displayName });
  175. // Show it now only if the VideoContainer is on top
  176. this.showRemoteConnectionMessage(
  177. this.state === VIDEO_CONTAINER_TYPE);
  178. }
  179. }
  180. /**
  181. * Update large video.
  182. * Switches to large video even if previously other container was visible.
  183. * @param userID the userID of the participant associated with the stream
  184. * @param {JitsiTrack?} stream new stream
  185. * @param {string?} videoType new video type
  186. * @returns {Promise}
  187. */
  188. updateLargeVideo (userID, stream, videoType) {
  189. if (this.newStreamData) {
  190. this.newStreamData.reject();
  191. }
  192. this.newStreamData = createDeferred();
  193. this.newStreamData.id = userID;
  194. this.newStreamData.stream = stream;
  195. this.newStreamData.videoType = videoType;
  196. this.scheduleLargeVideoUpdate();
  197. return this.newStreamData.promise;
  198. }
  199. /**
  200. * Update container size.
  201. */
  202. updateContainerSize () {
  203. this.width = UIUtil.getAvailableVideoWidth();
  204. this.height = window.innerHeight;
  205. }
  206. /**
  207. * Resize Large container of specified type.
  208. * @param {string} type type of container which should be resized.
  209. * @param {boolean} [animate=false] if resize process should be animated.
  210. */
  211. resizeContainer (type, animate = false) {
  212. let container = this.getContainer(type);
  213. container.resize(this.width, this.height, animate);
  214. }
  215. /**
  216. * Resize all Large containers.
  217. * @param {boolean} animate if resize process should be animated.
  218. */
  219. resize (animate) {
  220. // resize all containers
  221. Object.keys(this.containers)
  222. .forEach(type => this.resizeContainer(type, animate));
  223. this.$container.animate({
  224. width: this.width,
  225. height: this.height
  226. }, {
  227. queue: false,
  228. duration: animate ? 500 : 0
  229. });
  230. }
  231. /**
  232. * Enables/disables the filter indicating a video problem to the user caused
  233. * by the problems with local media connection.
  234. *
  235. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  236. */
  237. enableLocalConnectionProblemFilter (enable) {
  238. this.videoContainer.enableLocalConnectionProblemFilter(enable);
  239. }
  240. /**
  241. * Updates the src of the dominant speaker avatar
  242. */
  243. updateAvatar (avatarUrl) {
  244. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  245. }
  246. /**
  247. * Updates the audio level indicator of the large video.
  248. *
  249. * @param lvl the new audio level to set
  250. */
  251. updateLargeVideoAudioLevel (lvl) {
  252. AudioLevels.updateLargeVideoAudioLevel("dominantSpeaker", lvl);
  253. }
  254. /**
  255. * Show or hide watermark.
  256. * @param {boolean} show
  257. */
  258. showWatermark (show) {
  259. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  260. }
  261. /**
  262. * Shows/hides the message indicating problems with local media connection.
  263. * @param {boolean|null} show(optional) tells whether the message is to be
  264. * displayed or not. If missing the condition will be based on the value
  265. * obtained from {@link APP.conference.isConnectionInterrupted}.
  266. */
  267. showLocalConnectionMessage (show) {
  268. if (typeof show !== 'boolean') {
  269. show = APP.conference.isConnectionInterrupted();
  270. }
  271. let id = 'localConnectionMessage';
  272. UIUtil.setVisible(id, show);
  273. if (show) {
  274. // Avatar message conflicts with 'videoConnectionMessage',
  275. // so it must be hidden
  276. this.showRemoteConnectionMessage(false);
  277. }
  278. }
  279. /**
  280. * Shows hides the "avatar" message which is to be displayed either in
  281. * the middle of the screen or below the avatar image.
  282. *
  283. * @param {null|boolean} show (optional) <tt>true</tt> to show the avatar
  284. * message or <tt>false</tt> to hide it. If not provided then the connection
  285. * status of the user currently on the large video will be obtained form
  286. * "APP.conference" and the message will be displayed if the user's
  287. * connection is interrupted.
  288. */
  289. showRemoteConnectionMessage (show) {
  290. if (typeof show !== 'boolean') {
  291. show = !APP.conference.isParticipantConnectionActive(this.id);
  292. }
  293. if (show) {
  294. $('#remoteConnectionMessage').css({display: "block"});
  295. // 'videoConnectionMessage' message conflicts with 'avatarMessage',
  296. // so it must be hidden
  297. this.showLocalConnectionMessage(false);
  298. } else {
  299. $('#remoteConnectionMessage').hide();
  300. }
  301. }
  302. /**
  303. * Updates the text which describes that the remote user is having
  304. * connectivity issues.
  305. *
  306. * @param {string} msgKey the translation key which will be used to get
  307. * the message text.
  308. * @param {object} msgOptions translation options object.
  309. *
  310. * @private
  311. */
  312. _setRemoteConnectionMessage (msgKey, msgOptions) {
  313. if (msgKey) {
  314. $('#remoteConnectionMessage')
  315. .attr("data-i18n", msgKey)
  316. .attr("data-i18n-options", JSON.stringify(msgOptions));
  317. APP.translation.translateElement(
  318. $('#remoteConnectionMessage'), msgOptions);
  319. }
  320. this.videoContainer.positionRemoteConnectionMessage();
  321. }
  322. /**
  323. * Updated the text which is to be shown on the top of large video, when
  324. * local media connection is interrupted.
  325. *
  326. * @param {string} msgKey the translation key which will be used to get
  327. * the message text to be displayed on the large video.
  328. *
  329. * @private
  330. */
  331. _setLocalConnectionMessage (msgKey) {
  332. $('#localConnectionMessage')
  333. .attr("data-i18n", msgKey);
  334. APP.translation.translateElement($('#localConnectionMessage'));
  335. }
  336. /**
  337. * Add container of specified type.
  338. * @param {string} type container type
  339. * @param {LargeContainer} container container to add.
  340. */
  341. addContainer (type, container) {
  342. if (this.containers[type]) {
  343. throw new Error(`container of type ${type} already exist`);
  344. }
  345. this.containers[type] = container;
  346. this.resizeContainer(type);
  347. }
  348. /**
  349. * Get Large container of specified type.
  350. * @param {string} type container type.
  351. * @returns {LargeContainer}
  352. */
  353. getContainer (type) {
  354. let container = this.containers[type];
  355. if (!container) {
  356. throw new Error(`container of type ${type} doesn't exist`);
  357. }
  358. return container;
  359. }
  360. /**
  361. * Remove Large container of specified type.
  362. * @param {string} type container type.
  363. */
  364. removeContainer (type) {
  365. if (!this.containers[type]) {
  366. throw new Error(`container of type ${type} doesn't exist`);
  367. }
  368. delete this.containers[type];
  369. }
  370. /**
  371. * Show Large container of specified type.
  372. * Does nothing if such container is already visible.
  373. * @param {string} type container type.
  374. * @returns {Promise}
  375. */
  376. showContainer (type) {
  377. if (this.state === type) {
  378. return Promise.resolve();
  379. }
  380. let oldContainer = this.containers[this.state];
  381. // FIXME when video is being replaced with other content we need to hide
  382. // companion icons/messages. It would be best if the container would
  383. // be taking care of it by itself, but that is a bigger refactoring
  384. if (this.state === VIDEO_CONTAINER_TYPE) {
  385. this.showWatermark(false);
  386. this.showLocalConnectionMessage(false);
  387. this.showRemoteConnectionMessage(false);
  388. }
  389. oldContainer.hide();
  390. this.state = type;
  391. let container = this.getContainer(type);
  392. return container.show().then(() => {
  393. if (type === VIDEO_CONTAINER_TYPE) {
  394. // FIXME when video appears on top of other content we need to
  395. // show companion icons/messages. It would be best if
  396. // the container would be taking care of it by itself, but that
  397. // is a bigger refactoring
  398. this.showWatermark(true);
  399. // "avatar" and "video connection" can not be displayed both
  400. // at the same time, but the latter is of higher priority and it
  401. // will hide the avatar one if will be displayed.
  402. this.showRemoteConnectionMessage(/* fetch the current state */);
  403. this.showLocalConnectionMessage(/* fetch the current state */);
  404. }
  405. });
  406. }
  407. /**
  408. * Changes the flipX state of the local video.
  409. * @param val {boolean} true if flipped.
  410. */
  411. onLocalFlipXChange(val) {
  412. this.videoContainer.setLocalFlipX(val);
  413. }
  414. }