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

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