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 36KB

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