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

SmallVideo.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. * Selects the HTML image element which displays user's avatar.
  280. *
  281. * @return {jQuery|HTMLElement} a jQuery selector pointing to the HTML image
  282. * element which displays the user's avatar.
  283. */
  284. SmallVideo.prototype.$avatar = function () {
  285. return $('#' + this.videoSpanId + ' .userAvatar');
  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. * Checks whether the user associated with this <tt>SmallVideo</tt> is currently
  308. * being displayed on the "large video".
  309. *
  310. * @return {boolean} <tt>true</tt> if the user is displayed on the large video
  311. * or <tt>false</tt> otherwise.
  312. */
  313. SmallVideo.prototype.isCurrentlyOnLargeVideo = function () {
  314. return this.VideoLayout.isCurrentlyOnLarge(this.id);
  315. };
  316. /**
  317. * Hides or shows the user's avatar.
  318. * This update assumes that large video had been updated and we will
  319. * reflect it on this small video.
  320. *
  321. * @param show whether we should show the avatar or not
  322. * video because there is no dominant speaker and no focused speaker
  323. */
  324. SmallVideo.prototype.updateView = function () {
  325. if (!this.hasAvatar) {
  326. if (this.id) {
  327. // Init avatar
  328. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  329. } else {
  330. console.error("Unable to init avatar - no id", this);
  331. return;
  332. }
  333. }
  334. let video = this.selectVideoElement();
  335. let avatar = this.$avatar;
  336. var isCurrentlyOnLarge = this.isCurrentlyOnLargeVideo();
  337. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  338. var showAvatar;
  339. if ((!this.isLocal
  340. && !this.VideoLayout.isInLastN(this.id))
  341. || this.isVideoMuted) {
  342. showAvatar = true;
  343. } else {
  344. // We want to show the avatar when the video is muted or not exists
  345. // that is when 'true' or 'null' is returned
  346. showAvatar = !this.videoStream || this.videoStream.isMuted();
  347. }
  348. showAvatar = showAvatar && !isCurrentlyOnLarge;
  349. if (video && video.length > 0) {
  350. setVisibility(video, showVideo);
  351. }
  352. setVisibility(avatar, showAvatar);
  353. };
  354. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  355. var thumbnail = $('#' + this.videoSpanId);
  356. var avatarSel = this.$avatar();
  357. this.hasAvatar = true;
  358. // set the avatar in the thumbnail
  359. if (avatarSel && avatarSel.length > 0) {
  360. avatarSel[0].src = avatarUrl;
  361. } else {
  362. if (thumbnail && thumbnail.length > 0) {
  363. var avatarElement = document.createElement('img');
  364. avatarElement.className = 'userAvatar';
  365. avatarElement.src = avatarUrl;
  366. thumbnail.append(avatarElement);
  367. }
  368. }
  369. };
  370. /**
  371. * Shows or hides the dominant speaker indicator.
  372. * @param show whether to show or hide.
  373. */
  374. SmallVideo.prototype.showDominantSpeakerIndicator = function (show) {
  375. if (!this.container) {
  376. console.warn( "Unable to set dominant speaker indicator - "
  377. + this.videoSpanId + " does not exist");
  378. return;
  379. }
  380. var indicatorSpanId = "dominantspeakerindicator";
  381. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  382. indicatorSpan.innerHTML
  383. = "<i id='indicatoricon' class='fa fa-bullhorn'></i>";
  384. // adds a tooltip
  385. UIUtil.setTooltip(indicatorSpan, "speaker", "top");
  386. APP.translation.translateElement($(indicatorSpan));
  387. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  388. };
  389. /**
  390. * Shows or hides the raised hand indicator.
  391. * @param show whether to show or hide.
  392. */
  393. SmallVideo.prototype.showRaisedHandIndicator = function (show) {
  394. if (!this.container) {
  395. console.warn( "Unable to raised hand indication - "
  396. + this.videoSpanId + " does not exist");
  397. return;
  398. }
  399. var indicatorSpanId = "raisehandindicator";
  400. var indicatorSpan = this.getIndicatorSpan(indicatorSpanId);
  401. indicatorSpan.innerHTML
  402. = "<i id='indicatoricon' class='icon-raised-hand'></i>";
  403. // adds a tooltip
  404. UIUtil.setTooltip(indicatorSpan, "raisedHand", "top");
  405. APP.translation.translateElement($(indicatorSpan));
  406. $(indicatorSpan).css("visibility", show ? "visible" : "hidden");
  407. };
  408. /**
  409. * Gets (creating if necessary) the "indicator" span for this SmallVideo
  410. identified by an ID.
  411. */
  412. SmallVideo.prototype.getIndicatorSpan = function(id) {
  413. var indicatorSpan;
  414. var spans = $(`#${this.videoSpanId}>[id=${id}`);
  415. if (spans.length <= 0) {
  416. indicatorSpan = document.createElement('span');
  417. indicatorSpan.id = id;
  418. indicatorSpan.className = "indicator";
  419. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  420. } else {
  421. indicatorSpan = spans[0];
  422. }
  423. return indicatorSpan;
  424. };
  425. /**
  426. * Adds a listener for onresize events for this video, which will monitor for
  427. * resolution changes, will calculate the delay since the moment the listened
  428. * is added, and will fire a RESOLUTION_CHANGED event.
  429. */
  430. SmallVideo.prototype.waitForResolutionChange = function() {
  431. let self = this;
  432. let beforeChange = window.performance.now();
  433. let videos = this.selectVideoElement();
  434. if (!videos || !videos.length || videos.length <= 0)
  435. return;
  436. let video = videos[0];
  437. let oldWidth = video.videoWidth;
  438. let oldHeight = video.videoHeight;
  439. video.onresize = (event) => {
  440. if (video.videoWidth != oldWidth || video.videoHeight != oldHeight) {
  441. // Only run once.
  442. video.onresize = null;
  443. let delay = window.performance.now() - beforeChange;
  444. let emitter = self.VideoLayout.getEventEmitter();
  445. if (emitter) {
  446. emitter.emit(
  447. UIEvents.RESOLUTION_CHANGED,
  448. self.getId(),
  449. oldWidth + "x" + oldHeight,
  450. video.videoWidth + "x" + video.videoHeight,
  451. delay);
  452. }
  453. }
  454. };
  455. };
  456. /**
  457. * Initalizes any browser specific properties. Currently sets the overflow
  458. * property for Qt browsers on Windows to hidden, thus fixing the following
  459. * problem:
  460. * Some browsers don't have full support of the object-fit property for the
  461. * video element and when we set video object-fit to "cover" the video
  462. * actually overflows the boundaries of its container, so it's important
  463. * to indicate that the "overflow" should be hidden.
  464. *
  465. * Setting this property for all browsers will result in broken audio levels,
  466. * which makes this a temporary solution, before reworking audio levels.
  467. */
  468. SmallVideo.prototype.initBrowserSpecificProperties = function() {
  469. var userAgent = window.navigator.userAgent;
  470. if (userAgent.indexOf("QtWebEngine") > -1
  471. && (userAgent.indexOf("Windows") > -1
  472. || userAgent.indexOf("Linux") > -1)) {
  473. $('#' + this.videoSpanId).css("overflow", "hidden");
  474. }
  475. };
  476. export default SmallVideo;