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.

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