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 19KB

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