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

SmallVideo.js 18KB

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