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

VideoLayout.js 36KB

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