You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SmallVideo.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.oncontextmenu = function () { return false; };
  102. return element;
  103. };
  104. /**
  105. * Configures hoverIn/hoverOut handlers.
  106. */
  107. SmallVideo.prototype.bindHoverHandler = function () {
  108. // Add hover handler
  109. var self = this;
  110. $(this.container).hover(
  111. function () {
  112. self.showDisplayName(true);
  113. },
  114. function () {
  115. // If the video has been "pinned" by the user we want to
  116. // keep the display name on place.
  117. if (!LargeVideo.isLargeVideoVisible() ||
  118. !LargeVideo.isCurrentlyOnLarge(self.getResourceJid()))
  119. self.showDisplayName(false);
  120. }
  121. );
  122. };
  123. /**
  124. * Updates the data for the indicator
  125. * @param id the id of the indicator
  126. * @param percent the percent for connection quality
  127. * @param object the data
  128. */
  129. SmallVideo.prototype.updateStatsIndicator = function (percent, object) {
  130. if(this.connectionIndicator)
  131. this.connectionIndicator.updateConnectionQuality(percent, object);
  132. };
  133. SmallVideo.prototype.hideIndicator = function () {
  134. if(this.connectionIndicator)
  135. this.connectionIndicator.hideIndicator();
  136. };
  137. /**
  138. * Shows audio muted indicator over small videos.
  139. * @param {string} isMuted
  140. */
  141. SmallVideo.prototype.showAudioIndicator = function(isMuted) {
  142. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  143. if (!isMuted) {
  144. if (audioMutedSpan.length > 0) {
  145. audioMutedSpan.popover('hide');
  146. audioMutedSpan.remove();
  147. }
  148. }
  149. else {
  150. if (!audioMutedSpan.length) {
  151. audioMutedSpan = document.createElement('span');
  152. audioMutedSpan.className = 'audioMuted';
  153. UIUtil.setTooltip(audioMutedSpan,
  154. "videothumbnail.mute",
  155. "top");
  156. this.container.appendChild(audioMutedSpan);
  157. APP.translation.translateElement($('#' + this.videoSpanId + " > span"));
  158. var mutedIndicator = document.createElement('i');
  159. mutedIndicator.className = 'icon-mic-disabled';
  160. audioMutedSpan.appendChild(mutedIndicator);
  161. }
  162. this.updateIconPositions();
  163. }
  164. this.isMuted = isMuted;
  165. };
  166. /**
  167. * Shows video muted indicator over small videos.
  168. */
  169. SmallVideo.prototype.showVideoIndicator = function(isMuted) {
  170. this.showAvatar(isMuted);
  171. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  172. if (isMuted === false) {
  173. if (videoMutedSpan.length > 0) {
  174. videoMutedSpan.remove();
  175. }
  176. }
  177. else {
  178. if (!videoMutedSpan.length) {
  179. videoMutedSpan = document.createElement('span');
  180. videoMutedSpan.className = 'videoMuted';
  181. this.container.appendChild(videoMutedSpan);
  182. var mutedIndicator = document.createElement('i');
  183. mutedIndicator.className = 'icon-camera-disabled';
  184. UIUtil.setTooltip(mutedIndicator,
  185. "videothumbnail.videomute",
  186. "top");
  187. videoMutedSpan.appendChild(mutedIndicator);
  188. //translate texts for muted indicator
  189. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  190. }
  191. this.updateIconPositions();
  192. }
  193. };
  194. SmallVideo.prototype.enableDominantSpeaker = function (isEnable) {
  195. var resourceJid = this.getResourceJid();
  196. var displayName = resourceJid;
  197. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  198. if (nameSpan.length > 0)
  199. displayName = nameSpan.html();
  200. console.log("UI enable dominant speaker",
  201. displayName,
  202. resourceJid,
  203. isEnable);
  204. if (!this.container) {
  205. return;
  206. }
  207. if (isEnable) {
  208. this.showDisplayName(LargeVideo.getState() === "video");
  209. if (!this.container.classList.contains("dominantspeaker"))
  210. this.container.classList.add("dominantspeaker");
  211. }
  212. else {
  213. this.showDisplayName(false);
  214. if (this.container.classList.contains("dominantspeaker"))
  215. this.container.classList.remove("dominantspeaker");
  216. }
  217. this.showAvatar();
  218. };
  219. SmallVideo.prototype.updateIconPositions = function () {
  220. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  221. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  222. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  223. if(connectionIndicator.length > 0 &&
  224. connectionIndicator[0].style.display != "none") {
  225. audioMutedSpan.css({right: "23px"});
  226. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  227. } else {
  228. audioMutedSpan.css({right: "0px"});
  229. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  230. }
  231. };
  232. /**
  233. * Creates the element indicating the moderator(owner) of the conference.
  234. *
  235. * @param parentElement the parent element where the owner indicator will
  236. * be added
  237. */
  238. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  239. // Show moderator indicator
  240. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  241. if (!indicatorSpan || indicatorSpan.length === 0) {
  242. indicatorSpan = document.createElement('span');
  243. indicatorSpan.className = 'focusindicator';
  244. this.container.appendChild(indicatorSpan);
  245. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  246. }
  247. if (indicatorSpan.children().length !== 0)
  248. return;
  249. var moderatorIndicator = document.createElement('i');
  250. moderatorIndicator.className = 'fa fa-star';
  251. indicatorSpan[0].appendChild(moderatorIndicator);
  252. UIUtil.setTooltip(indicatorSpan[0],
  253. "videothumbnail.moderator",
  254. "top");
  255. //translates text in focus indicators
  256. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  257. };
  258. SmallVideo.prototype.selectVideoElement = function () {
  259. var videoElem = APP.RTC.getVideoElementName();
  260. if (!RTCBrowserType.isTemasysPluginUsed()) {
  261. return $('#' + this.videoSpanId).find(videoElem);
  262. } else {
  263. return $('#' + this.videoSpanId +
  264. (this.isLocal ? '>>' : '>') +
  265. videoElem + '>param[value="video"]').parent();
  266. }
  267. };
  268. SmallVideo.prototype.getSrc = function () {
  269. var videoElement = this.selectVideoElement().get(0);
  270. return APP.RTC.getVideoSrc(videoElement);
  271. };
  272. SmallVideo.prototype.focus = function(isFocused) {
  273. if(!isFocused) {
  274. this.container.classList.remove("videoContainerFocused");
  275. } else {
  276. this.container.classList.add("videoContainerFocused");
  277. }
  278. };
  279. SmallVideo.prototype.hasVideo = function () {
  280. return this.selectVideoElement().length !== 0;
  281. };
  282. /**
  283. * Hides or shows the user's avatar
  284. * @param show whether we should show the avatar or not
  285. * video because there is no dominant speaker and no focused speaker
  286. */
  287. SmallVideo.prototype.showAvatar = function (show) {
  288. if (!this.hasAvatar) {
  289. if (this.peerJid) {
  290. // Init avatar
  291. this.avatarChanged(Avatar.getThumbUrl(this.peerJid));
  292. } else {
  293. console.error("Unable to init avatar - no peerjid", this);
  294. return;
  295. }
  296. }
  297. var resourceJid = this.getResourceJid();
  298. var video = this.selectVideoElement();
  299. var avatar = $('#avatar_' + resourceJid);
  300. if (show === undefined || show === null) {
  301. if (!this.isLocal &&
  302. !this.VideoLayout.isInLastN(resourceJid)) {
  303. show = true;
  304. } else {
  305. // We want to show the avatar when the video is muted or not exists
  306. // that is when 'true' or 'null' is returned
  307. show = APP.RTC.isVideoMuted(this.peerJid) !== false;
  308. }
  309. }
  310. if (LargeVideo.showAvatar(resourceJid, show)) {
  311. setVisibility(avatar, false);
  312. setVisibility(video, false);
  313. } else {
  314. if (video && video.length > 0) {
  315. setVisibility(video, !show);
  316. }
  317. setVisibility(avatar, show);
  318. }
  319. };
  320. SmallVideo.prototype.avatarChanged = function (thumbUrl) {
  321. var thumbnail = $('#' + this.videoSpanId);
  322. var resourceJid = this.getResourceJid();
  323. var avatar = $('#avatar_' + resourceJid);
  324. this.hasAvatar = true;
  325. // set the avatar in the thumbnail
  326. if (avatar && avatar.length > 0) {
  327. avatar[0].src = thumbUrl;
  328. } else {
  329. if (thumbnail && thumbnail.length > 0) {
  330. avatar = document.createElement('img');
  331. avatar.id = 'avatar_' + resourceJid;
  332. avatar.className = 'userAvatar';
  333. avatar.src = thumbUrl;
  334. thumbnail.append(avatar);
  335. }
  336. }
  337. };
  338. module.exports = SmallVideo;