Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SmallVideo.js 20KB

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