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

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