選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SmallVideo.js 17KB

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