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

LargeVideoManager.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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._setLocalConnectionMessage("connection.RECONNECTING");
  78. // Show the message only if the video is currently being displayed
  79. this.showLocalConnectionMessage(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.showLocalConnectionMessage(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. let isUserSwitch = this.newStreamData.id != this.id;
  101. if (isUserSwitch) {
  102. preUpdate = container.hide();
  103. } else {
  104. preUpdate = Promise.resolve();
  105. }
  106. preUpdate.then(() => {
  107. let {id, stream, videoType, resolve} = this.newStreamData;
  108. this.newStreamData = null;
  109. console.info("hover in %s", id);
  110. this.state = videoType;
  111. let container = this.getContainer(this.state);
  112. container.setStream(stream, videoType);
  113. // change the avatar url on large
  114. this.updateAvatar(Avatar.getAvatarUrl(id));
  115. // FIXME that does not really make sense, because the videoType
  116. // (camera or desktop) is a completely different thing than
  117. // the video container type (Etherpad, SharedVideo, VideoContainer).
  118. // ----------------------------------------------------------------
  119. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  120. // its stream whether exist and is muted to set isVideoMuted
  121. // in rest of the cases it is false
  122. let showAvatar = false;
  123. if (videoType == VIDEO_CONTAINER_TYPE)
  124. showAvatar = stream ? stream.isMuted() : true;
  125. // If the user's connection is disrupted then the avatar will be
  126. // displayed in case we have no video image cached. That is if
  127. // there was a user switch(image is lost on stream detach) or if
  128. // the video was not rendered, before the connection has failed.
  129. let isHavingConnectivityIssues
  130. = APP.conference.isParticipantConnectionActive(id) === false;
  131. if (isHavingConnectivityIssues
  132. && (isUserSwitch | !container.wasVideoRendered)) {
  133. showAvatar = true;
  134. }
  135. let promise;
  136. // do not show stream if video is muted
  137. // but we still should show watermark
  138. if (showAvatar) {
  139. this.showWatermark(true);
  140. // If the intention of this switch is to show the avatar
  141. // we need to make sure that the video is hidden
  142. promise = container.hide();
  143. } else {
  144. promise = container.show();
  145. }
  146. // show the avatar on large if needed
  147. container.showAvatar(showAvatar);
  148. // Make sure no notification about remote failure is shown as
  149. // it's UI conflicts with the one for local connection interrupted.
  150. if (APP.conference.isConnectionInterrupted()) {
  151. this.updateParticipantConnStatusIndication(id, true);
  152. } else {
  153. this.updateParticipantConnStatusIndication(
  154. id, !isHavingConnectivityIssues);
  155. }
  156. // resolve updateLargeVideo promise after everything is done
  157. promise.then(resolve);
  158. return promise;
  159. }).then(() => {
  160. // after everything is done check again if there are any pending
  161. // new streams.
  162. this.updateInProcess = false;
  163. this.scheduleLargeVideoUpdate();
  164. });
  165. }
  166. /**
  167. * Shows/hides notification about participant's connectivity issues to be
  168. * shown on the large video area.
  169. *
  170. * @param {string} id the id of remote participant(MUC nickname)
  171. * @param {boolean} isConnected true if the connection is active or false
  172. * when the user is having connectivity issues.
  173. *
  174. * @private
  175. */
  176. updateParticipantConnStatusIndication (id, isConnected) {
  177. // Apply grey filter on the large video
  178. this.videoContainer.showRemoteConnectionProblemIndicator(!isConnected);
  179. if (isConnected) {
  180. // Hide the message
  181. this.showRemoteConnectionMessage(false);
  182. } else {
  183. // Get user's display name
  184. let displayName
  185. = APP.conference.getParticipantDisplayName(id);
  186. this._setRemoteConnectionMessage(
  187. "connection.USER_CONNECTION_INTERRUPTED",
  188. { displayName: displayName });
  189. // Show it now only if the VideoContainer is on top
  190. this.showRemoteConnectionMessage(
  191. this.state === VIDEO_CONTAINER_TYPE);
  192. }
  193. }
  194. /**
  195. * Update large video.
  196. * Switches to large video even if previously other container was visible.
  197. * @param userID the userID of the participant associated with the stream
  198. * @param {JitsiTrack?} stream new stream
  199. * @param {string?} videoType new video type
  200. * @returns {Promise}
  201. */
  202. updateLargeVideo (userID, stream, videoType) {
  203. if (this.newStreamData) {
  204. this.newStreamData.reject();
  205. }
  206. this.newStreamData = createDeferred();
  207. this.newStreamData.id = userID;
  208. this.newStreamData.stream = stream;
  209. this.newStreamData.videoType = videoType;
  210. this.scheduleLargeVideoUpdate();
  211. return this.newStreamData.promise;
  212. }
  213. /**
  214. * Update container size.
  215. */
  216. updateContainerSize () {
  217. this.width = UIUtil.getAvailableVideoWidth();
  218. this.height = window.innerHeight;
  219. }
  220. /**
  221. * Resize Large container of specified type.
  222. * @param {string} type type of container which should be resized.
  223. * @param {boolean} [animate=false] if resize process should be animated.
  224. */
  225. resizeContainer (type, animate = false) {
  226. let container = this.getContainer(type);
  227. container.resize(this.width, this.height, animate);
  228. }
  229. /**
  230. * Resize all Large containers.
  231. * @param {boolean} animate if resize process should be animated.
  232. */
  233. resize (animate) {
  234. // resize all containers
  235. Object.keys(this.containers)
  236. .forEach(type => this.resizeContainer(type, animate));
  237. this.$container.animate({
  238. width: this.width,
  239. height: this.height
  240. }, {
  241. queue: false,
  242. duration: animate ? 500 : 0
  243. });
  244. }
  245. /**
  246. * Enables/disables the filter indicating a video problem to the user caused
  247. * by the problems with local media connection.
  248. *
  249. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  250. */
  251. enableLocalConnectionProblemFilter (enable) {
  252. this.videoContainer.enableLocalConnectionProblemFilter(enable);
  253. }
  254. /**
  255. * Updates the src of the dominant speaker avatar
  256. */
  257. updateAvatar (avatarUrl) {
  258. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  259. }
  260. /**
  261. * Show or hide watermark.
  262. * @param {boolean} show
  263. */
  264. showWatermark (show) {
  265. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  266. }
  267. /**
  268. * Shows/hides the message indicating problems with local media connection.
  269. * @param {boolean|null} show(optional) tells whether the message is to be
  270. * displayed or not. If missing the condition will be based on the value
  271. * obtained from {@link APP.conference.isConnectionInterrupted}.
  272. */
  273. showLocalConnectionMessage (show) {
  274. if (typeof show !== 'boolean') {
  275. show = APP.conference.isConnectionInterrupted();
  276. }
  277. if (show) {
  278. $('#localConnectionMessage').css({display: "block"});
  279. // Avatar message conflicts with 'videoConnectionMessage',
  280. // so it must be hidden
  281. this.showRemoteConnectionMessage(false);
  282. } else {
  283. $('#localConnectionMessage').css({display: "none"});
  284. }
  285. }
  286. /**
  287. * Shows hides the "avatar" message which is to be displayed either in
  288. * the middle of the screen or below the avatar image.
  289. *
  290. * @param {null|boolean} show (optional) <tt>true</tt> to show the avatar
  291. * message or <tt>false</tt> to hide it. If not provided then the connection
  292. * status of the user currently on the large video will be obtained form
  293. * "APP.conference" and the message will be displayed if the user's
  294. * connection is interrupted.
  295. */
  296. showRemoteConnectionMessage (show) {
  297. if (typeof show !== 'boolean') {
  298. show = APP.conference.isParticipantConnectionActive(this.id);
  299. }
  300. if (show) {
  301. $('#remoteConnectionMessage').css({display: "block"});
  302. // 'videoConnectionMessage' message conflicts with 'avatarMessage',
  303. // so it must be hidden
  304. this.showLocalConnectionMessage(false);
  305. } else {
  306. $('#remoteConnectionMessage').hide();
  307. }
  308. }
  309. /**
  310. * Updates the text which describes that the remote user is having
  311. * connectivity issues.
  312. *
  313. * @param {string} msgKey the translation key which will be used to get
  314. * the message text.
  315. * @param {object} msgOptions translation options object.
  316. *
  317. * @private
  318. */
  319. _setRemoteConnectionMessage (msgKey, msgOptions) {
  320. if (msgKey) {
  321. let text = APP.translation.translateString(msgKey, msgOptions);
  322. $('#remoteConnectionMessage')
  323. .attr("data-i18n", msgKey).text(text);
  324. }
  325. this.videoContainer.positionRemoteConnectionMessage();
  326. }
  327. /**
  328. * Updated the text which is to be shown on the top of large video, when
  329. * local media connection is interrupted.
  330. *
  331. * @param {string} msgKey the translation key which will be used to get
  332. * the message text to be displayed on the large video.
  333. * @param {object} msgOptions translation options object
  334. *
  335. * @private
  336. */
  337. _setLocalConnectionMessage (msgKey, msgOptions) {
  338. $('#localConnectionMessage')
  339. .attr("data-i18n", msgKey)
  340. .text(APP.translation.translateString(msgKey, msgOptions));
  341. }
  342. /**
  343. * Add container of specified type.
  344. * @param {string} type container type
  345. * @param {LargeContainer} container container to add.
  346. */
  347. addContainer (type, container) {
  348. if (this.containers[type]) {
  349. throw new Error(`container of type ${type} already exist`);
  350. }
  351. this.containers[type] = container;
  352. this.resizeContainer(type);
  353. }
  354. /**
  355. * Get Large container of specified type.
  356. * @param {string} type container type.
  357. * @returns {LargeContainer}
  358. */
  359. getContainer (type) {
  360. let container = this.containers[type];
  361. if (!container) {
  362. throw new Error(`container of type ${type} doesn't exist`);
  363. }
  364. return container;
  365. }
  366. /**
  367. * Remove Large container of specified type.
  368. * @param {string} type container type.
  369. */
  370. removeContainer (type) {
  371. if (!this.containers[type]) {
  372. throw new Error(`container of type ${type} doesn't exist`);
  373. }
  374. delete this.containers[type];
  375. }
  376. /**
  377. * Show Large container of specified type.
  378. * Does nothing if such container is already visible.
  379. * @param {string} type container type.
  380. * @returns {Promise}
  381. */
  382. showContainer (type) {
  383. if (this.state === type) {
  384. return Promise.resolve();
  385. }
  386. let oldContainer = this.containers[this.state];
  387. // FIXME when video is being replaced with other content we need to hide
  388. // companion icons/messages. It would be best if the container would
  389. // be taking care of it by itself, but that is a bigger refactoring
  390. if (this.state === VIDEO_CONTAINER_TYPE) {
  391. this.showWatermark(false);
  392. this.showLocalConnectionMessage(false);
  393. this.showRemoteConnectionMessage(false);
  394. }
  395. oldContainer.hide();
  396. this.state = type;
  397. let container = this.getContainer(type);
  398. return container.show().then(() => {
  399. if (type === VIDEO_CONTAINER_TYPE) {
  400. // FIXME when video appears on top of other content we need to
  401. // show companion icons/messages. It would be best if
  402. // the container would be taking care of it by itself, but that
  403. // is a bigger refactoring
  404. this.showWatermark(true);
  405. // "avatar" and "video connection" can not be displayed both
  406. // at the same time, but the latter is of higher priority and it
  407. // will hide the avatar one if will be displayed.
  408. this.showRemoteConnectionMessage(/* fet the current state */);
  409. this.showLocalConnectionMessage(/* fetch the current state */);
  410. }
  411. });
  412. }
  413. /**
  414. * Changes the flipX state of the local video.
  415. * @param val {boolean} true if flipped.
  416. */
  417. onLocalFlipXChange(val) {
  418. this.videoContainer.setLocalFlipX(val);
  419. }
  420. }