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

RemoteVideo.js 18KB

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