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

RemoteVideo.js 16KB

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