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

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