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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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.isCurrentlyOnLargeVideo())
  182. this.VideoLayout.updateLargeVideo(this.id);
  183. };
  184. /**
  185. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  186. * has connectivity issues.
  187. *
  188. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  189. * <tt>false</tt> otherwise.
  190. */
  191. RemoteVideo.prototype.isConnectionActive = function() {
  192. return this.user.isConnectionActive();
  193. };
  194. /**
  195. * @inheritDoc
  196. */
  197. RemoteVideo.prototype.updateView = function () {
  198. SmallVideo.prototype.updateView.call(this);
  199. this.updateConnectionStatusIndicator(
  200. null /* will obtain the status from 'conference' */);
  201. };
  202. /**
  203. * Updates the UI to reflect user's connectivity status.
  204. * @param isActive {boolean|null} 'true' if user's connection is active or
  205. * 'false' when the use is having some connectivity issues and a warning
  206. * should be displayed. When 'null' is passed then the current value will be
  207. * obtained from the conference instance.
  208. */
  209. RemoteVideo.prototype.updateConnectionStatusIndicator = function (isActive) {
  210. // Check for initial value if 'isActive' is not defined
  211. if (typeof isActive !== "boolean") {
  212. isActive = this.isConnectionActive();
  213. if (isActive === null) {
  214. // Cancel processing at this point - no update
  215. return;
  216. }
  217. }
  218. console.debug(this.id + " thumbnail is connection active ? " + isActive);
  219. if(this.connectionIndicator)
  220. this.connectionIndicator.updateConnectionStatusIndicator(isActive);
  221. };
  222. /**
  223. * Removes RemoteVideo from the page.
  224. */
  225. RemoteVideo.prototype.remove = function () {
  226. console.log("Remove thumbnail", this.id);
  227. this.removeConnectionIndicator();
  228. // Make sure that the large video is updated if are removing its
  229. // corresponding small video.
  230. this.VideoLayout.updateAfterThumbRemoved(this.id);
  231. // Remove whole container
  232. if (this.container.parentNode) {
  233. this.container.parentNode.removeChild(this.container);
  234. }
  235. };
  236. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  237. var webRtcStream = stream.getOriginalStream();
  238. var isVideo = stream.isVideoTrack();
  239. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  240. return;
  241. }
  242. var self = this;
  243. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  244. // when video playback starts
  245. var onPlayingHandler = function () {
  246. self.VideoLayout.videoactive(streamElement, self.id);
  247. streamElement.onplaying = null;
  248. };
  249. streamElement.onplaying = onPlayingHandler;
  250. };
  251. /**
  252. * Checks whether or not video stream exists and has started for this
  253. * RemoteVideo instance. This is checked by trying to select video element in
  254. * this container and checking if 'currentTime' field's value is greater than 0.
  255. *
  256. * @returns {*|boolean} true if this RemoteVideo has active video stream running
  257. */
  258. RemoteVideo.prototype.hasVideoStarted = function () {
  259. var videoSelector = this.selectVideoElement();
  260. return videoSelector.length && videoSelector[0].currentTime > 0;
  261. };
  262. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  263. if (!this.container) {
  264. return;
  265. }
  266. let isVideo = stream.isVideoTrack();
  267. isVideo ? this.videoStream = stream : this.audioStream = stream;
  268. if (isVideo)
  269. this.setVideoType(stream.videoType);
  270. // Add click handler.
  271. let onClickHandler = (event) => {
  272. let source = event.target || event.srcElement;
  273. // ignore click if it was done in popup menu
  274. if ($(source).parents('.popupmenu').length === 0) {
  275. this.VideoLayout.handleVideoThumbClicked(this.id);
  276. }
  277. // On IE we need to populate this handler on video <object>
  278. // and it does not give event instance as an argument,
  279. // so we check here for methods.
  280. if (event.stopPropagation && event.preventDefault) {
  281. event.stopPropagation();
  282. event.preventDefault();
  283. }
  284. return false;
  285. };
  286. this.container.onclick = onClickHandler;
  287. if(!stream.getOriginalStream())
  288. return;
  289. let streamElement = SmallVideo.createStreamElement(stream);
  290. let newElementId = streamElement.id;
  291. // Put new stream element always in front
  292. UIUtils.prependChild(this.container, streamElement);
  293. // If we hide element when Temasys plugin is used then
  294. // we'll never receive 'onplay' event and other logic won't work as expected
  295. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  296. // calling attach will show it back
  297. $(streamElement).hide();
  298. // If the container is currently visible
  299. // we attach the stream to the element.
  300. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  301. this.waitForPlayback(streamElement, stream);
  302. streamElement = stream.attach(streamElement);
  303. }
  304. $(streamElement).click(onClickHandler);
  305. },
  306. /**
  307. * Show/hide peer container for the given id.
  308. */
  309. RemoteVideo.prototype.showPeerContainer = function (state) {
  310. if (!this.container)
  311. return;
  312. var isHide = state === 'hide';
  313. var resizeThumbnails = false;
  314. if (!isHide) {
  315. if (!$(this.container).is(':visible')) {
  316. resizeThumbnails = true;
  317. $(this.container).show();
  318. }
  319. // Call updateView, so that we'll figure out if avatar
  320. // should be displayed based on video muted status and whether or not
  321. // it's in the lastN set
  322. this.updateView();
  323. }
  324. else if ($(this.container).is(':visible') && isHide)
  325. {
  326. resizeThumbnails = true;
  327. $(this.container).hide();
  328. if(this.connectionIndicator)
  329. this.connectionIndicator.hide();
  330. }
  331. if (resizeThumbnails) {
  332. this.VideoLayout.resizeThumbnails();
  333. }
  334. // We want to be able to pin a participant from the contact list, even
  335. // if he's not in the lastN set!
  336. // ContactList.setClickable(id, !isHide);
  337. };
  338. RemoteVideo.prototype.updateResolution = function (resolution) {
  339. if (this.connectionIndicator) {
  340. this.connectionIndicator.updateResolution(resolution);
  341. }
  342. };
  343. RemoteVideo.prototype.removeConnectionIndicator = function () {
  344. if (this.connectionIndicator)
  345. this.connectionIndicator.remove();
  346. };
  347. RemoteVideo.prototype.hideConnectionIndicator = function () {
  348. if (this.connectionIndicator)
  349. this.connectionIndicator.hide();
  350. };
  351. /**
  352. * Sets the display name for the given video span id.
  353. */
  354. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  355. if (!this.container) {
  356. console.warn( "Unable to set displayName - " + this.videoSpanId +
  357. " does not exist");
  358. return;
  359. }
  360. var nameSpan = $('#' + this.videoSpanId + ' .displayname');
  361. // If we already have a display name for this video.
  362. if (nameSpan.length > 0) {
  363. if (displayName && displayName.length > 0) {
  364. var displaynameSpan = $('#' + this.videoSpanId + '_name');
  365. if (displaynameSpan.text() !== displayName)
  366. displaynameSpan.text(displayName);
  367. }
  368. else if (key && key.length > 0) {
  369. var nameHtml = APP.translation.generateTranslationHTML(key);
  370. $('#' + this.videoSpanId + '_name').html(nameHtml);
  371. }
  372. else
  373. $('#' + this.videoSpanId + '_name').text(
  374. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  375. } else {
  376. nameSpan = document.createElement('span');
  377. nameSpan.className = 'displayname';
  378. $('#' + this.videoSpanId)[0]
  379. .querySelector('.videocontainer__toolbar')
  380. .appendChild(nameSpan);
  381. if (displayName && displayName.length > 0) {
  382. $(nameSpan).text(displayName);
  383. } else {
  384. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  385. }
  386. nameSpan.id = this.videoSpanId + '_name';
  387. }
  388. };
  389. /**
  390. * Removes remote video menu element from video element identified by
  391. * given <tt>videoElementId</tt>.
  392. *
  393. * @param videoElementId the id of local or remote video element.
  394. */
  395. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  396. var menuSpan = $('#' + this.videoSpanId + '> .remotevideomenu');
  397. if (menuSpan.length) {
  398. this.popover.forceHide();
  399. menuSpan.remove();
  400. this.hasRemoteVideoMenu = false;
  401. }
  402. };
  403. RemoteVideo.createContainer = function (spanId) {
  404. let container = document.createElement('span');
  405. container.id = spanId;
  406. container.className = 'videocontainer';
  407. let toolbar = document.createElement('div');
  408. toolbar.className = "videocontainer__toolbar";
  409. container.appendChild(toolbar);
  410. var remotes = document.getElementById('remoteVideos');
  411. return remotes.appendChild(container);
  412. };
  413. export default RemoteVideo;