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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /* global config, APP, $, interfaceConfig */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import FilmStrip from "./FilmStrip";
  4. import UIEvents from "../../../service/UI/UIEvents";
  5. import UIUtil from "../util/UIUtil";
  6. import RemoteVideo from "./RemoteVideo";
  7. import LargeVideoManager from "./LargeVideoManager";
  8. import {VIDEO_CONTAINER_TYPE} from "./VideoContainer";
  9. import LocalVideo from "./LocalVideo";
  10. var remoteVideos = {};
  11. var localVideoThumbnail = null;
  12. var currentDominantSpeaker = null;
  13. var localLastNCount = config.channelLastN;
  14. var localLastNSet = [];
  15. var lastNEndpointsCache = [];
  16. var lastNPickupId = null;
  17. var eventEmitter = null;
  18. /**
  19. * Currently focused video jid
  20. * @type {String}
  21. */
  22. var pinnedId = null;
  23. /**
  24. * flipX state of the localVideo
  25. */
  26. let localFlipX = null;
  27. /**
  28. * On contact list item clicked.
  29. */
  30. function onContactClicked (id) {
  31. if (APP.conference.isLocalId(id)) {
  32. $("#localVideoContainer").click();
  33. return;
  34. }
  35. let remoteVideo = remoteVideos[id];
  36. if (remoteVideo && remoteVideo.hasVideo()) {
  37. // It is not always the case that a videoThumb exists (if there is
  38. // no actual video).
  39. if (remoteVideo.hasVideoStarted()) {
  40. // We have a video src, great! Let's update the large video
  41. // now.
  42. VideoLayout.handleVideoThumbClicked(id);
  43. } else {
  44. // If we don't have a video src for jid, there's absolutely
  45. // no point in calling handleVideoThumbClicked; Quite
  46. // simply, it won't work because it needs an src to attach
  47. // to the large video.
  48. //
  49. // Instead, we trigger the pinned endpoint changed event to
  50. // let the bridge adjust its lastN set for myjid and store
  51. // the pinned user in the lastNPickupId variable to be
  52. // picked up later by the lastN changed event handler.
  53. lastNPickupId = id;
  54. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, remoteVideo, true);
  55. }
  56. }
  57. }
  58. /**
  59. * Returns the corresponding resource id to the given peer container
  60. * DOM element.
  61. *
  62. * @return the corresponding resource id to the given peer container
  63. * DOM element
  64. */
  65. function getPeerContainerResourceId (containerElement) {
  66. if (localVideoThumbnail.container === containerElement) {
  67. return localVideoThumbnail.id;
  68. }
  69. let i = containerElement.id.indexOf('participant_');
  70. if (i >= 0) {
  71. return containerElement.id.substring(i + 12);
  72. }
  73. }
  74. let largeVideo;
  75. var VideoLayout = {
  76. init (emitter) {
  77. eventEmitter = emitter;
  78. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED, function (val) {
  79. localFlipX = val;
  80. if(largeVideo)
  81. largeVideo.onLocalFlipXChange(val);
  82. });
  83. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  84. // sets default video type of local video
  85. // FIXME container type is totally different thing from the video type
  86. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  87. // if we do not resize the thumbs here, if there is no video device
  88. // the local video thumb maybe one pixel
  89. this.resizeThumbnails(false, true);
  90. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  91. this.lastNCount = config.channelLastN;
  92. },
  93. initLargeVideo () {
  94. largeVideo = new LargeVideoManager(eventEmitter);
  95. if(localFlipX) {
  96. largeVideo.onLocalFlipXChange(localFlipX);
  97. }
  98. largeVideo.updateContainerSize();
  99. },
  100. /**
  101. * Sets the audio level of the video elements associated to the given id.
  102. *
  103. * @param id the video identifier in the form it comes from the library
  104. * @param lvl the new audio level to update to
  105. */
  106. setAudioLevel(id, lvl) {
  107. let smallVideo = this.getSmallVideo(id);
  108. if (smallVideo)
  109. smallVideo.updateAudioLevelIndicator(lvl);
  110. if (largeVideo && id === largeVideo.id)
  111. largeVideo.updateLargeVideoAudioLevel(lvl);
  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. logger.info("electLastVisibleVideo: " + id);
  227. return id;
  228. }
  229. // The RemoteVideo was removed (but the DOM elements may still
  230. // exist).
  231. }
  232. logger.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. logger.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. logger.info("Fallback to local video...");
  245. let id = APP.conference.getMyUserId();
  246. logger.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. // Remote stream may be removed after participant left the conference.
  268. if (remoteVideo) {
  269. remoteVideo.removeRemoteStreamElement(stream);
  270. }
  271. },
  272. /**
  273. * Return the type of the remote video.
  274. * @param id the id for the remote video
  275. * @returns {String} the video type video or screen.
  276. */
  277. getRemoteVideoType (id) {
  278. let smallVideo = VideoLayout.getSmallVideo(id);
  279. return smallVideo ? smallVideo.getVideoType() : null;
  280. },
  281. isPinned (id) {
  282. return (pinnedId) ? (id === pinnedId) : false;
  283. },
  284. getPinnedId () {
  285. return pinnedId;
  286. },
  287. /**
  288. * Handles the click on a video thumbnail.
  289. *
  290. * @param id the identifier of the video thumbnail
  291. */
  292. handleVideoThumbClicked (id) {
  293. if(pinnedId) {
  294. var oldSmallVideo = VideoLayout.getSmallVideo(pinnedId);
  295. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  296. oldSmallVideo.focus(false);
  297. }
  298. var smallVideo = VideoLayout.getSmallVideo(id);
  299. // Unpin if currently pinned.
  300. if (pinnedId === id)
  301. {
  302. pinnedId = null;
  303. // Enable the currently set dominant speaker.
  304. if (currentDominantSpeaker) {
  305. if(smallVideo && smallVideo.hasVideo()) {
  306. this.updateLargeVideo(currentDominantSpeaker);
  307. }
  308. }
  309. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  310. return;
  311. }
  312. // Lock new video
  313. pinnedId = id;
  314. // Update focused/pinned interface.
  315. if (id) {
  316. if (smallVideo && !interfaceConfig.filmStripOnly)
  317. smallVideo.focus(true);
  318. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  319. }
  320. this.updateLargeVideo(id);
  321. },
  322. /**
  323. * Creates or adds a participant container for the given id and smallVideo.
  324. *
  325. * @param {JitsiParticipant} user the participant to add
  326. * @param {SmallVideo} smallVideo optional small video instance to add as a
  327. * remote video, if undefined <tt>RemoteVideo</tt> will be created
  328. */
  329. addParticipantContainer (user, smallVideo) {
  330. let id = user.getId();
  331. let remoteVideo;
  332. if(smallVideo)
  333. remoteVideo = smallVideo;
  334. else
  335. remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  336. this.addRemoteVideoContainer(id, remoteVideo);
  337. },
  338. /**
  339. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  340. *
  341. * @param {string} the id of the video to add
  342. * @param {SmallVideo} smallVideo the small video instance to add as a
  343. * remote video
  344. */
  345. addRemoteVideoContainer (id, remoteVideo) {
  346. remoteVideos[id] = remoteVideo;
  347. if (!remoteVideo.getVideoType()) {
  348. // make video type the default one (camera)
  349. // FIXME container type is not a video type
  350. remoteVideo.setVideoType(VIDEO_CONTAINER_TYPE);
  351. }
  352. // In case this is not currently in the last n we don't show it.
  353. if (localLastNCount && localLastNCount > 0 &&
  354. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  355. remoteVideo.showPeerContainer('hide');
  356. } else {
  357. VideoLayout.resizeThumbnails(false, true);
  358. }
  359. // Initialize the view
  360. remoteVideo.updateView();
  361. },
  362. // FIXME: what does this do???
  363. remoteVideoActive(videoElement, resourceJid) {
  364. logger.info(resourceJid + " video is now active", videoElement);
  365. VideoLayout.resizeThumbnails(
  366. false, false, function() {$(videoElement).show();});
  367. // Update the large video to the last added video only if there's no
  368. // current dominant, focused speaker or update it to
  369. // the current dominant speaker.
  370. if ((!pinnedId &&
  371. !currentDominantSpeaker &&
  372. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  373. pinnedId === resourceJid ||
  374. (!pinnedId && resourceJid &&
  375. currentDominantSpeaker === resourceJid) ||
  376. /* Playback started while we're on the stage - may need to update
  377. video source with the new stream */
  378. this.isCurrentlyOnLarge(resourceJid)) {
  379. this.updateLargeVideo(resourceJid, true);
  380. }
  381. },
  382. /**
  383. * Shows a visual indicator for the moderator of the conference.
  384. * On local or remote participants.
  385. */
  386. showModeratorIndicator () {
  387. let isModerator = APP.conference.isModerator;
  388. if (isModerator) {
  389. localVideoThumbnail.addModeratorIndicator();
  390. } else {
  391. localVideoThumbnail.removeModeratorIndicator();
  392. }
  393. APP.conference.listMembers().forEach(function (member) {
  394. let id = member.getId();
  395. let remoteVideo = remoteVideos[id];
  396. if (!remoteVideo)
  397. return;
  398. if (member.isModerator()) {
  399. remoteVideo.addModeratorIndicator();
  400. }
  401. if (isModerator) {
  402. // We are moderator, but user is not - add menu
  403. if(!remoteVideo.hasRemoteVideoMenu) {
  404. remoteVideo.addRemoteVideoMenu();
  405. }
  406. }
  407. });
  408. },
  409. /*
  410. * Shows or hides the audio muted indicator over the local thumbnail video.
  411. * @param {boolean} isMuted
  412. */
  413. showLocalAudioIndicator (isMuted) {
  414. localVideoThumbnail.showAudioIndicator(isMuted);
  415. },
  416. /**
  417. * Shows/hides the indication about local connection being interrupted.
  418. *
  419. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  420. * currently in the interrupted state or <tt>false</tt> if the connection
  421. * is fine.
  422. */
  423. showLocalConnectionInterrupted (isInterrupted) {
  424. localVideoThumbnail.connectionIndicator
  425. .updateConnectionStatusIndicator(!isInterrupted);
  426. },
  427. /**
  428. * Resizes thumbnails.
  429. */
  430. resizeThumbnails ( animate = false,
  431. forceUpdate = false,
  432. onComplete = null) {
  433. const { localVideo, remoteVideo }
  434. = FilmStrip.calculateThumbnailSize();
  435. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  436. animate, forceUpdate)
  437. .then(function () {
  438. if (onComplete && typeof onComplete === "function")
  439. onComplete();
  440. });
  441. return { localVideo, remoteVideo };
  442. },
  443. /**
  444. * On audio muted event.
  445. */
  446. onAudioMute (id, isMuted) {
  447. if (APP.conference.isLocalId(id)) {
  448. localVideoThumbnail.showAudioIndicator(isMuted);
  449. } else {
  450. let remoteVideo = remoteVideos[id];
  451. if (!remoteVideo)
  452. return;
  453. remoteVideo.showAudioIndicator(isMuted);
  454. if (APP.conference.isModerator) {
  455. remoteVideo.updateRemoteVideoMenu(isMuted);
  456. }
  457. }
  458. },
  459. /**
  460. * On video muted event.
  461. */
  462. onVideoMute (id, value) {
  463. if (APP.conference.isLocalId(id)) {
  464. localVideoThumbnail.setVideoMutedView(value);
  465. } else {
  466. let remoteVideo = remoteVideos[id];
  467. if (remoteVideo)
  468. remoteVideo.setVideoMutedView(value);
  469. }
  470. if (this.isCurrentlyOnLarge(id)) {
  471. // large video will show avatar instead of muted stream
  472. this.updateLargeVideo(id, true);
  473. }
  474. },
  475. /**
  476. * Display name changed.
  477. */
  478. onDisplayNameChanged (id, displayName, status) {
  479. if (id === 'localVideoContainer' ||
  480. APP.conference.isLocalId(id)) {
  481. localVideoThumbnail.setDisplayName(displayName);
  482. } else {
  483. let remoteVideo = remoteVideos[id];
  484. if (remoteVideo)
  485. remoteVideo.setDisplayName(displayName, status);
  486. }
  487. },
  488. /**
  489. * Sets the "raised hand" status for a participant identified by 'id'.
  490. */
  491. setRaisedHandStatus(id, raisedHandStatus) {
  492. var video
  493. = APP.conference.isLocalId(id)
  494. ? localVideoThumbnail : remoteVideos[id];
  495. if (video) {
  496. video.showRaisedHandIndicator(raisedHandStatus);
  497. if (raisedHandStatus) {
  498. video.showDominantSpeakerIndicator(false);
  499. }
  500. }
  501. },
  502. /**
  503. * On dominant speaker changed event.
  504. */
  505. onDominantSpeakerChanged (id) {
  506. if (id === currentDominantSpeaker) {
  507. return;
  508. }
  509. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  510. // We ignore local user events, but just unmark remote user as dominant
  511. // while we are talking
  512. if (APP.conference.isLocalId(id)) {
  513. if(oldSpeakerRemoteVideo)
  514. {
  515. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  516. currentDominantSpeaker = null;
  517. }
  518. localVideoThumbnail.showDominantSpeakerIndicator(true);
  519. return;
  520. }
  521. let remoteVideo = remoteVideos[id];
  522. if (!remoteVideo) {
  523. return;
  524. }
  525. // Update the current dominant speaker.
  526. remoteVideo.showDominantSpeakerIndicator(true);
  527. localVideoThumbnail.showDominantSpeakerIndicator(false);
  528. // let's remove the indications from the remote video if any
  529. if (oldSpeakerRemoteVideo) {
  530. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  531. }
  532. currentDominantSpeaker = id;
  533. // Local video will not have container found, but that's ok
  534. // since we don't want to switch to local video.
  535. // Update the large video if the video source is already available,
  536. // otherwise wait for the "videoactive.jingle" event.
  537. // FIXME: there is no "videoactive.jingle" event.
  538. if (!pinnedId
  539. && remoteVideo.hasVideoStarted()
  540. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  541. this.updateLargeVideo(id);
  542. }
  543. },
  544. /**
  545. * Shows/hides warning about remote user's connectivity issues.
  546. *
  547. * @param {string} id the ID of the remote participant(MUC nickname)
  548. * @param {boolean} isActive true if the connection is ok or false when
  549. * the user is having connectivity issues.
  550. */
  551. // eslint-disable-next-line no-unused-vars
  552. onParticipantConnectionStatusChanged (id, isActive) {
  553. // Show/hide warning on the large video
  554. if (this.isCurrentlyOnLarge(id)) {
  555. if (largeVideo) {
  556. // We have to trigger full large video update to transition from
  557. // avatar to video on connectivity restored.
  558. this.updateLargeVideo(id, true /* force update */);
  559. }
  560. }
  561. // Show/hide warning on the thumbnail
  562. let remoteVideo = remoteVideos[id];
  563. if (remoteVideo) {
  564. // Updating only connection status indicator is not enough, because
  565. // when we the connection is restored while the avatar was displayed
  566. // (due to 'muted while disconnected' condition) we may want to show
  567. // the video stream again and in order to do that the display mode
  568. // must be updated.
  569. //remoteVideo.updateConnectionStatusIndicator(isActive);
  570. remoteVideo.updateView();
  571. }
  572. },
  573. /**
  574. * On last N change event.
  575. *
  576. * @param lastNEndpoints the list of last N endpoints
  577. * @param endpointsEnteringLastN the list currently entering last N
  578. * endpoints
  579. */
  580. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  581. if (this.lastNCount !== lastNEndpoints.length)
  582. this.lastNCount = lastNEndpoints.length;
  583. lastNEndpointsCache = lastNEndpoints;
  584. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  585. //
  586. // If LastN drops to, say, 2, because of adaptivity, then E should see
  587. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  588. // so E sees them. C is only in E's local LastN set.
  589. //
  590. // If F starts talking and LastN = 3, then E should see thumbnails for
  591. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  592. // enters E's local LastN ejecting C.
  593. // Increase the local LastN set size, if necessary.
  594. if (this.lastNCount > localLastNCount) {
  595. localLastNCount = this.lastNCount;
  596. }
  597. // Update the local LastN set preserving the order in which the
  598. // endpoints appeared in the LastN/local LastN set.
  599. var nextLocalLastNSet = lastNEndpoints.slice(0);
  600. for (var i = 0; i < localLastNSet.length; i++) {
  601. if (nextLocalLastNSet.length >= localLastNCount) {
  602. break;
  603. }
  604. var resourceJid = localLastNSet[i];
  605. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  606. nextLocalLastNSet.push(resourceJid);
  607. }
  608. }
  609. localLastNSet = nextLocalLastNSet;
  610. var updateLargeVideo = false;
  611. // Handle LastN/local LastN changes.
  612. FilmStrip.getThumbs().remoteThumbs.each(( index, element ) => {
  613. var resourceJid = getPeerContainerResourceId(element);
  614. var smallVideo = remoteVideos[resourceJid];
  615. // We do not want to process any logic for our own(local) video
  616. // because the local participant is never in the lastN set.
  617. // The code of this function might detect that the local participant
  618. // has been dropped out of the lastN set and will update the large
  619. // video
  620. // Detected from avatar tests, where lastN event override
  621. // local video pinning
  622. if(APP.conference.isLocalId(resourceJid))
  623. return;
  624. var isReceived = true;
  625. if (resourceJid &&
  626. lastNEndpoints.indexOf(resourceJid) < 0 &&
  627. localLastNSet.indexOf(resourceJid) < 0) {
  628. logger.log("Remove from last N", resourceJid);
  629. if (smallVideo)
  630. smallVideo.showPeerContainer('hide');
  631. else if (!APP.conference.isLocalId(resourceJid))
  632. logger.error("No remote video for: " + resourceJid);
  633. isReceived = false;
  634. } else if (resourceJid &&
  635. //TOFIX: smallVideo may be undefined
  636. smallVideo.isVisible() &&
  637. lastNEndpoints.indexOf(resourceJid) < 0 &&
  638. localLastNSet.indexOf(resourceJid) >= 0) {
  639. // TOFIX: if we're here we already know that the smallVideo
  640. // exists. Look at the previous FIX above.
  641. if (smallVideo)
  642. smallVideo.showPeerContainer('avatar');
  643. else if (!APP.conference.isLocalId(resourceJid))
  644. logger.error("No remote video for: " + resourceJid);
  645. isReceived = false;
  646. }
  647. if (!isReceived) {
  648. // resourceJid has dropped out of the server side lastN set, so
  649. // it is no longer being received. If resourceJid was being
  650. // displayed in the large video we have to switch to another
  651. // user.
  652. if (!updateLargeVideo &&
  653. this.isCurrentlyOnLarge(resourceJid)) {
  654. updateLargeVideo = true;
  655. }
  656. }
  657. });
  658. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  659. endpointsEnteringLastN = lastNEndpoints;
  660. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  661. endpointsEnteringLastN.forEach(function (resourceJid) {
  662. var remoteVideo = remoteVideos[resourceJid];
  663. if (remoteVideo)
  664. remoteVideo.showPeerContainer('show');
  665. if (!remoteVideo.isVisible()) {
  666. logger.log("Add to last N", resourceJid);
  667. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  668. if (lastNPickupId == resourceJid) {
  669. // Clean up the lastN pickup id.
  670. lastNPickupId = null;
  671. VideoLayout.handleVideoThumbClicked(resourceJid);
  672. updateLargeVideo = false;
  673. }
  674. remoteVideo.waitForPlayback(
  675. remoteVideo.selectVideoElement()[0],
  676. remoteVideo.videoStream);
  677. }
  678. });
  679. }
  680. // The endpoint that was being shown in the large video has dropped out
  681. // of the lastN set and there was no lastN pickup jid. We need to update
  682. // the large video now.
  683. if (updateLargeVideo) {
  684. var resource;
  685. // Find out which endpoint to show in the large video.
  686. for (i = 0; i < lastNEndpoints.length; i++) {
  687. resource = lastNEndpoints[i];
  688. if (!resource || APP.conference.isLocalId(resource))
  689. continue;
  690. // videoSrcToSsrc needs to be update for this call to succeed.
  691. this.updateLargeVideo(resource);
  692. break;
  693. }
  694. }
  695. },
  696. /**
  697. * Updates local stats
  698. * @param percent
  699. * @param object
  700. */
  701. updateLocalConnectionStats (percent, object) {
  702. let resolutions = object.resolution;
  703. object.resolution = resolutions[APP.conference.getMyUserId()];
  704. localVideoThumbnail.updateStatsIndicator(percent, object);
  705. Object.keys(resolutions).forEach(function (id) {
  706. if (APP.conference.isLocalId(id)) {
  707. return;
  708. }
  709. let resolution = resolutions[id];
  710. let remoteVideo = remoteVideos[id];
  711. if (resolution && remoteVideo) {
  712. remoteVideo.updateResolution(resolution);
  713. }
  714. });
  715. },
  716. /**
  717. * Updates remote stats.
  718. * @param id the id associated with the stats
  719. * @param percent the connection quality percent
  720. * @param object the stats data
  721. */
  722. updateConnectionStats (id, percent, object) {
  723. let remoteVideo = remoteVideos[id];
  724. if (remoteVideo) {
  725. remoteVideo.updateStatsIndicator(percent, object);
  726. }
  727. },
  728. /**
  729. * Hides the connection indicator
  730. * @param id
  731. */
  732. hideConnectionIndicator (id) {
  733. let remoteVideo = remoteVideos[id];
  734. if (remoteVideo)
  735. remoteVideo.hideConnectionIndicator();
  736. },
  737. /**
  738. * Hides all the indicators
  739. */
  740. hideStats () {
  741. for (var video in remoteVideos) {
  742. let remoteVideo = remoteVideos[video];
  743. if (remoteVideo)
  744. remoteVideo.hideIndicator();
  745. }
  746. localVideoThumbnail.hideIndicator();
  747. },
  748. removeParticipantContainer (id) {
  749. // Unlock large video
  750. if (pinnedId === id) {
  751. logger.info("Focused video owner has left the conference");
  752. pinnedId = null;
  753. }
  754. if (currentDominantSpeaker === id) {
  755. logger.info("Dominant speaker has left the conference");
  756. currentDominantSpeaker = null;
  757. }
  758. var remoteVideo = remoteVideos[id];
  759. if (remoteVideo) {
  760. // Remove remote video
  761. logger.info("Removing remote video: " + id);
  762. delete remoteVideos[id];
  763. remoteVideo.remove();
  764. } else {
  765. logger.warn("No remote video for " + id);
  766. }
  767. VideoLayout.resizeThumbnails();
  768. },
  769. onVideoTypeChanged (id, newVideoType) {
  770. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  771. return;
  772. }
  773. logger.info("Peer video type changed: ", id, newVideoType);
  774. var smallVideo;
  775. if (APP.conference.isLocalId(id)) {
  776. if (!localVideoThumbnail) {
  777. logger.warn("Local video not ready yet");
  778. return;
  779. }
  780. smallVideo = localVideoThumbnail;
  781. } else if (remoteVideos[id]) {
  782. smallVideo = remoteVideos[id];
  783. } else {
  784. return;
  785. }
  786. smallVideo.setVideoType(newVideoType);
  787. if (this.isCurrentlyOnLarge(id)) {
  788. this.updateLargeVideo(id, true);
  789. }
  790. },
  791. showMore (id) {
  792. if (id === 'local') {
  793. localVideoThumbnail.connectionIndicator.showMore();
  794. } else {
  795. let remoteVideo = remoteVideos[id];
  796. if (remoteVideo) {
  797. remoteVideo.connectionIndicator.showMore();
  798. } else {
  799. logger.info("Error - no remote video for id: " + id);
  800. }
  801. }
  802. },
  803. /**
  804. * Resizes the video area.
  805. *
  806. * @param forceUpdate indicates that hidden thumbnails will be shown
  807. * @param completeFunction a function to be called when the video area is
  808. * resized.
  809. */
  810. resizeVideoArea (forceUpdate = false,
  811. animate = false,
  812. completeFunction = null) {
  813. if (largeVideo) {
  814. largeVideo.updateContainerSize();
  815. largeVideo.resize(animate);
  816. }
  817. // Calculate available width and height.
  818. let availableHeight = window.innerHeight;
  819. let availableWidth = UIUtil.getAvailableVideoWidth();
  820. if (availableWidth < 0 || availableHeight < 0) {
  821. return;
  822. }
  823. // Resize the thumbnails first.
  824. this.resizeThumbnails(false, forceUpdate);
  825. // Resize the video area element.
  826. $('#videospace').animate({
  827. right: window.innerWidth - availableWidth,
  828. width: availableWidth,
  829. height: availableHeight
  830. }, {
  831. queue: false,
  832. duration: animate ? 500 : 1,
  833. complete: completeFunction
  834. });
  835. },
  836. getSmallVideo (id) {
  837. if (APP.conference.isLocalId(id)) {
  838. return localVideoThumbnail;
  839. } else {
  840. return remoteVideos[id];
  841. }
  842. },
  843. changeUserAvatar (id, avatarUrl) {
  844. var smallVideo = VideoLayout.getSmallVideo(id);
  845. if (smallVideo) {
  846. smallVideo.avatarChanged(avatarUrl);
  847. } else {
  848. logger.warn(
  849. "Missed avatar update - no small video yet for " + id
  850. );
  851. }
  852. if (this.isCurrentlyOnLarge(id)) {
  853. largeVideo.updateAvatar(avatarUrl);
  854. }
  855. },
  856. /**
  857. * Indicates that the video has been interrupted.
  858. */
  859. onVideoInterrupted () {
  860. if (largeVideo) {
  861. largeVideo.onVideoInterrupted();
  862. }
  863. },
  864. /**
  865. * Indicates that the video has been restored.
  866. */
  867. onVideoRestored () {
  868. if (largeVideo) {
  869. largeVideo.onVideoRestored();
  870. }
  871. },
  872. isLargeVideoVisible () {
  873. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  874. },
  875. /**
  876. * @return {LargeContainer} the currently displayed container on large
  877. * video.
  878. */
  879. getCurrentlyOnLargeContainer () {
  880. return largeVideo.getContainer(largeVideo.state);
  881. },
  882. isCurrentlyOnLarge (id) {
  883. return largeVideo && largeVideo.id === id;
  884. },
  885. updateLargeVideo (id, forceUpdate) {
  886. if (!largeVideo) {
  887. return;
  888. }
  889. let isOnLarge = this.isCurrentlyOnLarge(id);
  890. let currentId = largeVideo.id;
  891. if (!isOnLarge || forceUpdate) {
  892. let videoType = this.getRemoteVideoType(id);
  893. // FIXME video type is not the same thing as container type
  894. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  895. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  896. }
  897. let smallVideo = this.getSmallVideo(id);
  898. let oldSmallVideo;
  899. if (currentId) {
  900. oldSmallVideo = this.getSmallVideo(currentId);
  901. }
  902. smallVideo.waitForResolutionChange();
  903. if (oldSmallVideo)
  904. oldSmallVideo.waitForResolutionChange();
  905. largeVideo.updateLargeVideo(
  906. id,
  907. smallVideo.videoStream,
  908. videoType
  909. ).then(function() {
  910. // update current small video and the old one
  911. smallVideo.updateView();
  912. oldSmallVideo && oldSmallVideo.updateView();
  913. }, function () {
  914. // use clicked other video during update, nothing to do.
  915. });
  916. } else if (currentId) {
  917. let currentSmallVideo = this.getSmallVideo(currentId);
  918. currentSmallVideo.updateView();
  919. }
  920. },
  921. addLargeVideoContainer (type, container) {
  922. largeVideo && largeVideo.addContainer(type, container);
  923. },
  924. removeLargeVideoContainer (type) {
  925. largeVideo && largeVideo.removeContainer(type);
  926. },
  927. /**
  928. * @returns Promise
  929. */
  930. showLargeVideoContainer (type, show) {
  931. if (!largeVideo) {
  932. return Promise.reject();
  933. }
  934. let isVisible = this.isLargeContainerTypeVisible(type);
  935. if (isVisible === show) {
  936. return Promise.resolve();
  937. }
  938. let currentId = largeVideo.id;
  939. if(currentId) {
  940. var oldSmallVideo = this.getSmallVideo(currentId);
  941. }
  942. let containerTypeToShow = type;
  943. // if we are hiding a container and there is focusedVideo
  944. // (pinned remote video) use its video type,
  945. // if not then use default type - large video
  946. if (!show) {
  947. if(pinnedId)
  948. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  949. else
  950. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  951. }
  952. return largeVideo.showContainer(containerTypeToShow)
  953. .then(() => {
  954. if(oldSmallVideo)
  955. oldSmallVideo && oldSmallVideo.updateView();
  956. });
  957. },
  958. isLargeContainerTypeVisible (type) {
  959. return largeVideo && largeVideo.state === type;
  960. },
  961. /**
  962. * Returns the id of the current video shown on large.
  963. * Currently used by tests (torture).
  964. */
  965. getLargeVideoID () {
  966. return largeVideo.id;
  967. },
  968. /**
  969. * Returns the the current video shown on large.
  970. * Currently used by tests (torture).
  971. */
  972. getLargeVideo () {
  973. return largeVideo;
  974. },
  975. /**
  976. * Updates the resolution label, indicating to the user that the large
  977. * video stream is currently HD.
  978. */
  979. updateResolutionLabel(isResolutionHD) {
  980. let id = 'videoResolutionLabel';
  981. if (isResolutionHD) {
  982. UIUtil.showElement(id);
  983. } else {
  984. UIUtil.hideElement(id);
  985. }
  986. },
  987. /**
  988. * Sets the flipX state of the local video.
  989. * @param {boolean} true for flipped otherwise false;
  990. */
  991. setLocalFlipX: function (val) {
  992. this.localFlipX = val;
  993. },
  994. getEventEmitter: () => {return eventEmitter;}
  995. };
  996. export default VideoLayout;