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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* global $, APP, interfaceConfig */
  2. /* jshint -W101 */
  3. import Avatar from "../avatar/Avatar";
  4. import {createDeferred} from '../../util/helpers';
  5. import UIUtil from "../util/UIUtil";
  6. import {VideoContainer, VIDEO_CONTAINER_TYPE} from "./VideoContainer";
  7. import LargeContainer from "./LargeContainer";
  8. /**
  9. * Manager for all Large containers.
  10. */
  11. export default class LargeVideoManager {
  12. constructor (emitter) {
  13. /**
  14. * The map of <tt>LargeContainer</tt>s where the key is the video
  15. * container type.
  16. * @type {Object.<string, LargeContainer>}
  17. */
  18. this.containers = {};
  19. this.state = VIDEO_CONTAINER_TYPE;
  20. this.videoContainer = new VideoContainer(
  21. () => this.resizeContainer(VIDEO_CONTAINER_TYPE), emitter);
  22. this.addContainer(VIDEO_CONTAINER_TYPE, this.videoContainer);
  23. // use the same video container to handle and desktop tracks
  24. this.addContainer("desktop", this.videoContainer);
  25. this.width = 0;
  26. this.height = 0;
  27. this.$container = $('#largeVideoContainer');
  28. this.$container.css({
  29. display: 'inline-block'
  30. });
  31. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  32. let leftWatermarkDiv
  33. = this.$container.find("div.watermark.leftwatermark");
  34. leftWatermarkDiv.css({display: 'block'});
  35. UIUtil.setLinkHref(
  36. leftWatermarkDiv.parent(),
  37. interfaceConfig.JITSI_WATERMARK_LINK);
  38. }
  39. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  40. let rightWatermarkDiv
  41. = this.$container.find("div.watermark.rightwatermark");
  42. rightWatermarkDiv.css({
  43. display: 'block',
  44. backgroundImage: 'url(images/rightwatermark.png)'
  45. });
  46. UIUtil.setLinkHref(
  47. rightWatermarkDiv.parent(),
  48. interfaceConfig.BRAND_WATERMARK_LINK);
  49. }
  50. if (interfaceConfig.SHOW_POWERED_BY) {
  51. this.$container.children("a.poweredby").css({display: 'block'});
  52. }
  53. this.$container.hover(
  54. e => this.onHoverIn(e),
  55. e => this.onHoverOut(e)
  56. );
  57. }
  58. onHoverIn (e) {
  59. if (!this.state) {
  60. return;
  61. }
  62. let container = this.getContainer(this.state);
  63. container.onHoverIn(e);
  64. }
  65. onHoverOut (e) {
  66. if (!this.state) {
  67. return;
  68. }
  69. let container = this.getContainer(this.state);
  70. container.onHoverOut(e);
  71. }
  72. /**
  73. * Called when the media connection has been interrupted.
  74. */
  75. onVideoInterrupted () {
  76. this.enableLocalConnectionProblemFilter(true);
  77. this._setVideoConnectionMessage("connection.RECONNECTING")
  78. // Show the message only if the video is currently being displayed
  79. this.showVideoConnectionMessage(this.state === VIDEO_CONTAINER_TYPE);
  80. }
  81. /**
  82. * Called when the media connection has been restored.
  83. */
  84. onVideoRestored () {
  85. this.enableLocalConnectionProblemFilter(false);
  86. this.showVideoConnectionMessage(false);
  87. }
  88. get id () {
  89. let container = this.getContainer(this.state);
  90. return container.id;
  91. }
  92. scheduleLargeVideoUpdate () {
  93. if (this.updateInProcess || !this.newStreamData) {
  94. return;
  95. }
  96. this.updateInProcess = true;
  97. let container = this.getContainer(this.state);
  98. // Include hide()/fadeOut only if we're switching between users
  99. let preUpdate;
  100. if (this.newStreamData.id != this.id) {
  101. preUpdate = container.hide();
  102. } else {
  103. preUpdate = Promise.resolve();
  104. }
  105. preUpdate.then(() => {
  106. let {id, stream, videoType, resolve} = this.newStreamData;
  107. this.newStreamData = null;
  108. console.info("hover in %s", id);
  109. this.state = videoType;
  110. let container = this.getContainer(this.state);
  111. container.setStream(stream, videoType);
  112. // change the avatar url on large
  113. this.updateAvatar(Avatar.getAvatarUrl(id));
  114. // FIXME that does not really make sense, because the videoType
  115. // (camera or desktop) is a completely different thing than
  116. // the video container type (Etherpad, SharedVideo, VideoContainer).
  117. // ----------------------------------------------------------------
  118. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  119. // its stream whether exist and is muted to set isVideoMuted
  120. // in rest of the cases it is false
  121. let isVideoMuted = false;
  122. if (videoType == VIDEO_CONTAINER_TYPE)
  123. isVideoMuted = stream ? stream.isMuted() : true;
  124. // show the avatar on large if needed
  125. container.showAvatar(isVideoMuted);
  126. let promise;
  127. // do not show stream if video is muted
  128. // but we still should show watermark
  129. if (isVideoMuted) {
  130. this.showWatermark(true);
  131. // If the avatar is to be displayed the video should be hidden
  132. promise = container.hide();
  133. } else {
  134. promise = container.show();
  135. }
  136. // resolve updateLargeVideo promise after everything is done
  137. promise.then(resolve);
  138. return promise;
  139. }).then(() => {
  140. // after everything is done check again if there are any pending
  141. // new streams.
  142. this.updateInProcess = false;
  143. this.scheduleLargeVideoUpdate();
  144. });
  145. }
  146. /**
  147. * Update large video.
  148. * Switches to large video even if previously other container was visible.
  149. * @param userID the userID of the participant associated with the stream
  150. * @param {JitsiTrack?} stream new stream
  151. * @param {string?} videoType new video type
  152. * @returns {Promise}
  153. */
  154. updateLargeVideo (userID, stream, videoType) {
  155. if (this.newStreamData) {
  156. this.newStreamData.reject();
  157. }
  158. this.newStreamData = createDeferred();
  159. this.newStreamData.id = userID;
  160. this.newStreamData.stream = stream;
  161. this.newStreamData.videoType = videoType;
  162. this.scheduleLargeVideoUpdate();
  163. return this.newStreamData.promise;
  164. }
  165. /**
  166. * Update container size.
  167. */
  168. updateContainerSize () {
  169. this.width = UIUtil.getAvailableVideoWidth();
  170. this.height = window.innerHeight;
  171. }
  172. /**
  173. * Resize Large container of specified type.
  174. * @param {string} type type of container which should be resized.
  175. * @param {boolean} [animate=false] if resize process should be animated.
  176. */
  177. resizeContainer (type, animate = false) {
  178. let container = this.getContainer(type);
  179. container.resize(this.width, this.height, animate);
  180. }
  181. /**
  182. * Resize all Large containers.
  183. * @param {boolean} animate if resize process should be animated.
  184. */
  185. resize (animate) {
  186. // resize all containers
  187. Object.keys(this.containers)
  188. .forEach(type => this.resizeContainer(type, animate));
  189. this.$container.animate({
  190. width: this.width,
  191. height: this.height
  192. }, {
  193. queue: false,
  194. duration: animate ? 500 : 0
  195. });
  196. }
  197. /**
  198. * Enables/disables the filter indicating a video problem to the user caused
  199. * by the problems with local media connection.
  200. *
  201. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  202. */
  203. enableLocalConnectionProblemFilter (enable) {
  204. this.videoContainer.enableLocalConnectionProblemFilter(enable);
  205. }
  206. /**
  207. * Updates the src of the dominant speaker avatar
  208. */
  209. updateAvatar (avatarUrl) {
  210. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  211. }
  212. /**
  213. * Show or hide watermark.
  214. * @param {boolean} show
  215. */
  216. showWatermark (show) {
  217. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  218. }
  219. /**
  220. * Shows/hides the "video connection message".
  221. * @param {boolean|null} show(optional) tells whether the message is to be
  222. * displayed or not. If missing the condition will be based on the value
  223. * obtained from {@link APP.conference.isConnectionInterrupted}.
  224. */
  225. showVideoConnectionMessage (show) {
  226. if (typeof show !== 'boolean') {
  227. show = APP.conference.isConnectionInterrupted();
  228. }
  229. if (show) {
  230. $('#videoConnectionMessage').css({display: "block"});
  231. } else {
  232. $('#videoConnectionMessage').css({display: "none"});
  233. }
  234. }
  235. /**
  236. * Updated the text which is to be shown on the top of large video.
  237. *
  238. * @param {string} msgKey the translation key which will be used to get
  239. * the message text to be displayed on the large video.
  240. * @param {object} msgOptions translation options object
  241. *
  242. * @private
  243. */
  244. _setVideoConnectionMessage (msgKey, msgOptions) {
  245. $('#videoConnectionMessage')
  246. .attr("data-i18n", msgKey)
  247. .text(APP.translation.translateString(msgKey, msgOptions));
  248. }
  249. /**
  250. * Add container of specified type.
  251. * @param {string} type container type
  252. * @param {LargeContainer} container container to add.
  253. */
  254. addContainer (type, container) {
  255. if (this.containers[type]) {
  256. throw new Error(`container of type ${type} already exist`);
  257. }
  258. this.containers[type] = container;
  259. this.resizeContainer(type);
  260. }
  261. /**
  262. * Get Large container of specified type.
  263. * @param {string} type container type.
  264. * @returns {LargeContainer}
  265. */
  266. getContainer (type) {
  267. let container = this.containers[type];
  268. if (!container) {
  269. throw new Error(`container of type ${type} doesn't exist`);
  270. }
  271. return container;
  272. }
  273. /**
  274. * Remove Large container of specified type.
  275. * @param {string} type container type.
  276. */
  277. removeContainer (type) {
  278. if (!this.containers[type]) {
  279. throw new Error(`container of type ${type} doesn't exist`);
  280. }
  281. delete this.containers[type];
  282. }
  283. /**
  284. * Show Large container of specified type.
  285. * Does nothing if such container is already visible.
  286. * @param {string} type container type.
  287. * @returns {Promise}
  288. */
  289. showContainer (type) {
  290. if (this.state === type) {
  291. return Promise.resolve();
  292. }
  293. let oldContainer = this.containers[this.state];
  294. // FIXME when video is being replaced with other content we need to hide
  295. // companion icons/messages. It would be best if the container would
  296. // be taking care of it by itself, but that is a bigger refactoring
  297. if (this.state === VIDEO_CONTAINER_TYPE) {
  298. this.showWatermark(false);
  299. this.showVideoConnectionMessage(false);
  300. }
  301. oldContainer.hide();
  302. this.state = type;
  303. let container = this.getContainer(type);
  304. return container.show().then(() => {
  305. if (type === VIDEO_CONTAINER_TYPE) {
  306. // FIXME when video appears on top of other content we need to
  307. // show companion icons/messages. It would be best if
  308. // the container would be taking care of it by itself, but that
  309. // is a bigger refactoring
  310. this.showWatermark(true);
  311. this.showVideoConnectionMessage(/* fetch the current state */);
  312. }
  313. });
  314. }
  315. /**
  316. * Changes the flipX state of the local video.
  317. * @param val {boolean} true if flipped.
  318. */
  319. onLocalFlipXChange(val) {
  320. this.videoContainer.setLocalFlipX(val);
  321. }
  322. }