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.

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