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

SmallVideo.js 13KB

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