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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. element.onplay = function () {
  139. var type = (isVideo ? 'video' : 'audio');
  140. var now = APP.connectionTimes[type + ".render"]
  141. = window.performance.now();
  142. console.log("(TIME) Render " + type + ":\t",
  143. now);
  144. AnalyticsAdapter.sendEvent('render.' + type, now);
  145. var ttfm = now - APP.connectionTimes["document.ready"]
  146. - (APP.conference.getConnectionTimes()["session.initiate"]
  147. - APP.conference.getConnectionTimes()["muc.joined"]);
  148. console.log("(TIME) TTFM " + type + ":\t", ttfm);
  149. AnalyticsAdapter.sendEvent('ttfm.' + type, ttfm);
  150. };
  151. return element;
  152. };
  153. /**
  154. * Returns the element id for a particular MediaStream.
  155. */
  156. SmallVideo.getStreamElementID = function (stream) {
  157. let isVideo = stream.isVideoTrack();
  158. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  159. };
  160. /**
  161. * Configures hoverIn/hoverOut handlers.
  162. */
  163. SmallVideo.prototype.bindHoverHandler = function () {
  164. // Add hover handler
  165. var self = this;
  166. $(this.container).hover(
  167. function () {
  168. self.showDisplayName(true);
  169. },
  170. function () {
  171. // If the video has been "pinned" by the user we want to
  172. // keep the display name on place.
  173. if (!self.VideoLayout.isLargeVideoVisible() ||
  174. !self.VideoLayout.isCurrentlyOnLarge(self.id))
  175. self.showDisplayName(false);
  176. }
  177. );
  178. };
  179. /**
  180. * Updates the data for the indicator
  181. * @param id the id of the indicator
  182. * @param percent the percent for connection quality
  183. * @param object the data
  184. */
  185. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  186. if(this.connectionIndicator)
  187. this.connectionIndicator.updateConnectionQuality(percent, object);
  188. };
  189. SmallVideo.prototype.hideIndicator = function () {
  190. if(this.connectionIndicator)
  191. this.connectionIndicator.hideIndicator();
  192. };
  193. /**
  194. * Shows audio muted indicator over small videos.
  195. * @param {string} isMuted
  196. */
  197. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  198. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  199. if (!isMuted) {
  200. if (audioMutedSpan.length > 0) {
  201. audioMutedSpan.popover('hide');
  202. audioMutedSpan.remove();
  203. }
  204. }
  205. else {
  206. if (!audioMutedSpan.length) {
  207. audioMutedSpan = document.createElement('span');
  208. audioMutedSpan.className = 'audioMuted';
  209. UIUtil.setTooltip(audioMutedSpan,
  210. "videothumbnail.mute",
  211. "top");
  212. this.container.appendChild(audioMutedSpan);
  213. APP.translation.translateElement($('#' + this.videoSpanId + " > span"));
  214. var mutedIndicator = document.createElement('i');
  215. mutedIndicator.className = 'icon-mic-disabled';
  216. audioMutedSpan.appendChild(mutedIndicator);
  217. }
  218. this.updateIconPositions();
  219. }
  220. this.isMuted = isMuted;
  221. };
  222. /**
  223. * Shows video muted indicator over small videos and disables/enables avatar
  224. * if video muted.
  225. */
  226. SmallVideo.prototype.setMutedView = function(isMuted) {
  227. this.isVideoMuted = isMuted;
  228. this.updateView();
  229. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  230. if (isMuted === false) {
  231. if (videoMutedSpan.length > 0) {
  232. videoMutedSpan.remove();
  233. }
  234. }
  235. else {
  236. if (!videoMutedSpan.length) {
  237. videoMutedSpan = document.createElement('span');
  238. videoMutedSpan.className = 'videoMuted';
  239. this.container.appendChild(videoMutedSpan);
  240. var mutedIndicator = document.createElement('i');
  241. mutedIndicator.className = 'icon-camera-disabled';
  242. UIUtil.setTooltip(mutedIndicator,
  243. "videothumbnail.videomute",
  244. "top");
  245. videoMutedSpan.appendChild(mutedIndicator);
  246. //translate texts for muted indicator
  247. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  248. }
  249. this.updateIconPositions();
  250. }
  251. };
  252. SmallVideo.prototype.updateIconPositions = function () {
  253. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  254. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  255. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  256. if(connectionIndicator.length > 0 &&
  257. connectionIndicator[0].style.display != "none") {
  258. audioMutedSpan.css({right: "23px"});
  259. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  260. } else {
  261. audioMutedSpan.css({right: "0px"});
  262. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  263. }
  264. };
  265. /**
  266. * Creates the element indicating the moderator(owner) of the conference.
  267. */
  268. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  269. // Show moderator indicator
  270. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  271. if (!indicatorSpan || indicatorSpan.length === 0) {
  272. indicatorSpan = document.createElement('span');
  273. indicatorSpan.className = 'focusindicator';
  274. this.container.appendChild(indicatorSpan);
  275. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  276. }
  277. if (indicatorSpan.children().length !== 0)
  278. return;
  279. var moderatorIndicator = document.createElement('i');
  280. moderatorIndicator.className = 'fa fa-star';
  281. indicatorSpan[0].appendChild(moderatorIndicator);
  282. UIUtil.setTooltip(indicatorSpan[0],
  283. "videothumbnail.moderator",
  284. "top");
  285. //translates text in focus indicators
  286. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  287. };
  288. /**
  289. * Removes the element indicating the moderator(owner) of the conference.
  290. */
  291. SmallVideo.prototype.removeModeratorIndicatorElement = function () {
  292. $('#' + this.videoSpanId + ' .focusindicator').remove();
  293. };
  294. /**
  295. * This is an especially interesting function. A naive reader might think that
  296. * it returns this SmallVideo's "video" element. But it is much more exciting.
  297. * It first finds this video's parent element using jquery, then uses a utility
  298. * from lib-jitsi-meet to extract the video element from it (with two more
  299. * jquery calls), and finally uses jquery again to encapsulate the video element
  300. * in an array. This last step allows (some might prefer "forces") users of
  301. * this function to access the video element via the 0th element of the returned
  302. * array (after checking its length of course!).
  303. */
  304. SmallVideo.prototype.selectVideoElement = function () {
  305. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  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. * Hides or shows the user's avatar.
  328. * This update assumes that large video had been updated and we will
  329. * reflect it on this small video.
  330. *
  331. * @param show whether we should show the avatar or not
  332. * video because there is no dominant speaker and no focused speaker
  333. */
  334. SmallVideo.prototype.updateView = function () {
  335. if (!this.hasAvatar) {
  336. if (this.id) {
  337. // Init avatar
  338. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  339. } else {
  340. console.error("Unable to init avatar - no id", this);
  341. return;
  342. }
  343. }
  344. let video = this.selectVideoElement();
  345. let avatar = $('#' + this.videoSpanId + ' .userAvatar');
  346. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  347. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  348. var showAvatar;
  349. if ((!this.isLocal
  350. && !this.VideoLayout.isInLastN(this.id))
  351. || this.isVideoMuted) {
  352. showAvatar = true;
  353. } else {
  354. // We want to show the avatar when the video is muted or not exists
  355. // that is when 'true' or 'null' is returned
  356. showAvatar = !this.videoStream || this.videoStream.isMuted();
  357. }
  358. showAvatar = showAvatar && !isCurrentlyOnLarge;
  359. if (video && video.length > 0) {
  360. setVisibility(video, showVideo);
  361. }
  362. setVisibility(avatar, showAvatar);
  363. this.showDisplayName(!showVideo && !showAvatar);
  364. };
  365. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  366. var thumbnail = $('#' + this.videoSpanId);
  367. var avatar = $('#' + this.videoSpanId + ' .userAvatar');
  368. this.hasAvatar = true;
  369. // set the avatar in the thumbnail
  370. if (avatar && avatar.length > 0) {
  371. avatar[0].src = avatarUrl;
  372. } else {
  373. if (thumbnail && thumbnail.length > 0) {
  374. avatar = document.createElement('img');
  375. avatar.className = 'userAvatar';
  376. avatar.src = avatarUrl;
  377. thumbnail.append(avatar);
  378. }
  379. }
  380. };
  381. /**
  382. * Shows or hides the dominant speaker indicator.
  383. * @param show whether to show or hide.
  384. */
  385. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  386. if (!this.container) {
  387. console.warn( "Unable to set dominant speaker indicator - "
  388. + this.videoSpanId + " does not exist");
  389. return;
  390. }
  391. var indicatorSpanId = "dominantspeakerindicator";
  392. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  393. indicatorSpan.innerHTML
  394. = "<i id='indicatoricon' class='fa fa-bullhorn'></i>";
  395. // adds a tooltip
  396. UIUtil.setTooltip(indicatorSpan, "speaker", "left");
  397. APP.translation.translateElement($(indicatorSpan));
  398. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  399. };
  400. /**
  401. * Shows or hides the raised hand indicator.
  402. * @param show whether to show or hide.
  403. */
  404. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  405. if (!this.container) {
  406. console.warn( "Unable to raised hand indication - "
  407. + this.videoSpanId + " does not exist");
  408. return;
  409. }
  410. var indicatorSpanId = "raisehandindicator";
  411. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  412. indicatorSpan.style.background = "#D6D61E";
  413. indicatorSpan.innerHTML
  414. = "<i id='indicatoricon' class='fa fa-hand-paper-o'></i>";
  415. // adds a tooltip
  416. UIUtil.setTooltip(indicatorSpan, "raisedHand", "left");
  417. APP.translation.translateElement($(indicatorSpan));
  418. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  419. };
  420. /**
  421. * Gets (creating if necessary) the "indicator" span for this SmallVideo
  422. identified by an ID.
  423. */
  424. SmallVideo.prototype.getIndicatorSpan = function(id) {
  425. var indicatorSpan;
  426. var spans = $(`#${this.videoSpanId}>[id=${id}`);
  427. if (spans.length <= 0) {
  428. indicatorSpan = document.createElement('span');
  429. indicatorSpan.id = id;
  430. indicatorSpan.className = "indicator";
  431. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  432. } else {
  433. indicatorSpan = spans[0];
  434. }
  435. return indicatorSpan;
  436. };
  437. /**
  438. * Adds a listener for onresize events for this video, which will monitor for
  439. * resolution changes, will calculate the delay since the moment the listened
  440. * is added, and will fire a RESOLUTION_CHANGED event.
  441. */
  442. SmallVideo.prototype.waitForResolutionChange = function() {
  443. let self = this;
  444. let beforeChange = window.performance.now();
  445. let videos = this.selectVideoElement();
  446. if (!videos || !videos.length || videos.length <= 0)
  447. return;
  448. let video = videos[0];
  449. let oldWidth = video.videoWidth;
  450. let oldHeight = video.videoHeight;
  451. video.onresize = (event) => {
  452. if (video.videoWidth != oldWidth || video.videoHeight != oldHeight) {
  453. // Only run once.
  454. video.onresize = null;
  455. let delay = window.performance.now() - beforeChange;
  456. let emitter = self.VideoLayout.getEventEmitter();
  457. if (emitter) {
  458. emitter.emit(
  459. UIEvents.RESOLUTION_CHANGED,
  460. self.getId(),
  461. oldWidth + "x" + oldHeight,
  462. video.videoWidth + "x" + video.videoHeight,
  463. delay);
  464. }
  465. }
  466. };
  467. };
  468. export default SmallVideo;