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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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.resourceJid;
  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.resourceJid);
  137. };
  138. RemoteVideo.prototype.addRemoteStreamElement = function (sid, stream, thessrc) {
  139. if (!this.container)
  140. return;
  141. var self = this;
  142. var isVideo = stream.getVideoTracks().length > 0;
  143. var streamElement = SmallVideo.createStreamElement(sid, stream);
  144. var newElementId = streamElement.id;
  145. this.container.appendChild(streamElement);
  146. var sel = $('#' + newElementId);
  147. sel.hide();
  148. // If the container is currently visible we attach the stream.
  149. if (!isVideo
  150. || (this.container.offsetParent !== null && isVideo)) {
  151. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  152. // when video playback starts
  153. if (isVideo && stream.id !== 'mixedmslabel') {
  154. var onPlayingHandler = function () {
  155. // FIXME: why do i have to do this for FF?
  156. if (RTCBrowserType.isFirefox()) {
  157. APP.RTC.attachMediaStream(sel, stream);
  158. }
  159. if (RTCBrowserType.isTemasysPluginUsed()) {
  160. sel = $('#' + newElementId);
  161. }
  162. self.VideoLayout.videoactive(sel, self.resourceJid);
  163. sel[0].onplaying = null;
  164. if (RTCBrowserType.isTemasysPluginUsed()) {
  165. // 'currentTime' is used to check if the video has started
  166. // and the value is not set by the plugin, so we do it
  167. sel[0].currentTime = 1;
  168. }
  169. };
  170. sel[0].onplaying = onPlayingHandler;
  171. }
  172. APP.RTC.attachMediaStream(sel, stream);
  173. }
  174. var self = this;
  175. stream.onended = function () {
  176. console.log('stream ended', this);
  177. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  178. };
  179. // Name of video element name is different for IE/Safari
  180. var videoElem = APP.RTC.getVideoElementName();
  181. // Add click handler.
  182. var onClickHandler = function (event) {
  183. /*
  184. * FIXME It turns out that videoThumb may not exist (if there is
  185. * no actual video).
  186. */
  187. var videoThumb = $('#' + self.videoSpanId + '>' + videoElem).get(0);
  188. if (videoThumb) {
  189. self.VideoLayout.handleVideoThumbClicked(
  190. false,
  191. self.resourceJid);
  192. }
  193. // On IE we need to populate this handler on video <object>
  194. // and it does not give event instance as an argument,
  195. // so we check here for methods.
  196. if (event.stopPropagation && event.preventDefault) {
  197. event.stopPropagation();
  198. event.preventDefault();
  199. }
  200. return false;
  201. };
  202. this.container.onclick = onClickHandler;
  203. // reselect
  204. if (RTCBrowserType.isTemasysPluginUsed())
  205. sel = $('#' + newElementId);
  206. sel[0].onclick = onClickHandler;
  207. //FIXME
  208. // Add hover handler
  209. $(this.container).hover(
  210. function() {
  211. self.showDisplayName(true);
  212. },
  213. function() {
  214. var videoSrc = null;
  215. var videoSelector = $('#' + self.videoSpanId + '>' + videoElem);
  216. if (videoSelector && videoSelector.length > 0) {
  217. videoSrc = APP.RTC.getVideoSrc(videoSelector.get(0));
  218. }
  219. // If the video has been "pinned" by the user we want to
  220. // keep the display name on place.
  221. if (!LargeVideo.isLargeVideoVisible()
  222. || videoSrc !== APP.RTC.getVideoSrc($('#largeVideo')[0]))
  223. self.showDisplayName(false);
  224. }
  225. );
  226. },
  227. /**
  228. * Show/hide peer container for the given resourceJid.
  229. */
  230. RemoteVideo.prototype.showPeerContainer = function (state) {
  231. if (!this.container)
  232. return;
  233. var isHide = state === 'hide';
  234. var resizeThumbnails = false;
  235. if (!isHide) {
  236. if (!$(this.container).is(':visible')) {
  237. resizeThumbnails = true;
  238. $(this.container).show();
  239. }
  240. this.showAvatar(state !== 'show');
  241. }
  242. else if ($(this.container).is(':visible') && isHide)
  243. {
  244. resizeThumbnails = true;
  245. $(this.container).hide();
  246. if(this.connectionIndicator)
  247. this.connectionIndicator.hide();
  248. }
  249. if (resizeThumbnails) {
  250. this.VideoLayout.resizeThumbnails();
  251. }
  252. // We want to be able to pin a participant from the contact list, even
  253. // if he's not in the lastN set!
  254. // ContactList.setClickable(resourceJid, !isHide);
  255. };
  256. RemoteVideo.prototype.removeConnectionIndicator = function () {
  257. if(this.connectionIndicator)
  258. this.connectionIndicator.remove();
  259. }
  260. RemoteVideo.prototype.hideConnectionIndicator = function () {
  261. if(this.connectionIndicator)
  262. this.connectionIndicator.hide();
  263. }
  264. /**
  265. * Updates the remote video menu.
  266. *
  267. * @param jid the jid indicating the video for which we're adding a menu.
  268. * @param isMuted indicates the current mute state
  269. */
  270. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  271. var muteMenuItem
  272. = $('#remote_popupmenu_'
  273. + this.resourceJid
  274. + '>li>a.mutelink');
  275. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  276. if (muteMenuItem.length) {
  277. var muteLink = muteMenuItem.get(0);
  278. if (isMuted === 'true') {
  279. muteLink.innerHTML = mutedIndicator + ' Muted';
  280. muteLink.className = 'mutelink disabled';
  281. }
  282. else {
  283. muteLink.innerHTML = mutedIndicator + ' Mute';
  284. muteLink.className = 'mutelink';
  285. }
  286. }
  287. }
  288. /**
  289. * Sets the display name for the given video span id.
  290. */
  291. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  292. if (!this.container) {
  293. console.warn(
  294. "Unable to set displayName - " + this.videoSpanId + " does not exist");
  295. return;
  296. }
  297. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  298. // If we already have a display name for this video.
  299. if (nameSpan.length > 0) {
  300. if (displayName && displayName.length > 0)
  301. {
  302. $('#' + this.videoSpanId + '_name').html(displayName);
  303. }
  304. else if (key && key.length > 0)
  305. {
  306. var nameHtml = APP.translation.generateTranslationHTML(key);
  307. $('#' + this.videoSpanId + '_name').html(nameHtml);
  308. }
  309. else
  310. $('#' + this.videoSpanId + '_name').text(
  311. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  312. } else {
  313. nameSpan = document.createElement('span');
  314. nameSpan.className = 'displayname';
  315. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  316. if (displayName && displayName.length > 0) {
  317. nameSpan.innerText = displayName;
  318. }
  319. else
  320. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  321. nameSpan.id = this.videoSpanId + '_name';
  322. }
  323. }
  324. /**
  325. * Removes remote video menu element from video element identified by
  326. * given <tt>videoElementId</tt>.
  327. *
  328. * @param videoElementId the id of local or remote video element.
  329. */
  330. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  331. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  332. if (menuSpan.length) {
  333. menuSpan.remove();
  334. }
  335. }
  336. RemoteVideo.createContainer = function (spanId) {
  337. var container = document.createElement('span');
  338. container.id = spanId;
  339. container.className = 'videocontainer';
  340. var remotes = document.getElementById('remoteVideos');
  341. return remotes.appendChild(container);
  342. };
  343. module.exports = RemoteVideo;