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

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