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.

VideoLayout.js 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. var AudioLevels = require("../audio_levels/AudioLevels");
  2. var Avatar = require("../avatar/Avatar");
  3. var ContactList = require("../side_pannels/contactlist/ContactList");
  4. var MediaStreamType = require("../../../service/RTC/MediaStreamTypes");
  5. var UIEvents = require("../../../service/UI/UIEvents");
  6. var RTC = require("../../RTC/RTC");
  7. var RTCBrowserType = require('../../RTC/RTCBrowserType');
  8. var RemoteVideo = require("./RemoteVideo");
  9. var LargeVideo = require("./LargeVideo");
  10. var LocalVideo = require("./LocalVideo");
  11. var remoteVideos = {};
  12. var localVideoThumbnail = null;
  13. var currentDominantSpeaker = null;
  14. var lastNCount = config.channelLastN;
  15. var localLastNCount = config.channelLastN;
  16. var localLastNSet = [];
  17. var lastNEndpointsCache = [];
  18. var lastNPickupJid = null;
  19. var eventEmitter = null;
  20. /**
  21. * Currently focused video jid
  22. * @type {String}
  23. */
  24. var focusedVideoResourceJid = null;
  25. var VideoLayout = (function (my) {
  26. my.init = function (emitter) {
  27. eventEmitter = emitter;
  28. localVideoThumbnail = new LocalVideo(VideoLayout);
  29. VideoLayout.resizeLargeVideoContainer();
  30. LargeVideo.init(VideoLayout, emitter);
  31. };
  32. my.isInLastN = function(resource) {
  33. return lastNCount < 0 // lastN is disabled, return true
  34. || (lastNCount > 0 && lastNEndpointsCache.length == 0) // lastNEndpoints cache not built yet, return true
  35. || (lastNEndpointsCache && lastNEndpointsCache.indexOf(resource) !== -1);
  36. };
  37. my.changeLocalStream = function (stream, isMuted) {
  38. VideoLayout.changeLocalVideo(stream, isMuted);
  39. };
  40. my.changeLocalAudio = function(stream, isMuted) {
  41. if (isMuted)
  42. APP.UI.setAudioMuted(true, true);
  43. APP.RTC.attachMediaStream($('#localAudio'), stream.getOriginalStream());
  44. var localAudio = document.getElementById('localAudio');
  45. // Writing volume not allowed in IE
  46. if (!RTCBrowserType.isIExplorer()) {
  47. localAudio.autoplay = true;
  48. localAudio.volume = 0;
  49. }
  50. };
  51. my.changeLocalVideo = function(stream, isMuted) {
  52. // Set default display name.
  53. localVideoThumbnail.setDisplayName();
  54. localVideoThumbnail.createConnectionIndicator();
  55. AudioLevels.updateAudioLevelCanvas(null, VideoLayout);
  56. localVideoThumbnail.changeVideo(stream, isMuted);
  57. /* force update if we're currently being displayed */
  58. if (LargeVideo.isCurrentlyOnLarge(APP.xmpp.myResource())) {
  59. LargeVideo.updateLargeVideo(APP.xmpp.myResource(), true);
  60. }
  61. };
  62. my.mucJoined = function () {
  63. var myResourceJid = APP.xmpp.myResource();
  64. localVideoThumbnail.joined(APP.xmpp.myJid());
  65. if (!LargeVideo.getResourceJid())
  66. LargeVideo.updateLargeVideo(myResourceJid, true);
  67. };
  68. /**
  69. * Adds or removes icons for not available camera and microphone.
  70. * @param resourceJid the jid of user
  71. * @param devices available devices
  72. */
  73. my.setDeviceAvailabilityIcons = function (resourceJid, devices) {
  74. if(!devices)
  75. return;
  76. if(!resourceJid)
  77. {
  78. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  79. }
  80. else
  81. {
  82. if(remoteVideos[resourceJid])
  83. remoteVideos[resourceJid].setDeviceAvailabilityIcons(devices);
  84. }
  85. }
  86. /**
  87. * Checks if removed video is currently displayed and tries to display
  88. * another one instead.
  89. * @param removedVideoSrc src stream identifier of the video.
  90. */
  91. my.updateRemovedVideo = function(resourceJid) {
  92. if (resourceJid === LargeVideo.getResourceJid()) {
  93. var newResourceJid;
  94. // We'll show user's avatar if he is the dominant speaker or if
  95. // his video thumbnail is pinned
  96. if (resourceJid === focusedVideoResourceJid ||
  97. resourceJid === currentDominantSpeaker) {
  98. newResourceJid = resourceJid;
  99. } else {
  100. // Otherwise select last visible video
  101. newResourceJid = this.electLastVisibleVideo();
  102. }
  103. LargeVideo.updateLargeVideo(newResourceJid);
  104. }
  105. };
  106. my.electLastVisibleVideo = function() {
  107. // pick the last visible video in the row
  108. // if nobody else is left, this picks the local video
  109. var jid;
  110. var videoElem = RTC.getVideoElementName();
  111. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last>' + videoElem);
  112. if (pick.length && APP.RTC.getVideoSrc(pick[0])) {
  113. return VideoLayout.getPeerContainerResourceJid(pick[0].parentNode);
  114. } else {
  115. console.info("Last visible video no longer exists");
  116. pick = $('#remoteVideos>span[id!="mixedstream"]>' + videoElem);
  117. if (pick.length && APP.RTC.getVideoSrc(pick[0])) {
  118. return VideoLayout.getPeerContainerResourceJid(pick[0].parentNode);
  119. } else {
  120. // Try local video
  121. console.info("Fallback to local video...");
  122. return APP.xmpp.myResource();
  123. }
  124. }
  125. };
  126. my.onRemoteStreamAdded = function (stream) {
  127. if (stream.peerjid) {
  128. VideoLayout.ensurePeerContainerExists(stream.peerjid);
  129. var resourceJid = Strophe.getResourceFromJid(stream.peerjid);
  130. remoteVideos[resourceJid].addRemoteStreamElement(
  131. stream.sid,
  132. stream.getOriginalStream(),
  133. stream.ssrc);
  134. }
  135. };
  136. my.getLargeVideoJid = function () {
  137. return LargeVideo.getResourceJid();
  138. };
  139. /**
  140. * Called when large video update is finished
  141. * @param currentSmallVideo small video currently displayed on large video
  142. */
  143. my.largeVideoUpdated = function (currentSmallVideo) {
  144. // Makes sure that dominant speaker UI
  145. // is enabled only on current small video
  146. localVideoThumbnail.enableDominantSpeaker(
  147. localVideoThumbnail === currentSmallVideo);
  148. Object.keys(remoteVideos).forEach(
  149. function (resourceJid) {
  150. var remoteVideo = remoteVideos[resourceJid];
  151. if (remoteVideo) {
  152. remoteVideo.enableDominantSpeaker(
  153. remoteVideo === currentSmallVideo);
  154. }
  155. }
  156. );
  157. };
  158. my.handleVideoThumbClicked = function(noPinnedEndpointChangedEvent,
  159. resourceJid) {
  160. if(focusedVideoResourceJid) {
  161. var oldSmallVideo = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  162. if(oldSmallVideo)
  163. oldSmallVideo.focus(false);
  164. }
  165. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  166. // Unlock current focused.
  167. if (focusedVideoResourceJid === resourceJid)
  168. {
  169. focusedVideoResourceJid = null;
  170. // Enable the currently set dominant speaker.
  171. if (currentDominantSpeaker) {
  172. if(smallVideo && smallVideo.hasVideo())
  173. {
  174. LargeVideo.updateLargeVideo(currentDominantSpeaker);
  175. }
  176. }
  177. if (!noPinnedEndpointChangedEvent) {
  178. eventEmitter.emit(UIEvents.PINNED_ENDPOINT);
  179. }
  180. return;
  181. }
  182. // Lock new video
  183. focusedVideoResourceJid = resourceJid;
  184. // Update focused/pinned interface.
  185. if (resourceJid)
  186. {
  187. if(smallVideo)
  188. smallVideo.focus(true);
  189. if (!noPinnedEndpointChangedEvent) {
  190. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  191. }
  192. }
  193. // Triggers a "video.selected" event. The "false" parameter indicates
  194. // this isn't a prezi.
  195. $(document).trigger("video.selected", [false]);
  196. LargeVideo.updateLargeVideo(resourceJid);
  197. // Writing volume not allowed in IE
  198. if (!RTCBrowserType.isIExplorer()) {
  199. $('audio').each(function (idx, el) {
  200. el.volume = 0;
  201. el.volume = 1;
  202. });
  203. }
  204. };
  205. /**
  206. * Checks if container for participant identified by given peerJid exists
  207. * in the document and creates it eventually.
  208. *
  209. * @param peerJid peer Jid to check.
  210. *
  211. * @return Returns <tt>true</tt> if the peer container exists,
  212. * <tt>false</tt> - otherwise
  213. */
  214. my.ensurePeerContainerExists = function(peerJid) {
  215. ContactList.ensureAddContact(peerJid);
  216. var resourceJid = Strophe.getResourceFromJid(peerJid);
  217. if(!remoteVideos[resourceJid])
  218. {
  219. remoteVideos[resourceJid] = new RemoteVideo(peerJid, VideoLayout);
  220. // In case this is not currently in the last n we don't show it.
  221. if (localLastNCount
  222. && localLastNCount > 0
  223. && $('#remoteVideos>span').length >= localLastNCount + 2) {
  224. remoteVideos[resourceJid].showPeerContainer('hide');
  225. }
  226. else
  227. VideoLayout.resizeThumbnails();
  228. }
  229. };
  230. my.inputDisplayNameHandler = function (name) {
  231. localVideoThumbnail.inputDisplayNameHandler(name);
  232. };
  233. my.videoactive = function (videoelem, resourceJid) {
  234. console.info(resourceJid + " video is now active");
  235. videoelem.show();
  236. VideoLayout.resizeThumbnails();
  237. // Update the large video to the last added video only if there's no
  238. // current dominant, focused speaker or prezi playing or update it to
  239. // the current dominant speaker.
  240. if ((!focusedVideoResourceJid &&
  241. !currentDominantSpeaker &&
  242. !require("../prezi/Prezi").isPresentationVisible()) ||
  243. focusedVideoResourceJid === resourceJid ||
  244. (resourceJid &&
  245. currentDominantSpeaker === resourceJid)) {
  246. LargeVideo.updateLargeVideo(resourceJid, true);
  247. }
  248. }
  249. /**
  250. * Shows the presence status message for the given video.
  251. */
  252. my.setPresenceStatus = function (resourceJid, statusMsg) {
  253. remoteVideos[resourceJid].setPresenceStatus(statusMsg);
  254. };
  255. /**
  256. * Shows a visual indicator for the moderator of the conference.
  257. */
  258. my.showModeratorIndicator = function () {
  259. var isModerator = APP.xmpp.isModerator();
  260. if (isModerator) {
  261. localVideoThumbnail.createModeratorIndicatorElement();
  262. }
  263. var members = APP.xmpp.getMembers();
  264. Object.keys(members).forEach(function (jid) {
  265. if (Strophe.getResourceFromJid(jid) === 'focus') {
  266. // Skip server side focus
  267. return;
  268. }
  269. var resourceJid = Strophe.getResourceFromJid(jid);
  270. var member = members[jid];
  271. if (member.role === 'moderator') {
  272. remoteVideos[resourceJid].removeRemoteVideoMenu();
  273. remoteVideos[resourceJid].createModeratorIndicatorElement();
  274. } else if (isModerator) {
  275. // We are moderator, but user is not - add menu
  276. if ($('#remote_popupmenu_' + resourceJid).length <= 0) {
  277. remoteVideos[resourceJid].addRemoteVideoMenu();
  278. }
  279. }
  280. });
  281. };
  282. /*
  283. * Shows or hides the audio muted indicator over the local thumbnail video.
  284. * @param {boolean} isMuted
  285. */
  286. my.showLocalAudioIndicator = function(isMuted) {
  287. localVideoThumbnail.showAudioIndicator(isMuted);
  288. };
  289. /**
  290. * Resizes the large video container.
  291. */
  292. my.resizeLargeVideoContainer = function () {
  293. LargeVideo.resize();
  294. VideoLayout.resizeThumbnails();
  295. LargeVideo.position();
  296. };
  297. /**
  298. * Resizes thumbnails.
  299. */
  300. my.resizeThumbnails = function(animate) {
  301. var videoSpaceWidth = $('#remoteVideos').width();
  302. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  303. var width = thumbnailSize[0];
  304. var height = thumbnailSize[1];
  305. $('.userAvatar').css('left', (width - height) / 2);
  306. if(animate)
  307. {
  308. $('#remoteVideos').animate({
  309. height: height
  310. },
  311. {
  312. queue: false,
  313. duration: 500
  314. });
  315. $('#remoteVideos>span').animate({
  316. height: height,
  317. width: width
  318. },
  319. {
  320. queue: false,
  321. duration: 500,
  322. complete: function () {
  323. $(document).trigger(
  324. "remotevideo.resized",
  325. [width,
  326. height]);
  327. }
  328. });
  329. }
  330. else
  331. {
  332. // size videos so that while keeping AR and max height, we have a
  333. // nice fit
  334. $('#remoteVideos').height(height);
  335. $('#remoteVideos>span').width(width);
  336. $('#remoteVideos>span').height(height);
  337. $(document).trigger("remotevideo.resized", [width, height]);
  338. }
  339. };
  340. /**
  341. * Calculates the thumbnail size.
  342. *
  343. * @param videoSpaceWidth the width of the video space
  344. */
  345. my.calculateThumbnailSize = function (videoSpaceWidth) {
  346. // Calculate the available height, which is the inner window height minus
  347. // 39px for the header minus 2px for the delimiter lines on the top and
  348. // bottom of the large video, minus the 36px space inside the remoteVideos
  349. // container used for highlighting shadow.
  350. var availableHeight = 100;
  351. var numvids = $('#remoteVideos>span:visible').length;
  352. if (localLastNCount && localLastNCount > 0) {
  353. numvids = Math.min(localLastNCount + 1, numvids);
  354. }
  355. // Remove the 3px borders arround videos and border around the remote
  356. // videos area and the 4 pixels between the local video and the others
  357. //TODO: Find out where the 4 pixels come from and remove them
  358. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  359. var availableWidth = availableWinWidth / numvids;
  360. var aspectRatio = 16.0 / 9.0;
  361. var maxHeight = Math.min(160, availableHeight);
  362. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  363. if (availableHeight < availableWidth / aspectRatio) {
  364. availableWidth = Math.floor(availableHeight * aspectRatio);
  365. }
  366. return [availableWidth, availableHeight];
  367. };
  368. /**
  369. * Returns the corresponding resource jid to the given peer container
  370. * DOM element.
  371. *
  372. * @return the corresponding resource jid to the given peer container
  373. * DOM element
  374. */
  375. my.getPeerContainerResourceJid = function (containerElement) {
  376. var i = containerElement.id.indexOf('participant_');
  377. if (i >= 0)
  378. return containerElement.id.substring(i + 12);
  379. };
  380. my.getPeerVideoSel = function (peerResourceJid) {
  381. return $('#participant_' + peerResourceJid +
  382. '>' + APP.RTC.getVideoElementName());
  383. };
  384. /**
  385. * On contact list item clicked.
  386. */
  387. $(ContactList).bind('contactclicked', function(event, jid) {
  388. if (!jid) {
  389. return;
  390. }
  391. if (jid === APP.xmpp.myJid()) {
  392. $("#localVideoContainer").click();
  393. return;
  394. }
  395. var resource = Strophe.getResourceFromJid(jid);
  396. var videoSel = VideoLayout.getPeerVideoSel(resource);
  397. if (videoSel.length > 0) {
  398. var videoThumb = videoSel[0];
  399. // It is not always the case that a videoThumb exists (if there is
  400. // no actual video).
  401. if (RTC.getVideoSrc(videoThumb)) {
  402. // We have a video src, great! Let's update the large video
  403. // now.
  404. VideoLayout.handleVideoThumbClicked(
  405. false,
  406. Strophe.getResourceFromJid(jid));
  407. } else {
  408. // If we don't have a video src for jid, there's absolutely
  409. // no point in calling handleVideoThumbClicked; Quite
  410. // simply, it won't work because it needs an src to attach
  411. // to the large video.
  412. //
  413. // Instead, we trigger the pinned endpoint changed event to
  414. // let the bridge adjust its lastN set for myjid and store
  415. // the pinned user in the lastNPickupJid variable to be
  416. // picked up later by the lastN changed event handler.
  417. lastNPickupJid = jid;
  418. eventEmitter.emit(UIEvents.PINNED_ENDPOINT,
  419. Strophe.getResourceFromJid(jid));
  420. }
  421. }
  422. });
  423. /**
  424. * On audio muted event.
  425. */
  426. my.onAudioMute = function (jid, isMuted) {
  427. var resourceJid = Strophe.getResourceFromJid(jid);
  428. if (resourceJid === APP.xmpp.myResource()) {
  429. localVideoThumbnail.showAudioIndicator(isMuted);
  430. } else {
  431. VideoLayout.ensurePeerContainerExists(jid);
  432. remoteVideos[resourceJid].showAudioIndicator(isMuted);
  433. if (APP.xmpp.isModerator()) {
  434. remoteVideos[resourceJid].updateRemoteVideoMenu(isMuted);
  435. }
  436. }
  437. };
  438. /**
  439. * On video muted event.
  440. */
  441. my.onVideoMute = function (jid, value) {
  442. if(jid !== APP.xmpp.myJid() && !APP.RTC.muteRemoteVideoStream(jid, value))
  443. return;
  444. if (jid === APP.xmpp.myJid()) {
  445. localVideoThumbnail.showVideoIndicator(value);
  446. } else {
  447. var resource = Strophe.getResourceFromJid(jid);
  448. VideoLayout.ensurePeerContainerExists(jid);
  449. remoteVideos[resource].showVideoIndicator(value);
  450. var el = VideoLayout.getPeerVideoSel(resource);
  451. if (!value)
  452. el.show();
  453. else
  454. el.hide();
  455. }
  456. };
  457. /**
  458. * Display name changed.
  459. */
  460. my.onDisplayNameChanged =
  461. function (jid, displayName, status) {
  462. if (jid === 'localVideoContainer'
  463. || jid === APP.xmpp.myJid()) {
  464. localVideoThumbnail.setDisplayName(displayName);
  465. } else {
  466. VideoLayout.ensurePeerContainerExists(jid);
  467. remoteVideos[Strophe.getResourceFromJid(jid)].setDisplayName(
  468. displayName,
  469. status);
  470. }
  471. };
  472. /**
  473. * On dominant speaker changed event.
  474. */
  475. my.onDominantSpeakerChanged = function (resourceJid) {
  476. // We ignore local user events.
  477. if (resourceJid === APP.xmpp.myResource())
  478. return;
  479. var members = APP.xmpp.getMembers();
  480. // Update the current dominant speaker.
  481. if (resourceJid !== currentDominantSpeaker) {
  482. var currentJID = APP.xmpp.findJidFromResource(currentDominantSpeaker);
  483. var newJID = APP.xmpp.findJidFromResource(resourceJid);
  484. if(currentDominantSpeaker && (!members || !members[currentJID] ||
  485. !members[currentJID].displayName)) {
  486. remoteVideos[resourceJid].setDisplayName(null);
  487. }
  488. if(resourceJid && (!members || !members[newJID] ||
  489. !members[newJID].displayName)) {
  490. remoteVideos[resourceJid].setDisplayName(null,
  491. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  492. }
  493. currentDominantSpeaker = resourceJid;
  494. } else {
  495. return;
  496. }
  497. // Obtain container for new dominant speaker.
  498. var videoSel = VideoLayout.getPeerVideoSel(resourceJid);
  499. // Local video will not have container found, but that's ok
  500. // since we don't want to switch to local video.
  501. if (!focusedVideoResourceJid && videoSel.length)
  502. {
  503. // Update the large video if the video source is already available,
  504. // otherwise wait for the "videoactive.jingle" event.
  505. if (videoSel[0].currentTime > 0) {
  506. LargeVideo.updateLargeVideo(resourceJid);
  507. }
  508. }
  509. };
  510. /**
  511. * On last N change event.
  512. *
  513. * @param lastNEndpoints the list of last N endpoints
  514. * @param endpointsEnteringLastN the list currently entering last N
  515. * endpoints
  516. */
  517. my.onLastNEndpointsChanged = function ( lastNEndpoints,
  518. endpointsEnteringLastN,
  519. stream) {
  520. if (lastNCount !== lastNEndpoints.length)
  521. lastNCount = lastNEndpoints.length;
  522. lastNEndpointsCache = lastNEndpoints;
  523. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  524. //
  525. // If LastN drops to, say, 2, because of adaptivity, then E should see
  526. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  527. // so E sees them. C is only in E's local LastN set.
  528. //
  529. // If F starts talking and LastN = 3, then E should see thumbnails for
  530. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  531. // enters E's local LastN ejecting C.
  532. // Increase the local LastN set size, if necessary.
  533. if (lastNCount > localLastNCount) {
  534. localLastNCount = lastNCount;
  535. }
  536. // Update the local LastN set preserving the order in which the
  537. // endpoints appeared in the LastN/local LastN set.
  538. var nextLocalLastNSet = lastNEndpoints.slice(0);
  539. for (var i = 0; i < localLastNSet.length; i++) {
  540. if (nextLocalLastNSet.length >= localLastNCount) {
  541. break;
  542. }
  543. var resourceJid = localLastNSet[i];
  544. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  545. nextLocalLastNSet.push(resourceJid);
  546. }
  547. }
  548. localLastNSet = nextLocalLastNSet;
  549. var updateLargeVideo = false;
  550. // Handle LastN/local LastN changes.
  551. $('#remoteVideos>span').each(function( index, element ) {
  552. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  553. var isReceived = true;
  554. if (resourceJid
  555. && lastNEndpoints.indexOf(resourceJid) < 0
  556. && localLastNSet.indexOf(resourceJid) < 0) {
  557. console.log("Remove from last N", resourceJid);
  558. remoteVideos[resourceJid].showPeerContainer('hide');
  559. isReceived = false;
  560. } else if (resourceJid
  561. && $('#participant_' + resourceJid).is(':visible')
  562. && lastNEndpoints.indexOf(resourceJid) < 0
  563. && localLastNSet.indexOf(resourceJid) >= 0) {
  564. remoteVideos[resourceJid].showPeerContainer('avatar');
  565. isReceived = false;
  566. }
  567. if (!isReceived) {
  568. // resourceJid has dropped out of the server side lastN set, so
  569. // it is no longer being received. If resourceJid was being
  570. // displayed in the large video we have to switch to another
  571. // user.
  572. if (!updateLargeVideo && resourceJid === LargeVideo.getResourceJid()) {
  573. updateLargeVideo = true;
  574. }
  575. }
  576. });
  577. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  578. endpointsEnteringLastN = lastNEndpoints;
  579. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  580. endpointsEnteringLastN.forEach(function (resourceJid) {
  581. var isVisible = $('#participant_' + resourceJid).is(':visible');
  582. remoteVideos[resourceJid].showPeerContainer('show');
  583. if (!isVisible) {
  584. console.log("Add to last N", resourceJid);
  585. var jid = APP.xmpp.findJidFromResource(resourceJid);
  586. var mediaStream = APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  587. var sel = VideoLayout.getPeerVideoSel(resourceJid);
  588. APP.RTC.attachMediaStream(sel, mediaStream.stream);
  589. if (lastNPickupJid == mediaStream.peerjid) {
  590. // Clean up the lastN pickup jid.
  591. lastNPickupJid = null;
  592. // Don't fire the events again, they've already
  593. // been fired in the contact list click handler.
  594. VideoLayout.handleVideoThumbClicked(
  595. false,
  596. Strophe.getResourceFromJid(mediaStream.peerjid));
  597. updateLargeVideo = false;
  598. }
  599. remoteVideos[resourceJid].waitForPlayback(mediaStream.stream);
  600. }
  601. });
  602. }
  603. // The endpoint that was being shown in the large video has dropped out
  604. // of the lastN set and there was no lastN pickup jid. We need to update
  605. // the large video now.
  606. if (updateLargeVideo) {
  607. var resource, container, src;
  608. var myResource
  609. = APP.xmpp.myResource();
  610. // Find out which endpoint to show in the large video.
  611. for (var i = 0; i < lastNEndpoints.length; i++) {
  612. resource = lastNEndpoints[i];
  613. if (!resource || resource === myResource)
  614. continue;
  615. // videoSrcToSsrc needs to be update for this call to succeed.
  616. LargeVideo.updateLargeVideo(resource);
  617. break;
  618. }
  619. }
  620. };
  621. /**
  622. * Updates local stats
  623. * @param percent
  624. * @param object
  625. */
  626. my.updateLocalConnectionStats = function (percent, object) {
  627. var resolution = null;
  628. if(object.resolution !== null)
  629. {
  630. resolution = object.resolution;
  631. object.resolution = resolution[APP.xmpp.myJid()];
  632. delete resolution[APP.xmpp.myJid()];
  633. }
  634. localVideoThumbnail.updateStatsIndicator(percent, object);
  635. for(var jid in resolution)
  636. {
  637. if(resolution[jid] === null)
  638. continue;
  639. var resourceJid = Strophe.getResourceFromJid(jid);
  640. if(remoteVideos[resourceJid] && remoteVideos[resourceJid].connectionIndicator)
  641. {
  642. remoteVideos[resourceJid].connectionIndicator.updateResolution(resolution[jid]);
  643. }
  644. }
  645. };
  646. /**
  647. * Updates remote stats.
  648. * @param jid the jid associated with the stats
  649. * @param percent the connection quality percent
  650. * @param object the stats data
  651. */
  652. my.updateConnectionStats = function (jid, percent, object) {
  653. var resourceJid = Strophe.getResourceFromJid(jid);
  654. if(remoteVideos[resourceJid])
  655. remoteVideos[resourceJid].updateStatsIndicator(percent, object);
  656. };
  657. /**
  658. * Removes the connection
  659. * @param jid
  660. */
  661. my.removeConnectionIndicator = function (jid) {
  662. remoteVideos[Strophe.getResourceFromJid(jid)].removeConnectionIndicator();
  663. };
  664. /**
  665. * Hides the connection indicator
  666. * @param jid
  667. */
  668. my.hideConnectionIndicator = function (jid) {
  669. remoteVideos[Strophe.getResourceFromJid(jid)].hideConnectionIndicator();
  670. };
  671. /**
  672. * Hides all the indicators
  673. */
  674. my.onStatsStop = function () {
  675. for(var video in remoteVideos)
  676. {
  677. remoteVideos[video].hideIndicator();
  678. }
  679. localVideoThumbnail.hideIndicator();
  680. };
  681. my.participantLeft = function (jid) {
  682. // Unlock large video
  683. var resourceJid = Strophe.getResourceFromJid(jid);
  684. if (focusedVideoResourceJid === resourceJid)
  685. {
  686. console.info("Focused video owner has left the conference");
  687. focusedVideoResourceJid = null;
  688. }
  689. };
  690. my.onVideoTypeChanged = function (jid) {
  691. LargeVideo.onVideoTypeChanged(jid);
  692. };
  693. my.showMore = function (jid) {
  694. if (jid === 'local')
  695. {
  696. localVideoThumbnail.connectionIndicator.showMore();
  697. }
  698. else
  699. {
  700. var remoteVideo = remoteVideos[Strophe.getResourceFromJid(jid)];
  701. if (remoteVideo) {
  702. remoteVideo.connectionIndicator.showMore();
  703. } else {
  704. console.info("Error - no remote video for jid: " + jid);
  705. }
  706. }
  707. };
  708. my.addPreziContainer = function (id) {
  709. return RemoteVideo.createContainer(id);
  710. };
  711. my.setLargeVideoVisible = function (isVisible) {
  712. LargeVideo.setLargeVideoVisible(isVisible);
  713. if(!isVisible && focusedVideoResourceJid)
  714. {
  715. var smallVideo = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  716. if(smallVideo)
  717. smallVideo.focus(false);
  718. smallVideo.showAvatar();
  719. focusedVideoResourceJid = null;
  720. }
  721. };
  722. /**
  723. * Resizes the video area
  724. * @param completeFunction a function to be called when the video space is resized
  725. */
  726. my.resizeVideoArea = function(isVisible, completeFunction) {
  727. LargeVideo.resizeVideoAreaAnimated(isVisible, completeFunction);
  728. VideoLayout.resizeThumbnails(true);
  729. };
  730. my.getSmallVideo = function (resourceJid) {
  731. if(resourceJid == APP.xmpp.myResource())
  732. {
  733. return localVideoThumbnail;
  734. }
  735. else
  736. {
  737. if(!remoteVideos[resourceJid])
  738. return null;
  739. return remoteVideos[resourceJid];
  740. }
  741. };
  742. my.userAvatarChanged = function(resourceJid, thumbUrl)
  743. {
  744. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  745. if(smallVideo)
  746. smallVideo.avatarChanged(thumbUrl);
  747. else
  748. console.warn(
  749. "Missed avatar update - no small video yet for " + resourceJid);
  750. LargeVideo.updateAvatar(resourceJid, thumbUrl);
  751. };
  752. return my;
  753. }(VideoLayout || {}));
  754. module.exports = VideoLayout;