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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* global $, APP, require */
  2. /* jshint -W101 */
  3. import Avatar from "../avatar/Avatar";
  4. import UIUtil from "../util/UIUtil";
  5. import LargeVideo from "./LargeVideo";
  6. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  7. function SmallVideo() {
  8. this.isMuted = false;
  9. this.hasAvatar = false;
  10. this.stream = null;
  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. let isVideo = stream.isVideoTrack();
  95. let element = isVideo
  96. ? document.createElement('video')
  97. : document.createElement('audio');
  98. if (isVideo) {
  99. element.setAttribute("muted", "true");
  100. }
  101. if (!RTCBrowserType.isIExplorer()) {
  102. element.autoplay = true;
  103. }
  104. element.id = (isVideo ? 'remoteVideo_' : 'remoteAudio_') + stream.getId();
  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.id))
  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 displayName = this.id;
  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. this.id,
  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. return $('#' + this.videoSpanId).find(videoElem);
  267. // FIXME maybe move this to the library?
  268. var videoElem = APP.RTC.getVideoElementName();
  269. if (!RTCBrowserType.isTemasysPluginUsed()) {
  270. return $('#' + this.videoSpanId).find(videoElem);
  271. } else {
  272. var matching = $('#' + this.videoSpanId +
  273. (this.isLocal ? '>>' : '>') +
  274. videoElem + '>param[value="video"]');
  275. if (matching.length < 2) {
  276. return matching.parent();
  277. }
  278. // there are 2 video objects from FF
  279. // object with id which ends with '_default' (like 'remoteVideo_default')
  280. // doesn't contain video, so we ignore it
  281. for (var i = 0; i < matching.length; i += 1) {
  282. var el = matching[i].parentNode;
  283. // check id suffix
  284. if (el.id.substr(-8) !== '_default') {
  285. return $(el);
  286. }
  287. }
  288. return $([]);
  289. }
  290. };
  291. SmallVideo.prototype.getSrc = function () {
  292. var videoElement = this.selectVideoElement().get(0);
  293. return APP.RTC.getVideoSrc(videoElement);
  294. };
  295. SmallVideo.prototype.focus = function(isFocused) {
  296. if(!isFocused) {
  297. this.container.classList.remove("videoContainerFocused");
  298. } else {
  299. this.container.classList.add("videoContainerFocused");
  300. }
  301. };
  302. SmallVideo.prototype.hasVideo = function () {
  303. return this.selectVideoElement().length !== 0;
  304. };
  305. /**
  306. * Hides or shows the user's avatar
  307. * @param show whether we should show the avatar or not
  308. * video because there is no dominant speaker and no focused speaker
  309. */
  310. SmallVideo.prototype.showAvatar = function (show) {
  311. if (!this.hasAvatar) {
  312. if (this.id) {
  313. // Init avatar
  314. this.avatarChanged(Avatar.getThumbUrl(this.id));
  315. } else {
  316. console.error("Unable to init avatar - no id", this);
  317. return;
  318. }
  319. }
  320. let video = this.selectVideoElement();
  321. let avatar = $(`#avatar_${this.id}`);
  322. if (show === undefined || show === null) {
  323. if (!this.isLocal &&
  324. !this.VideoLayout.isInLastN(this.id)) {
  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 = !this.stream || this.stream.isMuted();
  330. }
  331. }
  332. if (LargeVideo.showAvatar(this.id, 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 avatar = $('#avatar_' + this.id);
  345. this.hasAvatar = true;
  346. // set the avatar in the thumbnail
  347. if (avatar && avatar.length > 0) {
  348. avatar[0].src = thumbUrl;
  349. } else {
  350. if (thumbnail && thumbnail.length > 0) {
  351. avatar = document.createElement('img');
  352. avatar.id = 'avatar_' + this.id;
  353. avatar.className = 'userAvatar';
  354. avatar.src = thumbUrl;
  355. thumbnail.append(avatar);
  356. }
  357. }
  358. };
  359. export default SmallVideo;