Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoLayout.js 29KB

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