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

LargeVideoManager.js 16KB

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