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

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