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.

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