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

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