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 33KB

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