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