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

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