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