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

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