Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LargeVideoManager.js 16KB

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