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