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. stream.onended = function () {
  194. console.log('stream ended', this);
  195. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  196. };
  197. // Add click handler.
  198. var onClickHandler = function (event) {
  199. self.VideoLayout.handleVideoThumbClicked(false, self.getResourceJid());
  200. // On IE we need to populate this handler on video <object>
  201. // and it does not give event instance as an argument,
  202. // so we check here for methods.
  203. if (event.stopPropagation && event.preventDefault) {
  204. event.stopPropagation();
  205. event.preventDefault();
  206. }
  207. return false;
  208. };
  209. this.container.onclick = onClickHandler;
  210. // reselect
  211. if (RTCBrowserType.isTemasysPluginUsed())
  212. sel = $('#' + newElementId);
  213. sel[0].onclick = onClickHandler;
  214. },
  215. /**
  216. * Show/hide peer container for the given resourceJid.
  217. */
  218. RemoteVideo.prototype.showPeerContainer = function (state) {
  219. if (!this.container)
  220. return;
  221. var isHide = state === 'hide';
  222. var resizeThumbnails = false;
  223. if (!isHide) {
  224. if (!$(this.container).is(':visible')) {
  225. resizeThumbnails = true;
  226. $(this.container).show();
  227. }
  228. // Call showAvatar with undefined, so that we'll figure out if avatar
  229. // should be displayed based on video muted status and whether or not
  230. // it's in the lastN set
  231. this.showAvatar(undefined);
  232. }
  233. else if ($(this.container).is(':visible') && isHide)
  234. {
  235. resizeThumbnails = true;
  236. $(this.container).hide();
  237. if(this.connectionIndicator)
  238. this.connectionIndicator.hide();
  239. }
  240. if (resizeThumbnails) {
  241. this.VideoLayout.resizeThumbnails();
  242. }
  243. // We want to be able to pin a participant from the contact list, even
  244. // if he's not in the lastN set!
  245. // ContactList.setClickable(resourceJid, !isHide);
  246. };
  247. RemoteVideo.prototype.removeConnectionIndicator = function () {
  248. if (this.connectionIndicator)
  249. this.connectionIndicator.remove();
  250. };
  251. RemoteVideo.prototype.hideConnectionIndicator = function () {
  252. if (this.connectionIndicator)
  253. this.connectionIndicator.hide();
  254. };
  255. /**
  256. * Updates the remote video menu.
  257. *
  258. * @param jid the jid indicating the video for which we're adding a menu.
  259. * @param isMuted indicates the current mute state
  260. */
  261. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  262. var muteMenuItem
  263. = $('#remote_popupmenu_' + this.getResourceJid() + '>li>a.mutelink');
  264. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  265. if (muteMenuItem.length) {
  266. var muteLink = muteMenuItem.get(0);
  267. if (isMuted === 'true') {
  268. muteLink.innerHTML = mutedIndicator + ' Muted';
  269. muteLink.className = 'mutelink disabled';
  270. }
  271. else {
  272. muteLink.innerHTML = mutedIndicator + ' Mute';
  273. muteLink.className = 'mutelink';
  274. }
  275. }
  276. };
  277. /**
  278. * Sets the display name for the given video span id.
  279. */
  280. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  281. if (!this.container) {
  282. console.warn( "Unable to set displayName - " + this.videoSpanId +
  283. " does not exist");
  284. return;
  285. }
  286. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  287. // If we already have a display name for this video.
  288. if (nameSpan.length > 0) {
  289. if (displayName && displayName.length > 0) {
  290. $('#' + this.videoSpanId + '_name').html(displayName);
  291. }
  292. else if (key && key.length > 0) {
  293. var nameHtml = APP.translation.generateTranslationHTML(key);
  294. $('#' + this.videoSpanId + '_name').html(nameHtml);
  295. }
  296. else
  297. $('#' + this.videoSpanId + '_name').text(
  298. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  299. } else {
  300. nameSpan = document.createElement('span');
  301. nameSpan.className = 'displayname';
  302. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  303. if (displayName && displayName.length > 0) {
  304. nameSpan.innerText = displayName;
  305. }
  306. else
  307. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  308. nameSpan.id = this.videoSpanId + '_name';
  309. }
  310. };
  311. /**
  312. * Removes remote video menu element from video element identified by
  313. * given <tt>videoElementId</tt>.
  314. *
  315. * @param videoElementId the id of local or remote video element.
  316. */
  317. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  318. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  319. if (menuSpan.length) {
  320. menuSpan.remove();
  321. }
  322. };
  323. RemoteVideo.prototype.getResourceJid = function () {
  324. if (!this.resourceJid) {
  325. console.error("Undefined resource jid");
  326. }
  327. return this.resourceJid;
  328. };
  329. RemoteVideo.createContainer = function (spanId) {
  330. var container = document.createElement('span');
  331. container.id = spanId;
  332. container.className = 'videocontainer';
  333. var remotes = document.getElementById('remoteVideos');
  334. return remotes.appendChild(container);
  335. };
  336. module.exports = RemoteVideo;