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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.bindHoverHandler();
  23. this.flipX = false;
  24. this.isLocal = false;
  25. }
  26. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  27. RemoteVideo.prototype.constructor = RemoteVideo;
  28. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  29. this.container = RemoteVideo.createContainer(this.videoSpanId);
  30. if (APP.xmpp.isModerator())
  31. this.addRemoteVideoMenu();
  32. AudioLevels.updateAudioLevelCanvas(this.peerJid, this.VideoLayout);
  33. return this.container;
  34. };
  35. /**
  36. * Adds the remote video menu element for the given <tt>jid</tt> in the
  37. * given <tt>parentElement</tt>.
  38. *
  39. * @param jid the jid indicating the video for which we're adding a menu.
  40. * @param parentElement the parent element where this menu will be added
  41. */
  42. if (!interfaceConfig.filmStripOnly) {
  43. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  44. var spanElement = document.createElement('span');
  45. spanElement.className = 'remotevideomenu';
  46. this.container.appendChild(spanElement);
  47. var menuElement = document.createElement('i');
  48. menuElement.className = 'fa fa-angle-down';
  49. menuElement.title = 'Remote user controls';
  50. spanElement.appendChild(menuElement);
  51. var popupmenuElement = document.createElement('ul');
  52. popupmenuElement.className = 'popupmenu';
  53. popupmenuElement.id = 'remote_popupmenu_' + this.getResourceJid();
  54. spanElement.appendChild(popupmenuElement);
  55. var muteMenuItem = document.createElement('li');
  56. var muteLinkItem = document.createElement('a');
  57. var mutedIndicator = "<i style='float:left;' " +
  58. "class='icon-mic-disabled'></i>";
  59. if (!this.isMuted) {
  60. muteLinkItem.innerHTML = mutedIndicator +
  61. " <div style='width: 90px;margin-left: 20px;' " +
  62. "data-i18n='videothumbnail.domute'></div>";
  63. muteLinkItem.className = 'mutelink';
  64. }
  65. else {
  66. muteLinkItem.innerHTML = mutedIndicator +
  67. " <div style='width: 90px;margin-left: 20px;' " +
  68. "data-i18n='videothumbnail.muted'></div>";
  69. muteLinkItem.className = 'mutelink disabled';
  70. }
  71. var self = this;
  72. muteLinkItem.onclick = function(){
  73. if ($(this).attr('disabled') != undefined) {
  74. event.preventDefault();
  75. }
  76. var isMute = self.isMuted == true;
  77. APP.xmpp.setMute(self.peerJid, !isMute);
  78. popupmenuElement.setAttribute('style', 'display:none;');
  79. if (isMute) {
  80. this.innerHTML = mutedIndicator +
  81. " <div style='width: 90px;margin-left: 20px;' " +
  82. "data-i18n='videothumbnail.muted'></div>";
  83. this.className = 'mutelink disabled';
  84. }
  85. else {
  86. this.innerHTML = mutedIndicator +
  87. " <div style='width: 90px;margin-left: 20px;' " +
  88. "data-i18n='videothumbnail.domute'></div>";
  89. this.className = 'mutelink';
  90. }
  91. };
  92. muteMenuItem.appendChild(muteLinkItem);
  93. popupmenuElement.appendChild(muteMenuItem);
  94. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  95. var ejectMenuItem = document.createElement('li');
  96. var ejectLinkItem = document.createElement('a');
  97. var ejectText = "<div style='width: 90px;margin-left: 20px;' " +
  98. "data-i18n='videothumbnail.kick'>&nbsp;</div>";
  99. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  100. ejectLinkItem.onclick = function(){
  101. APP.xmpp.eject(self.peerJid);
  102. popupmenuElement.setAttribute('style', 'display:none;');
  103. };
  104. ejectMenuItem.appendChild(ejectLinkItem);
  105. popupmenuElement.appendChild(ejectMenuItem);
  106. var paddingSpan = document.createElement('span');
  107. paddingSpan.className = 'popupmenuPadding';
  108. popupmenuElement.appendChild(paddingSpan);
  109. APP.translation.translateElement(
  110. $("#" + popupmenuElement.id + " > li > a > div"));
  111. };
  112. } else {
  113. RemoteVideo.prototype.addRemoteVideoMenu = function() {}
  114. }
  115. /**
  116. * Removes the remote stream element corresponding to the given stream and
  117. * parent container.
  118. *
  119. * @param stream the stream
  120. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  121. */
  122. RemoteVideo.prototype.removeRemoteStreamElement =
  123. function (stream, isVideo, id) {
  124. if (!this.container)
  125. return false;
  126. var select = null;
  127. if (isVideo) {
  128. select = $('#' + id);
  129. }
  130. else
  131. select = $('#' + this.videoSpanId + '>audio');
  132. select.remove();
  133. console.info((isVideo ? "Video" : "Audio") +
  134. " removed " + this.getResourceJid(), select);
  135. if (isVideo)
  136. this.VideoLayout.updateRemovedVideo(this.getResourceJid());
  137. };
  138. /**
  139. * Removes RemoteVideo from the page.
  140. */
  141. RemoteVideo.prototype.remove = function () {
  142. console.log("Remove thumbnail", this.peerJid);
  143. this.removeConnectionIndicator();
  144. // Remove whole container
  145. if (this.container.parentNode)
  146. this.container.parentNode.removeChild(this.container);
  147. };
  148. RemoteVideo.prototype.waitForPlayback = function (sel, stream) {
  149. var isVideo = stream.getVideoTracks().length > 0;
  150. if (!isVideo || stream.id === 'mixedmslabel') {
  151. return;
  152. }
  153. var self = this;
  154. var resourceJid = this.getResourceJid();
  155. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  156. // when video playback starts
  157. var onPlayingHandler = function () {
  158. // FIXME: why do i have to do this for FF?
  159. if (RTCBrowserType.isFirefox()) {
  160. APP.RTC.attachMediaStream(sel, stream);
  161. }
  162. if (RTCBrowserType.isTemasysPluginUsed()) {
  163. sel = self.selectVideoElement();
  164. }
  165. self.VideoLayout.videoactive(sel, resourceJid);
  166. sel[0].onplaying = null;
  167. if (RTCBrowserType.isTemasysPluginUsed()) {
  168. // 'currentTime' is used to check if the video has started
  169. // and the value is not set by the plugin, so we do it
  170. sel[0].currentTime = 1;
  171. }
  172. };
  173. sel[0].onplaying = onPlayingHandler;
  174. };
  175. RemoteVideo.prototype.addRemoteStreamElement = function (sid, stream, thessrc) {
  176. if (!this.container)
  177. return;
  178. var self = this;
  179. var isVideo = stream.getVideoTracks().length > 0;
  180. var streamElement = SmallVideo.createStreamElement(sid, stream);
  181. var newElementId = streamElement.id;
  182. // Put new stream element always in front
  183. UIUtils.prependChild(this.container, streamElement);
  184. var sel = $('#' + newElementId);
  185. sel.hide();
  186. // If the container is currently visible we attach the stream.
  187. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  188. this.waitForPlayback(sel, stream);
  189. APP.RTC.attachMediaStream(sel, stream);
  190. }
  191. stream.onended = function () {
  192. console.log('stream ended', this);
  193. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  194. };
  195. // Add click handler.
  196. var onClickHandler = function (event) {
  197. self.VideoLayout.handleVideoThumbClicked(false, self.getResourceJid());
  198. // On IE we need to populate this handler on video <object>
  199. // and it does not give event instance as an argument,
  200. // so we check here for methods.
  201. if (event.stopPropagation && event.preventDefault) {
  202. event.stopPropagation();
  203. event.preventDefault();
  204. }
  205. return false;
  206. };
  207. this.container.onclick = onClickHandler;
  208. // reselect
  209. if (RTCBrowserType.isTemasysPluginUsed())
  210. sel = $('#' + newElementId);
  211. sel[0].onclick = onClickHandler;
  212. },
  213. /**
  214. * Show/hide peer container for the given resourceJid.
  215. */
  216. RemoteVideo.prototype.showPeerContainer = function (state) {
  217. if (!this.container)
  218. return;
  219. var isHide = state === 'hide';
  220. var resizeThumbnails = false;
  221. if (!isHide) {
  222. if (!$(this.container).is(':visible')) {
  223. resizeThumbnails = true;
  224. $(this.container).show();
  225. }
  226. this.showAvatar(state !== 'show');
  227. }
  228. else if ($(this.container).is(':visible') && isHide)
  229. {
  230. resizeThumbnails = true;
  231. $(this.container).hide();
  232. if(this.connectionIndicator)
  233. this.connectionIndicator.hide();
  234. }
  235. if (resizeThumbnails) {
  236. this.VideoLayout.resizeThumbnails();
  237. }
  238. // We want to be able to pin a participant from the contact list, even
  239. // if he's not in the lastN set!
  240. // ContactList.setClickable(resourceJid, !isHide);
  241. };
  242. RemoteVideo.prototype.removeConnectionIndicator = function () {
  243. if (this.connectionIndicator)
  244. this.connectionIndicator.remove();
  245. };
  246. RemoteVideo.prototype.hideConnectionIndicator = function () {
  247. if (this.connectionIndicator)
  248. this.connectionIndicator.hide();
  249. };
  250. /**
  251. * Updates the remote video menu.
  252. *
  253. * @param jid the jid indicating the video for which we're adding a menu.
  254. * @param isMuted indicates the current mute state
  255. */
  256. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  257. var muteMenuItem
  258. = $('#remote_popupmenu_' + this.getResourceJid() + '>li>a.mutelink');
  259. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  260. if (muteMenuItem.length) {
  261. var muteLink = muteMenuItem.get(0);
  262. if (isMuted === 'true') {
  263. muteLink.innerHTML = mutedIndicator + ' Muted';
  264. muteLink.className = 'mutelink disabled';
  265. }
  266. else {
  267. muteLink.innerHTML = mutedIndicator + ' Mute';
  268. muteLink.className = 'mutelink';
  269. }
  270. }
  271. };
  272. /**
  273. * Sets the display name for the given video span id.
  274. */
  275. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  276. if (!this.container) {
  277. console.warn( "Unable to set displayName - " + this.videoSpanId +
  278. " does not exist");
  279. return;
  280. }
  281. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  282. // If we already have a display name for this video.
  283. if (nameSpan.length > 0) {
  284. if (displayName && displayName.length > 0) {
  285. $('#' + this.videoSpanId + '_name').html(displayName);
  286. }
  287. else if (key && key.length > 0) {
  288. var nameHtml = APP.translation.generateTranslationHTML(key);
  289. $('#' + this.videoSpanId + '_name').html(nameHtml);
  290. }
  291. else
  292. $('#' + this.videoSpanId + '_name').text(
  293. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  294. } else {
  295. nameSpan = document.createElement('span');
  296. nameSpan.className = 'displayname';
  297. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  298. if (displayName && displayName.length > 0) {
  299. nameSpan.innerText = displayName;
  300. }
  301. else
  302. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  303. nameSpan.id = this.videoSpanId + '_name';
  304. }
  305. };
  306. /**
  307. * Removes remote video menu element from video element identified by
  308. * given <tt>videoElementId</tt>.
  309. *
  310. * @param videoElementId the id of local or remote video element.
  311. */
  312. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  313. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  314. if (menuSpan.length) {
  315. menuSpan.remove();
  316. }
  317. };
  318. RemoteVideo.prototype.getResourceJid = function () {
  319. if (!this.resourceJid) {
  320. console.error("Undefined resource jid");
  321. }
  322. return this.resourceJid;
  323. };
  324. RemoteVideo.createContainer = function (spanId) {
  325. var container = document.createElement('span');
  326. container.id = spanId;
  327. container.className = 'videocontainer';
  328. var remotes = document.getElementById('remoteVideos');
  329. return remotes.appendChild(container);
  330. };
  331. module.exports = RemoteVideo;