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

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