Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoLayout.js 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /* global config, APP, $, Strophe, require, interfaceConfig */
  2. var AudioLevels = require("../audio_levels/AudioLevels");
  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 remoteVideoTypes = {};
  13. var localVideoThumbnail = null;
  14. var currentDominantSpeaker = null;
  15. var lastNCount = config.channelLastN;
  16. var localLastNCount = config.channelLastN;
  17. var localLastNSet = [];
  18. var lastNEndpointsCache = [];
  19. var lastNPickupJid = null;
  20. var eventEmitter = null;
  21. /**
  22. * Currently focused video jid
  23. * @type {String}
  24. */
  25. var focusedVideoResourceJid = null;
  26. var VideoLayout = (function (my) {
  27. my.init = function (emitter) {
  28. eventEmitter = emitter;
  29. localVideoThumbnail = new LocalVideo(VideoLayout);
  30. LargeVideo.init(VideoLayout, emitter);
  31. VideoLayout.resizeLargeVideoContainer();
  32. };
  33. my.isInLastN = function(resource) {
  34. return lastNCount < 0 || // lastN is disabled
  35. // lastNEndpoints cache not built yet
  36. (lastNCount > 0 && lastNEndpointsCache.length == 0) ||
  37. (lastNEndpointsCache &&
  38. lastNEndpointsCache.indexOf(resource) !== -1);
  39. };
  40. my.changeLocalStream = function (stream, isMuted) {
  41. VideoLayout.changeLocalVideo(stream, isMuted);
  42. };
  43. my.changeLocalAudio = function(stream, isMuted) {
  44. if (isMuted)
  45. APP.UI.setAudioMuted(true, true);
  46. APP.RTC.attachMediaStream($('#localAudio'), stream.getOriginalStream());
  47. var localAudio = document.getElementById('localAudio');
  48. // Writing volume not allowed in IE
  49. if (!RTCBrowserType.isIExplorer()) {
  50. localAudio.autoplay = true;
  51. localAudio.volume = 0;
  52. }
  53. };
  54. my.changeLocalVideo = function(stream, isMuted) {
  55. // Set default display name.
  56. localVideoThumbnail.setDisplayName();
  57. localVideoThumbnail.createConnectionIndicator();
  58. this.onVideoTypeChanged(APP.xmpp.myResource(), stream.videoType);
  59. AudioLevels.updateAudioLevelCanvas(null, VideoLayout);
  60. localVideoThumbnail.changeVideo(stream, isMuted);
  61. /* force update if we're currently being displayed */
  62. if (LargeVideo.isCurrentlyOnLarge(APP.xmpp.myResource())) {
  63. LargeVideo.updateLargeVideo(APP.xmpp.myResource(), true);
  64. }
  65. };
  66. my.mucJoined = function () {
  67. var myResourceJid = APP.xmpp.myResource();
  68. localVideoThumbnail.joined(APP.xmpp.myJid());
  69. if (!LargeVideo.getResourceJid())
  70. LargeVideo.updateLargeVideo(myResourceJid, true);
  71. };
  72. /**
  73. * Adds or removes icons for not available camera and microphone.
  74. * @param resourceJid the jid of user
  75. * @param devices available devices
  76. */
  77. my.setDeviceAvailabilityIcons = function (resourceJid, devices) {
  78. if(!devices)
  79. return;
  80. if(!resourceJid) {
  81. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  82. } else {
  83. if(remoteVideos[resourceJid])
  84. remoteVideos[resourceJid].setDeviceAvailabilityIcons(devices);
  85. }
  86. };
  87. /**
  88. * Checks if removed video is currently displayed and tries to display
  89. * another one instead.
  90. * @param removedVideoSrc src stream identifier of the video.
  91. */
  92. my.updateRemovedVideo = function(resourceJid) {
  93. if (resourceJid === LargeVideo.getResourceJid()) {
  94. var newResourceJid;
  95. // We'll show user's avatar if he is the dominant speaker or if
  96. // his video thumbnail is pinned
  97. if (remoteVideos[resourceJid] &&
  98. resourceJid === focusedVideoResourceJid ||
  99. resourceJid === currentDominantSpeaker) {
  100. newResourceJid = resourceJid;
  101. } else {
  102. // Otherwise select last visible video
  103. newResourceJid = this.electLastVisibleVideo();
  104. }
  105. LargeVideo.updateLargeVideo(newResourceJid);
  106. }
  107. };
  108. my.electLastVisibleVideo = function() {
  109. // pick the last visible video in the row
  110. // if nobody else is left, this picks the local video
  111. var jid;
  112. var videoElem = RTC.getVideoElementName();
  113. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last>' + videoElem);
  114. if (pick.length && APP.RTC.getVideoSrc(pick[0])) {
  115. jid = VideoLayout.getPeerContainerResourceJid(pick[0].parentNode);
  116. } else {
  117. console.info("Last visible video no longer exists");
  118. pick = $('#remoteVideos>span[id!="mixedstream"]>' + videoElem);
  119. if (pick.length && APP.RTC.getVideoSrc(pick[0])) {
  120. jid = VideoLayout.getPeerContainerResourceJid(pick[0].parentNode);
  121. } else {
  122. // Try local video
  123. console.info("Fallback to local video...");
  124. jid = APP.xmpp.myResource();
  125. }
  126. }
  127. console.info("electLastVisibleVideo: " + jid);
  128. return jid;
  129. };
  130. my.onRemoteStreamAdded = function (stream) {
  131. if (stream.peerjid) {
  132. VideoLayout.ensurePeerContainerExists(stream.peerjid);
  133. var resourceJid = Strophe.getResourceFromJid(stream.peerjid);
  134. remoteVideos[resourceJid].addRemoteStreamElement(
  135. stream.sid,
  136. stream.getOriginalStream(),
  137. stream.ssrc);
  138. }
  139. };
  140. my.getLargeVideoJid = function () {
  141. return LargeVideo.getResourceJid();
  142. };
  143. /**
  144. * Called when large video update is finished
  145. * @param currentSmallVideo small video currently displayed on large video
  146. */
  147. my.largeVideoUpdated = function (currentSmallVideo) {
  148. // Makes sure that dominant speaker UI
  149. // is enabled only on current small video
  150. localVideoThumbnail.enableDominantSpeaker(
  151. localVideoThumbnail === currentSmallVideo);
  152. Object.keys(remoteVideos).forEach(
  153. function (resourceJid) {
  154. var remoteVideo = remoteVideos[resourceJid];
  155. if (remoteVideo) {
  156. remoteVideo.enableDominantSpeaker(
  157. remoteVideo === currentSmallVideo);
  158. }
  159. }
  160. );
  161. };
  162. my.handleVideoThumbClicked = function(noPinnedEndpointChangedEvent,
  163. resourceJid) {
  164. if(focusedVideoResourceJid) {
  165. var oldSmallVideo = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  166. if(oldSmallVideo)
  167. oldSmallVideo.focus(false);
  168. }
  169. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  170. // Unlock current focused.
  171. if (focusedVideoResourceJid === resourceJid)
  172. {
  173. focusedVideoResourceJid = null;
  174. // Enable the currently set dominant speaker.
  175. if (currentDominantSpeaker) {
  176. if(smallVideo && smallVideo.hasVideo()) {
  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. if(smallVideo)
  190. smallVideo.focus(true);
  191. if (!noPinnedEndpointChangedEvent) {
  192. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  193. }
  194. }
  195. LargeVideo.setState("video");
  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. var remoteVideo = new RemoteVideo(peerJid, VideoLayout);
  219. remoteVideos[resourceJid] = remoteVideo;
  220. var videoType = remoteVideoTypes[resourceJid];
  221. if (videoType) {
  222. remoteVideo.setVideoType(videoType);
  223. }
  224. // In case this is not currently in the last n we don't show it.
  225. if (localLastNCount &&
  226. localLastNCount > 0 &&
  227. $('#remoteVideos>span').length >= localLastNCount + 2) {
  228. remoteVideo.showPeerContainer('hide');
  229. }
  230. else
  231. VideoLayout.resizeThumbnails();
  232. }
  233. };
  234. my.inputDisplayNameHandler = function (name) {
  235. localVideoThumbnail.inputDisplayNameHandler(name);
  236. };
  237. my.videoactive = function (videoelem, resourceJid) {
  238. console.info(resourceJid + " video is now active");
  239. videoelem.show();
  240. VideoLayout.resizeThumbnails();
  241. // Update the large video to the last added video only if there's no
  242. // current dominant, focused speaker or prezi playing or update it to
  243. // the current dominant speaker.
  244. if ((!focusedVideoResourceJid &&
  245. !currentDominantSpeaker &&
  246. !require("../prezi/Prezi").isPresentationVisible()) ||
  247. focusedVideoResourceJid === resourceJid ||
  248. (resourceJid &&
  249. currentDominantSpeaker === resourceJid)) {
  250. LargeVideo.updateLargeVideo(resourceJid, true);
  251. }
  252. };
  253. /**
  254. * Shows the presence status message for the given video.
  255. */
  256. my.setPresenceStatus = function (resourceJid, statusMsg) {
  257. remoteVideos[resourceJid].setPresenceStatus(statusMsg);
  258. };
  259. /**
  260. * Shows a visual indicator for the moderator of the conference.
  261. */
  262. my.showModeratorIndicator = function () {
  263. var isModerator = APP.xmpp.isModerator();
  264. if (isModerator) {
  265. localVideoThumbnail.createModeratorIndicatorElement();
  266. }
  267. var members = APP.xmpp.getMembers();
  268. Object.keys(members).forEach(function (jid) {
  269. if (Strophe.getResourceFromJid(jid) === 'focus') {
  270. // Skip server side focus
  271. return;
  272. }
  273. var resourceJid = Strophe.getResourceFromJid(jid);
  274. var member = members[jid];
  275. if (member.role === 'moderator') {
  276. remoteVideos[resourceJid].removeRemoteVideoMenu();
  277. remoteVideos[resourceJid].createModeratorIndicatorElement();
  278. } else if (isModerator) {
  279. // We are moderator, but user is not - add menu
  280. if ($('#remote_popupmenu_' + resourceJid).length <= 0) {
  281. remoteVideos[resourceJid].addRemoteVideoMenu();
  282. }
  283. }
  284. });
  285. };
  286. /*
  287. * Shows or hides the audio muted indicator over the local thumbnail video.
  288. * @param {boolean} isMuted
  289. */
  290. my.showLocalAudioIndicator = function(isMuted) {
  291. localVideoThumbnail.showAudioIndicator(isMuted);
  292. };
  293. /**
  294. * Resizes the large video container.
  295. */
  296. my.resizeLargeVideoContainer = function () {
  297. LargeVideo.resize();
  298. VideoLayout.resizeThumbnails();
  299. LargeVideo.position();
  300. };
  301. /**
  302. * Resizes thumbnails.
  303. */
  304. my.resizeThumbnails = function(animate) {
  305. var videoSpaceWidth = $('#remoteVideos').width();
  306. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  307. var width = thumbnailSize[0];
  308. var height = thumbnailSize[1];
  309. $('.userAvatar').css('left', (width - height) / 2);
  310. if(animate) {
  311. $('#remoteVideos').animate({
  312. height: height
  313. },
  314. {
  315. queue: false,
  316. duration: 500
  317. });
  318. $('#remoteVideos>span').animate({
  319. height: height,
  320. width: width
  321. },
  322. {
  323. queue: false,
  324. duration: 500,
  325. complete: function () {
  326. $(document).trigger(
  327. "remotevideo.resized",
  328. [width,
  329. height]);
  330. }
  331. });
  332. } else {
  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() &&
  444. !APP.RTC.muteRemoteVideoStream(jid, value))
  445. return;
  446. if (jid === APP.xmpp.myJid()) {
  447. localVideoThumbnail.showVideoIndicator(value);
  448. } else {
  449. var resource = Strophe.getResourceFromJid(jid);
  450. VideoLayout.ensurePeerContainerExists(jid);
  451. remoteVideos[resource].showVideoIndicator(value);
  452. var el = VideoLayout.getPeerVideoSel(resource);
  453. if (!value)
  454. el.show();
  455. else
  456. el.hide();
  457. }
  458. };
  459. /**
  460. * Display name changed.
  461. */
  462. my.onDisplayNameChanged =
  463. function (jid, displayName, status) {
  464. if (jid === 'localVideoContainer' ||
  465. jid === APP.xmpp.myJid()) {
  466. localVideoThumbnail.setDisplayName(displayName);
  467. } else {
  468. VideoLayout.ensurePeerContainerExists(jid);
  469. remoteVideos[Strophe.getResourceFromJid(jid)].setDisplayName(
  470. displayName,
  471. status);
  472. }
  473. };
  474. /**
  475. * On dominant speaker changed event.
  476. */
  477. my.onDominantSpeakerChanged = function (resourceJid) {
  478. // We ignore local user events.
  479. if (resourceJid === APP.xmpp.myResource())
  480. return;
  481. var members = APP.xmpp.getMembers();
  482. // Update the current dominant speaker.
  483. if (resourceJid !== currentDominantSpeaker) {
  484. var currentJID = APP.xmpp.findJidFromResource(currentDominantSpeaker);
  485. var newJID = APP.xmpp.findJidFromResource(resourceJid);
  486. if(currentDominantSpeaker && (!members || !members[currentJID] ||
  487. !members[currentJID].displayName) && remoteVideos[resourceJid]) {
  488. remoteVideos[resourceJid].setDisplayName(null);
  489. }
  490. if(resourceJid && (!members || !members[newJID] ||
  491. !members[newJID].displayName) && remoteVideos[resourceJid]) {
  492. remoteVideos[resourceJid].setDisplayName(null,
  493. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  494. }
  495. currentDominantSpeaker = resourceJid;
  496. } else {
  497. return;
  498. }
  499. // Obtain container for new dominant speaker.
  500. var videoSel = VideoLayout.getPeerVideoSel(resourceJid);
  501. // Local video will not have container found, but that's ok
  502. // since we don't want to switch to local video.
  503. if (!focusedVideoResourceJid && videoSel.length) {
  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 &&
  574. resourceJid === LargeVideo.getResourceJid()) {
  575. updateLargeVideo = true;
  576. }
  577. }
  578. });
  579. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  580. endpointsEnteringLastN = lastNEndpoints;
  581. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  582. endpointsEnteringLastN.forEach(function (resourceJid) {
  583. var isVisible = $('#participant_' + resourceJid).is(':visible');
  584. remoteVideos[resourceJid].showPeerContainer('show');
  585. if (!isVisible) {
  586. console.log("Add to last N", resourceJid);
  587. var jid = APP.xmpp.findJidFromResource(resourceJid);
  588. var mediaStream =
  589. APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  590. var sel = VideoLayout.getPeerVideoSel(resourceJid);
  591. APP.RTC.attachMediaStream(sel, mediaStream.stream);
  592. if (lastNPickupJid == mediaStream.peerjid) {
  593. // Clean up the lastN pickup jid.
  594. lastNPickupJid = null;
  595. // Don't fire the events again, they've already
  596. // been fired in the contact list click handler.
  597. VideoLayout.handleVideoThumbClicked(
  598. false,
  599. Strophe.getResourceFromJid(mediaStream.peerjid));
  600. updateLargeVideo = false;
  601. }
  602. remoteVideos[resourceJid].
  603. waitForPlayback(sel, mediaStream.stream);
  604. }
  605. });
  606. }
  607. // The endpoint that was being shown in the large video has dropped out
  608. // of the lastN set and there was no lastN pickup jid. We need to update
  609. // the large video now.
  610. if (updateLargeVideo) {
  611. var resource;
  612. var myResource
  613. = APP.xmpp.myResource();
  614. // Find out which endpoint to show in the large video.
  615. for (i = 0; i < lastNEndpoints.length; i++) {
  616. resource = lastNEndpoints[i];
  617. if (!resource || resource === myResource)
  618. continue;
  619. // videoSrcToSsrc needs to be update for this call to succeed.
  620. LargeVideo.updateLargeVideo(resource);
  621. break;
  622. }
  623. }
  624. };
  625. /**
  626. * Updates local stats
  627. * @param percent
  628. * @param object
  629. */
  630. my.updateLocalConnectionStats = function (percent, object) {
  631. var resolution = null;
  632. if (object.resolution !== null) {
  633. resolution = object.resolution;
  634. object.resolution = resolution[APP.xmpp.myJid()];
  635. delete resolution[APP.xmpp.myJid()];
  636. }
  637. localVideoThumbnail.updateStatsIndicator(percent, object);
  638. for (var jid in resolution) {
  639. if (resolution[jid] === null)
  640. continue;
  641. var resourceJid = Strophe.getResourceFromJid(jid);
  642. if (remoteVideos[resourceJid] &&
  643. remoteVideos[resourceJid].connectionIndicator) {
  644. remoteVideos[resourceJid].connectionIndicator.
  645. updateResolution(resolution[jid]);
  646. }
  647. }
  648. };
  649. /**
  650. * Updates remote stats.
  651. * @param jid the jid associated with the stats
  652. * @param percent the connection quality percent
  653. * @param object the stats data
  654. */
  655. my.updateConnectionStats = function (jid, percent, object) {
  656. var resourceJid = Strophe.getResourceFromJid(jid);
  657. if (remoteVideos[resourceJid])
  658. remoteVideos[resourceJid].updateStatsIndicator(percent, object);
  659. };
  660. /**
  661. * Hides the connection indicator
  662. * @param jid
  663. */
  664. my.hideConnectionIndicator = function (jid) {
  665. remoteVideos[Strophe.getResourceFromJid(jid)].hideConnectionIndicator();
  666. };
  667. /**
  668. * Hides all the indicators
  669. */
  670. my.onStatsStop = function () {
  671. for(var video in remoteVideos) {
  672. remoteVideos[video].hideIndicator();
  673. }
  674. localVideoThumbnail.hideIndicator();
  675. };
  676. my.participantLeft = function (jid) {
  677. // Unlock large video
  678. var resourceJid = Strophe.getResourceFromJid(jid);
  679. if (focusedVideoResourceJid === resourceJid) {
  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. // Remove remote video
  691. console.info("Removing remote video: " + resourceJid);
  692. remoteVideo.remove();
  693. delete remoteVideos[resourceJid];
  694. } else {
  695. console.warn("No remote video for " + resourceJid);
  696. }
  697. VideoLayout.resizeThumbnails();
  698. };
  699. my.onVideoTypeChanged = function (resourceJid, newVideoType) {
  700. if (remoteVideoTypes[resourceJid] === newVideoType) {
  701. return;
  702. }
  703. console.info("Peer video type changed: ", resourceJid, newVideoType);
  704. remoteVideoTypes[resourceJid] = newVideoType;
  705. var smallVideo;
  706. if (resourceJid === APP.xmpp.myResource()) {
  707. if (!localVideoThumbnail) {
  708. console.warn("Local video not ready yet");
  709. return;
  710. }
  711. smallVideo = localVideoThumbnail;
  712. } else if (remoteVideos[resourceJid]) {
  713. smallVideo = remoteVideos[resourceJid];
  714. } else {
  715. return;
  716. }
  717. smallVideo.setVideoType(newVideoType);
  718. LargeVideo.onVideoTypeChanged(resourceJid, newVideoType);
  719. };
  720. my.showMore = function (jid) {
  721. if (jid === 'local') {
  722. localVideoThumbnail.connectionIndicator.showMore();
  723. } else {
  724. var remoteVideo = remoteVideos[Strophe.getResourceFromJid(jid)];
  725. if (remoteVideo) {
  726. remoteVideo.connectionIndicator.showMore();
  727. } else {
  728. console.info("Error - no remote video for jid: " + jid);
  729. }
  730. }
  731. };
  732. my.addPreziContainer = function (id) {
  733. var container = RemoteVideo.createContainer(id);
  734. VideoLayout.resizeThumbnails();
  735. return container;
  736. };
  737. my.setLargeVideoVisible = function (isVisible) {
  738. LargeVideo.setLargeVideoVisible(isVisible);
  739. if(!isVisible && focusedVideoResourceJid) {
  740. var smallVideo = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  741. if(smallVideo) {
  742. smallVideo.focus(false);
  743. smallVideo.showAvatar();
  744. }
  745. focusedVideoResourceJid = null;
  746. }
  747. };
  748. /**
  749. * Resizes the video area
  750. * @param callback a function to be called when the video space is
  751. * resized.
  752. */
  753. my.resizeVideoArea = function(isVisible, callback) {
  754. LargeVideo.resizeVideoAreaAnimated(isVisible, callback);
  755. VideoLayout.resizeThumbnails(true);
  756. };
  757. my.getSmallVideo = function (resourceJid) {
  758. if(resourceJid == APP.xmpp.myResource()) {
  759. return localVideoThumbnail;
  760. } else {
  761. if(!remoteVideos[resourceJid])
  762. return null;
  763. return remoteVideos[resourceJid];
  764. }
  765. };
  766. my.userAvatarChanged = function(resourceJid, thumbUrl) {
  767. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  768. if(smallVideo)
  769. smallVideo.avatarChanged(thumbUrl);
  770. else
  771. console.warn(
  772. "Missed avatar update - no small video yet for " + resourceJid);
  773. LargeVideo.updateAvatar(resourceJid, thumbUrl);
  774. };
  775. my.createEtherpadIframe = function(src, onloadHandler)
  776. {
  777. return LargeVideo.createEtherpadIframe(src, onloadHandler);
  778. };
  779. my.setLargeVideoState = function (state) {
  780. LargeVideo.setState(state);
  781. };
  782. my.getLargeVideoState = function () {
  783. return LargeVideo.getState();
  784. };
  785. my.setLargeVideoHover = function (inHandler, outHandler) {
  786. LargeVideo.setHover(inHandler, outHandler);
  787. };
  788. /**
  789. * Indicates that the video has been interrupted.
  790. */
  791. my.onVideoInterrupted = function () {
  792. LargeVideo.enableVideoProblemFilter(true);
  793. var reconnectingKey = "connection.RECONNECTING";
  794. $('#videoConnectionMessage').attr("data-i18n", reconnectingKey);
  795. $('#videoConnectionMessage').text(APP.translation.translateString(reconnectingKey));
  796. $('#videoConnectionMessage').css({display: "block"});
  797. };
  798. /**
  799. * Indicates that the video has been restored.
  800. */
  801. my.onVideoRestored = function () {
  802. LargeVideo.enableVideoProblemFilter(false);
  803. $('#videoConnectionMessage').css({display: "none"});
  804. };
  805. return my;
  806. }(VideoLayout || {}));
  807. module.exports = VideoLayout;