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 13KB

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