Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RemoteVideo.js 21KB

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