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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. }
  24. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  25. RemoteVideo.prototype.constructor = RemoteVideo;
  26. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  27. this.container = RemoteVideo.createContainer(this.videoSpanId);
  28. if (APP.conference.isModerator) {
  29. this.addRemoteVideoMenu();
  30. }
  31. let {thumbWidth, thumbHeight} = this.VideoLayout.calculateThumbnailSize();
  32. AudioLevels.updateAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  33. return this.container;
  34. };
  35. /**
  36. * Adds the remote video menu element for the given <tt>id</tt> in the
  37. * given <tt>parentElement</tt>.
  38. *
  39. * @param id the id indicating the video for which we're adding a menu.
  40. * @param parentElement the parent element where this menu will be added
  41. */
  42. if (!interfaceConfig.filmStripOnly) {
  43. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  44. var spanElement = document.createElement('span');
  45. spanElement.className = 'remotevideomenu';
  46. this.container.appendChild(spanElement);
  47. var menuElement = document.createElement('i');
  48. menuElement.className = 'fa fa-angle-down';
  49. menuElement.title = 'Remote user controls';
  50. spanElement.appendChild(menuElement);
  51. var popupmenuElement = document.createElement('ul');
  52. popupmenuElement.className = 'popupmenu';
  53. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  54. spanElement.appendChild(popupmenuElement);
  55. var muteMenuItem = document.createElement('li');
  56. var muteLinkItem = document.createElement('a');
  57. var mutedIndicator = "<i style='float:left;' " +
  58. "class='icon-mic-disabled'></i>";
  59. if (!this.isMuted) {
  60. muteLinkItem.innerHTML = mutedIndicator +
  61. " <div style='width: 90px;margin-left: 20px;' " +
  62. "data-i18n='videothumbnail.domute'></div>";
  63. muteLinkItem.className = 'mutelink';
  64. }
  65. else {
  66. muteLinkItem.innerHTML = mutedIndicator +
  67. " <div style='width: 90px;margin-left: 20px;' " +
  68. "data-i18n='videothumbnail.muted'></div>";
  69. muteLinkItem.className = 'mutelink disabled';
  70. }
  71. var self = this;
  72. muteLinkItem.onclick = function(){
  73. if ($(this).attr('disabled')) {
  74. event.preventDefault();
  75. }
  76. var isMute = !!self.isMuted;
  77. self.emitter.emit(UIEvents.AUDIO_MUTED, !isMute);
  78. popupmenuElement.setAttribute('style', 'display:none;');
  79. if (isMute) {
  80. this.innerHTML = mutedIndicator +
  81. " <div style='width: 90px;margin-left: 20px;' " +
  82. "data-i18n='videothumbnail.muted'></div>";
  83. this.className = 'mutelink disabled';
  84. }
  85. else {
  86. this.innerHTML = mutedIndicator +
  87. " <div style='width: 90px;margin-left: 20px;' " +
  88. "data-i18n='videothumbnail.domute'></div>";
  89. this.className = 'mutelink';
  90. }
  91. };
  92. muteMenuItem.appendChild(muteLinkItem);
  93. popupmenuElement.appendChild(muteMenuItem);
  94. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  95. var ejectMenuItem = document.createElement('li');
  96. var ejectLinkItem = document.createElement('a');
  97. var ejectText = "<div style='width: 90px;margin-left: 20px;' " +
  98. "data-i18n='videothumbnail.kick'>&nbsp;</div>";
  99. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  100. ejectLinkItem.onclick = function(){
  101. self.emitter.emit(UIEvents.USER_KICKED, self.id);
  102. popupmenuElement.setAttribute('style', 'display:none;');
  103. };
  104. ejectMenuItem.appendChild(ejectLinkItem);
  105. popupmenuElement.appendChild(ejectMenuItem);
  106. var paddingSpan = document.createElement('span');
  107. paddingSpan.className = 'popupmenuPadding';
  108. popupmenuElement.appendChild(paddingSpan);
  109. APP.translation.translateElement(
  110. $("#" + popupmenuElement.id + " > li > a > div"));
  111. };
  112. } else {
  113. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  114. }
  115. /**
  116. * Removes the remote stream element corresponding to the given stream and
  117. * parent container.
  118. *
  119. * @param stream the stream
  120. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  121. */
  122. RemoteVideo.prototype.removeRemoteStreamElement =
  123. function (stream, isVideo, id) {
  124. if (!this.container)
  125. return false;
  126. var select = null;
  127. if (isVideo) {
  128. select = $('#' + id);
  129. }
  130. else
  131. select = $('#' + this.videoSpanId + '>audio');
  132. select.remove();
  133. console.info((isVideo ? "Video" : "Audio") +
  134. " removed " + this.id, select);
  135. if (isVideo)
  136. this.VideoLayout.updateRemovedVideo(this.id);
  137. };
  138. /**
  139. * Removes RemoteVideo from the page.
  140. */
  141. RemoteVideo.prototype.remove = function () {
  142. console.log("Remove thumbnail", this.id);
  143. this.removeConnectionIndicator();
  144. // Make sure that the large video is updated if are removing its
  145. // corresponding small video.
  146. this.VideoLayout.updateRemovedVideo(this.id);
  147. // Remove whole container
  148. if (this.container.parentNode) {
  149. this.container.parentNode.removeChild(this.container);
  150. }
  151. };
  152. RemoteVideo.prototype.waitForPlayback = function (sel, stream) {
  153. var webRtcStream = stream.getOriginalStream();
  154. var isVideo = stream.isVideoTrack();
  155. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  156. return;
  157. }
  158. var self = this;
  159. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  160. // when video playback starts
  161. var onPlayingHandler = function () {
  162. // FIXME: why do i have to do this for FF?
  163. if (RTCBrowserType.isFirefox()) {
  164. //FIXME: weshould use the lib here
  165. //APP.RTC.attachMediaStream(sel, webRtcStream);
  166. }
  167. if (RTCBrowserType.isTemasysPluginUsed()) {
  168. sel = self.selectVideoElement();
  169. }
  170. self.VideoLayout.videoactive(sel, self.id);
  171. sel[0].onplaying = null;
  172. if (RTCBrowserType.isTemasysPluginUsed()) {
  173. // 'currentTime' is used to check if the video has started
  174. // and the value is not set by the plugin, so we do it
  175. sel[0].currentTime = 1;
  176. }
  177. };
  178. sel[0].onplaying = onPlayingHandler;
  179. };
  180. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  181. if (!this.container) {
  182. return;
  183. }
  184. this.stream = stream;
  185. let isVideo = stream.isVideoTrack();
  186. let streamElement = SmallVideo.createStreamElement(stream);
  187. let newElementId = streamElement.id;
  188. // Put new stream element always in front
  189. UIUtils.prependChild(this.container, streamElement);
  190. let sel = $(`#${newElementId}`);
  191. sel.hide();
  192. // If the container is currently visible we attach the stream.
  193. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  194. this.waitForPlayback(sel, stream);
  195. stream.attach(sel);
  196. }
  197. // Add click handler.
  198. let onClickHandler = (event) => {
  199. this.VideoLayout.handleVideoThumbClicked(false, this.id);
  200. // On IE we need to populate this handler on video <object>
  201. // and it does not give event instance as an argument,
  202. // so we check here for methods.
  203. if (event.stopPropagation && event.preventDefault) {
  204. event.stopPropagation();
  205. event.preventDefault();
  206. }
  207. return false;
  208. };
  209. this.container.onclick = onClickHandler;
  210. // reselect
  211. if (RTCBrowserType.isTemasysPluginUsed()) {
  212. sel = $(`#${newElementId}`);
  213. }
  214. sel.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 showAvatar with undefined, 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.showAvatar(undefined);
  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. * Updates the Indicator for dominant speaker.
  284. *
  285. * @param isSpeaker indicates the current indicator state
  286. */
  287. RemoteVideo.prototype.updateDominantSpeakerIndicator = function (isSpeaker) {
  288. if (!this.container) {
  289. console.warn( "Unable to set dominant speaker indicator - "
  290. + this.videoSpanId + " does not exist");
  291. return;
  292. }
  293. var indicatorSpan
  294. = $('#' + this.videoSpanId + '>span.dominantspeakerindicator');
  295. // If we do not have an indicator for this video.
  296. if (indicatorSpan.length <= 0) {
  297. indicatorSpan = document.createElement('span');
  298. indicatorSpan.innerHTML
  299. = "<i id='speakerindicatoricon' class='fa fa-bullhorn'></i>";
  300. indicatorSpan.className = 'dominantspeakerindicator';
  301. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  302. // adds a tooltip
  303. UIUtils.setTooltip(indicatorSpan, "speaker", "left");
  304. APP.translation.translateElement($(indicatorSpan));
  305. }
  306. $(indicatorSpan).css("visibility", isSpeaker ? "visible" : "hidden");
  307. };
  308. /**
  309. * Sets the display name for the given video span id.
  310. */
  311. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  312. if (!this.container) {
  313. console.warn( "Unable to set displayName - " + this.videoSpanId +
  314. " does not exist");
  315. return;
  316. }
  317. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  318. // If we already have a display name for this video.
  319. if (nameSpan.length > 0) {
  320. if (displayName && displayName.length > 0) {
  321. $('#' + this.videoSpanId + '_name').html(displayName);
  322. }
  323. else if (key && key.length > 0) {
  324. var nameHtml = APP.translation.generateTranslationHTML(key);
  325. $('#' + this.videoSpanId + '_name').html(nameHtml);
  326. }
  327. else
  328. $('#' + this.videoSpanId + '_name').text(
  329. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  330. } else {
  331. nameSpan = document.createElement('span');
  332. nameSpan.className = 'displayname';
  333. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  334. if (displayName && displayName.length > 0) {
  335. nameSpan.innerText = displayName;
  336. }
  337. else
  338. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  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 + '>span.remotevideomenu');
  350. if (menuSpan.length) {
  351. menuSpan.remove();
  352. }
  353. };
  354. RemoteVideo.createContainer = function (spanId) {
  355. var container = document.createElement('span');
  356. container.id = spanId;
  357. container.className = 'videocontainer';
  358. var remotes = document.getElementById('remoteVideos');
  359. return remotes.appendChild(container);
  360. };
  361. export default RemoteVideo;