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

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