Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VideoLayout.js 34KB

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