Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VideoLayout.js 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /* global config, APP, $, interfaceConfig, JitsiMeetJS */
  2. /* jshint -W101 */
  3. import AudioLevels from "../audio_levels/AudioLevels";
  4. import Avatar from "../avatar/Avatar";
  5. import BottomToolbar from "../toolbars/BottomToolbar";
  6. import FilmStrip from "./FilmStrip";
  7. import UIEvents from "../../../service/UI/UIEvents";
  8. import UIUtil from "../util/UIUtil";
  9. import RemoteVideo from "./RemoteVideo";
  10. import LargeVideoManager, {VIDEO_CONTAINER_TYPE} from "./LargeVideo";
  11. import {SHARED_VIDEO_CONTAINER_TYPE} from '../shared_video/SharedVideo';
  12. import LocalVideo from "./LocalVideo";
  13. import PanelToggler from "../side_pannels/SidePanelToggler";
  14. const RTCUIUtil = JitsiMeetJS.util.RTCUIHelper;
  15. var remoteVideos = {};
  16. var localVideoThumbnail = null;
  17. var currentDominantSpeaker = null;
  18. var localLastNCount = config.channelLastN;
  19. var localLastNSet = [];
  20. var lastNEndpointsCache = [];
  21. var lastNPickupId = null;
  22. var eventEmitter = null;
  23. /**
  24. * Currently focused video jid
  25. * @type {String}
  26. */
  27. var pinnedId = null;
  28. /**
  29. * flipX state of the localVideo
  30. */
  31. let localFlipX = null;
  32. /**
  33. * On contact list item clicked.
  34. */
  35. function onContactClicked (id) {
  36. if (APP.conference.isLocalId(id)) {
  37. $("#localVideoContainer").click();
  38. return;
  39. }
  40. let remoteVideo = remoteVideos[id];
  41. if (remoteVideo && remoteVideo.hasVideo()) {
  42. // It is not always the case that a videoThumb exists (if there is
  43. // no actual video).
  44. if (remoteVideo.hasVideoStarted()) {
  45. // We have a video src, great! Let's update the large video
  46. // now.
  47. VideoLayout.handleVideoThumbClicked(id);
  48. } else {
  49. // If we don't have a video src for jid, there's absolutely
  50. // no point in calling handleVideoThumbClicked; Quite
  51. // simply, it won't work because it needs an src to attach
  52. // to the large video.
  53. //
  54. // Instead, we trigger the pinned endpoint changed event to
  55. // let the bridge adjust its lastN set for myjid and store
  56. // the pinned user in the lastNPickupId variable to be
  57. // picked up later by the lastN changed event handler.
  58. lastNPickupId = id;
  59. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, remoteVideo, true);
  60. }
  61. }
  62. }
  63. /**
  64. * Returns the corresponding resource id to the given peer container
  65. * DOM element.
  66. *
  67. * @return the corresponding resource id to the given peer container
  68. * DOM element
  69. */
  70. function getPeerContainerResourceId (containerElement) {
  71. if (localVideoThumbnail.container === containerElement) {
  72. return localVideoThumbnail.id;
  73. }
  74. let i = containerElement.id.indexOf('participant_');
  75. if (i >= 0) {
  76. return containerElement.id.substring(i + 12);
  77. }
  78. }
  79. let largeVideo;
  80. var VideoLayout = {
  81. init (emitter) {
  82. eventEmitter = emitter;
  83. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED, function (val) {
  84. localFlipX = val;
  85. if(largeVideo)
  86. largeVideo.onLocalFlipXChange(val);
  87. });
  88. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  89. // sets default video type of local video
  90. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  91. // if we do not resize the thumbs here, if there is no video device
  92. // the local video thumb maybe one pixel
  93. let {thumbWidth, thumbHeight}
  94. = this.resizeThumbnails(false, true, false);
  95. AudioLevels.updateAudioLevelCanvas(null, thumbWidth, thumbHeight);
  96. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  97. this.lastNCount = config.channelLastN;
  98. },
  99. initLargeVideo (isSideBarVisible) {
  100. largeVideo = new LargeVideoManager();
  101. if(localFlipX) {
  102. largeVideo.onLocalFlipXChange(localFlipX);
  103. }
  104. largeVideo.updateContainerSize(isSideBarVisible);
  105. AudioLevels.init();
  106. },
  107. setAudioLevel(id, lvl) {
  108. if (!largeVideo) {
  109. return;
  110. }
  111. AudioLevels.updateAudioLevel(
  112. id, lvl, largeVideo.id
  113. );
  114. },
  115. isInLastN (resource) {
  116. return this.lastNCount < 0 || // lastN is disabled
  117. // lastNEndpoints cache not built yet
  118. (this.lastNCount > 0 && !lastNEndpointsCache.length) ||
  119. (lastNEndpointsCache &&
  120. lastNEndpointsCache.indexOf(resource) !== -1);
  121. },
  122. changeLocalAudio (stream) {
  123. let localAudio = document.getElementById('localAudio');
  124. localAudio = stream.attach(localAudio);
  125. // Now when Temasys plugin is converting also <audio> elements to
  126. // plugin's <object>s, in current layout it will capture click events
  127. // before it reaches the local video object. We hide it here in order
  128. // to prevent that.
  129. //if (RTCBrowserType.isIExplorer()) {
  130. // The issue is not present on Safari. Also if we hide it in Safari
  131. // then the local audio track will have 'enabled' flag set to false
  132. // which will result in audio mute issues
  133. // $(localAudio).hide();
  134. localAudio.width = 1;
  135. localAudio.height = 1;
  136. //}
  137. },
  138. changeLocalVideo (stream) {
  139. let localId = APP.conference.getMyUserId();
  140. this.onVideoTypeChanged(localId, stream.videoType);
  141. if (!stream.isMuted()) {
  142. localVideoThumbnail.changeVideo(stream);
  143. }
  144. /* force update if we're currently being displayed */
  145. if (this.isCurrentlyOnLarge(localId)) {
  146. this.updateLargeVideo(localId, true);
  147. }
  148. },
  149. /**
  150. * Get's the localID of the conference and set it to the local video
  151. * (small one). This needs to be called as early as possible, when muc is
  152. * actually joined. Otherwise events can come with information like email
  153. * and setting them assume the id is already set.
  154. */
  155. mucJoined () {
  156. if (largeVideo && !largeVideo.id) {
  157. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  158. }
  159. },
  160. /**
  161. * Adds or removes icons for not available camera and microphone.
  162. * @param resourceJid the jid of user
  163. * @param devices available devices
  164. */
  165. setDeviceAvailabilityIcons (id, devices) {
  166. if (APP.conference.isLocalId(id)) {
  167. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  168. return;
  169. }
  170. let video = remoteVideos[id];
  171. if (!video) {
  172. return;
  173. }
  174. video.setDeviceAvailabilityIcons(devices);
  175. },
  176. /**
  177. * Enables/disables device availability icons for the given participant id.
  178. * The default value is {true}.
  179. * @param id the identifier of the participant
  180. * @param enable {true} to enable device availability icons
  181. */
  182. enableDeviceAvailabilityIcons (id, enable) {
  183. let video;
  184. if (APP.conference.isLocalId(id)) {
  185. video = localVideoThumbnail;
  186. }
  187. else {
  188. video = remoteVideos[id];
  189. }
  190. if (video)
  191. video.enableDeviceAvailabilityIcons(enable);
  192. },
  193. /**
  194. * Shows/hides local video.
  195. * @param {boolean} true to make the local video visible, false - otherwise
  196. */
  197. setLocalVideoVisible(visible) {
  198. localVideoThumbnail.setVisible(visible);
  199. },
  200. /**
  201. * Checks if removed video is currently displayed and tries to display
  202. * another one instead.
  203. * Uses focusedID if any or dominantSpeakerID if any,
  204. * otherwise elects new video, in this order.
  205. */
  206. updateAfterThumbRemoved (id) {
  207. if (!this.isCurrentlyOnLarge(id)) {
  208. return;
  209. }
  210. let newId;
  211. if (pinnedId)
  212. newId = pinnedId;
  213. else if (currentDominantSpeaker)
  214. newId = currentDominantSpeaker;
  215. else // Otherwise select last visible video
  216. newId = this.electLastVisibleVideo();
  217. this.updateLargeVideo(newId);
  218. },
  219. electLastVisibleVideo () {
  220. // pick the last visible video in the row
  221. // if nobody else is left, this picks the local video
  222. let thumbs = FilmStrip.getThumbs(true).filter('[id!="mixedstream"]');
  223. let lastVisible = thumbs.filter(':visible:last');
  224. if (lastVisible.length) {
  225. let id = getPeerContainerResourceId(lastVisible[0]);
  226. if (remoteVideos[id]) {
  227. console.info("electLastVisibleVideo: " + id);
  228. return id;
  229. }
  230. // The RemoteVideo was removed (but the DOM elements may still
  231. // exist).
  232. }
  233. console.info("Last visible video no longer exists");
  234. thumbs = FilmStrip.getThumbs();
  235. if (thumbs.length) {
  236. let id = getPeerContainerResourceId(thumbs[0]);
  237. if (remoteVideos[id]) {
  238. console.info("electLastVisibleVideo: " + id);
  239. return id;
  240. }
  241. // The RemoteVideo was removed (but the DOM elements may
  242. // still exist).
  243. }
  244. // Go with local video
  245. console.info("Fallback to local video...");
  246. let id = APP.conference.getMyUserId();
  247. console.info("electLastVisibleVideo: " + id);
  248. return id;
  249. },
  250. onRemoteStreamAdded (stream) {
  251. let id = stream.getParticipantId();
  252. let remoteVideo = remoteVideos[id];
  253. if (!remoteVideo)
  254. return;
  255. remoteVideo.addRemoteStreamElement(stream);
  256. // if track is muted make sure we reflect that
  257. if(stream.isMuted())
  258. {
  259. if(stream.getType() === "audio")
  260. this.onAudioMute(stream.getParticipantId(), true);
  261. else
  262. this.onVideoMute(stream.getParticipantId(), true);
  263. }
  264. },
  265. onRemoteStreamRemoved (stream) {
  266. let id = stream.getParticipantId();
  267. let remoteVideo = remoteVideos[id];
  268. if (remoteVideo) { // remote stream may be removed after participant left the conference
  269. remoteVideo.removeRemoteStreamElement(stream);
  270. }
  271. },
  272. /**
  273. * Return the type of the remote video.
  274. * @param id the id for the remote video
  275. * @returns {String} the video type video or screen.
  276. */
  277. getRemoteVideoType (id) {
  278. let smallVideo = VideoLayout.getSmallVideo(id);
  279. return smallVideo ? smallVideo.getVideoType() : null;
  280. },
  281. isPinned (id) {
  282. return (pinnedId) ? (id === pinnedId) : false;
  283. },
  284. getPinnedId () {
  285. return pinnedId;
  286. },
  287. /**
  288. * Handles the click on a video thumbnail.
  289. *
  290. * @param id the identifier of the video thumbnail
  291. */
  292. handleVideoThumbClicked (id) {
  293. if(pinnedId) {
  294. var oldSmallVideo = VideoLayout.getSmallVideo(pinnedId);
  295. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  296. oldSmallVideo.focus(false);
  297. }
  298. var smallVideo = VideoLayout.getSmallVideo(id);
  299. // Unpin if currently pinned.
  300. if (pinnedId === id)
  301. {
  302. pinnedId = null;
  303. // Enable the currently set dominant speaker.
  304. if (currentDominantSpeaker) {
  305. if(smallVideo && smallVideo.hasVideo()) {
  306. this.updateLargeVideo(currentDominantSpeaker);
  307. }
  308. }
  309. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  310. return;
  311. }
  312. // Lock new video
  313. pinnedId = id;
  314. // Update focused/pinned interface.
  315. if (id) {
  316. if (smallVideo && !interfaceConfig.filmStripOnly)
  317. smallVideo.focus(true);
  318. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  319. }
  320. this.updateLargeVideo(id);
  321. },
  322. /**
  323. * Creates a participant container for the given id and smallVideo.
  324. *
  325. * @param id the id of the participant to add
  326. * @param {SmallVideo} smallVideo optional small video instance to add as a
  327. * remote video, if undefined RemoteVideo will be created
  328. */
  329. addParticipantContainer (id, smallVideo) {
  330. let remoteVideo;
  331. if(smallVideo)
  332. remoteVideo = smallVideo;
  333. else
  334. remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  335. remoteVideos[id] = remoteVideo;
  336. let videoType = VideoLayout.getRemoteVideoType(id);
  337. if (!videoType) {
  338. // make video type the default one (camera)
  339. videoType = VIDEO_CONTAINER_TYPE;
  340. }
  341. remoteVideo.setVideoType(videoType);
  342. // In case this is not currently in the last n we don't show it.
  343. if (localLastNCount && localLastNCount > 0 &&
  344. FilmStrip.getThumbs().length >= localLastNCount + 2) {
  345. remoteVideo.showPeerContainer('hide');
  346. } else {
  347. VideoLayout.resizeThumbnails(false, true);
  348. }
  349. },
  350. videoactive (videoelem, resourceJid) {
  351. console.info(resourceJid + " video is now active", videoelem);
  352. VideoLayout.resizeThumbnails(
  353. false, false, false, function() {$(videoelem).show();});
  354. // Update the large video to the last added video only if there's no
  355. // current dominant, focused speaker or update it to
  356. // the current dominant speaker.
  357. if ((!pinnedId &&
  358. !currentDominantSpeaker &&
  359. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  360. pinnedId === resourceJid ||
  361. (!pinnedId && resourceJid &&
  362. currentDominantSpeaker === resourceJid) ||
  363. /* Playback started while we're on the stage - may need to update
  364. video source with the new stream */
  365. this.isCurrentlyOnLarge(resourceJid)) {
  366. this.updateLargeVideo(resourceJid, true);
  367. }
  368. },
  369. /**
  370. * Shows the presence status message for the given video.
  371. */
  372. setPresenceStatus (id, statusMsg) {
  373. let remoteVideo = remoteVideos[id];
  374. if (remoteVideo)
  375. remoteVideo.setPresenceStatus(statusMsg);
  376. },
  377. /**
  378. * Shows a visual indicator for the moderator of the conference.
  379. * On local or remote participants.
  380. */
  381. showModeratorIndicator () {
  382. let isModerator = APP.conference.isModerator;
  383. if (isModerator) {
  384. localVideoThumbnail.createModeratorIndicatorElement();
  385. } else {
  386. localVideoThumbnail.removeModeratorIndicatorElement();
  387. }
  388. APP.conference.listMembers().forEach(function (member) {
  389. let id = member.getId();
  390. let remoteVideo = remoteVideos[id];
  391. if (!remoteVideo)
  392. return;
  393. if (member.isModerator()) {
  394. remoteVideo.removeRemoteVideoMenu();
  395. remoteVideo.createModeratorIndicatorElement();
  396. } else if (isModerator) {
  397. // We are moderator, but user is not - add menu
  398. if(!remoteVideo.hasRemoteVideoMenu) {
  399. remoteVideo.addRemoteVideoMenu();
  400. }
  401. }
  402. });
  403. },
  404. /*
  405. * Shows or hides the audio muted indicator over the local thumbnail video.
  406. * @param {boolean} isMuted
  407. */
  408. showLocalAudioIndicator (isMuted) {
  409. localVideoThumbnail.showAudioIndicator(isMuted);
  410. },
  411. /**
  412. * Resizes thumbnails.
  413. */
  414. resizeThumbnails ( animate = false,
  415. forceUpdate = false,
  416. isSideBarVisible = null,
  417. onComplete = null) {
  418. isSideBarVisible
  419. = (isSideBarVisible !== null)
  420. ? isSideBarVisible : PanelToggler.isVisible();
  421. let {thumbWidth, thumbHeight}
  422. = FilmStrip.calculateThumbnailSize(isSideBarVisible);
  423. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  424. FilmStrip.resizeThumbnails(thumbWidth, thumbHeight,
  425. animate, forceUpdate)
  426. .then(function () {
  427. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  428. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  429. if (onComplete && typeof onComplete === "function")
  430. onComplete();
  431. });
  432. return {thumbWidth, thumbHeight};
  433. },
  434. /**
  435. * On audio muted event.
  436. */
  437. onAudioMute (id, isMuted) {
  438. if (APP.conference.isLocalId(id)) {
  439. localVideoThumbnail.showAudioIndicator(isMuted);
  440. } else {
  441. let remoteVideo = remoteVideos[id];
  442. if (!remoteVideo)
  443. return;
  444. remoteVideo.showAudioIndicator(isMuted);
  445. if (APP.conference.isModerator) {
  446. remoteVideo.updateRemoteVideoMenu(isMuted);
  447. }
  448. }
  449. },
  450. /**
  451. * On video muted event.
  452. */
  453. onVideoMute (id, value) {
  454. if (APP.conference.isLocalId(id)) {
  455. localVideoThumbnail.setMutedView(value);
  456. } else {
  457. let remoteVideo = remoteVideos[id];
  458. if (remoteVideo)
  459. remoteVideo.setMutedView(value);
  460. }
  461. if (this.isCurrentlyOnLarge(id)) {
  462. // large video will show avatar instead of muted stream
  463. this.updateLargeVideo(id, true);
  464. }
  465. },
  466. /**
  467. * Display name changed.
  468. */
  469. onDisplayNameChanged (id, displayName, status) {
  470. if (id === 'localVideoContainer' ||
  471. APP.conference.isLocalId(id)) {
  472. localVideoThumbnail.setDisplayName(displayName);
  473. } else {
  474. let remoteVideo = remoteVideos[id];
  475. if (remoteVideo)
  476. remoteVideo.setDisplayName(displayName, status);
  477. }
  478. },
  479. /**
  480. * Sets the "raised hand" status for a participant identified by 'id'.
  481. */
  482. setRaisedHandStatus(id, raisedHandStatus) {
  483. var video
  484. = APP.conference.isLocalId(id)
  485. ? localVideoThumbnail : remoteVideos[id];
  486. if (video) {
  487. video.showRaisedHandIndicator(raisedHandStatus);
  488. }
  489. },
  490. /**
  491. * On dominant speaker changed event.
  492. */
  493. onDominantSpeakerChanged (id) {
  494. if (id === currentDominantSpeaker) {
  495. return;
  496. }
  497. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  498. // We ignore local user events, but just unmark remote user as dominant
  499. // while we are talking
  500. if (APP.conference.isLocalId(id)) {
  501. if(oldSpeakerRemoteVideo)
  502. {
  503. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  504. currentDominantSpeaker = null;
  505. }
  506. localVideoThumbnail.showDominantSpeakerIndicator(true);
  507. return;
  508. }
  509. let remoteVideo = remoteVideos[id];
  510. if (!remoteVideo) {
  511. return;
  512. }
  513. // Update the current dominant speaker.
  514. remoteVideo.showDominantSpeakerIndicator(true);
  515. localVideoThumbnail.showDominantSpeakerIndicator(false);
  516. // let's remove the indications from the remote video if any
  517. if (oldSpeakerRemoteVideo) {
  518. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  519. }
  520. currentDominantSpeaker = id;
  521. // Local video will not have container found, but that's ok
  522. // since we don't want to switch to local video.
  523. // Update the large video if the video source is already available,
  524. // otherwise wait for the "videoactive.jingle" event.
  525. if (!pinnedId
  526. && remoteVideo.hasVideoStarted()
  527. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  528. this.updateLargeVideo(id);
  529. }
  530. },
  531. /**
  532. * On last N change event.
  533. *
  534. * @param lastNEndpoints the list of last N endpoints
  535. * @param endpointsEnteringLastN the list currently entering last N
  536. * endpoints
  537. */
  538. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  539. if (this.lastNCount !== lastNEndpoints.length)
  540. this.lastNCount = lastNEndpoints.length;
  541. lastNEndpointsCache = lastNEndpoints;
  542. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  543. //
  544. // If LastN drops to, say, 2, because of adaptivity, then E should see
  545. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  546. // so E sees them. C is only in E's local LastN set.
  547. //
  548. // If F starts talking and LastN = 3, then E should see thumbnails for
  549. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  550. // enters E's local LastN ejecting C.
  551. // Increase the local LastN set size, if necessary.
  552. if (this.lastNCount > localLastNCount) {
  553. localLastNCount = this.lastNCount;
  554. }
  555. // Update the local LastN set preserving the order in which the
  556. // endpoints appeared in the LastN/local LastN set.
  557. var nextLocalLastNSet = lastNEndpoints.slice(0);
  558. for (var i = 0; i < localLastNSet.length; i++) {
  559. if (nextLocalLastNSet.length >= localLastNCount) {
  560. break;
  561. }
  562. var resourceJid = localLastNSet[i];
  563. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  564. nextLocalLastNSet.push(resourceJid);
  565. }
  566. }
  567. localLastNSet = nextLocalLastNSet;
  568. var updateLargeVideo = false;
  569. // Handle LastN/local LastN changes.
  570. FilmStrip.getThumbs().each(( index, element ) => {
  571. var resourceJid = getPeerContainerResourceId(element);
  572. var smallVideo = remoteVideos[resourceJid];
  573. // We do not want to process any logic for our own(local) video
  574. // because the local participant is never in the lastN set.
  575. // The code of this function might detect that the local participant
  576. // has been dropped out of the lastN set and will update the large
  577. // video
  578. // Detected from avatar tests, where lastN event override
  579. // local video pinning
  580. if(APP.conference.isLocalId(resourceJid))
  581. return;
  582. var isReceived = true;
  583. if (resourceJid &&
  584. lastNEndpoints.indexOf(resourceJid) < 0 &&
  585. localLastNSet.indexOf(resourceJid) < 0) {
  586. console.log("Remove from last N", resourceJid);
  587. if (smallVideo)
  588. smallVideo.showPeerContainer('hide');
  589. else if (!APP.conference.isLocalId(resourceJid))
  590. console.error("No remote video for: " + resourceJid);
  591. isReceived = false;
  592. } else if (resourceJid &&
  593. //TOFIX: smallVideo may be undefined
  594. smallVideo.isVisible() &&
  595. lastNEndpoints.indexOf(resourceJid) < 0 &&
  596. localLastNSet.indexOf(resourceJid) >= 0) {
  597. // TOFIX: if we're here we already know that the smallVideo
  598. // exists. Look at the previous FIX above.
  599. if (smallVideo)
  600. smallVideo.showPeerContainer('avatar');
  601. else if (!APP.conference.isLocalId(resourceJid))
  602. console.error("No remote video for: " + resourceJid);
  603. isReceived = false;
  604. }
  605. if (!isReceived) {
  606. // resourceJid has dropped out of the server side lastN set, so
  607. // it is no longer being received. If resourceJid was being
  608. // displayed in the large video we have to switch to another
  609. // user.
  610. if (!updateLargeVideo &&
  611. this.isCurrentlyOnLarge(resourceJid)) {
  612. updateLargeVideo = true;
  613. }
  614. }
  615. });
  616. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  617. endpointsEnteringLastN = lastNEndpoints;
  618. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  619. endpointsEnteringLastN.forEach(function (resourceJid) {
  620. var remoteVideo = remoteVideos[resourceJid];
  621. if (remoteVideo)
  622. remoteVideo.showPeerContainer('show');
  623. if (!remoteVideo.isVisible()) {
  624. console.log("Add to last N", resourceJid);
  625. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  626. if (lastNPickupId == resourceJid) {
  627. // Clean up the lastN pickup id.
  628. lastNPickupId = null;
  629. VideoLayout.handleVideoThumbClicked(resourceJid);
  630. updateLargeVideo = false;
  631. }
  632. remoteVideo.waitForPlayback(
  633. remoteVideo.selectVideoElement()[0],
  634. remoteVideo.videoStream);
  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. // Find out which endpoint to show in the large video.
  644. for (i = 0; i < lastNEndpoints.length; i++) {
  645. resource = lastNEndpoints[i];
  646. if (!resource || APP.conference.isLocalId(resource))
  647. continue;
  648. // videoSrcToSsrc needs to be update for this call to succeed.
  649. this.updateLargeVideo(resource);
  650. break;
  651. }
  652. }
  653. },
  654. /**
  655. * Updates local stats
  656. * @param percent
  657. * @param object
  658. */
  659. updateLocalConnectionStats (percent, object) {
  660. let resolutions = object.resolution;
  661. object.resolution = resolutions[APP.conference.getMyUserId()];
  662. localVideoThumbnail.updateStatsIndicator(percent, object);
  663. Object.keys(resolutions).forEach(function (id) {
  664. if (APP.conference.isLocalId(id)) {
  665. return;
  666. }
  667. let resolution = resolutions[id];
  668. let remoteVideo = remoteVideos[id];
  669. if (resolution && remoteVideo) {
  670. remoteVideo.updateResolution(resolution);
  671. }
  672. });
  673. },
  674. /**
  675. * Updates remote stats.
  676. * @param id the id associated with the stats
  677. * @param percent the connection quality percent
  678. * @param object the stats data
  679. */
  680. updateConnectionStats (id, percent, object) {
  681. let remoteVideo = remoteVideos[id];
  682. if (remoteVideo) {
  683. remoteVideo.updateStatsIndicator(percent, object);
  684. }
  685. },
  686. /**
  687. * Hides the connection indicator
  688. * @param id
  689. */
  690. hideConnectionIndicator (id) {
  691. let remoteVideo = remoteVideos[id];
  692. if (remoteVideo)
  693. remoteVideo.hideConnectionIndicator();
  694. },
  695. /**
  696. * Hides all the indicators
  697. */
  698. hideStats () {
  699. for (var video in remoteVideos) {
  700. let remoteVideo = remoteVideos[video];
  701. if (remoteVideo)
  702. remoteVideo.hideIndicator();
  703. }
  704. localVideoThumbnail.hideIndicator();
  705. },
  706. removeParticipantContainer (id) {
  707. // Unlock large video
  708. if (pinnedId === id) {
  709. console.info("Focused video owner has left the conference");
  710. pinnedId = null;
  711. }
  712. if (currentDominantSpeaker === id) {
  713. console.info("Dominant speaker has left the conference");
  714. currentDominantSpeaker = null;
  715. }
  716. var remoteVideo = remoteVideos[id];
  717. if (remoteVideo) {
  718. // Remove remote video
  719. console.info("Removing remote video: " + id);
  720. delete remoteVideos[id];
  721. remoteVideo.remove();
  722. } else {
  723. console.warn("No remote video for " + id);
  724. }
  725. VideoLayout.resizeThumbnails();
  726. },
  727. onVideoTypeChanged (id, newVideoType) {
  728. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  729. return;
  730. }
  731. console.info("Peer video type changed: ", id, newVideoType);
  732. var smallVideo;
  733. if (APP.conference.isLocalId(id)) {
  734. if (!localVideoThumbnail) {
  735. console.warn("Local video not ready yet");
  736. return;
  737. }
  738. smallVideo = localVideoThumbnail;
  739. } else if (remoteVideos[id]) {
  740. smallVideo = remoteVideos[id];
  741. } else {
  742. return;
  743. }
  744. smallVideo.setVideoType(newVideoType);
  745. if (this.isCurrentlyOnLarge(id)) {
  746. this.updateLargeVideo(id, true);
  747. }
  748. },
  749. showMore (id) {
  750. if (id === 'local') {
  751. localVideoThumbnail.connectionIndicator.showMore();
  752. } else {
  753. let remoteVideo = remoteVideos[id];
  754. if (remoteVideo) {
  755. remoteVideo.connectionIndicator.showMore();
  756. } else {
  757. console.info("Error - no remote video for id: " + id);
  758. }
  759. }
  760. },
  761. /**
  762. * Resizes the video area.
  763. *
  764. * @param isSideBarVisible indicates if the side bar is currently visible
  765. * @param forceUpdate indicates that hidden thumbnails will be shown
  766. * @param completeFunction a function to be called when the video area is
  767. * resized.
  768. */
  769. resizeVideoArea (isSideBarVisible,
  770. forceUpdate = false,
  771. animate = false,
  772. completeFunction = null) {
  773. if (largeVideo) {
  774. largeVideo.updateContainerSize(isSideBarVisible);
  775. largeVideo.resize(animate);
  776. }
  777. // Calculate available width and height.
  778. let availableHeight = window.innerHeight;
  779. let availableWidth = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  780. if (availableWidth < 0 || availableHeight < 0) {
  781. return;
  782. }
  783. // Resize the thumbnails first.
  784. this.resizeThumbnails(false, forceUpdate, isSideBarVisible);
  785. // Resize the video area element.
  786. $('#videospace').animate({
  787. right: window.innerWidth - availableWidth,
  788. width: availableWidth,
  789. height: availableHeight
  790. }, {
  791. queue: false,
  792. duration: animate ? 500 : 1,
  793. complete: completeFunction
  794. });
  795. },
  796. getSmallVideo (id) {
  797. if (APP.conference.isLocalId(id)) {
  798. return localVideoThumbnail;
  799. } else {
  800. return remoteVideos[id];
  801. }
  802. },
  803. changeUserAvatar (id, avatarUrl) {
  804. var smallVideo = VideoLayout.getSmallVideo(id);
  805. if (smallVideo) {
  806. smallVideo.avatarChanged(avatarUrl);
  807. } else {
  808. console.warn(
  809. "Missed avatar update - no small video yet for " + id
  810. );
  811. }
  812. if (this.isCurrentlyOnLarge(id)) {
  813. largeVideo.updateAvatar(avatarUrl);
  814. }
  815. },
  816. /**
  817. * Indicates that the video has been interrupted.
  818. */
  819. onVideoInterrupted () {
  820. this.enableVideoProblemFilter(true);
  821. let reconnectingKey = "connection.RECONNECTING";
  822. $('#videoConnectionMessage')
  823. .attr("data-i18n", reconnectingKey)
  824. .text(APP.translation.translateString(reconnectingKey))
  825. .css({display: "block"});
  826. },
  827. /**
  828. * Indicates that the video has been restored.
  829. */
  830. onVideoRestored () {
  831. this.enableVideoProblemFilter(false);
  832. $('#videoConnectionMessage').css({display: "none"});
  833. },
  834. enableVideoProblemFilter (enable) {
  835. if (!largeVideo) {
  836. return;
  837. }
  838. largeVideo.enableVideoProblemFilter(enable);
  839. },
  840. isLargeVideoVisible () {
  841. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  842. },
  843. /**
  844. * @return {LargeContainer} the currently displayed container on large
  845. * video.
  846. */
  847. getCurrentlyOnLargeContainer () {
  848. return largeVideo.getContainer(largeVideo.state);
  849. },
  850. isCurrentlyOnLarge (id) {
  851. return largeVideo && largeVideo.id === id;
  852. },
  853. updateLargeVideo (id, forceUpdate) {
  854. if (!largeVideo) {
  855. return;
  856. }
  857. let isOnLarge = this.isCurrentlyOnLarge(id);
  858. let currentId = largeVideo.id;
  859. if (!isOnLarge || forceUpdate) {
  860. let videoType = this.getRemoteVideoType(id);
  861. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  862. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  863. }
  864. let smallVideo = this.getSmallVideo(id);
  865. let oldSmallVideo;
  866. if (currentId) {
  867. oldSmallVideo = this.getSmallVideo(currentId);
  868. }
  869. smallVideo.waitForResolutionChange();
  870. if (oldSmallVideo)
  871. oldSmallVideo.waitForResolutionChange();
  872. largeVideo.updateLargeVideo(
  873. id,
  874. smallVideo.videoStream,
  875. videoType
  876. ).then(function() {
  877. // update current small video and the old one
  878. smallVideo.updateView();
  879. oldSmallVideo && oldSmallVideo.updateView();
  880. }, function () {
  881. // use clicked other video during update, nothing to do.
  882. });
  883. } else if (currentId) {
  884. let currentSmallVideo = this.getSmallVideo(currentId);
  885. currentSmallVideo.updateView();
  886. }
  887. },
  888. addLargeVideoContainer (type, container) {
  889. largeVideo && largeVideo.addContainer(type, container);
  890. },
  891. removeLargeVideoContainer (type) {
  892. largeVideo && largeVideo.removeContainer(type);
  893. },
  894. /**
  895. * @returns Promise
  896. */
  897. showLargeVideoContainer (type, show) {
  898. if (!largeVideo) {
  899. return Promise.reject();
  900. }
  901. let isVisible = this.isLargeContainerTypeVisible(type);
  902. if (isVisible === show) {
  903. return Promise.resolve();
  904. }
  905. let currentId = largeVideo.id;
  906. if(currentId) {
  907. var oldSmallVideo = this.getSmallVideo(currentId);
  908. }
  909. let containerTypeToShow = type;
  910. // if we are hiding a container and there is focusedVideo
  911. // (pinned remote video) use its video type,
  912. // if not then use default type - large video
  913. if (!show) {
  914. if(pinnedId)
  915. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  916. else
  917. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  918. }
  919. return largeVideo.showContainer(containerTypeToShow)
  920. .then(() => {
  921. if(oldSmallVideo)
  922. oldSmallVideo && oldSmallVideo.updateView();
  923. });
  924. },
  925. isLargeContainerTypeVisible (type) {
  926. return largeVideo && largeVideo.state === type;
  927. },
  928. /**
  929. * Returns the id of the current video shown on large.
  930. * Currently used by tests (torture).
  931. */
  932. getLargeVideoID () {
  933. return largeVideo.id;
  934. },
  935. /**
  936. * Returns the the current video shown on large.
  937. * Currently used by tests (torture).
  938. */
  939. getLargeVideo () {
  940. return largeVideo;
  941. },
  942. /**
  943. * Updates the resolution label, indicating to the user that the large
  944. * video stream is currently HD.
  945. */
  946. updateResolutionLabel(isResolutionHD) {
  947. let videoResolutionLabel = $("#videoResolutionLabel");
  948. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  949. videoResolutionLabel.css({display: "block"});
  950. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  951. videoResolutionLabel.css({display: "none"});
  952. },
  953. /**
  954. * Sets the flipX state of the local video.
  955. * @param {boolean} true for flipped otherwise false;
  956. */
  957. setLocalFlipX: function (val) {
  958. this.localFlipX = val;
  959. },
  960. getEventEmitter: () => {return eventEmitter;}
  961. };
  962. export default VideoLayout;