Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VideoLayout.js 33KB

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