您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 31KB

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