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

RemoteVideo.js 18KB

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