您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 31KB

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