Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SmallVideo.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. return element;
  138. };
  139. /**
  140. * Returns the element id for a particular MediaStream.
  141. */
  142. SmallVideo.getStreamElementID = function (stream) {
  143. let isVideo = stream.isVideoTrack();
  144. return (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  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.remove();
  169. this.updateIconPositions();
  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
  181. .translateElement($('#' + this.videoSpanId + " > span"));
  182. var mutedIndicator = document.createElement('i');
  183. mutedIndicator.className = 'icon-mic-disabled';
  184. audioMutedSpan.appendChild(mutedIndicator);
  185. }
  186. this.updateIconPositions();
  187. }
  188. this.isMuted = isMuted;
  189. };
  190. /**
  191. * Shows video muted indicator over small videos and disables/enables avatar
  192. * if video muted.
  193. */
  194. SmallVideo.prototype.setMutedView = function(isMuted) {
  195. this.isVideoMuted = isMuted;
  196. this.updateView();
  197. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  198. if (isMuted === false) {
  199. if (videoMutedSpan.length > 0) {
  200. videoMutedSpan.remove();
  201. this.updateIconPositions();
  202. }
  203. }
  204. else {
  205. if (!videoMutedSpan.length) {
  206. videoMutedSpan = document.createElement('span');
  207. videoMutedSpan.className = 'videoMuted';
  208. this.container.appendChild(videoMutedSpan);
  209. var mutedIndicator = document.createElement('i');
  210. mutedIndicator.className = 'icon-camera-disabled';
  211. UIUtil.setTooltip(mutedIndicator,
  212. "videothumbnail.videomute",
  213. "top");
  214. videoMutedSpan.appendChild(mutedIndicator);
  215. //translate texts for muted indicator
  216. APP.translation
  217. .translateElement($('#' + this.videoSpanId + " > span > i"));
  218. }
  219. this.updateIconPositions();
  220. }
  221. };
  222. SmallVideo.prototype.updateIconPositions = function () {
  223. let audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  224. let videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  225. audioMutedSpan.css({left: "0px"});
  226. videoMutedSpan.css({left: (audioMutedSpan.length > 0? 25 : 0) + "px"});
  227. var connectionIndicator
  228. = $('#' + this.videoSpanId + '>div.connectionindicator');
  229. if(connectionIndicator.length > 0 &&
  230. connectionIndicator[0].style.display != "none") {
  231. audioMutedSpan.css({right: "23px"});
  232. videoMutedSpan.css({right:
  233. ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  234. } else {
  235. audioMutedSpan.css({right: "0px"});
  236. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  237. }
  238. };
  239. /**
  240. * Creates the element indicating the moderator(owner) of the conference.
  241. */
  242. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  243. // Show moderator indicator
  244. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  245. if (!indicatorSpan || indicatorSpan.length === 0) {
  246. indicatorSpan = document.createElement('span');
  247. indicatorSpan.className = 'focusindicator';
  248. this.container.appendChild(indicatorSpan);
  249. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  250. }
  251. if (indicatorSpan.children().length !== 0)
  252. return;
  253. var moderatorIndicator = document.createElement('i');
  254. moderatorIndicator.className = 'icon-star';
  255. indicatorSpan[0].appendChild(moderatorIndicator);
  256. UIUtil.setTooltip(indicatorSpan[0],
  257. "videothumbnail.moderator",
  258. "top-left");
  259. //translates text in focus indicators
  260. APP.translation
  261. .translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  262. };
  263. /**
  264. * Removes the element indicating the moderator(owner) of the conference.
  265. */
  266. SmallVideo.prototype.removeModeratorIndicatorElement = function () {
  267. $('#' + this.videoSpanId + ' .focusindicator').remove();
  268. };
  269. /**
  270. * This is an especially interesting function. A naive reader might think that
  271. * it returns this SmallVideo's "video" element. But it is much more exciting.
  272. * It first finds this video's parent element using jquery, then uses a utility
  273. * from lib-jitsi-meet to extract the video element from it (with two more
  274. * jquery calls), and finally uses jquery again to encapsulate the video element
  275. * in an array. This last step allows (some might prefer "forces") users of
  276. * this function to access the video element via the 0th element of the returned
  277. * array (after checking its length of course!).
  278. */
  279. SmallVideo.prototype.selectVideoElement = function () {
  280. return $(RTCUIHelper.findVideoElement($('#' + this.videoSpanId)[0]));
  281. };
  282. /**
  283. * Enables / disables the css responsible for focusing/pinning a video
  284. * thumbnail.
  285. *
  286. * @param isFocused indicates if the thumbnail should be focused/pinned or not
  287. */
  288. SmallVideo.prototype.focus = function(isFocused) {
  289. var focusedCssClass = "videoContainerFocused";
  290. var isFocusClassEnabled = $(this.container).hasClass(focusedCssClass);
  291. if (!isFocused && isFocusClassEnabled) {
  292. $(this.container).removeClass(focusedCssClass);
  293. }
  294. else if (isFocused && !isFocusClassEnabled) {
  295. $(this.container).addClass(focusedCssClass);
  296. }
  297. };
  298. SmallVideo.prototype.hasVideo = function () {
  299. return this.selectVideoElement().length !== 0;
  300. };
  301. /**
  302. * Hides or shows the user's avatar.
  303. * This update assumes that large video had been updated and we will
  304. * reflect it on this small video.
  305. *
  306. * @param show whether we should show the avatar or not
  307. * video because there is no dominant speaker and no focused speaker
  308. */
  309. SmallVideo.prototype.updateView = function () {
  310. if (!this.hasAvatar) {
  311. if (this.id) {
  312. // Init avatar
  313. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  314. } else {
  315. console.error("Unable to init avatar - no id", this);
  316. return;
  317. }
  318. }
  319. let video = this.selectVideoElement();
  320. let avatar = $('#' + this.videoSpanId + ' .userAvatar');
  321. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  322. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  323. var showAvatar;
  324. if ((!this.isLocal
  325. && !this.VideoLayout.isInLastN(this.id))
  326. || this.isVideoMuted) {
  327. showAvatar = true;
  328. } else {
  329. // We want to show the avatar when the video is muted or not exists
  330. // that is when 'true' or 'null' is returned
  331. showAvatar = !this.videoStream || this.videoStream.isMuted();
  332. }
  333. showAvatar = showAvatar && !isCurrentlyOnLarge;
  334. if (video && video.length > 0) {
  335. setVisibility(video, showVideo);
  336. }
  337. setVisibility(avatar, showAvatar);
  338. };
  339. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  340. var thumbnail = $('#' + this.videoSpanId);
  341. var avatar = $('#' + this.videoSpanId + ' .userAvatar');
  342. this.hasAvatar = true;
  343. // set the avatar in the thumbnail
  344. if (avatar && avatar.length > 0) {
  345. avatar[0].src = avatarUrl;
  346. } else {
  347. if (thumbnail && thumbnail.length > 0) {
  348. avatar = document.createElement('img');
  349. avatar.className = 'userAvatar';
  350. avatar.src = avatarUrl;
  351. thumbnail.append(avatar);
  352. }
  353. }
  354. };
  355. /**
  356. * Shows or hides the dominant speaker indicator.
  357. * @param show whether to show or hide.
  358. */
  359. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  360. if (!this.container) {
  361. console.warn( "Unable to set dominant speaker indicator - "
  362. + this.videoSpanId + " does not exist");
  363. return;
  364. }
  365. var indicatorSpanId = "dominantspeakerindicator";
  366. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  367. indicatorSpan.innerHTML
  368. = "<i id='indicatoricon' class='fa fa-bullhorn'></i>";
  369. // adds a tooltip
  370. UIUtil.setTooltip(indicatorSpan, "speaker", "left");
  371. APP.translation.translateElement($(indicatorSpan));
  372. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  373. };
  374. /**
  375. * Shows or hides the raised hand indicator.
  376. * @param show whether to show or hide.
  377. */
  378. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  379. if (!this.container) {
  380. console.warn( "Unable to raised hand indication - "
  381. + this.videoSpanId + " does not exist");
  382. return;
  383. }
  384. var indicatorSpanId = "raisehandindicator";
  385. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  386. indicatorSpan.innerHTML
  387. = "<i id='indicatoricon' class='icon-raised-hand'></i>";
  388. // adds a tooltip
  389. UIUtil.setTooltip(indicatorSpan, "raisedHand", "left");
  390. APP.translation.translateElement($(indicatorSpan));
  391. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  392. };
  393. /**
  394. * Gets (creating if necessary) the "indicator" span for this SmallVideo
  395. identified by an ID.
  396. */
  397. SmallVideo.prototype.getIndicatorSpan = function(id) {
  398. var indicatorSpan;
  399. var spans = $(`#${this.videoSpanId}>[id=${id}`);
  400. if (spans.length <= 0) {
  401. indicatorSpan = document.createElement('span');
  402. indicatorSpan.id = id;
  403. indicatorSpan.className = "indicator";
  404. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  405. } else {
  406. indicatorSpan = spans[0];
  407. }
  408. return indicatorSpan;
  409. };
  410. /**
  411. * Adds a listener for onresize events for this video, which will monitor for
  412. * resolution changes, will calculate the delay since the moment the listened
  413. * is added, and will fire a RESOLUTION_CHANGED event.
  414. */
  415. SmallVideo.prototype.waitForResolutionChange = function() {
  416. let self = this;
  417. let beforeChange = window.performance.now();
  418. let videos = this.selectVideoElement();
  419. if (!videos || !videos.length || videos.length <= 0)
  420. return;
  421. let video = videos[0];
  422. let oldWidth = video.videoWidth;
  423. let oldHeight = video.videoHeight;
  424. video.onresize = (event) => {
  425. if (video.videoWidth != oldWidth || video.videoHeight != oldHeight) {
  426. // Only run once.
  427. video.onresize = null;
  428. let delay = window.performance.now() - beforeChange;
  429. let emitter = self.VideoLayout.getEventEmitter();
  430. if (emitter) {
  431. emitter.emit(
  432. UIEvents.RESOLUTION_CHANGED,
  433. self.getId(),
  434. oldWidth + "x" + oldHeight,
  435. video.videoWidth + "x" + video.videoHeight,
  436. delay);
  437. }
  438. }
  439. };
  440. };
  441. /**
  442. * Initalizes any browser specific properties. Currently sets the overflow
  443. * property for Qt browsers on Windows to hidden, thus fixing the following
  444. * problem:
  445. * Some browsers don't have full support of the object-fit property for the
  446. * video element and when we set video object-fit to "cover" the video
  447. * actually overflows the boundaries of its container, so it's important
  448. * to indicate that the "overflow" should be hidden.
  449. *
  450. * Setting this property for all browsers will result in broken audio levels,
  451. * which makes this a temporary solution, before reworking audio levels.
  452. */
  453. SmallVideo.prototype.initBrowserSpecificProperties = function() {
  454. var userAgent = window.navigator.userAgent;
  455. if (userAgent.indexOf("QtWebEngine") > -1
  456. && (userAgent.indexOf("Windows") > -1
  457. || userAgent.indexOf("Linux") > -1)) {
  458. $('#' + this.videoSpanId).css("overflow", "hidden");
  459. }
  460. };
  461. export default SmallVideo;