You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoLayout.js 35KB

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