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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* global $, APP, require */
  2. /* jshint -W101 */
  3. import Avatar from "../avatar/Avatar";
  4. import UIUtil from "../util/UIUtil";
  5. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  6. function SmallVideo() {
  7. this.isMuted = false;
  8. this.hasAvatar = false;
  9. this.isVideoMuted = 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 'desktop'
  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 (!self.VideoLayout.isLargeVideoVisible() ||
  126. !self.VideoLayout.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 and disables/enables avatar
  176. * if video muted.
  177. */
  178. SmallVideo.prototype.setMutedView = function(isMuted) {
  179. this.isVideoMuted = isMuted;
  180. this.updateView();
  181. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  182. if (isMuted === false) {
  183. if (videoMutedSpan.length > 0) {
  184. videoMutedSpan.remove();
  185. }
  186. }
  187. else {
  188. if (!videoMutedSpan.length) {
  189. videoMutedSpan = document.createElement('span');
  190. videoMutedSpan.className = 'videoMuted';
  191. this.container.appendChild(videoMutedSpan);
  192. var mutedIndicator = document.createElement('i');
  193. mutedIndicator.className = 'icon-camera-disabled';
  194. UIUtil.setTooltip(mutedIndicator,
  195. "videothumbnail.videomute",
  196. "top");
  197. videoMutedSpan.appendChild(mutedIndicator);
  198. //translate texts for muted indicator
  199. APP.translation.translateElement($('#' + this.videoSpanId + " > span > i"));
  200. }
  201. this.updateIconPositions();
  202. }
  203. };
  204. SmallVideo.prototype.updateIconPositions = function () {
  205. var audioMutedSpan = $('#' + this.videoSpanId + '>span.audioMuted');
  206. var connectionIndicator = $('#' + this.videoSpanId + '>div.connectionindicator');
  207. var videoMutedSpan = $('#' + this.videoSpanId + '>span.videoMuted');
  208. if(connectionIndicator.length > 0 &&
  209. connectionIndicator[0].style.display != "none") {
  210. audioMutedSpan.css({right: "23px"});
  211. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  212. } else {
  213. audioMutedSpan.css({right: "0px"});
  214. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  215. }
  216. };
  217. /**
  218. * Creates the element indicating the moderator(owner) of the conference.
  219. *
  220. * @param parentElement the parent element where the owner indicator will
  221. * be added
  222. */
  223. SmallVideo.prototype.createModeratorIndicatorElement = function () {
  224. // Show moderator indicator
  225. var indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  226. if (!indicatorSpan || indicatorSpan.length === 0) {
  227. indicatorSpan = document.createElement('span');
  228. indicatorSpan.className = 'focusindicator';
  229. this.container.appendChild(indicatorSpan);
  230. indicatorSpan = $('#' + this.videoSpanId + ' .focusindicator');
  231. }
  232. if (indicatorSpan.children().length !== 0)
  233. return;
  234. var moderatorIndicator = document.createElement('i');
  235. moderatorIndicator.className = 'fa fa-star';
  236. indicatorSpan[0].appendChild(moderatorIndicator);
  237. UIUtil.setTooltip(indicatorSpan[0],
  238. "videothumbnail.moderator",
  239. "top");
  240. //translates text in focus indicators
  241. APP.translation.translateElement($('#' + this.videoSpanId + ' .focusindicator'));
  242. };
  243. SmallVideo.prototype.selectVideoElement = function () {
  244. var videoElemName;
  245. if (!RTCBrowserType.isTemasysPluginUsed()) {
  246. videoElemName = 'video';
  247. return $('#' + this.videoSpanId).find(videoElemName);
  248. } else {
  249. videoElemName = 'object';
  250. var matching = $('#' + this.videoSpanId +
  251. (this.isLocal ? '>>' : '>') +
  252. videoElemName + '>param[value="video"]');
  253. if (matching.length < 2) {
  254. return matching.parent();
  255. }
  256. // there are 2 video objects from FF
  257. // object with id which ends with '_default' (like 'remoteVideo_default')
  258. // doesn't contain video, so we ignore it
  259. for (var i = 0; i < matching.length; i += 1) {
  260. var el = matching[i].parentNode;
  261. // check id suffix
  262. if (el.id.substr(-8) !== '_default') {
  263. return $(el);
  264. }
  265. }
  266. return $([]);
  267. }
  268. };
  269. SmallVideo.prototype.focus = function(isFocused) {
  270. if(!isFocused) {
  271. this.container.classList.remove("videoContainerFocused");
  272. } else {
  273. this.container.classList.add("videoContainerFocused");
  274. }
  275. };
  276. SmallVideo.prototype.hasVideo = function () {
  277. return this.selectVideoElement().length !== 0;
  278. };
  279. /**
  280. * Hides or shows the user's avatar.
  281. * This update assumes that large video had been updated and we will
  282. * reflect it on this small video.
  283. *
  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.updateView = function () {
  288. if (!this.hasAvatar) {
  289. if (this.id) {
  290. // Init avatar
  291. this.avatarChanged(Avatar.getAvatarUrl(this.id));
  292. } else {
  293. console.error("Unable to init avatar - no id", this);
  294. return;
  295. }
  296. }
  297. let video = this.selectVideoElement();
  298. let avatar = $(`#avatar_${this.id}`);
  299. var isCurrentlyOnLarge = this.VideoLayout.isCurrentlyOnLarge(this.id);
  300. var showVideo = !this.isVideoMuted && !isCurrentlyOnLarge;
  301. var showAvatar;
  302. if ((!this.isLocal
  303. && !this.VideoLayout.isInLastN(this.id))
  304. || this.isVideoMuted) {
  305. showAvatar = true;
  306. } else {
  307. // We want to show the avatar when the video is muted or not exists
  308. // that is when 'true' or 'null' is returned
  309. showAvatar = !this.stream || this.stream.isMuted();
  310. }
  311. showAvatar = showAvatar && !isCurrentlyOnLarge;
  312. if (video && video.length > 0) {
  313. setVisibility(video, showVideo);
  314. }
  315. setVisibility(avatar, showAvatar);
  316. var showDisplayName = !showVideo && !showAvatar;
  317. if (showDisplayName) {
  318. this.showDisplayName(this.VideoLayout.isLargeVideoVisible());
  319. }
  320. else {
  321. this.showDisplayName(false);
  322. }
  323. };
  324. SmallVideo.prototype.avatarChanged = function (avatarUrl) {
  325. var thumbnail = $('#' + this.videoSpanId);
  326. var avatar = $('#avatar_' + this.id);
  327. this.hasAvatar = true;
  328. // set the avatar in the thumbnail
  329. if (avatar && avatar.length > 0) {
  330. avatar[0].src = avatarUrl;
  331. } else {
  332. if (thumbnail && thumbnail.length > 0) {
  333. avatar = document.createElement('img');
  334. avatar.id = 'avatar_' + this.id;
  335. avatar.className = 'userAvatar';
  336. avatar.src = avatarUrl;
  337. thumbnail.append(avatar);
  338. }
  339. }
  340. };
  341. export default SmallVideo;