Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SmallVideo.js 12KB

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