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

SmallVideo.js 19KB

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