您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SmallVideo.js 13KB

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