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

LargeVideoManager.js 16KB

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