Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SmallVideo.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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.performanceTimes[type + ".render"]
  139. = window.performance.now();
  140. console.log("(TIME) Render " + type + ":\t",
  141. now);
  142. };
  143. element.oncontextmenu = function () { return false; };
  144. return element;
  145. };
  146. /**
  147. * Returns the element id for a particular MediaStream.
  148. */
  149. SmallVideo.getStreamElementID = function (stream) {
  150. let isVideo = stream.isVideoTrack();
  151. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  152. };
  153. /**
  154. * Configures hoverIn/hoverOut handlers.
  155. */
  156. SmallVideo.prototype.bindHoverHandler = function () {
  157. // Add hover handler
  158. var self = this;
  159. $(this.container).hover(
  160. function () {
  161. self.showDisplayName(true);
  162. },
  163. function () {
  164. // If the video has been "pinned" by the user we want to
  165. // keep the display name on place.
  166. if (!self.VideoLayout.isLargeVideoVisible() ||
  167. !self.VideoLayout.isCurrentlyOnLarge(self.id))
  168. self.showDisplayName(false);
  169. }
  170. );
  171. };
  172. /**
  173. * Updates the data for the indicator
  174. * @param id the id of the indicator
  175. * @param percent the percent for connection quality
  176. * @param object the data
  177. */
  178. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  179. if(this.connectionIndicator)
  180. this.connectionIndicator.updateConnectionQuality(percent, object);
  181. };
  182. SmallVideo.prototype.hideIndicator = function () {
  183. if(this.connectionIndicator)
  184. this.connectionIndicator.hideIndicator();
  185. };
  186. /**
  187. * Shows audio muted indicator over small videos.
  188. * @param {string} isMuted
  189. */
  190. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  191. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  192. if (!isMuted) {
  193. if (audioMutedSpan.length > 0) {
  194. audioMutedSpan.popover('hide');
  195. audioMutedSpan.remove();
  196. }
  197. }
  198. else {
  199. if (!audioMutedSpan.length) {
  200. audioMutedSpan = document.createElement('span');
  201. audioMutedSpan.className = 'audioMuted';
  202. UIUtil.setTooltip(audioMutedSpan,
  203. "videothumbnail.mute",
  204. "top");
  205. this.container.appendChild(audioMutedSpan);
  206. APP.translation.translateElement($('#' + this.videoSpanId + " > span"));
  207. var mutedIndicator = document.createElement('i');
  208. mutedIndicator.className = 'icon-mic-disabled';
  209. audioMutedSpan.appendChild(mutedIndicator);
  210. }
  211. this.updateIconPositions();
  212. }
  213. this.isMuted = isMuted;
  214. };
  215. /**
  216. * Shows video muted indicator over small videos and disables/enables avatar
  217. * if video muted.
  218. */
  219. SmallVideo.prototype.setMutedView = function(isMuted) {
  220. this.isVideoMuted = isMuted;
  221. this.updateView();
  222. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  223. if (isMuted === false) {
  224. if (videoMutedSpan.length > 0) {
  225. videoMutedSpan.remove();
  226. }
  227. }
  228. else {
  229. if (!videoMutedSpan.length) {
  230. videoMutedSpan = document.createElement('span');
  231. videoMutedSpan.className = 'videoMuted';
  232. this.container.appendChild(videoMutedSpan);
  233. var mutedIndicator = document.createElement('i');
  234. mutedIndicator.className = 'icon-camera-disabled';
  235. UIUtil.setTooltip(mutedIndicator,
  236. "videothumbnail.videomute",
  237. "top");
  238. videoMutedSpan.appendChild(mutedIndicator);
  239. //translate texts for muted indicator
  240. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  241. }
  242. this.updateIconPositions();
  243. }
  244. };
  245. SmallVideo.prototype.updateIconPositions = function () {
  246. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  247. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  248. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  249. if(connectionIndicator.length > 0 &&
  250. connectionIndicator[0].style.display != "none") {
  251. audioMutedSpan.css({right: "23px"});
  252. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  253. } else {
  254. audioMutedSpan.css({right: "0px"});
  255. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  256. }
  257. };
  258. /**
  259. * Creates the element indicating the moderator(owner) of the conference.
  260. *
  261. * @param parentElement the parent element where the owner indicator will
  262. * be added
  263. */
  264. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  265. // Show moderator indicator
  266. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  267. if (!indicatorSpan || indicatorSpan.length === 0) {
  268. indicatorSpan = document.createElement('span');
  269. indicatorSpan.className = 'focusindicator';
  270. this.container.appendChild(indicatorSpan);
  271. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  272. }
  273. if (indicatorSpan.children().length !== 0)
  274. return;
  275. var moderatorIndicator = document.createElement('i');
  276. moderatorIndicator.className = 'fa fa-star';
  277. indicatorSpan[0].appendChild(moderatorIndicator);
  278. UIUtil.setTooltip(indicatorSpan[0],
  279. "videothumbnail.moderator",
  280. "top");
  281. //translates text in focus indicators
  282. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  283. };
  284. SmallVideo.prototype.selectVideoElement = function () {
  285. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  286. };
  287. /**
  288. * Enables / disables the css responsible for focusing/pinning a video
  289. * thumbnail.
  290. *
  291. * @param isFocused indicates if the thumbnail should be focused/pinned or not
  292. */
  293. SmallVideo.prototype.focus = function(isFocused) {
  294. var focusedCssClass = "videoContainerFocused";
  295. var isFocusClassEnabled = $(this.container).hasClass(focusedCssClass);
  296. if (!isFocused && isFocusClassEnabled) {
  297. $(this.container).removeClass(focusedCssClass);
  298. }
  299. else if (isFocused && !isFocusClassEnabled) {
  300. $(this.container).addClass(focusedCssClass);
  301. }
  302. };
  303. SmallVideo.prototype.hasVideo = function () {
  304. return this.selectVideoElement().length !== 0;
  305. };
  306. /**
  307. * Hides or shows the user's avatar.
  308. * This update assumes that large video had been updated and we will
  309. * reflect it on this small video.
  310. *
  311. * @param show whether we should show the avatar or not
  312. * video because there is no dominant speaker and no focused speaker
  313. */
  314. SmallVideo.prototype.updateView = function () {
  315. if (!this.hasAvatar) {
  316. if (this.id) {
  317. // Init avatar
  318. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  319. } else {
  320. console.error("Unable to init avatar - no id", this);
  321. return;
  322. }
  323. }
  324. let video = this.selectVideoElement();
  325. let avatar = $(`#avatar_${this.id}`);
  326. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  327. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  328. var showAvatar;
  329. if ((!this.isLocal
  330. && !this.VideoLayout.isInLastN(this.id))
  331. || this.isVideoMuted) {
  332. showAvatar = true;
  333. } else {
  334. // We want to show the avatar when the video is muted or not exists
  335. // that is when 'true' or 'null' is returned
  336. showAvatar = !this.videoStream || this.videoStream.isMuted();
  337. }
  338. showAvatar = showAvatar && !isCurrentlyOnLarge;
  339. if (video && video.length > 0) {
  340. setVisibility(video, showVideo);
  341. }
  342. setVisibility(avatar, showAvatar);
  343. this.showDisplayName(!showVideo && !showAvatar);
  344. };
  345. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  346. var thumbnail = $('#' + this.videoSpanId);
  347. var avatar = $('#avatar_' + this.id);
  348. this.hasAvatar = true;
  349. // set the avatar in the thumbnail
  350. if (avatar && avatar.length > 0) {
  351. avatar[0].src = avatarUrl;
  352. } else {
  353. if (thumbnail && thumbnail.length > 0) {
  354. avatar = document.createElement('img');
  355. avatar.id = 'avatar_' + this.id;
  356. avatar.className = 'userAvatar';
  357. avatar.src = avatarUrl;
  358. thumbnail.append(avatar);
  359. }
  360. }
  361. };
  362. /**
  363. * Updates the Indicator for dominant speaker.
  364. *
  365. * @param isSpeaker indicates the current indicator state
  366. */
  367. SmallVideo.prototype.updateDominantSpeakerIndicator = function (isSpeaker) {
  368. if (!this.container) {
  369. console.warn( "Unable to set dominant speaker indicator - "
  370. + this.videoSpanId + " does not exist");
  371. return;
  372. }
  373. var indicatorSpan
  374. = $('#' + this.videoSpanId + '>span.dominantspeakerindicator');
  375. // If we do not have an indicator for this video.
  376. if (indicatorSpan.length <= 0) {
  377. indicatorSpan = document.createElement('span');
  378. indicatorSpan.innerHTML
  379. = "<i id='speakerindicatoricon' class='fa fa-bullhorn'></i>";
  380. indicatorSpan.className = 'dominantspeakerindicator';
  381. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  382. // adds a tooltip
  383. UIUtil.setTooltip(indicatorSpan, "speaker", "left");
  384. APP.translation.translateElement($(indicatorSpan));
  385. }
  386. $(indicatorSpan).css("visibility", isSpeaker ? "visible" : "hidden");
  387. };
  388. export default SmallVideo;