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

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