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

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