您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LargeVideoManager.js 11KB

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