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

LargeVideoManager.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.enableVideoProblemFilter(true);
  77. let reconnectingKey = "connection.RECONNECTING";
  78. $('#videoConnectionMessage')
  79. .attr("data-i18n", reconnectingKey)
  80. .text(APP.translation.translateString(reconnectingKey));
  81. // Show the message only if the video is currently being displayed
  82. this.showVideoConnectionMessage(this.state === VIDEO_CONTAINER_TYPE);
  83. }
  84. /**
  85. * Called when the media connection has been restored.
  86. */
  87. onVideoRestored () {
  88. this.enableVideoProblemFilter(false);
  89. this.showVideoConnectionMessage(false);
  90. }
  91. get id () {
  92. let container = this.getContainer(this.state);
  93. return container.id;
  94. }
  95. scheduleLargeVideoUpdate () {
  96. if (this.updateInProcess || !this.newStreamData) {
  97. return;
  98. }
  99. this.updateInProcess = true;
  100. let container = this.getContainer(this.state);
  101. // Include hide()/fadeOut only if we're switching between users
  102. let preUpdate;
  103. if (this.newStreamData.id != this.id) {
  104. preUpdate = container.hide();
  105. } else {
  106. preUpdate = Promise.resolve();
  107. }
  108. preUpdate.then(() => {
  109. let {id, stream, videoType, resolve} = this.newStreamData;
  110. this.newStreamData = null;
  111. console.info("hover in %s", id);
  112. this.state = videoType;
  113. let container = this.getContainer(this.state);
  114. container.setStream(stream, videoType);
  115. // change the avatar url on large
  116. this.updateAvatar(Avatar.getAvatarUrl(id));
  117. // FIXME that does not really make sense, because the videoType
  118. // (camera or desktop) is a completely different thing than
  119. // the video container type (Etherpad, SharedVideo, VideoContainer).
  120. // ----------------------------------------------------------------
  121. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  122. // its stream whether exist and is muted to set isVideoMuted
  123. // in rest of the cases it is false
  124. let isVideoMuted = false;
  125. if (videoType == VIDEO_CONTAINER_TYPE)
  126. isVideoMuted = stream ? stream.isMuted() : true;
  127. // show the avatar on large if needed
  128. container.showAvatar(isVideoMuted);
  129. let promise;
  130. // do not show stream if video is muted
  131. // but we still should show watermark
  132. if (isVideoMuted) {
  133. this.showWatermark(true);
  134. // If the avatar is to be displayed the video should be hidden
  135. promise = container.hide();
  136. } else {
  137. promise = container.show();
  138. }
  139. // resolve updateLargeVideo promise after everything is done
  140. promise.then(resolve);
  141. return promise;
  142. }).then(() => {
  143. // after everything is done check again if there are any pending
  144. // new streams.
  145. this.updateInProcess = false;
  146. this.scheduleLargeVideoUpdate();
  147. });
  148. }
  149. /**
  150. * Update large video.
  151. * Switches to large video even if previously other container was visible.
  152. * @param userID the userID of the participant associated with the stream
  153. * @param {JitsiTrack?} stream new stream
  154. * @param {string?} videoType new video type
  155. * @returns {Promise}
  156. */
  157. updateLargeVideo (userID, stream, videoType) {
  158. if (this.newStreamData) {
  159. this.newStreamData.reject();
  160. }
  161. this.newStreamData = createDeferred();
  162. this.newStreamData.id = userID;
  163. this.newStreamData.stream = stream;
  164. this.newStreamData.videoType = videoType;
  165. this.scheduleLargeVideoUpdate();
  166. return this.newStreamData.promise;
  167. }
  168. /**
  169. * Update container size.
  170. */
  171. updateContainerSize () {
  172. this.width = UIUtil.getAvailableVideoWidth();
  173. this.height = window.innerHeight;
  174. }
  175. /**
  176. * Resize Large container of specified type.
  177. * @param {string} type type of container which should be resized.
  178. * @param {boolean} [animate=false] if resize process should be animated.
  179. */
  180. resizeContainer (type, animate = false) {
  181. let container = this.getContainer(type);
  182. container.resize(this.width, this.height, animate);
  183. }
  184. /**
  185. * Resize all Large containers.
  186. * @param {boolean} animate if resize process should be animated.
  187. */
  188. resize (animate) {
  189. // resize all containers
  190. Object.keys(this.containers)
  191. .forEach(type => this.resizeContainer(type, animate));
  192. this.$container.animate({
  193. width: this.width,
  194. height: this.height
  195. }, {
  196. queue: false,
  197. duration: animate ? 500 : 0
  198. });
  199. }
  200. /**
  201. * Enables/disables the filter indicating a video problem to the user.
  202. *
  203. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  204. */
  205. enableVideoProblemFilter (enable) {
  206. this.videoContainer.enableVideoProblemFilter(enable);
  207. }
  208. /**
  209. * Updates the src of the dominant speaker avatar
  210. */
  211. updateAvatar (avatarUrl) {
  212. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  213. }
  214. /**
  215. * Show or hide watermark.
  216. * @param {boolean} show
  217. */
  218. showWatermark (show) {
  219. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  220. }
  221. /**
  222. * Shows/hides the "video connection message".
  223. * @param {boolean|null} show(optional) tells whether the message is to be
  224. * displayed or not. If missing the condition will be based on the value
  225. * obtained from {@link APP.conference.isConnectionInterrupted}.
  226. */
  227. showVideoConnectionMessage (show) {
  228. if (typeof show !== 'boolean') {
  229. show = APP.conference.isConnectionInterrupted();
  230. }
  231. if (show) {
  232. $('#videoConnectionMessage').css({display: "block"});
  233. } else {
  234. $('#videoConnectionMessage').css({display: "none"});
  235. }
  236. }
  237. /**
  238. * Add container of specified type.
  239. * @param {string} type container type
  240. * @param {LargeContainer} container container to add.
  241. */
  242. addContainer (type, container) {
  243. if (this.containers[type]) {
  244. throw new Error(`container of type ${type} already exist`);
  245. }
  246. this.containers[type] = container;
  247. this.resizeContainer(type);
  248. }
  249. /**
  250. * Get Large container of specified type.
  251. * @param {string} type container type.
  252. * @returns {LargeContainer}
  253. */
  254. getContainer (type) {
  255. let container = this.containers[type];
  256. if (!container) {
  257. throw new Error(`container of type ${type} doesn't exist`);
  258. }
  259. return container;
  260. }
  261. /**
  262. * Remove Large container of specified type.
  263. * @param {string} type container type.
  264. */
  265. removeContainer (type) {
  266. if (!this.containers[type]) {
  267. throw new Error(`container of type ${type} doesn't exist`);
  268. }
  269. delete this.containers[type];
  270. }
  271. /**
  272. * Show Large container of specified type.
  273. * Does nothing if such container is already visible.
  274. * @param {string} type container type.
  275. * @returns {Promise}
  276. */
  277. showContainer (type) {
  278. if (this.state === type) {
  279. return Promise.resolve();
  280. }
  281. let oldContainer = this.containers[this.state];
  282. // FIXME when video is being replaced with other content we need to hide
  283. // companion icons/messages. It would be best if the container would
  284. // be taking care of it by itself, but that is a bigger refactoring
  285. if (this.state === VIDEO_CONTAINER_TYPE) {
  286. this.showWatermark(false);
  287. this.showVideoConnectionMessage(false);
  288. }
  289. oldContainer.hide();
  290. this.state = type;
  291. let container = this.getContainer(type);
  292. return container.show().then(() => {
  293. if (type === VIDEO_CONTAINER_TYPE) {
  294. // FIXME when video appears on top of other content we need to
  295. // show companion icons/messages. It would be best if
  296. // the container would be taking care of it by itself, but that
  297. // is a bigger refactoring
  298. this.showWatermark(true);
  299. this.showVideoConnectionMessage(/* fetch the current state */);
  300. }
  301. });
  302. }
  303. /**
  304. * Changes the flipX state of the local video.
  305. * @param val {boolean} true if flipped.
  306. */
  307. onLocalFlipXChange(val) {
  308. this.videoContainer.setLocalFlipX(val);
  309. }
  310. }