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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* global $, APP, JitsiMeetJS */
  2. /* jshint -W101 */
  3. import Avatar from "../avatar/Avatar";
  4. import UIUtil from "../util/UIUtil";
  5. import UIEvents from "../../../service/UI/UIEvents";
  6. const RTCUIHelper = JitsiMeetJS.util.RTCUIHelper;
  7. function SmallVideo(VideoLayout) {
  8. this.isMuted = false;
  9. this.hasAvatar = false;
  10. this.isVideoMuted = false;
  11. this.videoStream = null;
  12. this.audioStream = null;
  13. this.VideoLayout = VideoLayout;
  14. }
  15. function setVisibility(selector, show) {
  16. if (selector && selector.length > 0) {
  17. selector.css("visibility", show ? "visible" : "hidden");
  18. }
  19. }
  20. /**
  21. * Returns the identifier of this small video.
  22. *
  23. * @returns the identifier of this small video
  24. */
  25. SmallVideo.prototype.getId = function () {
  26. return this.id;
  27. };
  28. /* Indicates if this small video is currently visible.
  29. *
  30. * @return <tt>true</tt> if this small video isn't currently visible and
  31. * <tt>false</tt> - otherwise.
  32. */
  33. SmallVideo.prototype.isVisible = function () {
  34. return $('#' + this.videoSpanId).is(':visible');
  35. };
  36. SmallVideo.prototype.showDisplayName = function(isShow) {
  37. var nameSpan = $('#' + this.videoSpanId + '>span.displayname').get(0);
  38. if (isShow) {
  39. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  40. nameSpan.setAttribute("style", "display:inline-block;");
  41. }
  42. else {
  43. if (nameSpan)
  44. nameSpan.setAttribute("style", "display:none;");
  45. }
  46. };
  47. /**
  48. * Enables / disables the device availability icons for this small video.
  49. * @param {enable} set to {true} to enable and {false} to disable
  50. */
  51. SmallVideo.prototype.enableDeviceAvailabilityIcons = function (enable) {
  52. if (typeof enable === "undefined")
  53. return;
  54. this.deviceAvailabilityIconsEnabled = enable;
  55. };
  56. /**
  57. * Sets the device "non" availability icons.
  58. * @param devices the devices, which will be checked for availability
  59. */
  60. SmallVideo.prototype.setDeviceAvailabilityIcons = function (devices) {
  61. if (!this.deviceAvailabilityIconsEnabled)
  62. return;
  63. if(!this.container)
  64. return;
  65. var noMic = $("#" + this.videoSpanId + " > .noMic");
  66. var noVideo = $("#" + this.videoSpanId + " > .noVideo");
  67. noMic.remove();
  68. noVideo.remove();
  69. if (!devices.audio) {
  70. this.container.appendChild(
  71. document.createElement("div")).setAttribute("class", "noMic");
  72. }
  73. if (!devices.video) {
  74. this.container.appendChild(
  75. document.createElement("div")).setAttribute("class", "noVideo");
  76. }
  77. if (!devices.audio && !devices.video) {
  78. noMic.css("background-position", "75%");
  79. noVideo.css("background-position", "25%");
  80. noVideo.css("background-color", "transparent");
  81. }
  82. };
  83. /**
  84. * Sets the type of the video displayed by this instance.
  85. * @param videoType 'camera' or 'desktop'
  86. */
  87. SmallVideo.prototype.setVideoType = function (videoType) {
  88. this.videoType = videoType;
  89. };
  90. /**
  91. * Returns the type of the video displayed by this instance.
  92. * @returns {String} 'camera', 'screen' or undefined.
  93. */
  94. SmallVideo.prototype.getVideoType = function () {
  95. return this.videoType;
  96. };
  97. /**
  98. * Shows the presence status message for the given video.
  99. */
  100. SmallVideo.prototype.setPresenceStatus = function (statusMsg) {
  101. if (!this.container) {
  102. // No container
  103. return;
  104. }
  105. var statusSpan = $('#' + this.videoSpanId + '>span.status');
  106. if (!statusSpan.length) {
  107. //Add status span
  108. statusSpan = document.createElement('span');
  109. statusSpan.className = 'status';
  110. statusSpan.id = this.videoSpanId + '_status';
  111. $('#' + this.videoSpanId)[0].appendChild(statusSpan);
  112. statusSpan = $('#' + this.videoSpanId + '>span.status');
  113. }
  114. // Display status
  115. if (statusMsg && statusMsg.length) {
  116. $('#' + this.videoSpanId + '_status').text(statusMsg);
  117. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  118. }
  119. else {
  120. // Hide
  121. statusSpan.get(0).setAttribute("style", "display:none;");
  122. }
  123. };
  124. /**
  125. * Creates an audio or video element for a particular MediaStream.
  126. */
  127. SmallVideo.createStreamElement = function (stream) {
  128. let isVideo = stream.isVideoTrack();
  129. let element = isVideo
  130. ? document.createElement('video')
  131. : document.createElement('audio');
  132. if (isVideo) {
  133. element.setAttribute("muted", "true");
  134. }
  135. RTCUIHelper.setAutoPlay(element, true);
  136. element.id = SmallVideo.getStreamElementID(stream);
  137. element.onplay = function () {
  138. var type = (isVideo ? 'video' : 'audio');
  139. var now = APP.connectionTimes[type + ".render"]
  140. = window.performance.now();
  141. console.log("(TIME) Render " + type + ":\t",
  142. now);
  143. };
  144. return element;
  145. };
  146. /**
  147. * Returns the element id for a particular MediaStream.
  148. */
  149. SmallVideo.getStreamElementID = function (stream) {
  150. let isVideo = stream.isVideoTrack();
  151. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  152. };
  153. /**
  154. * Configures hoverIn/hoverOut handlers.
  155. */
  156. SmallVideo.prototype.bindHoverHandler = function () {
  157. // Add hover handler
  158. var self = this;
  159. $(this.container).hover(
  160. function () {
  161. self.showDisplayName(true);
  162. },
  163. function () {
  164. // If the video has been "pinned" by the user we want to
  165. // keep the display name on place.
  166. if (!self.VideoLayout.isLargeVideoVisible() ||
  167. !self.VideoLayout.isCurrentlyOnLarge(self.id))
  168. self.showDisplayName(false);
  169. }
  170. );
  171. };
  172. /**
  173. * Updates the data for the indicator
  174. * @param id the id of the indicator
  175. * @param percent the percent for connection quality
  176. * @param object the data
  177. */
  178. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  179. if(this.connectionIndicator)
  180. this.connectionIndicator.updateConnectionQuality(percent, object);
  181. };
  182. SmallVideo.prototype.hideIndicator = function () {
  183. if(this.connectionIndicator)
  184. this.connectionIndicator.hideIndicator();
  185. };
  186. /**
  187. * Shows audio muted indicator over small videos.
  188. * @param {string} isMuted
  189. */
  190. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  191. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  192. if (!isMuted) {
  193. if (audioMutedSpan.length > 0) {
  194. audioMutedSpan.popover('hide');
  195. audioMutedSpan.remove();
  196. }
  197. }
  198. else {
  199. if (!audioMutedSpan.length) {
  200. audioMutedSpan = document.createElement('span');
  201. audioMutedSpan.className = 'audioMuted';
  202. UIUtil.setTooltip(audioMutedSpan,
  203. "videothumbnail.mute",
  204. "top");
  205. this.container.appendChild(audioMutedSpan);
  206. APP.translation.translateElement($('#' + this.videoSpanId + " > span"));
  207. var mutedIndicator = document.createElement('i');
  208. mutedIndicator.className = 'icon-mic-disabled';
  209. audioMutedSpan.appendChild(mutedIndicator);
  210. }
  211. this.updateIconPositions();
  212. }
  213. this.isMuted = isMuted;
  214. };
  215. /**
  216. * Shows video muted indicator over small videos and disables/enables avatar
  217. * if video muted.
  218. */
  219. SmallVideo.prototype.setMutedView = function(isMuted) {
  220. this.isVideoMuted = isMuted;
  221. this.updateView();
  222. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  223. if (isMuted === false) {
  224. if (videoMutedSpan.length > 0) {
  225. videoMutedSpan.remove();
  226. }
  227. }
  228. else {
  229. if (!videoMutedSpan.length) {
  230. videoMutedSpan = document.createElement('span');
  231. videoMutedSpan.className = 'videoMuted';
  232. this.container.appendChild(videoMutedSpan);
  233. var mutedIndicator = document.createElement('i');
  234. mutedIndicator.className = 'icon-camera-disabled';
  235. UIUtil.setTooltip(mutedIndicator,
  236. "videothumbnail.videomute",
  237. "top");
  238. videoMutedSpan.appendChild(mutedIndicator);
  239. //translate texts for muted indicator
  240. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  241. }
  242. this.updateIconPositions();
  243. }
  244. };
  245. SmallVideo.prototype.updateIconPositions = function () {
  246. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  247. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  248. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  249. if(connectionIndicator.length > 0 &&
  250. connectionIndicator[0].style.display != "none") {
  251. audioMutedSpan.css({right: "23px"});
  252. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  253. } else {
  254. audioMutedSpan.css({right: "0px"});
  255. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  256. }
  257. };
  258. /**
  259. * Creates the element indicating the moderator(owner) of the conference.
  260. *
  261. * @param parentElement the parent element where the owner indicator will
  262. * be added
  263. */
  264. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  265. // Show moderator indicator
  266. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  267. if (!indicatorSpan || indicatorSpan.length === 0) {
  268. indicatorSpan = document.createElement('span');
  269. indicatorSpan.className = 'focusindicator';
  270. this.container.appendChild(indicatorSpan);
  271. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  272. }
  273. if (indicatorSpan.children().length !== 0)
  274. return;
  275. var moderatorIndicator = document.createElement('i');
  276. moderatorIndicator.className = 'fa fa-star';
  277. indicatorSpan[0].appendChild(moderatorIndicator);
  278. UIUtil.setTooltip(indicatorSpan[0],
  279. "videothumbnail.moderator",
  280. "top");
  281. //translates text in focus indicators
  282. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  283. };
  284. SmallVideo.prototype.selectVideoElement = function () {
  285. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  286. };
  287. /**
  288. * Enables / disables the css responsible for focusing/pinning a video
  289. * thumbnail.
  290. *
  291. * @param isFocused indicates if the thumbnail should be focused/pinned or not
  292. */
  293. SmallVideo.prototype.focus = function(isFocused) {
  294. var focusedCssClass = "videoContainerFocused";
  295. var isFocusClassEnabled = $(this.container).hasClass(focusedCssClass);
  296. if (!isFocused && isFocusClassEnabled) {
  297. $(this.container).removeClass(focusedCssClass);
  298. }
  299. else if (isFocused && !isFocusClassEnabled) {
  300. $(this.container).addClass(focusedCssClass);
  301. }
  302. };
  303. SmallVideo.prototype.hasVideo = function () {
  304. return this.selectVideoElement().length !== 0;
  305. };
  306. /**
  307. * Hides or shows the user's avatar.
  308. * This update assumes that large video had been updated and we will
  309. * reflect it on this small video.
  310. *
  311. * @param show whether we should show the avatar or not
  312. * video because there is no dominant speaker and no focused speaker
  313. */
  314. SmallVideo.prototype.updateView = function () {
  315. if (!this.hasAvatar) {
  316. if (this.id) {
  317. // Init avatar
  318. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  319. } else {
  320. console.error("Unable to init avatar - no id", this);
  321. return;
  322. }
  323. }
  324. let video = this.selectVideoElement();
  325. let avatar = $(`#avatar_${this.id}`);
  326. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  327. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  328. var showAvatar;
  329. if ((!this.isLocal
  330. && !this.VideoLayout.isInLastN(this.id))
  331. || this.isVideoMuted) {
  332. showAvatar = true;
  333. } else {
  334. // We want to show the avatar when the video is muted or not exists
  335. // that is when 'true' or 'null' is returned
  336. showAvatar = !this.videoStream || this.videoStream.isMuted();
  337. }
  338. showAvatar = showAvatar && !isCurrentlyOnLarge;
  339. if (video && video.length > 0) {
  340. setVisibility(video, showVideo);
  341. }
  342. setVisibility(avatar, showAvatar);
  343. this.showDisplayName(!showVideo && !showAvatar);
  344. };
  345. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  346. var thumbnail = $('#' + this.videoSpanId);
  347. var avatar = $('#avatar_' + this.id);
  348. this.hasAvatar = true;
  349. // set the avatar in the thumbnail
  350. if (avatar && avatar.length > 0) {
  351. avatar[0].src = avatarUrl;
  352. } else {
  353. if (thumbnail && thumbnail.length > 0) {
  354. avatar = document.createElement('img');
  355. avatar.id = 'avatar_' + this.id;
  356. avatar.className = 'userAvatar';
  357. avatar.src = avatarUrl;
  358. thumbnail.append(avatar);
  359. }
  360. }
  361. };
  362. /**
  363. * Shows or hides the dominant speaker indicator.
  364. * @param show whether to show or hide.
  365. */
  366. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  367. if (!this.container) {
  368. console.warn( "Unable to set dominant speaker indicator - "
  369. + this.videoSpanId + " does not exist");
  370. return;
  371. }
  372. var indicatorSpanId = "dominantspeakerindicator";
  373. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  374. indicatorSpan.innerHTML
  375. = "<i id='indicatoricon' class='fa fa-bullhorn'></i>";
  376. // adds a tooltip
  377. UIUtil.setTooltip(indicatorSpan, "speaker", "left");
  378. APP.translation.translateElement($(indicatorSpan));
  379. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  380. };
  381. /**
  382. * Shows or hides the raised hand indicator.
  383. * @param show whether to show or hide.
  384. */
  385. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  386. if (!this.container) {
  387. console.warn( "Unable to raised hand indication - "
  388. + this.videoSpanId + " does not exist");
  389. return;
  390. }
  391. var indicatorSpanId = "raisehandindicator";
  392. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  393. indicatorSpan.style.background = "#D6D61E";
  394. indicatorSpan.innerHTML
  395. = "<i id='indicatoricon' class='fa fa-hand-paper-o'></i>";
  396. // adds a tooltip
  397. UIUtil.setTooltip(indicatorSpan, "raisedHand", "left");
  398. APP.translation.translateElement($(indicatorSpan));
  399. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  400. };
  401. /**
  402. * Gets (creating if necessary) the "indicator" span for this SmallVideo
  403. identified by an ID.
  404. */
  405. SmallVideo.prototype.getIndicatorSpan = function(id) {
  406. var indicatorSpan;
  407. var spans = $(`#${this.videoSpanId}>[id=${id}`);
  408. if (spans.length <= 0) {
  409. indicatorSpan = document.createElement('span');
  410. indicatorSpan.id = id;
  411. indicatorSpan.className = "indicator";
  412. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  413. } else {
  414. indicatorSpan = spans[0];
  415. }
  416. return indicatorSpan;
  417. };
  418. /**
  419. * Adds a listener for onresize events for this video, which will monitor for
  420. * resolution changes, will calculate the delay since the moment the listened
  421. * is added, and will fire a RESOLUTION_CHANGED event.
  422. */
  423. SmallVideo.prototype.waitForResolutionChange = function() {
  424. let self = this;
  425. let beforeChange = window.performance.now();
  426. let videos = this.selectVideoElement();
  427. if (!videos || !videos.length || videos.length <= 0)
  428. return;
  429. let video = videos[0];
  430. let oldWidth = video.videoWidth;
  431. let oldHeight = video.videoHeight;
  432. video.onresize = (event) => {
  433. if (video.videoWidth != oldWidth || video.videoHeight != oldHeight) {
  434. // Only run once.
  435. video.onresize = null;
  436. let delay = window.performance.now() - beforeChange;
  437. let emitter = self.VideoLayout.getEventEmitter();
  438. if (emitter) {
  439. emitter.emit(
  440. UIEvents.RESOLUTION_CHANGED,
  441. self.getId(),
  442. oldWidth + "x" + oldHeight,
  443. video.videoWidth + "x" + video.videoHeight,
  444. delay);
  445. }
  446. }
  447. };
  448. };
  449. export default SmallVideo;