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.

RemoteVideo.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* global $, APP, interfaceConfig */
  2. import ConnectionIndicator from './ConnectionIndicator';
  3. import SmallVideo from "./SmallVideo";
  4. import AudioLevels from "../audio_levels/AudioLevels";
  5. import UIUtils from "../util/UIUtil";
  6. import UIEvents from '../../../service/UI/UIEvents';
  7. import JitsiPopover from "../util/JitsiPopover";
  8. function RemoteVideo(id, VideoLayout, emitter) {
  9. this.id = id;
  10. this.emitter = emitter;
  11. this.videoSpanId = `participant_${id}`;
  12. SmallVideo.call(this, VideoLayout);
  13. this.hasRemoteVideoMenu = false;
  14. this.addRemoteVideoContainer();
  15. this.connectionIndicator = new ConnectionIndicator(this, id);
  16. this.setDisplayName();
  17. this.bindHoverHandler();
  18. this.flipX = false;
  19. this.isLocal = false;
  20. this.isMuted = false;
  21. }
  22. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  23. RemoteVideo.prototype.constructor = RemoteVideo;
  24. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  25. this.container = RemoteVideo.createContainer(this.videoSpanId);
  26. this.initBrowserSpecificProperties();
  27. if (APP.conference.isModerator) {
  28. this.addRemoteVideoMenu();
  29. }
  30. let {thumbWidth, thumbHeight} = this.VideoLayout.resizeThumbnails();
  31. AudioLevels.updateAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  32. return this.container;
  33. };
  34. /**
  35. * Initializes the remote participant popup menu, by specifying previously
  36. * constructed popupMenuElement, containing all the menu items.
  37. *
  38. * @param popupMenuElement a pre-constructed element, containing the menu items
  39. * to display in the popup
  40. */
  41. RemoteVideo.prototype._initPopupMenu = function (popupMenuElement) {
  42. this.popover = new JitsiPopover(
  43. $("#" + this.videoSpanId + " > .remotevideomenu"),
  44. { content: popupMenuElement.outerHTML,
  45. skin: "black"});
  46. // override popover show method to make sure we will update the content
  47. // before showing the popover
  48. var origShowFunc = this.popover.show;
  49. this.popover.show = function () {
  50. // update content by forcing it, to finish even if popover
  51. // is not visible
  52. this.updateRemoteVideoMenu(this.isMuted, true);
  53. // call the original show, passing its actual this
  54. origShowFunc.call(this.popover);
  55. }.bind(this);
  56. };
  57. /**
  58. * Generates the popup menu content.
  59. *
  60. * @returns {Element|*} the constructed element, containing popup menu items
  61. * @private
  62. */
  63. RemoteVideo.prototype._generatePopupContent = function () {
  64. var popupmenuElement = document.createElement('ul');
  65. popupmenuElement.className = 'popupmenu';
  66. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  67. var muteMenuItem = document.createElement('li');
  68. var muteLinkItem = document.createElement('a');
  69. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  70. var doMuteHTML = mutedIndicator +
  71. " <div " +
  72. "data-i18n='videothumbnail.domute'>" +
  73. APP.translation.translateString("videothumbnail.domute") +
  74. "</div>";
  75. var mutedHTML = mutedIndicator +
  76. " <div " +
  77. "data-i18n='videothumbnail.muted'>" +
  78. APP.translation.translateString("videothumbnail.muted") +
  79. "</div>";
  80. muteLinkItem.id = "mutelink_" + this.id;
  81. if (this.isMuted) {
  82. muteLinkItem.innerHTML = mutedHTML;
  83. muteLinkItem.className = 'mutelink disabled';
  84. }
  85. else {
  86. muteLinkItem.innerHTML = doMuteHTML;
  87. muteLinkItem.className = 'mutelink';
  88. }
  89. // Delegate event to the document.
  90. $(document).on("click", "#mutelink_" + this.id, function(){
  91. if (this.isMuted)
  92. return;
  93. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  94. this.popover.forceHide();
  95. }.bind(this));
  96. muteMenuItem.appendChild(muteLinkItem);
  97. popupmenuElement.appendChild(muteMenuItem);
  98. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  99. var ejectMenuItem = document.createElement('li');
  100. var ejectLinkItem = document.createElement('a');
  101. var ejectText = "<div " +
  102. "data-i18n='videothumbnail.kick'>" +
  103. APP.translation.translateString("videothumbnail.kick") +
  104. "</div>";
  105. /**
  106. * TO FIX: Disabled kicks until they're fixed.
  107. * @type {string}
  108. */
  109. ejectLinkItem.className = 'ejectlink disabled';
  110. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  111. ejectLinkItem.id = "ejectlink_" + this.id;
  112. $(document).on("click", "#ejectlink_" + this.id, function(){
  113. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  114. this.popover.forceHide();
  115. }.bind(this));
  116. ejectMenuItem.appendChild(ejectLinkItem);
  117. popupmenuElement.appendChild(ejectMenuItem);
  118. return popupmenuElement;
  119. };
  120. /**
  121. * Updates the remote video menu.
  122. *
  123. * @param isMuted the new muted state to update to
  124. * @param force to work even if popover is not visible
  125. */
  126. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted, force) {
  127. this.isMuted = isMuted;
  128. // generate content, translate it and add it to document only if
  129. // popover is visible or we force to do so.
  130. if(this.popover.popoverShown || force) {
  131. this.popover.updateContent(this._generatePopupContent());
  132. }
  133. };
  134. /**
  135. * Adds the remote video menu element for the given <tt>id</tt> in the
  136. * given <tt>parentElement</tt>.
  137. *
  138. * @param id the id indicating the video for which we're adding a menu.
  139. * @param parentElement the parent element where this menu will be added
  140. */
  141. if (!interfaceConfig.filmStripOnly) {
  142. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  143. var spanElement = document.createElement('div');
  144. spanElement.className = 'remotevideomenu';
  145. this.container.appendChild(spanElement);
  146. var menuElement = document.createElement('i');
  147. menuElement.className = 'fa fa-angle-down';
  148. menuElement.title = 'Remote user controls';
  149. spanElement.appendChild(menuElement);
  150. this._initPopupMenu(this._generatePopupContent());
  151. this.hasRemoteVideoMenu = true;
  152. };
  153. } else {
  154. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  155. }
  156. /**
  157. * Removes the remote stream element corresponding to the given stream and
  158. * parent container.
  159. *
  160. * @param stream the MediaStream
  161. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  162. */
  163. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  164. if (!this.container)
  165. return false;
  166. var isVideo = stream.isVideoTrack();
  167. var elementID = SmallVideo.getStreamElementID(stream);
  168. var select = $('#' + elementID);
  169. select.remove();
  170. console.info((isVideo ? "Video" : "Audio") +
  171. " removed " + this.id, select);
  172. // when removing only the video element and we are on stage
  173. // update the stage
  174. if (isVideo && this.VideoLayout.isCurrentlyOnLarge(this.id))
  175. this.VideoLayout.updateLargeVideo(this.id);
  176. };
  177. /**
  178. * Removes RemoteVideo from the page.
  179. */
  180. RemoteVideo.prototype.remove = function () {
  181. console.log("Remove thumbnail", this.id);
  182. this.removeConnectionIndicator();
  183. // Make sure that the large video is updated if are removing its
  184. // corresponding small video.
  185. this.VideoLayout.updateAfterThumbRemoved(this.id);
  186. // Remove whole container
  187. if (this.container.parentNode) {
  188. this.container.parentNode.removeChild(this.container);
  189. }
  190. };
  191. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  192. var webRtcStream = stream.getOriginalStream();
  193. var isVideo = stream.isVideoTrack();
  194. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  195. return;
  196. }
  197. var self = this;
  198. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  199. // when video playback starts
  200. var onPlayingHandler = function () {
  201. self.VideoLayout.videoactive(streamElement, self.id);
  202. streamElement.onplaying = null;
  203. };
  204. streamElement.onplaying = onPlayingHandler;
  205. };
  206. /**
  207. * Checks whether or not video stream exists and has started for this
  208. * RemoteVideo instance. This is checked by trying to select video element in
  209. * this container and checking if 'currentTime' field's value is greater than 0.
  210. *
  211. * @returns {*|boolean} true if this RemoteVideo has active video stream running
  212. */
  213. RemoteVideo.prototype.hasVideoStarted = function () {
  214. var videoSelector = this.selectVideoElement();
  215. return videoSelector.length && videoSelector[0].currentTime > 0;
  216. };
  217. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  218. if (!this.container) {
  219. return;
  220. }
  221. let isVideo = stream.isVideoTrack();
  222. isVideo ? this.videoStream = stream : this.audioStream = stream;
  223. if (isVideo)
  224. this.setVideoType(stream.videoType);
  225. // Add click handler.
  226. let onClickHandler = (event) => {
  227. let source = event.target || event.srcElement;
  228. // ignore click if it was done in popup menu
  229. if ($(source).parents('.popupmenu').length === 0) {
  230. this.VideoLayout.handleVideoThumbClicked(this.id);
  231. }
  232. // On IE we need to populate this handler on video <object>
  233. // and it does not give event instance as an argument,
  234. // so we check here for methods.
  235. if (event.stopPropagation && event.preventDefault) {
  236. event.stopPropagation();
  237. event.preventDefault();
  238. }
  239. return false;
  240. };
  241. this.container.onclick = onClickHandler;
  242. if(!stream.getOriginalStream())
  243. return;
  244. let streamElement = SmallVideo.createStreamElement(stream);
  245. let newElementId = streamElement.id;
  246. // Put new stream element always in front
  247. UIUtils.prependChild(this.container, streamElement);
  248. // If we hide element when Temasys plugin is used then
  249. // we'll never receive 'onplay' event and other logic won't work as expected
  250. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  251. // calling attach will show it back
  252. $(streamElement).hide();
  253. // If the container is currently visible
  254. // we attach the stream to the element.
  255. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  256. this.waitForPlayback(streamElement, stream);
  257. streamElement = stream.attach(streamElement);
  258. }
  259. $(streamElement).click(onClickHandler);
  260. },
  261. /**
  262. * Show/hide peer container for the given id.
  263. */
  264. RemoteVideo.prototype.showPeerContainer = function (state) {
  265. if (!this.container)
  266. return;
  267. var isHide = state === 'hide';
  268. var resizeThumbnails = false;
  269. if (!isHide) {
  270. if (!$(this.container).is(':visible')) {
  271. resizeThumbnails = true;
  272. $(this.container).show();
  273. }
  274. // Call updateView, so that we'll figure out if avatar
  275. // should be displayed based on video muted status and whether or not
  276. // it's in the lastN set
  277. this.updateView();
  278. }
  279. else if ($(this.container).is(':visible') && isHide)
  280. {
  281. resizeThumbnails = true;
  282. $(this.container).hide();
  283. if(this.connectionIndicator)
  284. this.connectionIndicator.hide();
  285. }
  286. if (resizeThumbnails) {
  287. this.VideoLayout.resizeThumbnails();
  288. }
  289. // We want to be able to pin a participant from the contact list, even
  290. // if he's not in the lastN set!
  291. // ContactList.setClickable(id, !isHide);
  292. };
  293. RemoteVideo.prototype.updateResolution = function (resolution) {
  294. if (this.connectionIndicator) {
  295. this.connectionIndicator.updateResolution(resolution);
  296. }
  297. };
  298. RemoteVideo.prototype.removeConnectionIndicator = function () {
  299. if (this.connectionIndicator)
  300. this.connectionIndicator.remove();
  301. };
  302. RemoteVideo.prototype.hideConnectionIndicator = function () {
  303. if (this.connectionIndicator)
  304. this.connectionIndicator.hide();
  305. };
  306. /**
  307. * Sets the display name for the given video span id.
  308. */
  309. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  310. if (!this.container) {
  311. console.warn( "Unable to set displayName - " + this.videoSpanId +
  312. " does not exist");
  313. return;
  314. }
  315. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  316. // If we already have a display name for this video.
  317. if (nameSpan.length > 0) {
  318. if (displayName && displayName.length > 0) {
  319. var displaynameSpan = $('#' + this.videoSpanId + '_name');
  320. if (displaynameSpan.text() !== displayName)
  321. displaynameSpan.text(displayName);
  322. }
  323. else if (key && key.length > 0) {
  324. var nameHtml = APP.translation.generateTranslationHTML(key);
  325. $('#' + this.videoSpanId + '_name').html(nameHtml);
  326. }
  327. else
  328. $('#' + this.videoSpanId + '_name').text(
  329. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  330. } else {
  331. nameSpan = document.createElement('span');
  332. nameSpan.className = 'displayname';
  333. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  334. if (displayName && displayName.length > 0) {
  335. $(nameSpan).text(displayName);
  336. } else {
  337. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  338. }
  339. nameSpan.id = this.videoSpanId + '_name';
  340. }
  341. };
  342. /**
  343. * Removes remote video menu element from video element identified by
  344. * given <tt>videoElementId</tt>.
  345. *
  346. * @param videoElementId the id of local or remote video element.
  347. */
  348. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  349. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  350. if (menuSpan.length) {
  351. this.popover.forceHide();
  352. menuSpan.remove();
  353. this.hasRemoteVideoMenu = false;
  354. }
  355. };
  356. RemoteVideo.createContainer = function (spanId) {
  357. var container = document.createElement('span');
  358. container.id = spanId;
  359. container.className = 'videocontainer';
  360. var remotes = document.getElementById('remoteVideos');
  361. return remotes.appendChild(container);
  362. };
  363. export default RemoteVideo;