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

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