您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SmallVideo.js 12KB

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