You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SmallVideo.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* global $, JitsiMeetJS, interfaceConfig */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import Avatar from "../avatar/Avatar";
  4. import UIUtil from "../util/UIUtil";
  5. import UIEvents from "../../../service/UI/UIEvents";
  6. import AudioLevels from "../audio_levels/AudioLevels";
  7. const RTCUIHelper = JitsiMeetJS.util.RTCUIHelper;
  8. /**
  9. * Display mode constant used when video is being displayed on the small video.
  10. * @type {number}
  11. * @constant
  12. */
  13. const DISPLAY_VIDEO = 0;
  14. /**
  15. * Display mode constant used when the user's avatar is being displayed on
  16. * the small video.
  17. * @type {number}
  18. * @constant
  19. */
  20. const DISPLAY_AVATAR = 1;
  21. /**
  22. * Display mode constant used when neither video nor avatar is being displayed
  23. * on the small video. And we just show the display name.
  24. * @type {number}
  25. * @constant
  26. */
  27. const DISPLAY_BLACKNESS_WITH_NAME = 2;
  28. /**
  29. * Display mode constant used when video is displayed and display name
  30. * at the same time.
  31. * @type {number}
  32. * @constant
  33. */
  34. const DISPLAY_VIDEO_WITH_NAME = 3;
  35. /**
  36. * Display mode constant used when neither video nor avatar is being displayed
  37. * on the small video. And we just show the display name.
  38. * @type {number}
  39. * @constant
  40. */
  41. const DISPLAY_AVATAR_WITH_NAME = 4;
  42. function SmallVideo(VideoLayout) {
  43. this.isAudioMuted = false;
  44. this.hasAvatar = false;
  45. this.isVideoMuted = false;
  46. this.videoStream = null;
  47. this.audioStream = null;
  48. this.VideoLayout = VideoLayout;
  49. this.videoIsHovered = false;
  50. this.hideDisplayName = false;
  51. // we can stop updating the thumbnail
  52. this.disableUpdateView = false;
  53. }
  54. /**
  55. * Returns the identifier of this small video.
  56. *
  57. * @returns the identifier of this small video
  58. */
  59. SmallVideo.prototype.getId = function () {
  60. return this.id;
  61. };
  62. /* Indicates if this small video is currently visible.
  63. *
  64. * @return <tt>true</tt> if this small video isn't currently visible and
  65. * <tt>false</tt> - otherwise.
  66. */
  67. SmallVideo.prototype.isVisible = function () {
  68. return $('#' + this.videoSpanId).is(':visible');
  69. };
  70. /**
  71. * Enables / disables the device availability icons for this small video.
  72. * @param {enable} set to {true} to enable and {false} to disable
  73. */
  74. SmallVideo.prototype.enableDeviceAvailabilityIcons = function (enable) {
  75. if (typeof enable === "undefined")
  76. return;
  77. this.deviceAvailabilityIconsEnabled = enable;
  78. };
  79. /**
  80. * Sets the device "non" availability icons.
  81. * @param devices the devices, which will be checked for availability
  82. */
  83. SmallVideo.prototype.setDeviceAvailabilityIcons = function (devices) {
  84. if (!this.deviceAvailabilityIconsEnabled)
  85. return;
  86. if(!this.container)
  87. return;
  88. var noMic = $("#" + this.videoSpanId + " > .noMic");
  89. var noVideo = $("#" + this.videoSpanId + " > .noVideo");
  90. noMic.remove();
  91. noVideo.remove();
  92. if (!devices.audio) {
  93. this.container.appendChild(
  94. document.createElement("div")).setAttribute("class", "noMic");
  95. }
  96. if (!devices.video) {
  97. this.container.appendChild(
  98. document.createElement("div")).setAttribute("class", "noVideo");
  99. }
  100. if (!devices.audio && !devices.video) {
  101. noMic.css("background-position", "75%");
  102. noVideo.css("background-position", "25%");
  103. noVideo.css("background-color", "transparent");
  104. }
  105. };
  106. /**
  107. * Sets the type of the video displayed by this instance.
  108. * Note that this is a string without clearly defined or checked values, and
  109. * it is NOT one of the strings defined in service/RTC/VideoType in
  110. * lib-jitsi-meet.
  111. * @param videoType 'camera' or 'desktop', or 'sharedvideo'.
  112. */
  113. SmallVideo.prototype.setVideoType = function (videoType) {
  114. this.videoType = videoType;
  115. };
  116. /**
  117. * Returns the type of the video displayed by this instance.
  118. * Note that this is a string without clearly defined or checked values, and
  119. * it is NOT one of the strings defined in service/RTC/VideoType in
  120. * lib-jitsi-meet.
  121. * @returns {String} 'camera', 'screen', 'sharedvideo', or undefined.
  122. */
  123. SmallVideo.prototype.getVideoType = function () {
  124. return this.videoType;
  125. };
  126. /**
  127. * Creates an audio or video element for a particular MediaStream.
  128. */
  129. SmallVideo.createStreamElement = function (stream) {
  130. let isVideo = stream.isVideoTrack();
  131. let element = isVideo
  132. ? document.createElement('video')
  133. : document.createElement('audio');
  134. if (isVideo) {
  135. element.setAttribute("muted", "true");
  136. }
  137. RTCUIHelper.setAutoPlay(element, true);
  138. element.id = SmallVideo.getStreamElementID(stream);
  139. return element;
  140. };
  141. /**
  142. * Returns the element id for a particular MediaStream.
  143. */
  144. SmallVideo.getStreamElementID = function (stream) {
  145. let isVideo = stream.isVideoTrack();
  146. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  147. };
  148. /**
  149. * Configures hoverIn/hoverOut handlers. Depends on connection indicator.
  150. */
  151. SmallVideo.prototype.bindHoverHandler = function () {
  152. // Add hover handler
  153. $(this.container).hover(
  154. () => {
  155. this.videoIsHovered = true;
  156. this.updateView();
  157. },
  158. () => {
  159. this.videoIsHovered = false;
  160. this.updateView();
  161. }
  162. );
  163. if (this.connectionIndicator) {
  164. this.connectionIndicator.addPopoverHoverListener(
  165. () => {
  166. this.updateView();
  167. });
  168. }
  169. };
  170. /**
  171. * Updates the data for the indicator
  172. * @param id the id of the indicator
  173. * @param percent the percent for connection quality
  174. * @param object the data
  175. */
  176. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  177. if(this.connectionIndicator)
  178. this.connectionIndicator.updateConnectionQuality(percent, object);
  179. };
  180. SmallVideo.prototype.hideIndicator = function () {
  181. if(this.connectionIndicator)
  182. this.connectionIndicator.hideIndicator();
  183. };
  184. /**
  185. * Shows / hides the audio muted indicator over small videos.
  186. *
  187. * @param {boolean} isMuted indicates if the muted element should be shown
  188. * or hidden
  189. */
  190. SmallVideo.prototype.showAudioIndicator = function (isMuted) {
  191. let mutedIndicator = this.getAudioMutedIndicator();
  192. UIUtil.setVisible(mutedIndicator, isMuted);
  193. this.isAudioMuted = isMuted;
  194. };
  195. /**
  196. * Returns the audio muted indicator jquery object. If it doesn't exists -
  197. * creates it.
  198. *
  199. * @returns {HTMLElement} the audio muted indicator
  200. */
  201. SmallVideo.prototype.getAudioMutedIndicator = function () {
  202. let selector = '#' + this.videoSpanId + ' .audioMuted';
  203. let audioMutedSpan = document.querySelector(selector);
  204. if (audioMutedSpan) {
  205. return audioMutedSpan;
  206. }
  207. audioMutedSpan = document.createElement('span');
  208. audioMutedSpan.className = 'audioMuted toolbar-icon';
  209. UIUtil.setTooltip(audioMutedSpan,
  210. "videothumbnail.mute",
  211. "top");
  212. let mutedIndicator = document.createElement('i');
  213. mutedIndicator.className = 'icon-mic-disabled';
  214. audioMutedSpan.appendChild(mutedIndicator);
  215. this.container
  216. .querySelector('.videocontainer__toolbar')
  217. .appendChild(audioMutedSpan);
  218. return audioMutedSpan;
  219. };
  220. /**
  221. * Shows video muted indicator over small videos and disables/enables avatar
  222. * if video muted.
  223. *
  224. * @param {boolean} isMuted indicates if we should set the view to muted view
  225. * or not
  226. */
  227. SmallVideo.prototype.setVideoMutedView = function(isMuted) {
  228. this.isVideoMuted = isMuted;
  229. this.updateView();
  230. let element = this.getVideoMutedIndicator();
  231. UIUtil.setVisible(element, isMuted);
  232. };
  233. /**
  234. * Returns the video muted indicator jquery object. If it doesn't exists -
  235. * creates it.
  236. *
  237. * @returns {jQuery|HTMLElement} the video muted indicator
  238. */
  239. SmallVideo.prototype.getVideoMutedIndicator = function () {
  240. var selector = '#' + this.videoSpanId + ' .videoMuted';
  241. var videoMutedSpan = document.querySelector(selector);
  242. if (videoMutedSpan) {
  243. return videoMutedSpan;
  244. }
  245. videoMutedSpan = document.createElement('span');
  246. videoMutedSpan.className = 'videoMuted toolbar-icon';
  247. this.container
  248. .querySelector('.videocontainer__toolbar')
  249. .appendChild(videoMutedSpan);
  250. var mutedIndicator = document.createElement('i');
  251. mutedIndicator.className = 'icon-camera-disabled';
  252. UIUtil.setTooltip(mutedIndicator,
  253. "videothumbnail.videomute",
  254. "top");
  255. videoMutedSpan.appendChild(mutedIndicator);
  256. return videoMutedSpan;
  257. };
  258. /**
  259. * Adds the element indicating the moderator(owner) of the conference.
  260. */
  261. SmallVideo.prototype.addModeratorIndicator = function () {
  262. // Don't create moderator indicator if DISABLE_FOCUS_INDICATOR is true
  263. if (interfaceConfig.DISABLE_FOCUS_INDICATOR)
  264. return false;
  265. // Show moderator indicator
  266. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  267. if (indicatorSpan.length) {
  268. return;
  269. }
  270. indicatorSpan = document.createElement('span');
  271. indicatorSpan.className = 'focusindicator toolbar-icon right';
  272. this.container
  273. .querySelector('.videocontainer__toolbar')
  274. .appendChild(indicatorSpan);
  275. var moderatorIndicator = document.createElement('i');
  276. moderatorIndicator.className = 'icon-star';
  277. UIUtil.setTooltip(moderatorIndicator,
  278. "videothumbnail.moderator",
  279. "top-left");
  280. indicatorSpan.appendChild(moderatorIndicator);
  281. };
  282. /**
  283. * Adds the element indicating the audio level of the participant.
  284. */
  285. SmallVideo.prototype.addAudioLevelIndicator = function () {
  286. var audioSpan = $('#' + this.videoSpanId + ' .audioindicator');
  287. if (audioSpan.length) {
  288. return;
  289. }
  290. this.container.appendChild(
  291. AudioLevels.createThumbnailAudioLevelIndicator());
  292. };
  293. /**
  294. * Updates the audio level for this small video.
  295. *
  296. * @param lvl the new audio level to set
  297. */
  298. SmallVideo.prototype.updateAudioLevelIndicator = function (lvl) {
  299. AudioLevels.updateThumbnailAudioLevel(this.videoSpanId, lvl);
  300. };
  301. /**
  302. * Removes the element indicating the moderator(owner) of the conference.
  303. */
  304. SmallVideo.prototype.removeModeratorIndicator = function () {
  305. $('#' + this.videoSpanId + ' .focusindicator').remove();
  306. };
  307. /**
  308. * This is an especially interesting function. A naive reader might think that
  309. * it returns this SmallVideo's "video" element. But it is much more exciting.
  310. * It first finds this video's parent element using jquery, then uses a utility
  311. * from lib-jitsi-meet to extract the video element from it (with two more
  312. * jquery calls), and finally uses jquery again to encapsulate the video element
  313. * in an array. This last step allows (some might prefer "forces") users of
  314. * this function to access the video element via the 0th element of the returned
  315. * array (after checking its length of course!).
  316. */
  317. SmallVideo.prototype.selectVideoElement = function () {
  318. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  319. };
  320. /**
  321. * Selects the HTML image element which displays user's avatar.
  322. *
  323. * @return {jQuery|HTMLElement} a jQuery selector pointing to the HTML image
  324. * element which displays the user's avatar.
  325. */
  326. SmallVideo.prototype.$avatar = function () {
  327. return $('#' + this.videoSpanId + ' .userAvatar');
  328. };
  329. /**
  330. * Returns the display name element, which appears on the video thumbnail.
  331. *
  332. * @return {jQuery} a jQuery selector pointing to the display name element of
  333. * the video thumbnail
  334. */
  335. SmallVideo.prototype.$displayName = function () {
  336. return $('#' + this.videoSpanId + ' .displayname');
  337. };
  338. /**
  339. * Enables / disables the css responsible for focusing/pinning a video
  340. * thumbnail.
  341. *
  342. * @param isFocused indicates if the thumbnail should be focused/pinned or not
  343. */
  344. SmallVideo.prototype.focus = function(isFocused) {
  345. var focusedCssClass = "videoContainerFocused";
  346. var isFocusClassEnabled = $(this.container).hasClass(focusedCssClass);
  347. if (!isFocused && isFocusClassEnabled) {
  348. $(this.container).removeClass(focusedCssClass);
  349. }
  350. else if (isFocused && !isFocusClassEnabled) {
  351. $(this.container).addClass(focusedCssClass);
  352. }
  353. };
  354. SmallVideo.prototype.hasVideo = function () {
  355. return this.selectVideoElement().length !== 0;
  356. };
  357. /**
  358. * Checks whether the user associated with this <tt>SmallVideo</tt> is currently
  359. * being displayed on the "large video".
  360. *
  361. * @return {boolean} <tt>true</tt> if the user is displayed on the large video
  362. * or <tt>false</tt> otherwise.
  363. */
  364. SmallVideo.prototype.isCurrentlyOnLargeVideo = function () {
  365. return this.VideoLayout.isCurrentlyOnLarge(this.id);
  366. };
  367. /**
  368. * Checks whether there is a playable video stream available for the user
  369. * associated with this <tt>SmallVideo</tt>.
  370. *
  371. * @return {boolean} <tt>true</tt> if there is a playable video stream available
  372. * or <tt>false</tt> otherwise.
  373. */
  374. SmallVideo.prototype.isVideoPlayable = function() {
  375. return this.videoStream // Is there anything to display ?
  376. && !this.isVideoMuted && !this.videoStream.isMuted() // Muted ?
  377. && (this.isLocal || this.VideoLayout.isInLastN(this.id));
  378. };
  379. /**
  380. * Determines what should be display on the thumbnail.
  381. *
  382. * @return {number} one of <tt>DISPLAY_VIDEO</tt>,<tt>DISPLAY_AVATAR</tt>
  383. * or <tt>DISPLAY_BLACKNESS_WITH_NAME</tt>.
  384. */
  385. SmallVideo.prototype.selectDisplayMode = function() {
  386. // Display name is always and only displayed when user is on the stage
  387. if (this.isCurrentlyOnLargeVideo()) {
  388. return DISPLAY_BLACKNESS_WITH_NAME;
  389. } else if (this.isVideoPlayable() && this.selectVideoElement().length) {
  390. // check hovering and change state to video with name
  391. return this._isHovered() ?
  392. DISPLAY_VIDEO_WITH_NAME : DISPLAY_VIDEO;
  393. } else {
  394. // check hovering and change state to avatar with name
  395. return this._isHovered() ?
  396. DISPLAY_AVATAR_WITH_NAME : DISPLAY_AVATAR;
  397. }
  398. };
  399. /**
  400. * Checks whether current video is considered hovered. Currently it is hovered
  401. * if the mouse is over the video, or if the connection
  402. * indicator is shown(hovered).
  403. * @private
  404. */
  405. SmallVideo.prototype._isHovered = function () {
  406. return this.videoIsHovered
  407. || (this.connectionIndicator
  408. && this.connectionIndicator.popover.popoverIsHovered);
  409. };
  410. /**
  411. * Hides or shows the user's avatar.
  412. * This update assumes that large video had been updated and we will
  413. * reflect it on this small video.
  414. *
  415. * @param show whether we should show the avatar or not
  416. * video because there is no dominant speaker and no focused speaker
  417. */
  418. SmallVideo.prototype.updateView = function () {
  419. if (this.disableUpdateView)
  420. return;
  421. if (!this.hasAvatar) {
  422. if (this.id) {
  423. // Init avatar
  424. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  425. } else {
  426. logger.error("Unable to init avatar - no id", this);
  427. return;
  428. }
  429. }
  430. // Determine whether video, avatar or blackness should be displayed
  431. let displayMode = this.selectDisplayMode();
  432. // Show/hide video.
  433. UIUtil.setVisibleBySelector(this.selectVideoElement(),
  434. (displayMode === DISPLAY_VIDEO
  435. || displayMode === DISPLAY_VIDEO_WITH_NAME));
  436. // Show/hide the avatar.
  437. UIUtil.setVisibleBySelector(this.$avatar(),
  438. (displayMode === DISPLAY_AVATAR
  439. || displayMode === DISPLAY_AVATAR_WITH_NAME));
  440. // Show/hide the display name.
  441. UIUtil.setVisibleBySelector(this.$displayName(),
  442. !this.hideDisplayName
  443. && (displayMode === DISPLAY_BLACKNESS_WITH_NAME
  444. || displayMode === DISPLAY_VIDEO_WITH_NAME
  445. || displayMode === DISPLAY_AVATAR_WITH_NAME));
  446. // show hide overlay when there is a video or avatar under
  447. // the display name
  448. UIUtil.setVisibleBySelector($('#' + this.videoSpanId
  449. + ' .videocontainer__hoverOverlay'),
  450. (displayMode === DISPLAY_AVATAR_WITH_NAME
  451. || displayMode === DISPLAY_VIDEO_WITH_NAME));
  452. };
  453. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  454. var thumbnail = $('#' + this.videoSpanId);
  455. var avatarSel = this.$avatar();
  456. this.hasAvatar = true;
  457. // set the avatar in the thumbnail
  458. if (avatarSel && avatarSel.length > 0) {
  459. avatarSel[0].src = avatarUrl;
  460. } else {
  461. if (thumbnail && thumbnail.length > 0) {
  462. var avatarElement = document.createElement('img');
  463. avatarElement.className = 'userAvatar';
  464. avatarElement.src = avatarUrl;
  465. thumbnail.append(avatarElement);
  466. }
  467. }
  468. };
  469. /**
  470. * Shows or hides the dominant speaker indicator.
  471. * @param show whether to show or hide.
  472. */
  473. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  474. // Don't create and show dominant speaker indicator if
  475. // DISABLE_DOMINANT_SPEAKER_INDICATOR is true
  476. if (interfaceConfig.DISABLE_DOMINANT_SPEAKER_INDICATOR)
  477. return;
  478. if (!this.container) {
  479. logger.warn( "Unable to set dominant speaker indicator - "
  480. + this.videoSpanId + " does not exist");
  481. return;
  482. }
  483. let indicatorSpanId = "dominantspeakerindicator";
  484. let content = `<i id="indicatoricon"
  485. ' class="indicatoricon fa fa-bullhorn"></i>`;
  486. let indicatorSpan = UIUtil.getVideoThumbnailIndicatorSpan({
  487. videoSpanId: this.videoSpanId,
  488. indicatorId: indicatorSpanId,
  489. content,
  490. tooltip: 'speaker'
  491. });
  492. UIUtil.setVisible(indicatorSpan, show);
  493. };
  494. /**
  495. * Shows or hides the raised hand indicator.
  496. * @param show whether to show or hide.
  497. */
  498. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  499. if (!this.container) {
  500. logger.warn( "Unable to raised hand indication - "
  501. + this.videoSpanId + " does not exist");
  502. return;
  503. }
  504. let indicatorSpanId = "raisehandindicator";
  505. let content = `<i id="indicatoricon"
  506. class="icon-raised-hand indicatoricon"></i>`;
  507. let indicatorSpan = UIUtil.getVideoThumbnailIndicatorSpan({
  508. indicatorId: indicatorSpanId,
  509. videoSpanId: this.videoSpanId,
  510. content,
  511. tooltip: 'raisedHand'
  512. });
  513. UIUtil.setVisible(indicatorSpan, show);
  514. };
  515. /**
  516. * Adds a listener for onresize events for this video, which will monitor for
  517. * resolution changes, will calculate the delay since the moment the listened
  518. * is added, and will fire a RESOLUTION_CHANGED event.
  519. */
  520. SmallVideo.prototype.waitForResolutionChange = function() {
  521. let beforeChange = window.performance.now();
  522. let videos = this.selectVideoElement();
  523. if (!videos || !videos.length || videos.length <= 0)
  524. return;
  525. let video = videos[0];
  526. let oldWidth = video.videoWidth;
  527. let oldHeight = video.videoHeight;
  528. video.onresize = () => {
  529. if (video.videoWidth != oldWidth || video.videoHeight != oldHeight) {
  530. // Only run once.
  531. video.onresize = null;
  532. let delay = window.performance.now() - beforeChange;
  533. let emitter = this.VideoLayout.getEventEmitter();
  534. if (emitter) {
  535. emitter.emit(
  536. UIEvents.RESOLUTION_CHANGED,
  537. this.getId(),
  538. oldWidth + "x" + oldHeight,
  539. video.videoWidth + "x" + video.videoHeight,
  540. delay);
  541. }
  542. }
  543. };
  544. };
  545. /**
  546. * Initalizes any browser specific properties. Currently sets the overflow
  547. * property for Qt browsers on Windows to hidden, thus fixing the following
  548. * problem:
  549. * Some browsers don't have full support of the object-fit property for the
  550. * video element and when we set video object-fit to "cover" the video
  551. * actually overflows the boundaries of its container, so it's important
  552. * to indicate that the "overflow" should be hidden.
  553. *
  554. * Setting this property for all browsers will result in broken audio levels,
  555. * which makes this a temporary solution, before reworking audio levels.
  556. */
  557. SmallVideo.prototype.initBrowserSpecificProperties = function() {
  558. var userAgent = window.navigator.userAgent;
  559. if (userAgent.indexOf("QtWebEngine") > -1
  560. && (userAgent.indexOf("Windows") > -1
  561. || userAgent.indexOf("Linux") > -1)) {
  562. $('#' + this.videoSpanId).css("overflow", "hidden");
  563. }
  564. };
  565. export default SmallVideo;