You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteVideo.js 14KB

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