Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RemoteVideo.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. var removedVideoSrc = null;
  114. if (isVideo) {
  115. select = $('#' + id);
  116. removedVideoSrc = APP.RTC.getVideoSrc(select.get(0));
  117. }
  118. else
  119. select = $('#' + this.videoSpanId + '>audio');
  120. // Mark video as removed to cancel waiting loop(if video is removed
  121. // before has started)
  122. select.removed = true;
  123. select.remove();
  124. var audioCount = $('#' + this.videoSpanId + '>audio').length;
  125. var videoCount = $('#' + this.videoSpanId + '>video').length;
  126. if (!audioCount && !videoCount) {
  127. console.log("Remove whole user", this.videoSpanId);
  128. if(this.connectionIndicator)
  129. this.connectionIndicator.remove();
  130. // Remove whole container
  131. this.container.remove();
  132. this.VideoLayout.resizeThumbnails();
  133. }
  134. if (removedVideoSrc)
  135. this.VideoLayout.updateRemovedVideo(removedVideoSrc);
  136. };
  137. RemoteVideo.prototype.addRemoteStreamElement = function (sid, stream, thessrc) {
  138. var isVideo = stream.getVideoTracks().length > 0;
  139. if(!this.container)
  140. return;
  141. var streamElement = SmallVideo.createStreamElement(sid, stream);
  142. var newElementId = streamElement.id;
  143. this.container.appendChild(streamElement);
  144. var sel = $('#' + newElementId);
  145. sel.hide();
  146. // If the container is currently visible we attach the stream.
  147. if (!isVideo
  148. || (this.container.offsetParent !== null && isVideo)) {
  149. APP.RTC.attachMediaStream(sel, stream);
  150. if (isVideo)
  151. this.waitForRemoteVideo(sel, thessrc, stream);
  152. }
  153. var self = this;
  154. stream.onended = function () {
  155. console.log('stream ended', this);
  156. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  157. };
  158. // Add click handler.
  159. this.container.onclick = function (event) {
  160. /*
  161. * FIXME It turns out that videoThumb may not exist (if there is
  162. * no actual video).
  163. */
  164. var videoThumb = $('#' + self.videoSpanId + '>video').get(0);
  165. if (videoThumb) {
  166. self.VideoLayout.handleVideoThumbClicked(
  167. false,
  168. self.resourceJid);
  169. }
  170. event.stopPropagation();
  171. event.preventDefault();
  172. return false;
  173. };
  174. //FIXME
  175. // Add hover handler
  176. $(this.container).hover(
  177. function() {
  178. self.showDisplayName(true);
  179. },
  180. function() {
  181. var videoSrc = null;
  182. if ($('#' + self.videoSpanId + '>video')
  183. && $('#' + self.videoSpanId + '>video').length > 0) {
  184. videoSrc = APP.RTC.getVideoSrc($('#' + self.videoSpanId + '>video').get(0));
  185. }
  186. // If the video has been "pinned" by the user we want to
  187. // keep the display name on place.
  188. if (!LargeVideo.isLargeVideoVisible()
  189. || videoSrc !== APP.RTC.getVideoSrc($('#largeVideo')[0]))
  190. self.showDisplayName(false);
  191. }
  192. );
  193. }
  194. RemoteVideo.prototype.waitForRemoteVideo = function(selector, ssrc, stream) {
  195. if (selector.removed || !selector.parent().is(":visible")) {
  196. console.warn("Media removed before had started", selector);
  197. return;
  198. }
  199. if (stream.id === 'mixedmslabel') return;
  200. if (selector[0].currentTime > 0) {
  201. APP.RTC.attachMediaStream(selector, stream); // FIXME: why do i have to do this for FF?
  202. this.VideoLayout.videoactive(selector, this.resourceJid);
  203. } else {
  204. var self = this;
  205. setTimeout(function () {
  206. self.waitForRemoteVideo(selector, ssrc, stream);
  207. }, 250);
  208. }
  209. }
  210. /**
  211. * Show/hide peer container for the given resourceJid.
  212. */
  213. RemoteVideo.prototype.showPeerContainer = function (state) {
  214. if (!this.container)
  215. return;
  216. var isHide = state === 'hide';
  217. var resizeThumbnails = false;
  218. if (!isHide) {
  219. if (!$(this.container).is(':visible')) {
  220. resizeThumbnails = true;
  221. $(this.container).show();
  222. }
  223. Avatar.showUserAvatar(this.peerJid, (state !== 'show'));
  224. }
  225. else if ($(this.container).is(':visible') && isHide)
  226. {
  227. resizeThumbnails = true;
  228. $(this.container).hide();
  229. if(this.connectionIndicator)
  230. this.connectionIndicator.hide();
  231. }
  232. if (resizeThumbnails) {
  233. this.VideoLayout.resizeThumbnails();
  234. }
  235. // We want to be able to pin a participant from the contact list, even
  236. // if he's not in the lastN set!
  237. // ContactList.setClickable(resourceJid, !isHide);
  238. };
  239. RemoteVideo.prototype.removeConnectionIndicator = function () {
  240. if(this.connectionIndicator)
  241. this.connectionIndicator.remove();
  242. }
  243. RemoteVideo.prototype.hideConnectionIndicator = function () {
  244. if(this.connectionIndicator)
  245. this.connectionIndicator.hide();
  246. }
  247. /**
  248. * Updates the remote video menu.
  249. *
  250. * @param jid the jid indicating the video for which we're adding a menu.
  251. * @param isMuted indicates the current mute state
  252. */
  253. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  254. var muteMenuItem
  255. = $('#remote_popupmenu_'
  256. + this.resourceJid
  257. + '>li>a.mutelink');
  258. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  259. if (muteMenuItem.length) {
  260. var muteLink = muteMenuItem.get(0);
  261. if (isMuted === 'true') {
  262. muteLink.innerHTML = mutedIndicator + ' Muted';
  263. muteLink.className = 'mutelink disabled';
  264. }
  265. else {
  266. muteLink.innerHTML = mutedIndicator + ' Mute';
  267. muteLink.className = 'mutelink';
  268. }
  269. }
  270. }
  271. /**
  272. * Sets the display name for the given video span id.
  273. */
  274. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  275. if (!this.container) {
  276. console.warn(
  277. "Unable to set displayName - " + this.videoSpanId + " does not exist");
  278. return;
  279. }
  280. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  281. // If we already have a display name for this video.
  282. if (nameSpan.length > 0) {
  283. if (displayName && displayName.length > 0)
  284. {
  285. $('#' + this.videoSpanId + '_name').html(displayName);
  286. }
  287. else if (key && key.length > 0)
  288. {
  289. var nameHtml = APP.translation.generateTranslatonHTML(key);
  290. $('#' + this.videoSpanId + '_name').html(nameHtml);
  291. }
  292. else
  293. $('#' + this.videoSpanId + '_name').text(
  294. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  295. } else {
  296. nameSpan = document.createElement('span');
  297. nameSpan.className = 'displayname';
  298. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  299. if (displayName && displayName.length > 0) {
  300. nameSpan.innerText = displayName;
  301. }
  302. else
  303. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  304. nameSpan.id = this.videoSpanId + '_name';
  305. }
  306. }
  307. /**
  308. * Removes remote video menu element from video element identified by
  309. * given <tt>videoElementId</tt>.
  310. *
  311. * @param videoElementId the id of local or remote video element.
  312. */
  313. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  314. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  315. if (menuSpan.length) {
  316. menuSpan.remove();
  317. }
  318. }
  319. RemoteVideo.createContainer = function (spanId) {
  320. var container = document.createElement('span');
  321. container.id = spanId;
  322. container.className = 'videocontainer';
  323. var remotes = document.getElementById('remoteVideos');
  324. return remotes.appendChild(container);
  325. };
  326. module.exports = RemoteVideo;