Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SmallVideo.js 11KB

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