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 RTCBrowserType = require("../../RTC/RTCBrowserType");
  6. var UIUtils = require("../util/UIUtil");
  7. var XMPPEvents = require("../../../service/xmpp/XMPPEvents");
  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.bindHoverHandler();
  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')) {
  73. event.preventDefault();
  74. }
  75. var isMute = !!self.isMuted;
  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. // Make sure that the large video is updated if are removing its
  144. // corresponding small video.
  145. this.VideoLayout.updateRemovedVideo(this.getResourceJid());
  146. // Remove whole container
  147. if (this.container.parentNode)
  148. this.container.parentNode.removeChild(this.container);
  149. };
  150. RemoteVideo.prototype.waitForPlayback = function (sel, stream) {
  151. var isVideo = stream.getVideoTracks().length > 0;
  152. if (!isVideo || stream.id === 'mixedmslabel') {
  153. return;
  154. }
  155. var self = this;
  156. var resourceJid = this.getResourceJid();
  157. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  158. // when video playback starts
  159. var onPlayingHandler = function () {
  160. // FIXME: why do i have to do this for FF?
  161. if (RTCBrowserType.isFirefox()) {
  162. APP.RTC.attachMediaStream(sel, stream);
  163. }
  164. if (RTCBrowserType.isTemasysPluginUsed()) {
  165. sel = self.selectVideoElement();
  166. }
  167. self.VideoLayout.videoactive(sel, resourceJid);
  168. sel[0].onplaying = null;
  169. if (RTCBrowserType.isTemasysPluginUsed()) {
  170. // 'currentTime' is used to check if the video has started
  171. // and the value is not set by the plugin, so we do it
  172. sel[0].currentTime = 1;
  173. }
  174. };
  175. sel[0].onplaying = onPlayingHandler;
  176. };
  177. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  178. if (!this.container)
  179. return;
  180. var self = this;
  181. var isVideo = stream.getVideoTracks().length > 0;
  182. var streamElement = SmallVideo.createStreamElement(stream);
  183. var newElementId = streamElement.id;
  184. // Put new stream element always in front
  185. UIUtils.prependChild(this.container, streamElement);
  186. var sel = $('#' + newElementId);
  187. sel.hide();
  188. // If the container is currently visible we attach the stream.
  189. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  190. this.waitForPlayback(sel, stream);
  191. APP.RTC.attachMediaStream(sel, stream);
  192. }
  193. APP.RTC.addMediaStreamInactiveHandler(
  194. stream, function () {
  195. console.log('stream ended', this);
  196. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  197. });
  198. // Add click handler.
  199. var onClickHandler = function (event) {
  200. self.VideoLayout.handleVideoThumbClicked(false, self.getResourceJid());
  201. // On IE we need to populate this handler on video <object>
  202. // and it does not give event instance as an argument,
  203. // so we check here for methods.
  204. if (event.stopPropagation && event.preventDefault) {
  205. event.stopPropagation();
  206. event.preventDefault();
  207. }
  208. return false;
  209. };
  210. this.container.onclick = onClickHandler;
  211. // reselect
  212. if (RTCBrowserType.isTemasysPluginUsed())
  213. sel = $('#' + newElementId);
  214. sel[0].onclick = onClickHandler;
  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. // Call showAvatar with undefined, so that we'll figure out if avatar
  230. // should be displayed based on video muted status and whether or not
  231. // it's in the lastN set
  232. this.showAvatar(undefined);
  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( "Unable to set displayName - " + this.videoSpanId +
  284. " 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. $('#' + this.videoSpanId + '_name').html(displayName);
  292. }
  293. else if (key && key.length > 0) {
  294. var nameHtml = APP.translation.generateTranslationHTML(key);
  295. $('#' + this.videoSpanId + '_name').html(nameHtml);
  296. }
  297. else
  298. $('#' + this.videoSpanId + '_name').text(
  299. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  300. } else {
  301. nameSpan = document.createElement('span');
  302. nameSpan.className = 'displayname';
  303. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  304. if (displayName && displayName.length > 0) {
  305. nameSpan.innerText = displayName;
  306. }
  307. else
  308. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  309. nameSpan.id = this.videoSpanId + '_name';
  310. }
  311. };
  312. /**
  313. * Removes remote video menu element from video element identified by
  314. * given <tt>videoElementId</tt>.
  315. *
  316. * @param videoElementId the id of local or remote video element.
  317. */
  318. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  319. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  320. if (menuSpan.length) {
  321. menuSpan.remove();
  322. }
  323. };
  324. RemoteVideo.prototype.getResourceJid = function () {
  325. if (!this.resourceJid) {
  326. console.error("Undefined resource jid");
  327. }
  328. return this.resourceJid;
  329. };
  330. RemoteVideo.createContainer = function (spanId) {
  331. var container = document.createElement('span');
  332. container.id = spanId;
  333. container.className = 'videocontainer';
  334. var remotes = document.getElementById('remoteVideos');
  335. return remotes.appendChild(container);
  336. };
  337. module.exports = RemoteVideo;