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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* global $, APP, JitsiMeetJS, interfaceConfig */
  2. import Avatar from "../avatar/Avatar";
  3. import UIUtil from "../util/UIUtil";
  4. import UIEvents from "../../../service/UI/UIEvents";
  5. const RTCUIHelper = JitsiMeetJS.util.RTCUIHelper;
  6. function SmallVideo(VideoLayout) {
  7. this.isAudioMuted = 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. /**
  20. * Returns the identifier of this small video.
  21. *
  22. * @returns the identifier of this small video
  23. */
  24. SmallVideo.prototype.getId = function () {
  25. return this.id;
  26. };
  27. /* Indicates if this small video is currently visible.
  28. *
  29. * @return <tt>true</tt> if this small video isn't currently visible and
  30. * <tt>false</tt> - otherwise.
  31. */
  32. SmallVideo.prototype.isVisible = function () {
  33. return $('#' + this.videoSpanId).is(':visible');
  34. };
  35. SmallVideo.prototype.showDisplayName = function(isShow) {
  36. var nameSpan = $('#' + this.videoSpanId + ' .displayname').get(0);
  37. if (isShow) {
  38. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  39. nameSpan.setAttribute("style", "display:inline-block;");
  40. }
  41. else {
  42. if (nameSpan)
  43. nameSpan.setAttribute("style", "display:none;");
  44. }
  45. };
  46. /**
  47. * Enables / disables the device availability icons for this small video.
  48. * @param {enable} set to {true} to enable and {false} to disable
  49. */
  50. SmallVideo.prototype.enableDeviceAvailabilityIcons = function (enable) {
  51. if (typeof enable === "undefined")
  52. return;
  53. this.deviceAvailabilityIconsEnabled = enable;
  54. };
  55. /**
  56. * Sets the device "non" availability icons.
  57. * @param devices the devices, which will be checked for availability
  58. */
  59. SmallVideo.prototype.setDeviceAvailabilityIcons = function (devices) {
  60. if (!this.deviceAvailabilityIconsEnabled)
  61. return;
  62. if(!this.container)
  63. return;
  64. var noMic = $("#" + this.videoSpanId + " > .noMic");
  65. var noVideo = $("#" + this.videoSpanId + " > .noVideo");
  66. noMic.remove();
  67. noVideo.remove();
  68. if (!devices.audio) {
  69. this.container.appendChild(
  70. document.createElement("div")).setAttribute("class", "noMic");
  71. }
  72. if (!devices.video) {
  73. this.container.appendChild(
  74. document.createElement("div")).setAttribute("class", "noVideo");
  75. }
  76. if (!devices.audio && !devices.video) {
  77. noMic.css("background-position", "75%");
  78. noVideo.css("background-position", "25%");
  79. noVideo.css("background-color", "transparent");
  80. }
  81. };
  82. /**
  83. * Sets the type of the video displayed by this instance.
  84. * @param videoType 'camera' or 'desktop'
  85. */
  86. SmallVideo.prototype.setVideoType = function (videoType) {
  87. this.videoType = videoType;
  88. };
  89. /**
  90. * Returns the type of the video displayed by this instance.
  91. * @returns {String} 'camera', 'screen' or undefined.
  92. */
  93. SmallVideo.prototype.getVideoType = function () {
  94. return this.videoType;
  95. };
  96. /**
  97. * Shows the presence status message for the given video.
  98. */
  99. SmallVideo.prototype.setPresenceStatus = function (statusMsg) {
  100. if (!this.container) {
  101. // No container
  102. return;
  103. }
  104. var statusSpan = $('#' + this.videoSpanId + '>span.status');
  105. if (!statusSpan.length) {
  106. //Add status span
  107. statusSpan = document.createElement('span');
  108. statusSpan.className = 'status';
  109. statusSpan.id = this.videoSpanId + '_status';
  110. $('#' + this.videoSpanId)[0].appendChild(statusSpan);
  111. statusSpan = $('#' + this.videoSpanId + '>span.status');
  112. }
  113. // Display status
  114. if (statusMsg && statusMsg.length) {
  115. $('#' + this.videoSpanId + '_status').text(statusMsg);
  116. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  117. }
  118. else {
  119. // Hide
  120. statusSpan.get(0).setAttribute("style", "display:none;");
  121. }
  122. };
  123. /**
  124. * Creates an audio or video element for a particular MediaStream.
  125. */
  126. SmallVideo.createStreamElement = function (stream) {
  127. let isVideo = stream.isVideoTrack();
  128. let element = isVideo
  129. ? document.createElement('video')
  130. : document.createElement('audio');
  131. if (isVideo) {
  132. element.setAttribute("muted", "true");
  133. }
  134. RTCUIHelper.setAutoPlay(element, true);
  135. element.id = SmallVideo.getStreamElementID(stream);
  136. return element;
  137. };
  138. /**
  139. * Returns the element id for a particular MediaStream.
  140. */
  141. SmallVideo.getStreamElementID = function (stream) {
  142. let isVideo = stream.isVideoTrack();
  143. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  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 / hides the audio muted indicator over small videos.
  161. *
  162. * @param {boolean} isMuted indicates if the muted element should be shown
  163. * or hidden
  164. */
  165. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  166. var audioMutedIndicator = this.getAudioMutedIndicator();
  167. if (!isMuted) {
  168. audioMutedIndicator.hide();
  169. }
  170. else {
  171. audioMutedIndicator.show();
  172. }
  173. this.isAudioMuted = isMuted;
  174. };
  175. /**
  176. * Returns the audio muted indicator jquery object. If it doesn't exists -
  177. * creates it.
  178. *
  179. * @returns {jQuery|HTMLElement} the audio muted indicator
  180. */
  181. SmallVideo.prototype.getAudioMutedIndicator = function () {
  182. var audioMutedSpan = $('#' + this.videoSpanId + ' .audioMuted');
  183. if (audioMutedSpan.length) {
  184. return audioMutedSpan;
  185. }
  186. audioMutedSpan = document.createElement('span');
  187. audioMutedSpan.className = 'audioMuted toolbar-icon';
  188. UIUtil.setTooltip(audioMutedSpan,
  189. "videothumbnail.mute",
  190. "top");
  191. this.container
  192. .querySelector('.videocontainer__toolbar')
  193. .appendChild(audioMutedSpan);
  194. var mutedIndicator = document.createElement('i');
  195. mutedIndicator.className = 'icon-mic-disabled';
  196. audioMutedSpan.appendChild(mutedIndicator);
  197. return $('#' + this.videoSpanId + ' .audioMuted');
  198. };
  199. /**
  200. * Shows video muted indicator over small videos and disables/enables avatar
  201. * if video muted.
  202. *
  203. * @param {boolean} isMuted indicates if we should set the view to muted view
  204. * or not
  205. */
  206. SmallVideo.prototype.setMutedView = function(isMuted) {
  207. this.isVideoMuted = isMuted;
  208. this.updateView();
  209. var videoMutedSpan = this.getVideoMutedIndicator();
  210. videoMutedSpan[isMuted ? 'show' : 'hide']();
  211. };
  212. /**
  213. * Returns the video muted indicator jquery object. If it doesn't exists -
  214. * creates it.
  215. *
  216. * @returns {jQuery|HTMLElement} the video muted indicator
  217. */
  218. SmallVideo.prototype.getVideoMutedIndicator = function () {
  219. var videoMutedSpan = $('#' + this.videoSpanId + ' .videoMuted');
  220. if (videoMutedSpan.length) {
  221. return videoMutedSpan;
  222. }
  223. videoMutedSpan = document.createElement('span');
  224. videoMutedSpan.className = 'videoMuted toolbar-icon';
  225. this.container
  226. .querySelector('.videocontainer__toolbar')
  227. .appendChild(videoMutedSpan);
  228. var mutedIndicator = document.createElement('i');
  229. mutedIndicator.className = 'icon-camera-disabled';
  230. UIUtil.setTooltip(mutedIndicator,
  231. "videothumbnail.videomute",
  232. "top");
  233. videoMutedSpan.appendChild(mutedIndicator);
  234. return $('#' + this.videoSpanId + ' .videoMuted');
  235. };
  236. /**
  237. * Creates the element indicating the moderator(owner) of the conference.
  238. */
  239. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  240. // don't create moderator indicator if DISABLE_FOCUS_INDICATOR is true
  241. if (interfaceConfig.DISABLE_FOCUS_INDICATOR) return false;
  242. // Show moderator indicator
  243. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  244. if (indicatorSpan.length) {
  245. return;
  246. }
  247. indicatorSpan = document.createElement('span');
  248. indicatorSpan.className = 'focusindicator toolbar-icon right';
  249. this.container
  250. .querySelector('.videocontainer__toolbar')
  251. .appendChild(indicatorSpan);
  252. var moderatorIndicator = document.createElement('i');
  253. moderatorIndicator.className = 'icon-star';
  254. UIUtil.setTooltip(moderatorIndicator,
  255. "videothumbnail.moderator",
  256. "top-left");
  257. indicatorSpan.appendChild(moderatorIndicator);
  258. };
  259. /**
  260. * Removes the element indicating the moderator(owner) of the conference.
  261. */
  262. SmallVideo.prototype.removeModeratorIndicatorElement = function () {
  263. $('#' + this.videoSpanId + ' .focusindicator').remove();
  264. };
  265. /**
  266. * This is an especially interesting function. A naive reader might think that
  267. * it returns this SmallVideo's "video" element. But it is much more exciting.
  268. * It first finds this video's parent element using jquery, then uses a utility
  269. * from lib-jitsi-meet to extract the video element from it (with two more
  270. * jquery calls), and finally uses jquery again to encapsulate the video element
  271. * in an array. This last step allows (some might prefer "forces") users of
  272. * this function to access the video element via the 0th element of the returned
  273. * array (after checking its length of course!).
  274. */
  275. SmallVideo.prototype.selectVideoElement = function () {
  276. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  277. };
  278. /**
  279. * Enables / disables the css responsible for focusing/pinning a video
  280. * thumbnail.
  281. *
  282. * @param isFocused indicates if the thumbnail should be focused/pinned or not
  283. */
  284. SmallVideo.prototype.focus = function(isFocused) {
  285. var focusedCssClass = "videoContainerFocused";
  286. var isFocusClassEnabled = $(this.container).hasClass(focusedCssClass);
  287. if (!isFocused && isFocusClassEnabled) {
  288. $(this.container).removeClass(focusedCssClass);
  289. }
  290. else if (isFocused && !isFocusClassEnabled) {
  291. $(this.container).addClass(focusedCssClass);
  292. }
  293. };
  294. SmallVideo.prototype.hasVideo = function () {
  295. return this.selectVideoElement().length !== 0;
  296. };
  297. /**
  298. * Hides or shows the user's avatar.
  299. * This update assumes that large video had been updated and we will
  300. * reflect it on this small video.
  301. *
  302. * @param show whether we should show the avatar or not
  303. * video because there is no dominant speaker and no focused speaker
  304. */
  305. SmallVideo.prototype.updateView = function () {
  306. if (!this.hasAvatar) {
  307. if (this.id) {
  308. // Init avatar
  309. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  310. } else {
  311. console.error("Unable to init avatar - no id", this);
  312. return;
  313. }
  314. }
  315. let video = this.selectVideoElement();
  316. let avatar = $('#' + this.videoSpanId + ' .userAvatar');
  317. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  318. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  319. var showAvatar;
  320. if ((!this.isLocal
  321. && !this.VideoLayout.isInLastN(this.id))
  322. || this.isVideoMuted) {
  323. showAvatar = true;
  324. } else {
  325. // We want to show the avatar when the video is muted or not exists
  326. // that is when 'true' or 'null' is returned
  327. showAvatar = !this.videoStream || this.videoStream.isMuted();
  328. }
  329. showAvatar = showAvatar && !isCurrentlyOnLarge;
  330. if (video && video.length > 0) {
  331. setVisibility(video, showVideo);
  332. }
  333. setVisibility(avatar, showAvatar);
  334. };
  335. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  336. var thumbnail = $('#' + this.videoSpanId);
  337. var avatar = $('#' + this.videoSpanId + ' .userAvatar');
  338. this.hasAvatar = true;
  339. // set the avatar in the thumbnail
  340. if (avatar && avatar.length > 0) {
  341. avatar[0].src = avatarUrl;
  342. } else {
  343. if (thumbnail && thumbnail.length > 0) {
  344. avatar = document.createElement('img');
  345. avatar.className = 'userAvatar';
  346. avatar.src = avatarUrl;
  347. thumbnail.append(avatar);
  348. }
  349. }
  350. };
  351. /**
  352. * Shows or hides the dominant speaker indicator.
  353. * @param show whether to show or hide.
  354. */
  355. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  356. if (!this.container) {
  357. console.warn( "Unable to set dominant speaker indicator - "
  358. + this.videoSpanId + " does not exist");
  359. return;
  360. }
  361. var indicatorSpanId = "dominantspeakerindicator";
  362. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  363. indicatorSpan.innerHTML
  364. = "<i id='indicatoricon' class='fa fa-bullhorn'></i>";
  365. // adds a tooltip
  366. UIUtil.setTooltip(indicatorSpan, "speaker", "top");
  367. APP.translation.translateElement($(indicatorSpan));
  368. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  369. };
  370. /**
  371. * Shows or hides the raised hand indicator.
  372. * @param show whether to show or hide.
  373. */
  374. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  375. if (!this.container) {
  376. console.warn( "Unable to raised hand indication - "
  377. + this.videoSpanId + " does not exist");
  378. return;
  379. }
  380. var indicatorSpanId = "raisehandindicator";
  381. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  382. indicatorSpan.innerHTML
  383. = "<i id='indicatoricon' class='icon-raised-hand'></i>";
  384. // adds a tooltip
  385. UIUtil.setTooltip(indicatorSpan, "raisedHand", "top");
  386. APP.translation.translateElement($(indicatorSpan));
  387. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  388. };
  389. /**
  390. * Gets (creating if necessary) the "indicator" span for this SmallVideo
  391. identified by an ID.
  392. */
  393. SmallVideo.prototype.getIndicatorSpan = function(id) {
  394. var indicatorSpan;
  395. var spans = $(`#${this.videoSpanId}>[id=${id}`);
  396. if (spans.length <= 0) {
  397. indicatorSpan = document.createElement('span');
  398. indicatorSpan.id = id;
  399. indicatorSpan.className = "indicator";
  400. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  401. } else {
  402. indicatorSpan = spans[0];
  403. }
  404. return indicatorSpan;
  405. };
  406. /**
  407. * Adds a listener for onresize events for this video, which will monitor for
  408. * resolution changes, will calculate the delay since the moment the listened
  409. * is added, and will fire a RESOLUTION_CHANGED event.
  410. */
  411. SmallVideo.prototype.waitForResolutionChange = function() {
  412. let self = this;
  413. let beforeChange = window.performance.now();
  414. let videos = this.selectVideoElement();
  415. if (!videos || !videos.length || videos.length <= 0)
  416. return;
  417. let video = videos[0];
  418. let oldWidth = video.videoWidth;
  419. let oldHeight = video.videoHeight;
  420. video.onresize = (event) => {
  421. if (video.videoWidth != oldWidth || video.videoHeight != oldHeight) {
  422. // Only run once.
  423. video.onresize = null;
  424. let delay = window.performance.now() - beforeChange;
  425. let emitter = self.VideoLayout.getEventEmitter();
  426. if (emitter) {
  427. emitter.emit(
  428. UIEvents.RESOLUTION_CHANGED,
  429. self.getId(),
  430. oldWidth + "x" + oldHeight,
  431. video.videoWidth + "x" + video.videoHeight,
  432. delay);
  433. }
  434. }
  435. };
  436. };
  437. /**
  438. * Initalizes any browser specific properties. Currently sets the overflow
  439. * property for Qt browsers on Windows to hidden, thus fixing the following
  440. * problem:
  441. * Some browsers don't have full support of the object-fit property for the
  442. * video element and when we set video object-fit to "cover" the video
  443. * actually overflows the boundaries of its container, so it's important
  444. * to indicate that the "overflow" should be hidden.
  445. *
  446. * Setting this property for all browsers will result in broken audio levels,
  447. * which makes this a temporary solution, before reworking audio levels.
  448. */
  449. SmallVideo.prototype.initBrowserSpecificProperties = function() {
  450. var userAgent = window.navigator.userAgent;
  451. if (userAgent.indexOf("QtWebEngine") > -1
  452. && (userAgent.indexOf("Windows") > -1
  453. || userAgent.indexOf("Linux") > -1)) {
  454. $('#' + this.videoSpanId).css("overflow", "hidden");
  455. }
  456. };
  457. export default SmallVideo;