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

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