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

VideoLayout.js 35KB

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