Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VideoLayout.js 29KB

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