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

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