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

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