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

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