Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LargeVideoManager.js 17KB

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