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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* global $, APP, interfaceConfig */
  2. import ConnectionIndicator from './ConnectionIndicator';
  3. import SmallVideo from "./SmallVideo";
  4. import UIUtils from "../util/UIUtil";
  5. import UIEvents from '../../../service/UI/UIEvents';
  6. import JitsiPopover from "../util/JitsiPopover";
  7. /**
  8. * Creates new instance of the <tt>RemoteVideo</tt>.
  9. * @param user {JitsiParticipant} the user for whom remote video instance will
  10. * be created.
  11. * @param {VideoLayout} VideoLayout the video layout instance.
  12. * @param {EventEmitter} emitter the event emitter which will be used by
  13. * the new instance to emit events.
  14. * @constructor
  15. */
  16. function RemoteVideo(user, VideoLayout, emitter) {
  17. this.user = user;
  18. this.id = user.getId();
  19. this.emitter = emitter;
  20. this.videoSpanId = `participant_${this.id}`;
  21. SmallVideo.call(this, VideoLayout);
  22. this.hasRemoteVideoMenu = false;
  23. this.addRemoteVideoContainer();
  24. this.connectionIndicator = new ConnectionIndicator(this, this.id);
  25. this.setDisplayName();
  26. this.flipX = false;
  27. this.isLocal = false;
  28. /**
  29. * The flag is set to <tt>true</tt> after the 'onplay' event has been
  30. * triggered on the current video element. It goes back to <tt>false</tt>
  31. * when the stream is removed. It is used to determine whether the video
  32. * playback has ever started.
  33. * @type {boolean}
  34. */
  35. this.wasVideoPlayed = false;
  36. /**
  37. * The flag is set to <tt>true</tt> if remote participant's video gets muted
  38. * during his media connection disruption. This is to prevent black video
  39. * being render on the thumbnail, because even though once the video has
  40. * been played the image usually remains on the video element it seems that
  41. * after longer period of the video element being hidden this image can be
  42. * lost.
  43. * @type {boolean}
  44. */
  45. this.mutedWhileDisconnected = false;
  46. }
  47. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  48. RemoteVideo.prototype.constructor = RemoteVideo;
  49. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  50. this.container = RemoteVideo.createContainer(this.videoSpanId);
  51. this.initBrowserSpecificProperties();
  52. if (APP.conference.isModerator) {
  53. this.addRemoteVideoMenu();
  54. }
  55. this.VideoLayout.resizeThumbnails(false, true);
  56. this.addAudioLevelIndicator();
  57. return this.container;
  58. };
  59. /**
  60. * Initializes the remote participant popup menu, by specifying previously
  61. * constructed popupMenuElement, containing all the menu items.
  62. *
  63. * @param popupMenuElement a pre-constructed element, containing the menu items
  64. * to display in the popup
  65. */
  66. RemoteVideo.prototype._initPopupMenu = function (popupMenuElement) {
  67. let options = {
  68. content: popupMenuElement.outerHTML,
  69. skin: "black",
  70. hasArrow: false,
  71. onBeforePosition: el => APP.translation.translateElement(el)
  72. };
  73. let element = $("#" + this.videoSpanId + " .remotevideomenu");
  74. this.popover = new JitsiPopover(element, options);
  75. // override popover show method to make sure we will update the content
  76. // before showing the popover
  77. let origShowFunc = this.popover.show;
  78. this.popover.show = function () {
  79. // update content by forcing it, to finish even if popover
  80. // is not visible
  81. this.updateRemoteVideoMenu(this.isAudioMuted, true);
  82. // call the original show, passing its actual this
  83. origShowFunc.call(this.popover);
  84. }.bind(this);
  85. };
  86. /**
  87. * Generates the popup menu content.
  88. *
  89. * @returns {Element|*} the constructed element, containing popup menu items
  90. * @private
  91. */
  92. RemoteVideo.prototype._generatePopupContent = function () {
  93. var popupmenuElement = document.createElement('ul');
  94. popupmenuElement.className = 'popupmenu';
  95. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  96. var muteMenuItem = document.createElement('li');
  97. var muteLinkItem = document.createElement('a');
  98. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  99. var doMuteHTML = mutedIndicator +
  100. " <div data-i18n='videothumbnail.domute'></div>";
  101. var mutedHTML = mutedIndicator +
  102. " <div data-i18n='videothumbnail.muted'></div>";
  103. muteLinkItem.id = "mutelink_" + this.id;
  104. if (this.isAudioMuted) {
  105. muteLinkItem.innerHTML = mutedHTML;
  106. muteLinkItem.className = 'mutelink disabled';
  107. }
  108. else {
  109. muteLinkItem.innerHTML = doMuteHTML;
  110. muteLinkItem.className = 'mutelink';
  111. }
  112. // Delegate event to the document.
  113. $(document).on("click", "#mutelink_" + this.id, function(){
  114. if (this.isAudioMuted)
  115. return;
  116. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  117. this.popover.forceHide();
  118. }.bind(this));
  119. muteMenuItem.appendChild(muteLinkItem);
  120. popupmenuElement.appendChild(muteMenuItem);
  121. var ejectIndicator = "<i style='float:left;' class='icon-kick'></i>";
  122. var ejectMenuItem = document.createElement('li');
  123. var ejectLinkItem = document.createElement('a');
  124. var ejectText = "<div data-i18n='videothumbnail.kick'></div>";
  125. ejectLinkItem.className = 'ejectlink';
  126. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  127. ejectLinkItem.id = "ejectlink_" + this.id;
  128. $(document).on("click", "#ejectlink_" + this.id, function(){
  129. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  130. this.popover.forceHide();
  131. }.bind(this));
  132. ejectMenuItem.appendChild(ejectLinkItem);
  133. popupmenuElement.appendChild(ejectMenuItem);
  134. APP.translation.translateElement($(popupmenuElement));
  135. return popupmenuElement;
  136. };
  137. /**
  138. * Updates the remote video menu.
  139. *
  140. * @param isMuted the new muted state to update to
  141. * @param force to work even if popover is not visible
  142. */
  143. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted, force) {
  144. this.isAudioMuted = isMuted;
  145. // generate content, translate it and add it to document only if
  146. // popover is visible or we force to do so.
  147. if(this.popover.popoverShown || force) {
  148. this.popover.updateContent(this._generatePopupContent());
  149. }
  150. };
  151. /**
  152. * @inheritDoc
  153. */
  154. RemoteVideo.prototype.setMutedView = function(isMuted) {
  155. SmallVideo.prototype.setMutedView.call(this, isMuted);
  156. // Update 'mutedWhileDisconnected' flag
  157. this._figureOutMutedWhileDisconnected(this.isConnectionActive() === false);
  158. };
  159. /**
  160. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  161. * account remote participant's network connectivity and video muted status.
  162. *
  163. * @param {boolean} isDisconnected <tt>true</tt> if the remote participant is
  164. * currently having connectivity issues or <tt>false</tt> otherwise.
  165. *
  166. * @private
  167. */
  168. RemoteVideo.prototype._figureOutMutedWhileDisconnected
  169. = function(isDisconnected) {
  170. if (isDisconnected && this.isVideoMuted) {
  171. this.mutedWhileDisconnected = true;
  172. } else if (!isDisconnected && !this.isVideoMuted) {
  173. this.mutedWhileDisconnected = false;
  174. }
  175. };
  176. /**
  177. * Adds the remote video menu element for the given <tt>id</tt> in the
  178. * given <tt>parentElement</tt>.
  179. *
  180. * @param id the id indicating the video for which we're adding a menu.
  181. * @param parentElement the parent element where this menu will be added
  182. */
  183. if (!interfaceConfig.filmStripOnly) {
  184. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  185. var spanElement = document.createElement('span');
  186. spanElement.className = 'remotevideomenu toolbar-icon right';
  187. this.container
  188. .querySelector('.videocontainer__toolbar')
  189. .appendChild(spanElement);
  190. var menuElement = document.createElement('i');
  191. menuElement.className = 'icon-menu-up';
  192. menuElement.title = 'Remote user controls';
  193. spanElement.appendChild(menuElement);
  194. this._initPopupMenu(this._generatePopupContent());
  195. this.hasRemoteVideoMenu = true;
  196. };
  197. } else {
  198. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  199. }
  200. /**
  201. * Removes the remote stream element corresponding to the given stream and
  202. * parent container.
  203. *
  204. * @param stream the MediaStream
  205. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  206. */
  207. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  208. if (!this.container)
  209. return false;
  210. var isVideo = stream.isVideoTrack();
  211. var elementID = SmallVideo.getStreamElementID(stream);
  212. var select = $('#' + elementID);
  213. select.remove();
  214. if (isVideo) {
  215. this.wasVideoPlayed = false;
  216. }
  217. console.info((isVideo ? "Video" : "Audio") +
  218. " removed " + this.id, select);
  219. // when removing only the video element and we are on stage
  220. // update the stage
  221. if (isVideo && this.isCurrentlyOnLargeVideo())
  222. this.VideoLayout.updateLargeVideo(this.id);
  223. else
  224. // Missing video stream will affect display mode
  225. this.updateView();
  226. };
  227. /**
  228. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  229. * has connectivity issues.
  230. *
  231. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  232. * <tt>false</tt> otherwise.
  233. */
  234. RemoteVideo.prototype.isConnectionActive = function() {
  235. return this.user.isConnectionActive();
  236. };
  237. /**
  238. * The remote video is considered "playable" once the stream has started
  239. * according to the {@link #hasVideoStarted} result.
  240. *
  241. * @inheritdoc
  242. * @override
  243. */
  244. RemoteVideo.prototype.isVideoPlayable = function () {
  245. return SmallVideo.prototype.isVideoPlayable.call(this)
  246. && this.hasVideoStarted() && !this.mutedWhileDisconnected;
  247. };
  248. /**
  249. * @inheritDoc
  250. */
  251. RemoteVideo.prototype.updateView = function () {
  252. this.updateConnectionStatusIndicator(
  253. null /* will obtain the status from 'conference' */);
  254. // This must be called after 'updateConnectionStatusIndicator' because it
  255. // affects the display mode by modifying 'mutedWhileDisconnected' flag
  256. SmallVideo.prototype.updateView.call(this);
  257. };
  258. /**
  259. * Updates the UI to reflect user's connectivity status.
  260. * @param isActive {boolean|null} 'true' if user's connection is active or
  261. * 'false' when the use is having some connectivity issues and a warning
  262. * should be displayed. When 'null' is passed then the current value will be
  263. * obtained from the conference instance.
  264. */
  265. RemoteVideo.prototype.updateConnectionStatusIndicator = function (isActive) {
  266. // Check for initial value if 'isActive' is not defined
  267. if (typeof isActive !== "boolean") {
  268. isActive = this.isConnectionActive();
  269. if (isActive === null) {
  270. // Cancel processing at this point - no update
  271. return;
  272. }
  273. }
  274. console.debug(this.id + " thumbnail is connection active ? " + isActive);
  275. // Update 'mutedWhileDisconnected' flag
  276. this._figureOutMutedWhileDisconnected(!isActive);
  277. if(this.connectionIndicator)
  278. this.connectionIndicator.updateConnectionStatusIndicator(isActive);
  279. // Toggle thumbnail video problem filter
  280. this.selectVideoElement().toggleClass(
  281. "videoThumbnailProblemFilter", !isActive);
  282. this.$avatar().toggleClass(
  283. "videoThumbnailProblemFilter", !isActive);
  284. };
  285. /**
  286. * Removes RemoteVideo from the page.
  287. */
  288. RemoteVideo.prototype.remove = function () {
  289. console.log("Remove thumbnail", this.id);
  290. this.removeConnectionIndicator();
  291. // Make sure that the large video is updated if are removing its
  292. // corresponding small video.
  293. this.VideoLayout.updateAfterThumbRemoved(this.id);
  294. // Remove whole container
  295. if (this.container.parentNode) {
  296. this.container.parentNode.removeChild(this.container);
  297. }
  298. };
  299. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  300. var webRtcStream = stream.getOriginalStream();
  301. var isVideo = stream.isVideoTrack();
  302. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  303. return;
  304. }
  305. var self = this;
  306. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  307. // when video playback starts
  308. var onPlayingHandler = function () {
  309. self.wasVideoPlayed = true;
  310. self.VideoLayout.videoactive(streamElement, self.id);
  311. streamElement.onplaying = null;
  312. // Refresh to show the video
  313. self.updateView();
  314. };
  315. streamElement.onplaying = onPlayingHandler;
  316. };
  317. /**
  318. * Checks whether the video stream has started for this RemoteVideo instance.
  319. *
  320. * @returns {boolean} true if this RemoteVideo has a video stream for which
  321. * the playback has been started.
  322. */
  323. RemoteVideo.prototype.hasVideoStarted = function () {
  324. return this.wasVideoPlayed;
  325. };
  326. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  327. if (!this.container) {
  328. return;
  329. }
  330. let isVideo = stream.isVideoTrack();
  331. isVideo ? this.videoStream = stream : this.audioStream = stream;
  332. if (isVideo)
  333. this.setVideoType(stream.videoType);
  334. // Add click handler.
  335. let onClickHandler = (event) => {
  336. let source = event.target || event.srcElement;
  337. // ignore click if it was done in popup menu
  338. if ($(source).parents('.popupmenu').length === 0) {
  339. this.VideoLayout.handleVideoThumbClicked(this.id);
  340. }
  341. // On IE we need to populate this handler on video <object>
  342. // and it does not give event instance as an argument,
  343. // so we check here for methods.
  344. if (event.stopPropagation && event.preventDefault) {
  345. event.stopPropagation();
  346. event.preventDefault();
  347. }
  348. return false;
  349. };
  350. this.container.onclick = onClickHandler;
  351. if(!stream.getOriginalStream())
  352. return;
  353. let streamElement = SmallVideo.createStreamElement(stream);
  354. // Put new stream element always in front
  355. UIUtils.prependChild(this.container, streamElement);
  356. // If we hide element when Temasys plugin is used then
  357. // we'll never receive 'onplay' event and other logic won't work as expected
  358. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  359. // calling attach will show it back
  360. $(streamElement).hide();
  361. // If the container is currently visible
  362. // we attach the stream to the element.
  363. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  364. this.waitForPlayback(streamElement, stream);
  365. streamElement = stream.attach(streamElement);
  366. }
  367. $(streamElement).click(onClickHandler);
  368. },
  369. /**
  370. * Show/hide peer container for the given id.
  371. */
  372. RemoteVideo.prototype.showPeerContainer = function (state) {
  373. if (!this.container)
  374. return;
  375. var isHide = state === 'hide';
  376. var resizeThumbnails = false;
  377. if (!isHide) {
  378. if (!$(this.container).is(':visible')) {
  379. resizeThumbnails = true;
  380. $(this.container).show();
  381. }
  382. // Call updateView, so that we'll figure out if avatar
  383. // should be displayed based on video muted status and whether or not
  384. // it's in the lastN set
  385. this.updateView();
  386. }
  387. else if ($(this.container).is(':visible') && isHide)
  388. {
  389. resizeThumbnails = true;
  390. $(this.container).hide();
  391. if(this.connectionIndicator)
  392. this.connectionIndicator.hide();
  393. }
  394. if (resizeThumbnails) {
  395. this.VideoLayout.resizeThumbnails();
  396. }
  397. // We want to be able to pin a participant from the contact list, even
  398. // if he's not in the lastN set!
  399. // ContactList.setClickable(id, !isHide);
  400. };
  401. RemoteVideo.prototype.updateResolution = function (resolution) {
  402. if (this.connectionIndicator) {
  403. this.connectionIndicator.updateResolution(resolution);
  404. }
  405. };
  406. RemoteVideo.prototype.removeConnectionIndicator = function () {
  407. if (this.connectionIndicator)
  408. this.connectionIndicator.remove();
  409. };
  410. RemoteVideo.prototype.hideConnectionIndicator = function () {
  411. if (this.connectionIndicator)
  412. this.connectionIndicator.hide();
  413. };
  414. /**
  415. * Sets the display name for the given video span id.
  416. */
  417. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  418. if (!this.container) {
  419. console.warn( "Unable to set displayName - " + this.videoSpanId +
  420. " does not exist");
  421. return;
  422. }
  423. var nameSpan = $('#' + this.videoSpanId + ' .displayname');
  424. // If we already have a display name for this video.
  425. if (nameSpan.length > 0) {
  426. if (displayName && displayName.length > 0) {
  427. var displaynameSpan = $('#' + this.videoSpanId + '_name');
  428. if (displaynameSpan.text() !== displayName)
  429. displaynameSpan.text(displayName);
  430. }
  431. else if (key && key.length > 0) {
  432. var nameHtml = APP.translation.generateTranslationHTML(key);
  433. $('#' + this.videoSpanId + '_name').html(nameHtml);
  434. }
  435. else
  436. $('#' + this.videoSpanId + '_name').text(
  437. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  438. } else {
  439. nameSpan = document.createElement('span');
  440. nameSpan.className = 'displayname';
  441. $('#' + this.videoSpanId)[0]
  442. .querySelector('.videocontainer__toolbar')
  443. .appendChild(nameSpan);
  444. if (displayName && displayName.length > 0) {
  445. $(nameSpan).text(displayName);
  446. } else {
  447. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  448. }
  449. nameSpan.id = this.videoSpanId + '_name';
  450. }
  451. };
  452. /**
  453. * Removes remote video menu element from video element identified by
  454. * given <tt>videoElementId</tt>.
  455. *
  456. * @param videoElementId the id of local or remote video element.
  457. */
  458. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  459. var menuSpan = $('#' + this.videoSpanId + '> .remotevideomenu');
  460. if (menuSpan.length) {
  461. this.popover.forceHide();
  462. menuSpan.remove();
  463. this.hasRemoteVideoMenu = false;
  464. }
  465. };
  466. RemoteVideo.createContainer = function (spanId) {
  467. let container = document.createElement('span');
  468. container.id = spanId;
  469. container.className = 'videocontainer';
  470. let toolbar = document.createElement('div');
  471. toolbar.className = "videocontainer__toolbar";
  472. container.appendChild(toolbar);
  473. var remotes = document.getElementById('remoteVideos');
  474. return remotes.appendChild(container);
  475. };
  476. export default RemoteVideo;