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

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