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

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