Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SmallVideo.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* global $, APP, require */
  2. /* jshint -W101 */
  3. var Avatar = require("../avatar/Avatar");
  4. var UIUtil = require("../util/UIUtil");
  5. var LargeVideo = require("./LargeVideo");
  6. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  7. function SmallVideo() {
  8. this.isMuted = false;
  9. this.hasAvatar = false;
  10. }
  11. function setVisibility(selector, show) {
  12. if (selector && selector.length > 0) {
  13. selector.css("visibility", show ? "visible" : "hidden");
  14. }
  15. }
  16. SmallVideo.prototype.showDisplayName = function(isShow) {
  17. var nameSpan = $('#' + this.videoSpanId + '>span.displayname').get(0);
  18. if (isShow) {
  19. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  20. nameSpan.setAttribute("style", "display:inline-block;");
  21. }
  22. else {
  23. if (nameSpan)
  24. nameSpan.setAttribute("style", "display:none;");
  25. }
  26. };
  27. SmallVideo.prototype.setDeviceAvailabilityIcons = function (devices) {
  28. if(!this.container)
  29. return;
  30. var noMic = $("#" + this.videoSpanId + " > .noMic");
  31. var noVideo = $("#" + this.videoSpanId + " > .noVideo");
  32. noMic.remove();
  33. noVideo.remove();
  34. if (!devices.audio) {
  35. this.container.appendChild(
  36. document.createElement("div")).setAttribute("class", "noMic");
  37. }
  38. if (!devices.video) {
  39. this.container.appendChild(
  40. document.createElement("div")).setAttribute("class", "noVideo");
  41. }
  42. if (!devices.audio && !devices.video) {
  43. noMic.css("background-position", "75%");
  44. noVideo.css("background-position", "25%");
  45. noVideo.css("background-color", "transparent");
  46. }
  47. };
  48. /**
  49. * Sets the type of the video displayed by this instance.
  50. * @param videoType 'camera' or 'screen'
  51. */
  52. SmallVideo.prototype.setVideoType = function (videoType) {
  53. this.videoType = videoType;
  54. };
  55. /**
  56. * Returns the type of the video displayed by this instance.
  57. * @returns {String} 'camera', 'screen' or undefined.
  58. */
  59. SmallVideo.prototype.getVideoType = function () {
  60. return this.videoType;
  61. };
  62. /**
  63. * Shows the presence status message for the given video.
  64. */
  65. SmallVideo.prototype.setPresenceStatus = function (statusMsg) {
  66. if (!this.container) {
  67. // No container
  68. return;
  69. }
  70. var statusSpan = $('#' + this.videoSpanId + '>span.status');
  71. if (!statusSpan.length) {
  72. //Add status span
  73. statusSpan = document.createElement('span');
  74. statusSpan.className = 'status';
  75. statusSpan.id = this.videoSpanId + '_status';
  76. $('#' + this.videoSpanId)[0].appendChild(statusSpan);
  77. statusSpan = $('#' + this.videoSpanId + '>span.status');
  78. }
  79. // Display status
  80. if (statusMsg && statusMsg.length) {
  81. $('#' + this.videoSpanId + '_status').text(statusMsg);
  82. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  83. }
  84. else {
  85. // Hide
  86. statusSpan.get(0).setAttribute("style", "display:none;");
  87. }
  88. };
  89. /**
  90. * Creates an audio or video element for a particular MediaStream.
  91. */
  92. SmallVideo.createStreamElement = function (stream) {
  93. var isVideo = stream.getVideoTracks().length > 0;
  94. var element = isVideo ? document.createElement('video')
  95. : document.createElement('audio');
  96. if (!RTCBrowserType.isIExplorer()) {
  97. element.autoplay = true;
  98. }
  99. element.id = (isVideo ? 'remoteVideo_' : 'remoteAudio_') +
  100. APP.RTC.getStreamID(stream);
  101. element.onplay = function() {
  102. console.log("(TIME) Render " + (isVideo ? 'video' : 'audio') + ":\t",
  103. window.performance.now());
  104. };
  105. element.oncontextmenu = function () { return false; };
  106. return element;
  107. };
  108. /**
  109. * Configures hoverIn/hoverOut handlers.
  110. */
  111. SmallVideo.prototype.bindHoverHandler = function () {
  112. // Add hover handler
  113. var self = this;
  114. $(this.container).hover(
  115. function () {
  116. self.showDisplayName(true);
  117. },
  118. function () {
  119. // If the video has been "pinned" by the user we want to
  120. // keep the display name on place.
  121. if (!LargeVideo.isLargeVideoVisible() ||
  122. !LargeVideo.isCurrentlyOnLarge(self.getResourceJid()))
  123. self.showDisplayName(false);
  124. }
  125. );
  126. };
  127. /**
  128. * Updates the data for the indicator
  129. * @param id the id of the indicator
  130. * @param percent the percent for connection quality
  131. * @param object the data
  132. */
  133. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  134. if(this.connectionIndicator)
  135. this.connectionIndicator.updateConnectionQuality(percent, object);
  136. };
  137. SmallVideo.prototype.hideIndicator = function () {
  138. if(this.connectionIndicator)
  139. this.connectionIndicator.hideIndicator();
  140. };
  141. /**
  142. * Shows audio muted indicator over small videos.
  143. * @param {string} isMuted
  144. */
  145. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  146. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  147. if (!isMuted) {
  148. if (audioMutedSpan.length > 0) {
  149. audioMutedSpan.popover('hide');
  150. audioMutedSpan.remove();
  151. }
  152. }
  153. else {
  154. if (!audioMutedSpan.length) {
  155. audioMutedSpan = document.createElement('span');
  156. audioMutedSpan.className = 'audioMuted';
  157. UIUtil.setTooltip(audioMutedSpan,
  158. "videothumbnail.mute",
  159. "top");
  160. this.container.appendChild(audioMutedSpan);
  161. APP.translation.translateElement($('#' + this.videoSpanId + " > span"));
  162. var mutedIndicator = document.createElement('i');
  163. mutedIndicator.className = 'icon-mic-disabled';
  164. audioMutedSpan.appendChild(mutedIndicator);
  165. }
  166. this.updateIconPositions();
  167. }
  168. this.isMuted = isMuted;
  169. };
  170. /**
  171. * Shows video muted indicator over small videos.
  172. */
  173. SmallVideo.prototype.showVideoIndicator = function(isMuted) {
  174. this.showAvatar(isMuted);
  175. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  176. if (isMuted === false) {
  177. if (videoMutedSpan.length > 0) {
  178. videoMutedSpan.remove();
  179. }
  180. }
  181. else {
  182. if (!videoMutedSpan.length) {
  183. videoMutedSpan = document.createElement('span');
  184. videoMutedSpan.className = 'videoMuted';
  185. this.container.appendChild(videoMutedSpan);
  186. var mutedIndicator = document.createElement('i');
  187. mutedIndicator.className = 'icon-camera-disabled';
  188. UIUtil.setTooltip(mutedIndicator,
  189. "videothumbnail.videomute",
  190. "top");
  191. videoMutedSpan.appendChild(mutedIndicator);
  192. //translate texts for muted indicator
  193. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  194. }
  195. this.updateIconPositions();
  196. }
  197. };
  198. SmallVideo.prototype.enableDominantSpeaker = function (isEnable) {
  199. var resourceJid = this.getResourceJid();
  200. var displayName = resourceJid;
  201. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  202. if (nameSpan.length > 0)
  203. displayName = nameSpan.html();
  204. console.log("UI enable dominant speaker",
  205. displayName,
  206. resourceJid,
  207. isEnable);
  208. if (!this.container) {
  209. return;
  210. }
  211. if (isEnable) {
  212. this.showDisplayName(LargeVideo.getState() === "video");
  213. if (!this.container.classList.contains("dominantspeaker"))
  214. this.container.classList.add("dominantspeaker");
  215. }
  216. else {
  217. this.showDisplayName(false);
  218. if (this.container.classList.contains("dominantspeaker"))
  219. this.container.classList.remove("dominantspeaker");
  220. }
  221. this.showAvatar();
  222. };
  223. SmallVideo.prototype.updateIconPositions = function () {
  224. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  225. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  226. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  227. if(connectionIndicator.length > 0 &&
  228. connectionIndicator[0].style.display != "none") {
  229. audioMutedSpan.css({right: "23px"});
  230. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  231. } else {
  232. audioMutedSpan.css({right: "0px"});
  233. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  234. }
  235. };
  236. /**
  237. * Creates the element indicating the moderator(owner) of the conference.
  238. *
  239. * @param parentElement the parent element where the owner indicator will
  240. * be added
  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 = 'fa fa-star';
  255. indicatorSpan[0].appendChild(moderatorIndicator);
  256. UIUtil.setTooltip(indicatorSpan[0],
  257. "videothumbnail.moderator",
  258. "top");
  259. //translates text in focus indicators
  260. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  261. };
  262. SmallVideo.prototype.selectVideoElement = function () {
  263. var videoElem = APP.RTC.getVideoElementName();
  264. if (!RTCBrowserType.isTemasysPluginUsed()) {
  265. return $('#' + this.videoSpanId).find(videoElem);
  266. } else {
  267. return $('#' + this.videoSpanId +
  268. (this.isLocal ? '>>' : '>') +
  269. videoElem + '>param[value="video"]').parent();
  270. }
  271. };
  272. SmallVideo.prototype.getSrc = function () {
  273. var videoElement = this.selectVideoElement().get(0);
  274. return APP.RTC.getVideoSrc(videoElement);
  275. };
  276. SmallVideo.prototype.focus = function(isFocused) {
  277. if(!isFocused) {
  278. this.container.classList.remove("videoContainerFocused");
  279. } else {
  280. this.container.classList.add("videoContainerFocused");
  281. }
  282. };
  283. SmallVideo.prototype.hasVideo = function () {
  284. return this.selectVideoElement().length !== 0;
  285. };
  286. /**
  287. * Hides or shows the user's avatar
  288. * @param show whether we should show the avatar or not
  289. * video because there is no dominant speaker and no focused speaker
  290. */
  291. SmallVideo.prototype.showAvatar = function (show) {
  292. if (!this.hasAvatar) {
  293. if (this.peerJid) {
  294. // Init avatar
  295. this.avatarChanged(Avatar.getThumbUrl(this.peerJid));
  296. } else {
  297. console.error("Unable to init avatar - no peerjid", this);
  298. return;
  299. }
  300. }
  301. var resourceJid = this.getResourceJid();
  302. var video = this.selectVideoElement();
  303. var avatar = $('#avatar_' + resourceJid);
  304. if (show === undefined || show === null) {
  305. if (!this.isLocal &&
  306. !this.VideoLayout.isInLastN(resourceJid)) {
  307. show = true;
  308. } else {
  309. // We want to show the avatar when the video is muted or not exists
  310. // that is when 'true' or 'null' is returned
  311. show = APP.RTC.isVideoMuted(this.peerJid) !== false;
  312. }
  313. }
  314. if (LargeVideo.showAvatar(resourceJid, show)) {
  315. setVisibility(avatar, false);
  316. setVisibility(video, false);
  317. } else {
  318. if (video && video.length > 0) {
  319. setVisibility(video, !show);
  320. }
  321. setVisibility(avatar, show);
  322. }
  323. };
  324. SmallVideo.prototype.avatarChanged = function (thumbUrl) {
  325. var thumbnail = $('#' + this.videoSpanId);
  326. var resourceJid = this.getResourceJid();
  327. var avatar = $('#avatar_' + resourceJid);
  328. this.hasAvatar = true;
  329. // set the avatar in the thumbnail
  330. if (avatar && avatar.length > 0) {
  331. avatar[0].src = thumbUrl;
  332. } else {
  333. if (thumbnail && thumbnail.length > 0) {
  334. avatar = document.createElement('img');
  335. avatar.id = 'avatar_' + resourceJid;
  336. avatar.className = 'userAvatar';
  337. avatar.src = thumbUrl;
  338. thumbnail.append(avatar);
  339. }
  340. }
  341. };
  342. module.exports = SmallVideo;