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.

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