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

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