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

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