You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoLayout.js 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. * Uses focusedID if any or dominantSpeakerID if any,
  171. * otherwise elects new video, in this order.
  172. */
  173. updateAfterThumbRemoved (id) {
  174. if (!this.isCurrentlyOnLarge(id)) {
  175. return;
  176. }
  177. let newId;
  178. if (pinnedId)
  179. newId = pinnedId;
  180. else if (currentDominantSpeaker)
  181. newId = currentDominantSpeaker;
  182. else // 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 = VideoLayout.getSmallVideo(pinnedId);
  260. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  261. oldSmallVideo.focus(false);
  262. }
  263. var smallVideo = VideoLayout.getSmallVideo(id);
  264. // Unpin if currently pinned.
  265. if (pinnedId === id)
  266. {
  267. pinnedId = null;
  268. // Enable the currently set dominant speaker.
  269. if (currentDominantSpeaker) {
  270. if(smallVideo && smallVideo.hasVideo()) {
  271. this.updateLargeVideo(currentDominantSpeaker);
  272. }
  273. }
  274. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  275. return;
  276. }
  277. // Lock new video
  278. pinnedId = id;
  279. // Update focused/pinned interface.
  280. if (id) {
  281. if (smallVideo && !interfaceConfig.filmStripOnly)
  282. smallVideo.focus(true);
  283. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  284. }
  285. this.updateLargeVideo(id);
  286. },
  287. /**
  288. * Creates a remote video for participant for the given id.
  289. * @param id the id of the participant to add
  290. * @param {SmallVideo} smallVideo optional small video instance to add as a
  291. * remote video, if undefined RemoteVideo will be created
  292. */
  293. addParticipantContainer (id, smallVideo) {
  294. let remoteVideo;
  295. if(smallVideo)
  296. remoteVideo = smallVideo;
  297. else
  298. remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  299. remoteVideos[id] = remoteVideo;
  300. let videoType = VideoLayout.getRemoteVideoType(id);
  301. if (!videoType) {
  302. // make video type the default one (camera)
  303. videoType = VIDEO_CONTAINER_TYPE;
  304. }
  305. remoteVideo.setVideoType(videoType);
  306. // In case this is not currently in the last n we don't show it.
  307. if (localLastNCount && localLastNCount > 0 &&
  308. FilmStrip.getThumbs().length >= localLastNCount + 2) {
  309. remoteVideo.showPeerContainer('hide');
  310. } else {
  311. VideoLayout.resizeThumbnails(false, true);
  312. }
  313. },
  314. videoactive (videoelem, resourceJid) {
  315. console.info(resourceJid + " video is now active", videoelem);
  316. VideoLayout.resizeThumbnails(
  317. false, false, false, function() {$(videoelem).show();});
  318. // Update the large video to the last added video only if there's no
  319. // current dominant, focused speaker or update it to
  320. // the current dominant speaker.
  321. if ((!pinnedId &&
  322. !currentDominantSpeaker &&
  323. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  324. pinnedId === resourceJid ||
  325. (resourceJid &&
  326. currentDominantSpeaker === resourceJid)) {
  327. this.updateLargeVideo(resourceJid, true);
  328. }
  329. },
  330. /**
  331. * Shows the presence status message for the given video.
  332. */
  333. setPresenceStatus (id, statusMsg) {
  334. remoteVideos[id].setPresenceStatus(statusMsg);
  335. },
  336. /**
  337. * Shows a visual indicator for the moderator of the conference.
  338. * On local or remote participants.
  339. */
  340. showModeratorIndicator () {
  341. let isModerator = APP.conference.isModerator;
  342. if (isModerator) {
  343. localVideoThumbnail.createModeratorIndicatorElement();
  344. }
  345. APP.conference.listMembers().forEach(function (member) {
  346. let id = member.getId();
  347. if (member.isModerator()) {
  348. remoteVideos[id].removeRemoteVideoMenu();
  349. remoteVideos[id].createModeratorIndicatorElement();
  350. } else if (isModerator) {
  351. // We are moderator, but user is not - add menu
  352. if ($(`#remote_popupmenu_${id}`).length <= 0) {
  353. remoteVideos[id].addRemoteVideoMenu();
  354. }
  355. }
  356. });
  357. },
  358. /*
  359. * Shows or hides the audio muted indicator over the local thumbnail video.
  360. * @param {boolean} isMuted
  361. */
  362. showLocalAudioIndicator (isMuted) {
  363. localVideoThumbnail.showAudioIndicator(isMuted);
  364. },
  365. /**
  366. * Resizes thumbnails.
  367. */
  368. resizeThumbnails ( animate = false,
  369. forceUpdate = false,
  370. isSideBarVisible = null,
  371. onComplete = null) {
  372. isSideBarVisible
  373. = (isSideBarVisible !== null)
  374. ? isSideBarVisible : PanelToggler.isVisible();
  375. let {thumbWidth, thumbHeight}
  376. = FilmStrip.calculateThumbnailSize(isSideBarVisible);
  377. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  378. FilmStrip.resizeThumbnails(thumbWidth, thumbHeight,
  379. animate, forceUpdate)
  380. .then(function () {
  381. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  382. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  383. if (onComplete && typeof onComplete === "function")
  384. onComplete();
  385. });
  386. return {thumbWidth, thumbHeight};
  387. },
  388. /**
  389. * On audio muted event.
  390. */
  391. onAudioMute (id, isMuted) {
  392. if (APP.conference.isLocalId(id)) {
  393. localVideoThumbnail.showAudioIndicator(isMuted);
  394. } else {
  395. remoteVideos[id].showAudioIndicator(isMuted);
  396. if (APP.conference.isModerator) {
  397. remoteVideos[id].updateRemoteVideoMenu(isMuted);
  398. }
  399. }
  400. },
  401. /**
  402. * On video muted event.
  403. */
  404. onVideoMute (id, value) {
  405. if (APP.conference.isLocalId(id)) {
  406. localVideoThumbnail.setMutedView(value);
  407. } else {
  408. var remoteVideo = remoteVideos[id];
  409. remoteVideo.setMutedView(value);
  410. }
  411. if (this.isCurrentlyOnLarge(id)) {
  412. // large video will show avatar instead of muted stream
  413. this.updateLargeVideo(id, true);
  414. }
  415. },
  416. /**
  417. * Display name changed.
  418. */
  419. onDisplayNameChanged (id, displayName, status) {
  420. if (id === 'localVideoContainer' ||
  421. APP.conference.isLocalId(id)) {
  422. localVideoThumbnail.setDisplayName(displayName);
  423. } else {
  424. remoteVideos[id].setDisplayName(displayName, status);
  425. }
  426. },
  427. /**
  428. * On dominant speaker changed event.
  429. */
  430. onDominantSpeakerChanged (id) {
  431. if (id === currentDominantSpeaker) {
  432. return;
  433. }
  434. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  435. // We ignore local user events, but just unmark remote user as dominant
  436. // while we are talking
  437. if (APP.conference.isLocalId(id)) {
  438. if(oldSpeakerRemoteVideo)
  439. {
  440. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  441. localVideoThumbnail.updateDominantSpeakerIndicator(true);
  442. currentDominantSpeaker = null;
  443. }
  444. return;
  445. }
  446. let remoteVideo = remoteVideos[id];
  447. if (!remoteVideo) {
  448. return;
  449. }
  450. // Update the current dominant speaker.
  451. remoteVideo.updateDominantSpeakerIndicator(true);
  452. localVideoThumbnail.updateDominantSpeakerIndicator(false);
  453. // let's remove the indications from the remote video if any
  454. if (oldSpeakerRemoteVideo) {
  455. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  456. }
  457. currentDominantSpeaker = id;
  458. // Local video will not have container found, but that's ok
  459. // since we don't want to switch to local video.
  460. // Update the large video if the video source is already available,
  461. // otherwise wait for the "videoactive.jingle" event.
  462. if (!pinnedId
  463. && remoteVideo.hasVideoStarted()
  464. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  465. this.updateLargeVideo(id);
  466. }
  467. },
  468. /**
  469. * On last N change event.
  470. *
  471. * @param lastNEndpoints the list of last N endpoints
  472. * @param endpointsEnteringLastN the list currently entering last N
  473. * endpoints
  474. */
  475. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  476. if (this.lastNCount !== lastNEndpoints.length)
  477. this.lastNCount = lastNEndpoints.length;
  478. lastNEndpointsCache = lastNEndpoints;
  479. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  480. //
  481. // If LastN drops to, say, 2, because of adaptivity, then E should see
  482. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  483. // so E sees them. C is only in E's local LastN set.
  484. //
  485. // If F starts talking and LastN = 3, then E should see thumbnails for
  486. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  487. // enters E's local LastN ejecting C.
  488. // Increase the local LastN set size, if necessary.
  489. if (this.lastNCount > localLastNCount) {
  490. localLastNCount = this.lastNCount;
  491. }
  492. // Update the local LastN set preserving the order in which the
  493. // endpoints appeared in the LastN/local LastN set.
  494. var nextLocalLastNSet = lastNEndpoints.slice(0);
  495. for (var i = 0; i < localLastNSet.length; i++) {
  496. if (nextLocalLastNSet.length >= localLastNCount) {
  497. break;
  498. }
  499. var resourceJid = localLastNSet[i];
  500. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  501. nextLocalLastNSet.push(resourceJid);
  502. }
  503. }
  504. localLastNSet = nextLocalLastNSet;
  505. var updateLargeVideo = false;
  506. // Handle LastN/local LastN changes.
  507. FilmStrip.getThumbs().each(( index, element ) => {
  508. var resourceJid = getPeerContainerResourceId(element);
  509. var smallVideo = remoteVideos[resourceJid];
  510. // We do not want to process any logic for our own(local) video
  511. // because the local participant is never in the lastN set.
  512. // The code of this function might detect that the local participant
  513. // has been dropped out of the lastN set and will update the large
  514. // video
  515. // Detected from avatar tests, where lastN event override
  516. // local video pinning
  517. if(APP.conference.isLocalId(resourceJid))
  518. return;
  519. var isReceived = true;
  520. if (resourceJid &&
  521. lastNEndpoints.indexOf(resourceJid) < 0 &&
  522. localLastNSet.indexOf(resourceJid) < 0) {
  523. console.log("Remove from last N", resourceJid);
  524. if (smallVideo)
  525. smallVideo.showPeerContainer('hide');
  526. else if (!APP.conference.isLocalId(resourceJid))
  527. console.error("No remote video for: " + resourceJid);
  528. isReceived = false;
  529. } else if (resourceJid &&
  530. smallVideo.isVisible() &&
  531. lastNEndpoints.indexOf(resourceJid) < 0 &&
  532. localLastNSet.indexOf(resourceJid) >= 0) {
  533. if (smallVideo)
  534. smallVideo.showPeerContainer('avatar');
  535. else if (!APP.conference.isLocalId(resourceJid))
  536. console.error("No remote video for: " + resourceJid);
  537. isReceived = false;
  538. }
  539. if (!isReceived) {
  540. // resourceJid has dropped out of the server side lastN set, so
  541. // it is no longer being received. If resourceJid was being
  542. // displayed in the large video we have to switch to another
  543. // user.
  544. if (!updateLargeVideo &&
  545. this.isCurrentlyOnLarge(resourceJid)) {
  546. updateLargeVideo = true;
  547. }
  548. }
  549. });
  550. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  551. endpointsEnteringLastN = lastNEndpoints;
  552. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  553. endpointsEnteringLastN.forEach(function (resourceJid) {
  554. var remoteVideo = remoteVideos[resourceJid];
  555. if (remoteVideo)
  556. remoteVideo.showPeerContainer('show');
  557. if (!remoteVideo.isVisible()) {
  558. console.log("Add to last N", resourceJid);
  559. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  560. if (lastNPickupId == resourceJid) {
  561. // Clean up the lastN pickup id.
  562. lastNPickupId = null;
  563. VideoLayout.handleVideoThumbClicked(resourceJid);
  564. updateLargeVideo = false;
  565. }
  566. remoteVideo.waitForPlayback(
  567. remoteVideo.selectVideoElement()[0],
  568. remoteVideo.videoStream);
  569. }
  570. });
  571. }
  572. // The endpoint that was being shown in the large video has dropped out
  573. // of the lastN set and there was no lastN pickup jid. We need to update
  574. // the large video now.
  575. if (updateLargeVideo) {
  576. var resource;
  577. // Find out which endpoint to show in the large video.
  578. for (i = 0; i < lastNEndpoints.length; i++) {
  579. resource = lastNEndpoints[i];
  580. if (!resource || APP.conference.isLocalId(resource))
  581. continue;
  582. // videoSrcToSsrc needs to be update for this call to succeed.
  583. this.updateLargeVideo(resource);
  584. break;
  585. }
  586. }
  587. },
  588. /**
  589. * Updates local stats
  590. * @param percent
  591. * @param object
  592. */
  593. updateLocalConnectionStats (percent, object) {
  594. let resolutions = object.resolution;
  595. object.resolution = resolutions[APP.conference.localId];
  596. localVideoThumbnail.updateStatsIndicator(percent, object);
  597. Object.keys(resolutions).forEach(function (id) {
  598. if (APP.conference.isLocalId(id)) {
  599. return;
  600. }
  601. let resolution = resolutions[id];
  602. let remoteVideo = remoteVideos[id];
  603. if (resolution && remoteVideo) {
  604. remoteVideo.updateResolution(resolution);
  605. }
  606. });
  607. },
  608. /**
  609. * Updates remote stats.
  610. * @param id the id associated with the stats
  611. * @param percent the connection quality percent
  612. * @param object the stats data
  613. */
  614. updateConnectionStats (id, percent, object) {
  615. if (remoteVideos[id]) {
  616. remoteVideos[id].updateStatsIndicator(percent, object);
  617. }
  618. },
  619. /**
  620. * Hides the connection indicator
  621. * @param id
  622. */
  623. hideConnectionIndicator (id) {
  624. remoteVideos[id].hideConnectionIndicator();
  625. },
  626. /**
  627. * Hides all the indicators
  628. */
  629. hideStats () {
  630. for(var video in remoteVideos) {
  631. remoteVideos[video].hideIndicator();
  632. }
  633. localVideoThumbnail.hideIndicator();
  634. },
  635. removeParticipantContainer (id) {
  636. // Unlock large video
  637. if (pinnedId === id) {
  638. console.info("Focused video owner has left the conference");
  639. pinnedId = null;
  640. }
  641. if (currentDominantSpeaker === id) {
  642. console.info("Dominant speaker has left the conference");
  643. currentDominantSpeaker = null;
  644. }
  645. var remoteVideo = remoteVideos[id];
  646. if (remoteVideo) {
  647. // Remove remote video
  648. console.info("Removing remote video: " + id);
  649. delete remoteVideos[id];
  650. remoteVideo.remove();
  651. } else {
  652. console.warn("No remote video for " + id);
  653. }
  654. VideoLayout.resizeThumbnails();
  655. },
  656. onVideoTypeChanged (id, newVideoType) {
  657. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  658. return;
  659. }
  660. console.info("Peer video type changed: ", id, newVideoType);
  661. var smallVideo;
  662. if (APP.conference.isLocalId(id)) {
  663. if (!localVideoThumbnail) {
  664. console.warn("Local video not ready yet");
  665. return;
  666. }
  667. smallVideo = localVideoThumbnail;
  668. } else if (remoteVideos[id]) {
  669. smallVideo = remoteVideos[id];
  670. } else {
  671. return;
  672. }
  673. smallVideo.setVideoType(newVideoType);
  674. if (this.isCurrentlyOnLarge(id)) {
  675. this.updateLargeVideo(id, true);
  676. }
  677. },
  678. showMore (id) {
  679. if (id === 'local') {
  680. localVideoThumbnail.connectionIndicator.showMore();
  681. } else {
  682. let remoteVideo = remoteVideos[id];
  683. if (remoteVideo) {
  684. remoteVideo.connectionIndicator.showMore();
  685. } else {
  686. console.info("Error - no remote video for id: " + id);
  687. }
  688. }
  689. },
  690. /**
  691. * Resizes the video area.
  692. *
  693. * @param isSideBarVisible indicates if the side bar is currently visible
  694. * @param forceUpdate indicates that hidden thumbnails will be shown
  695. * @param completeFunction a function to be called when the video area is
  696. * resized.
  697. */
  698. resizeVideoArea (isSideBarVisible,
  699. forceUpdate = false,
  700. animate = false,
  701. completeFunction = null) {
  702. if (largeVideo) {
  703. largeVideo.updateContainerSize(isSideBarVisible);
  704. largeVideo.resize(animate);
  705. }
  706. // Calculate available width and height.
  707. let availableHeight = window.innerHeight;
  708. let availableWidth = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  709. if (availableWidth < 0 || availableHeight < 0) {
  710. return;
  711. }
  712. // Resize the thumbnails first.
  713. this.resizeThumbnails(false, forceUpdate, isSideBarVisible);
  714. // Resize the video area element.
  715. $('#videospace').animate({
  716. right: window.innerWidth - availableWidth,
  717. width: availableWidth,
  718. height: availableHeight
  719. }, {
  720. queue: false,
  721. duration: animate ? 500 : 1,
  722. complete: completeFunction
  723. });
  724. },
  725. getSmallVideo (id) {
  726. if (APP.conference.isLocalId(id)) {
  727. return localVideoThumbnail;
  728. } else {
  729. return remoteVideos[id];
  730. }
  731. },
  732. changeUserAvatar (id, avatarUrl) {
  733. var smallVideo = VideoLayout.getSmallVideo(id);
  734. if (smallVideo) {
  735. smallVideo.avatarChanged(avatarUrl);
  736. } else {
  737. console.warn(
  738. "Missed avatar update - no small video yet for " + id
  739. );
  740. }
  741. if (this.isCurrentlyOnLarge(id)) {
  742. largeVideo.updateAvatar(avatarUrl);
  743. }
  744. },
  745. /**
  746. * Indicates that the video has been interrupted.
  747. */
  748. onVideoInterrupted () {
  749. this.enableVideoProblemFilter(true);
  750. let reconnectingKey = "connection.RECONNECTING";
  751. $('#videoConnectionMessage')
  752. .attr("data-i18n", reconnectingKey)
  753. .text(APP.translation.translateString(reconnectingKey))
  754. .css({display: "block"});
  755. },
  756. /**
  757. * Indicates that the video has been restored.
  758. */
  759. onVideoRestored () {
  760. this.enableVideoProblemFilter(false);
  761. $('#videoConnectionMessage').css({display: "none"});
  762. },
  763. enableVideoProblemFilter (enable) {
  764. if (!largeVideo) {
  765. return;
  766. }
  767. largeVideo.enableVideoProblemFilter(enable);
  768. },
  769. isLargeVideoVisible () {
  770. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  771. },
  772. /**
  773. * @return {LargeContainer} the currently displayed container on large
  774. * video.
  775. */
  776. getCurrentlyOnLargeContainer () {
  777. return largeVideo.getContainer(largeVideo.state);
  778. },
  779. isCurrentlyOnLarge (id) {
  780. return largeVideo && largeVideo.id === id;
  781. },
  782. updateLargeVideo (id, forceUpdate) {
  783. if (!largeVideo) {
  784. return;
  785. }
  786. let isOnLarge = this.isCurrentlyOnLarge(id);
  787. let currentId = largeVideo.id;
  788. if (!isOnLarge || forceUpdate) {
  789. if (id !== currentId) {
  790. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  791. }
  792. if (currentId) {
  793. var oldSmallVideo = this.getSmallVideo(currentId);
  794. }
  795. let smallVideo = this.getSmallVideo(id);
  796. let videoType = this.getRemoteVideoType(id);
  797. largeVideo.updateLargeVideo(
  798. id,
  799. smallVideo.videoStream,
  800. videoType
  801. ).then(function() {
  802. // update current small video and the old one
  803. smallVideo.updateView();
  804. oldSmallVideo && oldSmallVideo.updateView();
  805. }, function () {
  806. // use clicked other video during update, nothing to do.
  807. });
  808. } else if (currentId) {
  809. let currentSmallVideo = this.getSmallVideo(currentId);
  810. currentSmallVideo.updateView();
  811. }
  812. },
  813. addLargeVideoContainer (type, container) {
  814. largeVideo && largeVideo.addContainer(type, container);
  815. },
  816. removeLargeVideoContainer (type) {
  817. largeVideo && largeVideo.removeContainer(type);
  818. },
  819. /**
  820. * @returns Promise
  821. */
  822. showLargeVideoContainer (type, show) {
  823. if (!largeVideo) {
  824. return Promise.reject();
  825. }
  826. let isVisible = this.isLargeContainerTypeVisible(type);
  827. if (isVisible === show) {
  828. return Promise.resolve();
  829. }
  830. let currentId = largeVideo.id;
  831. if(currentId) {
  832. var oldSmallVideo = this.getSmallVideo(currentId);
  833. }
  834. let containerTypeToShow = type;
  835. // if we are hiding a container and there is focusedVideo
  836. // (pinned remote video) use its video type,
  837. // if not then use default type - large video
  838. if (!show) {
  839. if(focusedVideoResourceJid)
  840. containerTypeToShow = this.getRemoteVideoType(
  841. focusedVideoResourceJid);
  842. else
  843. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  844. }
  845. return largeVideo.showContainer(containerTypeToShow)
  846. .then(() => {
  847. if(oldSmallVideo)
  848. oldSmallVideo && oldSmallVideo.updateView();
  849. });
  850. },
  851. isLargeContainerTypeVisible (type) {
  852. return largeVideo && largeVideo.state === type;
  853. },
  854. /**
  855. * Returns the id of the current video shown on large.
  856. * Currently used by tests (torture).
  857. */
  858. getLargeVideoID () {
  859. return largeVideo.id;
  860. },
  861. /**
  862. * Returns the the current video shown on large.
  863. * Currently used by tests (torture).
  864. */
  865. getLargeVideo () {
  866. return largeVideo;
  867. },
  868. /**
  869. * Updates the resolution label, indicating to the user that the large
  870. * video stream is currently HD.
  871. */
  872. updateResolutionLabel(isResolutionHD) {
  873. let videoResolutionLabel = $("#videoResolutionLabel");
  874. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  875. videoResolutionLabel.css({display: "block"});
  876. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  877. videoResolutionLabel.css({display: "none"});
  878. }
  879. };
  880. export default VideoLayout;