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.

VideoLayout.js 28KB

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