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

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