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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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.VideoLayout.getPeerVideoSel(resourceJid);
  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. // Name of video element name is different for IE/Safari
  195. var videoElem = APP.RTC.getVideoElementName();
  196. // Add click handler.
  197. var onClickHandler = function (event) {
  198. self.VideoLayout.handleVideoThumbClicked(false, self.getResourceJid());
  199. // On IE we need to populate this handler on video <object>
  200. // and it does not give event instance as an argument,
  201. // so we check here for methods.
  202. if (event.stopPropagation && event.preventDefault) {
  203. event.stopPropagation();
  204. event.preventDefault();
  205. }
  206. return false;
  207. };
  208. this.container.onclick = onClickHandler;
  209. // reselect
  210. if (RTCBrowserType.isTemasysPluginUsed())
  211. sel = $('#' + newElementId);
  212. sel[0].onclick = onClickHandler;
  213. //FIXME
  214. // Add hover handler
  215. $(this.container).hover(
  216. function() {
  217. self.showDisplayName(true);
  218. },
  219. function() {
  220. // If the video has been "pinned" by the user we want to
  221. // keep the display name on place.
  222. if (!LargeVideo.isLargeVideoVisible() ||
  223. !LargeVideo.isCurrentlyOnLarge(self.getResourceJid()))
  224. self.showDisplayName(false);
  225. }
  226. );
  227. },
  228. /**
  229. * Show/hide peer container for the given resourceJid.
  230. */
  231. RemoteVideo.prototype.showPeerContainer = function (state) {
  232. if (!this.container)
  233. return;
  234. var isHide = state === 'hide';
  235. var resizeThumbnails = false;
  236. if (!isHide) {
  237. if (!$(this.container).is(':visible')) {
  238. resizeThumbnails = true;
  239. $(this.container).show();
  240. }
  241. this.showAvatar(state !== 'show');
  242. }
  243. else if ($(this.container).is(':visible') && isHide)
  244. {
  245. resizeThumbnails = true;
  246. $(this.container).hide();
  247. if(this.connectionIndicator)
  248. this.connectionIndicator.hide();
  249. }
  250. if (resizeThumbnails) {
  251. this.VideoLayout.resizeThumbnails();
  252. }
  253. // We want to be able to pin a participant from the contact list, even
  254. // if he's not in the lastN set!
  255. // ContactList.setClickable(resourceJid, !isHide);
  256. };
  257. RemoteVideo.prototype.removeConnectionIndicator = function () {
  258. if (this.connectionIndicator)
  259. this.connectionIndicator.remove();
  260. };
  261. RemoteVideo.prototype.hideConnectionIndicator = function () {
  262. if (this.connectionIndicator)
  263. this.connectionIndicator.hide();
  264. };
  265. /**
  266. * Updates the remote video menu.
  267. *
  268. * @param jid the jid indicating the video for which we're adding a menu.
  269. * @param isMuted indicates the current mute state
  270. */
  271. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  272. var muteMenuItem
  273. = $('#remote_popupmenu_' + this.getResourceJid() + '>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( "Unable to set displayName - " + this.videoSpanId +
  293. " 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. $('#' + this.videoSpanId + '_name').html(displayName);
  301. }
  302. else if (key && key.length > 0) {
  303. var nameHtml = APP.translation.generateTranslationHTML(key);
  304. $('#' + this.videoSpanId + '_name').html(nameHtml);
  305. }
  306. else
  307. $('#' + this.videoSpanId + '_name').text(
  308. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  309. } else {
  310. nameSpan = document.createElement('span');
  311. nameSpan.className = 'displayname';
  312. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  313. if (displayName && displayName.length > 0) {
  314. nameSpan.innerText = displayName;
  315. }
  316. else
  317. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  318. nameSpan.id = this.videoSpanId + '_name';
  319. }
  320. };
  321. /**
  322. * Removes remote video menu element from video element identified by
  323. * given <tt>videoElementId</tt>.
  324. *
  325. * @param videoElementId the id of local or remote video element.
  326. */
  327. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  328. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  329. if (menuSpan.length) {
  330. menuSpan.remove();
  331. }
  332. };
  333. RemoteVideo.prototype.getResourceJid = function () {
  334. if (!this.resourceJid) {
  335. console.error("Undefined resource jid");
  336. }
  337. return this.resourceJid;
  338. };
  339. RemoteVideo.createContainer = function (spanId) {
  340. var container = document.createElement('span');
  341. container.id = spanId;
  342. container.className = 'videocontainer';
  343. var remotes = document.getElementById('remoteVideos');
  344. return remotes.appendChild(container);
  345. };
  346. module.exports = RemoteVideo;