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

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