Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RemoteVideo.js 12KB

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