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

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