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

RemoteVideo.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. function RemoteVideo(id, VideoLayout, emitter) {
  9. this.id = id;
  10. this.emitter = emitter;
  11. this.videoSpanId = `participant_${id}`;
  12. SmallVideo.call(this, VideoLayout);
  13. this.addRemoteVideoContainer();
  14. this.connectionIndicator = new ConnectionIndicator(this, id);
  15. this.setDisplayName();
  16. this.bindHoverHandler();
  17. this.flipX = false;
  18. this.isLocal = false;
  19. this.isMuted = false;
  20. }
  21. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  22. RemoteVideo.prototype.constructor = RemoteVideo;
  23. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  24. this.container = RemoteVideo.createContainer(this.videoSpanId);
  25. this.initBrowserSpecificProperties();
  26. if (APP.conference.isModerator) {
  27. this.addRemoteVideoMenu();
  28. }
  29. let {thumbWidth, thumbHeight} = this.VideoLayout.resizeThumbnails();
  30. AudioLevels.updateAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  31. return this.container;
  32. };
  33. /**
  34. * Initializes the remote participant popup menu, by specifying previously
  35. * constructed popupMenuElement, containing all the menu items.
  36. *
  37. * @param popupMenuElement a pre-constructed element, containing the menu items
  38. * to display in the popup
  39. */
  40. RemoteVideo.prototype._initPopupMenu = function (popupMenuElement) {
  41. this.popover = new JitsiPopover(
  42. $("#" + this.videoSpanId + " > .remotevideomenu"),
  43. { content: popupMenuElement.outerHTML,
  44. skin: "black"});
  45. // override popover show method to make sure we will update the content
  46. // before showing the popover
  47. var origShowFunc = this.popover.show;
  48. this.popover.show = function () {
  49. // update content by forcing it, to finish even if popover
  50. // is not visible
  51. this.updateRemoteVideoMenu(this.isMuted, true);
  52. // call the original show, passing its actual this
  53. origShowFunc.call(this.popover);
  54. }.bind(this);
  55. };
  56. /**
  57. * Generates the popup menu content.
  58. *
  59. * @returns {Element|*} the constructed element, containing popup menu items
  60. * @private
  61. */
  62. RemoteVideo.prototype._generatePopupContent = function () {
  63. var popupmenuElement = document.createElement('ul');
  64. popupmenuElement.className = 'popupmenu';
  65. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  66. var muteMenuItem = document.createElement('li');
  67. var muteLinkItem = document.createElement('a');
  68. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  69. var doMuteHTML = mutedIndicator +
  70. " <div " +
  71. "data-i18n='videothumbnail.domute'>" +
  72. APP.translation.translateString("videothumbnail.domute") +
  73. "</div>";
  74. var mutedHTML = mutedIndicator +
  75. " <div " +
  76. "data-i18n='videothumbnail.muted'>" +
  77. APP.translation.translateString("videothumbnail.muted") +
  78. "</div>";
  79. muteLinkItem.id = "muteLinkItem";
  80. if (this.isMuted) {
  81. muteLinkItem.innerHTML = mutedHTML;
  82. muteLinkItem.className = 'mutelink disabled';
  83. }
  84. else {
  85. muteLinkItem.innerHTML = doMuteHTML;
  86. muteLinkItem.className = 'mutelink';
  87. }
  88. // Delegate event to the document.
  89. $(document).on("click", ".mutelink", function(){
  90. if (this.isMuted)
  91. return;
  92. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  93. this.popover.forceHide();
  94. }.bind(this));
  95. muteMenuItem.appendChild(muteLinkItem);
  96. popupmenuElement.appendChild(muteMenuItem);
  97. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  98. var ejectMenuItem = document.createElement('li');
  99. var ejectLinkItem = document.createElement('a');
  100. var ejectText = "<div " +
  101. "data-i18n='videothumbnail.kick'>" +
  102. APP.translation.translateString("videothumbnail.kick") +
  103. "</div>";
  104. ejectLinkItem.className = 'ejectlink';
  105. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  106. $(document).on("click", ".ejectlink", function(){
  107. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  108. this.popover.forceHide();
  109. }.bind(this));
  110. ejectMenuItem.appendChild(ejectLinkItem);
  111. popupmenuElement.appendChild(ejectMenuItem);
  112. return popupmenuElement;
  113. };
  114. /**
  115. * Updates the remote video menu.
  116. *
  117. * @param isMuted the new muted state to update to
  118. * @param force to work even if popover is not visible
  119. */
  120. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted, force) {
  121. this.isMuted = isMuted;
  122. // generate content, translate it and add it to document only if
  123. // popover is visible or we force to do so.
  124. if(this.popover.popoverShown || force) {
  125. this.popover.updateContent(this._generatePopupContent());
  126. }
  127. };
  128. /**
  129. * Adds the remote video menu element for the given <tt>id</tt> in the
  130. * given <tt>parentElement</tt>.
  131. *
  132. * @param id the id indicating the video for which we're adding a menu.
  133. * @param parentElement the parent element where this menu will be added
  134. */
  135. if (!interfaceConfig.filmStripOnly) {
  136. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  137. var spanElement = document.createElement('div');
  138. spanElement.className = 'remotevideomenu';
  139. this.container.appendChild(spanElement);
  140. var menuElement = document.createElement('i');
  141. menuElement.className = 'fa fa-angle-down';
  142. menuElement.title = 'Remote user controls';
  143. spanElement.appendChild(menuElement);
  144. this._initPopupMenu(this._generatePopupContent());
  145. };
  146. } else {
  147. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  148. }
  149. /**
  150. * Removes the remote stream element corresponding to the given stream and
  151. * parent container.
  152. *
  153. * @param stream the MediaStream
  154. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  155. */
  156. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  157. if (!this.container)
  158. return false;
  159. var isVideo = stream.isVideoTrack();
  160. var elementID = SmallVideo.getStreamElementID(stream);
  161. var select = $('#' + elementID);
  162. select.remove();
  163. console.info((isVideo ? "Video" : "Audio") +
  164. " removed " + this.id, select);
  165. // when removing only the video element and we are on stage
  166. // update the stage
  167. if (isVideo && this.VideoLayout.isCurrentlyOnLarge(this.id))
  168. this.VideoLayout.updateLargeVideo(this.id);
  169. };
  170. /**
  171. * Removes RemoteVideo from the page.
  172. */
  173. RemoteVideo.prototype.remove = function () {
  174. console.log("Remove thumbnail", this.id);
  175. this.removeConnectionIndicator();
  176. // Make sure that the large video is updated if are removing its
  177. // corresponding small video.
  178. this.VideoLayout.updateAfterThumbRemoved(this.id);
  179. // Remove whole container
  180. if (this.container.parentNode) {
  181. this.container.parentNode.removeChild(this.container);
  182. }
  183. };
  184. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  185. var webRtcStream = stream.getOriginalStream();
  186. var isVideo = stream.isVideoTrack();
  187. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  188. return;
  189. }
  190. var self = this;
  191. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  192. // when video playback starts
  193. var onPlayingHandler = function () {
  194. self.VideoLayout.videoactive(streamElement, self.id);
  195. streamElement.onplaying = null;
  196. };
  197. streamElement.onplaying = onPlayingHandler;
  198. };
  199. /**
  200. * Checks whether or not video stream exists and has started for this
  201. * RemoteVideo instance. This is checked by trying to select video element in
  202. * this container and checking if 'currentTime' field's value is greater than 0.
  203. *
  204. * @returns {*|boolean} true if this RemoteVideo has active video stream running
  205. */
  206. RemoteVideo.prototype.hasVideoStarted = function () {
  207. var videoSelector = this.selectVideoElement();
  208. return videoSelector.length && videoSelector[0].currentTime > 0;
  209. };
  210. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  211. if (!this.container) {
  212. return;
  213. }
  214. let isVideo = stream.isVideoTrack();
  215. isVideo ? this.videoStream = stream : this.audioStream = stream;
  216. if (isVideo)
  217. this.setVideoType(stream.videoType);
  218. // Add click handler.
  219. let onClickHandler = (event) => {
  220. let source = event.target || event.srcElement;
  221. // ignore click if it was done in popup menu
  222. if ($(source).parents('.popupmenu').length === 0) {
  223. this.VideoLayout.handleVideoThumbClicked(this.id);
  224. }
  225. // On IE we need to populate this handler on video <object>
  226. // and it does not give event instance as an argument,
  227. // so we check here for methods.
  228. if (event.stopPropagation && event.preventDefault) {
  229. event.stopPropagation();
  230. event.preventDefault();
  231. }
  232. return false;
  233. };
  234. this.container.onclick = onClickHandler;
  235. if(!stream.getOriginalStream())
  236. return;
  237. let streamElement = SmallVideo.createStreamElement(stream);
  238. let newElementId = streamElement.id;
  239. // Put new stream element always in front
  240. UIUtils.prependChild(this.container, streamElement);
  241. // If we hide element when Temasys plugin is used then
  242. // we'll never receive 'onplay' event and other logic won't work as expected
  243. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  244. // calling attach will show it back
  245. $(streamElement).hide();
  246. // If the container is currently visible
  247. // we attach the stream to the element.
  248. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  249. this.waitForPlayback(streamElement, stream);
  250. streamElement = stream.attach(streamElement);
  251. }
  252. $(streamElement).click(onClickHandler);
  253. },
  254. /**
  255. * Show/hide peer container for the given id.
  256. */
  257. RemoteVideo.prototype.showPeerContainer = function (state) {
  258. if (!this.container)
  259. return;
  260. var isHide = state === 'hide';
  261. var resizeThumbnails = false;
  262. if (!isHide) {
  263. if (!$(this.container).is(':visible')) {
  264. resizeThumbnails = true;
  265. $(this.container).show();
  266. }
  267. // Call updateView, so that we'll figure out if avatar
  268. // should be displayed based on video muted status and whether or not
  269. // it's in the lastN set
  270. this.updateView();
  271. }
  272. else if ($(this.container).is(':visible') && isHide)
  273. {
  274. resizeThumbnails = true;
  275. $(this.container).hide();
  276. if(this.connectionIndicator)
  277. this.connectionIndicator.hide();
  278. }
  279. if (resizeThumbnails) {
  280. this.VideoLayout.resizeThumbnails();
  281. }
  282. // We want to be able to pin a participant from the contact list, even
  283. // if he's not in the lastN set!
  284. // ContactList.setClickable(id, !isHide);
  285. };
  286. RemoteVideo.prototype.updateResolution = function (resolution) {
  287. if (this.connectionIndicator) {
  288. this.connectionIndicator.updateResolution(resolution);
  289. }
  290. };
  291. RemoteVideo.prototype.removeConnectionIndicator = function () {
  292. if (this.connectionIndicator)
  293. this.connectionIndicator.remove();
  294. };
  295. RemoteVideo.prototype.hideConnectionIndicator = function () {
  296. if (this.connectionIndicator)
  297. this.connectionIndicator.hide();
  298. };
  299. /**
  300. * Sets the display name for the given video span id.
  301. */
  302. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  303. if (!this.container) {
  304. console.warn( "Unable to set displayName - " + this.videoSpanId +
  305. " does not exist");
  306. return;
  307. }
  308. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  309. // If we already have a display name for this video.
  310. if (nameSpan.length > 0) {
  311. if (displayName && displayName.length > 0) {
  312. var displaynameSpan = $('#' + this.videoSpanId + '_name');
  313. if (displaynameSpan.text() !== displayName)
  314. displaynameSpan.text(displayName);
  315. }
  316. else if (key && key.length > 0) {
  317. var nameHtml = APP.translation.generateTranslationHTML(key);
  318. $('#' + this.videoSpanId + '_name').html(nameHtml);
  319. }
  320. else
  321. $('#' + this.videoSpanId + '_name').text(
  322. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  323. } else {
  324. nameSpan = document.createElement('span');
  325. nameSpan.className = 'displayname';
  326. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  327. if (displayName && displayName.length > 0) {
  328. $(nameSpan).text(displayName);
  329. } else {
  330. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  331. }
  332. nameSpan.id = this.videoSpanId + '_name';
  333. }
  334. };
  335. /**
  336. * Removes remote video menu element from video element identified by
  337. * given <tt>videoElementId</tt>.
  338. *
  339. * @param videoElementId the id of local or remote video element.
  340. */
  341. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  342. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  343. if (menuSpan.length) {
  344. this.popover.forceHide();
  345. menuSpan.remove();
  346. }
  347. };
  348. RemoteVideo.createContainer = function (spanId) {
  349. var container = document.createElement('span');
  350. container.id = spanId;
  351. container.className = 'videocontainer';
  352. var remotes = document.getElementById('remoteVideos');
  353. return remotes.appendChild(container);
  354. };
  355. export default RemoteVideo;