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

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