選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LargeVideoManager.js 20KB

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