選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RemoteVideo.js 20KB

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