Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SmallVideo.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* global $, APP, JitsiMeetJS */
  2. /* jshint -W101 */
  3. import Avatar from "../avatar/Avatar";
  4. import UIUtil from "../util/UIUtil";
  5. const RTCUIHelper = JitsiMeetJS.util.RTCUIHelper;
  6. function SmallVideo(VideoLayout) {
  7. this.isMuted = false;
  8. this.hasAvatar = false;
  9. this.isVideoMuted = false;
  10. this.videoStream = null;
  11. this.audioStream = null;
  12. this.VideoLayout = VideoLayout;
  13. }
  14. function setVisibility(selector, show) {
  15. if (selector && selector.length > 0) {
  16. selector.css("visibility", show ? "visible" : "hidden");
  17. }
  18. }
  19. /**
  20. * Returns the identifier of this small video.
  21. *
  22. * @returns the identifier of this small video
  23. */
  24. SmallVideo.prototype.getId = function () {
  25. return this.id;
  26. };
  27. /* Indicates if this small video is currently visible.
  28. *
  29. * @return <tt>true</tt> if this small video isn't currently visible and
  30. * <tt>false</tt> - otherwise.
  31. */
  32. SmallVideo.prototype.isVisible = function () {
  33. return $('#' + this.videoSpanId).is(':visible');
  34. };
  35. SmallVideo.prototype.showDisplayName = function(isShow) {
  36. var nameSpan = $('#' + this.videoSpanId + '>span.displayname').get(0);
  37. if (isShow) {
  38. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  39. nameSpan.setAttribute("style", "display:inline-block;");
  40. }
  41. else {
  42. if (nameSpan)
  43. nameSpan.setAttribute("style", "display:none;");
  44. }
  45. };
  46. /**
  47. * Enables / disables the device availability icons for this small video.
  48. * @param {enable} set to {true} to enable and {false} to disable
  49. */
  50. SmallVideo.prototype.enableDeviceAvailabilityIcons = function (enable) {
  51. if (typeof enable === "undefined")
  52. return;
  53. this.deviceAvailabilityIconsEnabled = enable;
  54. };
  55. /**
  56. * Sets the device "non" availability icons.
  57. * @param devices the devices, which will be checked for availability
  58. */
  59. SmallVideo.prototype.setDeviceAvailabilityIcons = function (devices) {
  60. if (!this.deviceAvailabilityIconsEnabled)
  61. return;
  62. if(!this.container)
  63. return;
  64. var noMic = $("#" + this.videoSpanId + " > .noMic");
  65. var noVideo = $("#" + this.videoSpanId + " > .noVideo");
  66. noMic.remove();
  67. noVideo.remove();
  68. if (!devices.audio) {
  69. this.container.appendChild(
  70. document.createElement("div")).setAttribute("class", "noMic");
  71. }
  72. if (!devices.video) {
  73. this.container.appendChild(
  74. document.createElement("div")).setAttribute("class", "noVideo");
  75. }
  76. if (!devices.audio && !devices.video) {
  77. noMic.css("background-position", "75%");
  78. noVideo.css("background-position", "25%");
  79. noVideo.css("background-color", "transparent");
  80. }
  81. };
  82. /**
  83. * Sets the type of the video displayed by this instance.
  84. * @param videoType 'camera' or 'desktop'
  85. */
  86. SmallVideo.prototype.setVideoType = function (videoType) {
  87. this.videoType = videoType;
  88. };
  89. /**
  90. * Returns the type of the video displayed by this instance.
  91. * @returns {String} 'camera', 'screen' or undefined.
  92. */
  93. SmallVideo.prototype.getVideoType = function () {
  94. return this.videoType;
  95. };
  96. /**
  97. * Shows the presence status message for the given video.
  98. */
  99. SmallVideo.prototype.setPresenceStatus = function (statusMsg) {
  100. if (!this.container) {
  101. // No container
  102. return;
  103. }
  104. var statusSpan = $('#' + this.videoSpanId + '>span.status');
  105. if (!statusSpan.length) {
  106. //Add status span
  107. statusSpan = document.createElement('span');
  108. statusSpan.className = 'status';
  109. statusSpan.id = this.videoSpanId + '_status';
  110. $('#' + this.videoSpanId)[0].appendChild(statusSpan);
  111. statusSpan = $('#' + this.videoSpanId + '>span.status');
  112. }
  113. // Display status
  114. if (statusMsg && statusMsg.length) {
  115. $('#' + this.videoSpanId + '_status').text(statusMsg);
  116. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  117. }
  118. else {
  119. // Hide
  120. statusSpan.get(0).setAttribute("style", "display:none;");
  121. }
  122. };
  123. /**
  124. * Creates an audio or video element for a particular MediaStream.
  125. */
  126. SmallVideo.createStreamElement = function (stream) {
  127. let isVideo = stream.isVideoTrack();
  128. let element = isVideo
  129. ? document.createElement('video')
  130. : document.createElement('audio');
  131. if (isVideo) {
  132. element.setAttribute("muted", "true");
  133. }
  134. RTCUIHelper.setAutoPlay(element, true);
  135. element.id = SmallVideo.getStreamElementID(stream);
  136. element.onplay = function () {
  137. var type = (isVideo ? 'video' : 'audio');
  138. var now = APP.connectionTimes[type + ".render"]
  139. = window.performance.now();
  140. console.log("(TIME) Render " + type + ":\t",
  141. now);
  142. };
  143. return element;
  144. };
  145. /**
  146. * Returns the element id for a particular MediaStream.
  147. */
  148. SmallVideo.getStreamElementID = function (stream) {
  149. let isVideo = stream.isVideoTrack();
  150. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  151. };
  152. /**
  153. * Configures hoverIn/hoverOut handlers.
  154. */
  155. SmallVideo.prototype.bindHoverHandler = function () {
  156. // Add hover handler
  157. var self = this;
  158. $(this.container).hover(
  159. function () {
  160. self.showDisplayName(true);
  161. },
  162. function () {
  163. // If the video has been "pinned" by the user we want to
  164. // keep the display name on place.
  165. if (!self.VideoLayout.isLargeVideoVisible() ||
  166. !self.VideoLayout.isCurrentlyOnLarge(self.id))
  167. self.showDisplayName(false);
  168. }
  169. );
  170. };
  171. /**
  172. * Updates the data for the indicator
  173. * @param id the id of the indicator
  174. * @param percent the percent for connection quality
  175. * @param object the data
  176. */
  177. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  178. if(this.connectionIndicator)
  179. this.connectionIndicator.updateConnectionQuality(percent, object);
  180. };
  181. SmallVideo.prototype.hideIndicator = function () {
  182. if(this.connectionIndicator)
  183. this.connectionIndicator.hideIndicator();
  184. };
  185. /**
  186. * Shows audio muted indicator over small videos.
  187. * @param {string} isMuted
  188. */
  189. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  190. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  191. if (!isMuted) {
  192. if (audioMutedSpan.length > 0) {
  193. audioMutedSpan.popover('hide');
  194. audioMutedSpan.remove();
  195. }
  196. }
  197. else {
  198. if (!audioMutedSpan.length) {
  199. audioMutedSpan = document.createElement('span');
  200. audioMutedSpan.className = 'audioMuted';
  201. UIUtil.setTooltip(audioMutedSpan,
  202. "videothumbnail.mute",
  203. "top");
  204. this.container.appendChild(audioMutedSpan);
  205. APP.translation.translateElement($('#' + this.videoSpanId + " > span"));
  206. var mutedIndicator = document.createElement('i');
  207. mutedIndicator.className = 'icon-mic-disabled';
  208. audioMutedSpan.appendChild(mutedIndicator);
  209. }
  210. this.updateIconPositions();
  211. }
  212. this.isMuted = isMuted;
  213. };
  214. /**
  215. * Shows video muted indicator over small videos and disables/enables avatar
  216. * if video muted.
  217. */
  218. SmallVideo.prototype.setMutedView = function(isMuted) {
  219. this.isVideoMuted = isMuted;
  220. this.updateView();
  221. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  222. if (isMuted === false) {
  223. if (videoMutedSpan.length > 0) {
  224. videoMutedSpan.remove();
  225. }
  226. }
  227. else {
  228. if (!videoMutedSpan.length) {
  229. videoMutedSpan = document.createElement('span');
  230. videoMutedSpan.className = 'videoMuted';
  231. this.container.appendChild(videoMutedSpan);
  232. var mutedIndicator = document.createElement('i');
  233. mutedIndicator.className = 'icon-camera-disabled';
  234. UIUtil.setTooltip(mutedIndicator,
  235. "videothumbnail.videomute",
  236. "top");
  237. videoMutedSpan.appendChild(mutedIndicator);
  238. //translate texts for muted indicator
  239. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  240. }
  241. this.updateIconPositions();
  242. }
  243. };
  244. SmallVideo.prototype.updateIconPositions = function () {
  245. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  246. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  247. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  248. if(connectionIndicator.length > 0 &&
  249. connectionIndicator[0].style.display != "none") {
  250. audioMutedSpan.css({right: "23px"});
  251. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  252. } else {
  253. audioMutedSpan.css({right: "0px"});
  254. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  255. }
  256. };
  257. /**
  258. * Creates the element indicating the moderator(owner) of the conference.
  259. *
  260. * @param parentElement the parent element where the owner indicator will
  261. * be added
  262. */
  263. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  264. // Show moderator indicator
  265. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  266. if (!indicatorSpan || indicatorSpan.length === 0) {
  267. indicatorSpan = document.createElement('span');
  268. indicatorSpan.className = 'focusindicator';
  269. this.container.appendChild(indicatorSpan);
  270. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  271. }
  272. if (indicatorSpan.children().length !== 0)
  273. return;
  274. var moderatorIndicator = document.createElement('i');
  275. moderatorIndicator.className = 'fa fa-star';
  276. indicatorSpan[0].appendChild(moderatorIndicator);
  277. UIUtil.setTooltip(indicatorSpan[0],
  278. "videothumbnail.moderator",
  279. "top");
  280. //translates text in focus indicators
  281. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  282. };
  283. SmallVideo.prototype.selectVideoElement = function () {
  284. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  285. };
  286. /**
  287. * Enables / disables the css responsible for focusing/pinning a video
  288. * thumbnail.
  289. *
  290. * @param isFocused indicates if the thumbnail should be focused/pinned or not
  291. */
  292. SmallVideo.prototype.focus = function(isFocused) {
  293. var focusedCssClass = "videoContainerFocused";
  294. var isFocusClassEnabled = $(this.container).hasClass(focusedCssClass);
  295. if (!isFocused && isFocusClassEnabled) {
  296. $(this.container).removeClass(focusedCssClass);
  297. }
  298. else if (isFocused && !isFocusClassEnabled) {
  299. $(this.container).addClass(focusedCssClass);
  300. }
  301. };
  302. SmallVideo.prototype.hasVideo = function () {
  303. return this.selectVideoElement().length !== 0;
  304. };
  305. /**
  306. * Hides or shows the user's avatar.
  307. * This update assumes that large video had been updated and we will
  308. * reflect it on this small video.
  309. *
  310. * @param show whether we should show the avatar or not
  311. * video because there is no dominant speaker and no focused speaker
  312. */
  313. SmallVideo.prototype.updateView = function () {
  314. if (!this.hasAvatar) {
  315. if (this.id) {
  316. // Init avatar
  317. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  318. } else {
  319. console.error("Unable to init avatar - no id", this);
  320. return;
  321. }
  322. }
  323. let video = this.selectVideoElement();
  324. let avatar = $(`#avatar_${this.id}`);
  325. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  326. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  327. var showAvatar;
  328. if ((!this.isLocal
  329. && !this.VideoLayout.isInLastN(this.id))
  330. || this.isVideoMuted) {
  331. showAvatar = true;
  332. } else {
  333. // We want to show the avatar when the video is muted or not exists
  334. // that is when 'true' or 'null' is returned
  335. showAvatar = !this.videoStream || this.videoStream.isMuted();
  336. }
  337. showAvatar = showAvatar && !isCurrentlyOnLarge;
  338. if (video && video.length > 0) {
  339. setVisibility(video, showVideo);
  340. }
  341. setVisibility(avatar, showAvatar);
  342. this.showDisplayName(!showVideo && !showAvatar);
  343. };
  344. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  345. var thumbnail = $('#' + this.videoSpanId);
  346. var avatar = $('#avatar_' + this.id);
  347. this.hasAvatar = true;
  348. // set the avatar in the thumbnail
  349. if (avatar && avatar.length > 0) {
  350. avatar[0].src = avatarUrl;
  351. } else {
  352. if (thumbnail && thumbnail.length > 0) {
  353. avatar = document.createElement('img');
  354. avatar.id = 'avatar_' + this.id;
  355. avatar.className = 'userAvatar';
  356. avatar.src = avatarUrl;
  357. thumbnail.append(avatar);
  358. }
  359. }
  360. };
  361. /**
  362. * Shows or hides the dominant speaker indicator.
  363. * @param show whether to show or hide.
  364. */
  365. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  366. if (!this.container) {
  367. console.warn( "Unable to set dominant speaker indicator - "
  368. + this.videoSpanId + " does not exist");
  369. return;
  370. }
  371. var indicatorSpanId = "dominantspeakerindicator";
  372. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  373. indicatorSpan.innerHTML
  374. = "<i id='indicatoricon' class='fa fa-bullhorn'></i>";
  375. // adds a tooltip
  376. UIUtil.setTooltip(indicatorSpan, "speaker", "left");
  377. APP.translation.translateElement($(indicatorSpan));
  378. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  379. };
  380. /**
  381. * Shows or hides the raised hand indicator.
  382. * @param show whether to show or hide.
  383. */
  384. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  385. if (!this.container) {
  386. console.warn( "Unable to raised hand indication - "
  387. + this.videoSpanId + " does not exist");
  388. return;
  389. }
  390. var indicatorSpanId = "raisehandindicator";
  391. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  392. indicatorSpan.style.background = "#D6D61E";
  393. indicatorSpan.innerHTML
  394. = "<i id='indicatoricon' class='fa fa-hand-paper-o'></i>";
  395. // adds a tooltip
  396. UIUtil.setTooltip(indicatorSpan, "raisedHand", "left");
  397. APP.translation.translateElement($(indicatorSpan));
  398. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  399. };
  400. /**
  401. * Gets (creating if necessary) the "indicator" span for this SmallVideo
  402. identified by an ID.
  403. */
  404. SmallVideo.prototype.getIndicatorSpan = function(id) {
  405. var indicatorSpan;
  406. var spans = $(`#${this.videoSpanId}>[id=${id}`);
  407. if (spans.length <= 0) {
  408. indicatorSpan = document.createElement('span');
  409. indicatorSpan.id = id;
  410. indicatorSpan.className = "indicator";
  411. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  412. } else {
  413. indicatorSpan = spans[0];
  414. }
  415. return indicatorSpan;
  416. };
  417. export default SmallVideo;