您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoteVideo.js 18KB

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