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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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, {VideoContainerType} from "./LargeVideo";
  11. import {PreziContainerType} from '../prezi/Prezi';
  12. import LocalVideo from "./LocalVideo";
  13. const RTCUIUtil = JitsiMeetJS.util.RTCUIHelper;
  14. var remoteVideos = {};
  15. var remoteVideoTypes = {};
  16. var localVideoThumbnail = null;
  17. var currentDominantSpeaker = null;
  18. var lastNCount = config.channelLastN;
  19. var localLastNCount = config.channelLastN;
  20. var localLastNSet = [];
  21. var lastNEndpointsCache = [];
  22. var lastNPickupId = null;
  23. var eventEmitter = null;
  24. /**
  25. * Currently focused video jid
  26. * @type {String}
  27. */
  28. var focusedVideoResourceJid = null;
  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.resizeThumbnails(false, true);
  126. AudioLevels.updateAudioLevelCanvas(null, thumbWidth, thumbHeight);
  127. if (!stream.isMuted()) {
  128. localVideoThumbnail.changeVideo(stream);
  129. }
  130. /* force update if we're currently being displayed */
  131. if (this.isCurrentlyOnLarge(localId)) {
  132. this.updateLargeVideo(localId, true);
  133. }
  134. },
  135. /**
  136. * Get's the localID of the conference and set it to the local video
  137. * (small one). This needs to be called as early as possible, when muc is
  138. * actually joined. Otherwise events can come with information like email
  139. * and setting them assume the id is already set.
  140. */
  141. mucJoined () {
  142. if (largeVideo && !largeVideo.id) {
  143. this.updateLargeVideo(APP.conference.localId, true);
  144. }
  145. },
  146. /**
  147. * Adds or removes icons for not available camera and microphone.
  148. * @param resourceJid the jid of user
  149. * @param devices available devices
  150. */
  151. setDeviceAvailabilityIcons (id, devices) {
  152. if (APP.conference.isLocalId(id)) {
  153. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  154. return;
  155. }
  156. let video = remoteVideos[id];
  157. if (!video) {
  158. return;
  159. }
  160. video.setDeviceAvailabilityIcons(devices);
  161. },
  162. /**
  163. * Checks if removed video is currently displayed and tries to display
  164. * another one instead.
  165. */
  166. updateRemovedVideo (id) {
  167. if (!this.isCurrentlyOnLarge(id)) {
  168. return;
  169. }
  170. let newId;
  171. // We'll show user's avatar if he is the dominant speaker or if
  172. // his video thumbnail is pinned
  173. if (remoteVideos[id] && (id === focusedVideoResourceJid
  174. || id === currentDominantSpeaker)) {
  175. newId = id;
  176. } else {
  177. // Otherwise select last visible video
  178. newId = this.electLastVisibleVideo();
  179. }
  180. this.updateLargeVideo(newId);
  181. },
  182. electLastVisibleVideo () {
  183. // pick the last visible video in the row
  184. // if nobody else is left, this picks the local video
  185. let thumbs = FilmStrip.getThumbs(true).filter('[id!="mixedstream"]');
  186. let lastVisible = thumbs.filter(':visible:last');
  187. if (lastVisible.length) {
  188. let id = getPeerContainerResourceId(lastVisible[0]);
  189. if (remoteVideos[id]) {
  190. console.info("electLastVisibleVideo: " + id);
  191. return id;
  192. }
  193. // The RemoteVideo was removed (but the DOM elements may still
  194. // exist).
  195. }
  196. console.info("Last visible video no longer exists");
  197. thumbs = FilmStrip.getThumbs();
  198. if (thumbs.length) {
  199. let id = getPeerContainerResourceId(thumbs[0]);
  200. if (remoteVideos[id]) {
  201. console.info("electLastVisibleVideo: " + id);
  202. return id;
  203. }
  204. // The RemoteVideo was removed (but the DOM elements may
  205. // still exist).
  206. }
  207. // Go with local video
  208. console.info("Fallback to local video...");
  209. let id = APP.conference.localId;
  210. console.info("electLastVisibleVideo: " + id);
  211. return id;
  212. },
  213. onRemoteStreamAdded (stream) {
  214. let id = stream.getParticipantId();
  215. remoteVideos[id].addRemoteStreamElement(stream);
  216. // if track is muted make sure we reflect that
  217. if(stream.isMuted())
  218. {
  219. if(stream.getType() === "audio")
  220. this.onAudioMute(stream.getParticipantId(), true);
  221. else
  222. this.onVideoMute(stream.getParticipantId(), true);
  223. }
  224. },
  225. onRemoteStreamRemoved (stream) {
  226. let id = stream.getParticipantId();
  227. remoteVideos[id].removeRemoteStreamElement(stream);
  228. },
  229. /**
  230. * Return the type of the remote video.
  231. * @param id the id for the remote video
  232. * @returns the video type video or screen.
  233. */
  234. getRemoteVideoType (id) {
  235. return remoteVideoTypes[id];
  236. },
  237. handleVideoThumbClicked (noPinnedEndpointChangedEvent,
  238. resourceJid) {
  239. if(focusedVideoResourceJid) {
  240. var oldSmallVideo
  241. = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  242. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  243. oldSmallVideo.focus(false);
  244. }
  245. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  246. // Unlock current focused.
  247. if (focusedVideoResourceJid === resourceJid)
  248. {
  249. focusedVideoResourceJid = null;
  250. // Enable the currently set dominant speaker.
  251. if (currentDominantSpeaker) {
  252. if(smallVideo && smallVideo.hasVideo()) {
  253. this.updateLargeVideo(currentDominantSpeaker);
  254. }
  255. }
  256. if (!noPinnedEndpointChangedEvent) {
  257. eventEmitter.emit(UIEvents.PINNED_ENDPOINT);
  258. }
  259. return;
  260. }
  261. // Lock new video
  262. focusedVideoResourceJid = resourceJid;
  263. // Update focused/pinned interface.
  264. if (resourceJid) {
  265. if (smallVideo && !interfaceConfig.filmStripOnly)
  266. smallVideo.focus(true);
  267. if (!noPinnedEndpointChangedEvent) {
  268. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  269. }
  270. }
  271. this.updateLargeVideo(resourceJid);
  272. },
  273. /**
  274. * Checks if container for participant identified by given id exists
  275. * in the document and creates it eventually.
  276. *
  277. * @return Returns <tt>true</tt> if the peer container exists,
  278. * <tt>false</tt> - otherwise
  279. */
  280. addParticipantContainer (id) {
  281. let remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  282. remoteVideos[id] = remoteVideo;
  283. let videoType = remoteVideoTypes[id];
  284. if (videoType) {
  285. remoteVideo.setVideoType(videoType);
  286. }
  287. // In case this is not currently in the last n we don't show it.
  288. if (localLastNCount && localLastNCount > 0 &&
  289. FilmStrip.getThumbs().length >= localLastNCount + 2) {
  290. remoteVideo.showPeerContainer('hide');
  291. } else {
  292. VideoLayout.resizeThumbnails(false, true);
  293. }
  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 thumbnails.
  348. */
  349. resizeThumbnails ( animate = false,
  350. forceUpdate = false,
  351. videoAreaAvailableWidth = null) {
  352. let {thumbWidth, thumbHeight}
  353. = FilmStrip.calculateThumbnailSize(videoAreaAvailableWidth);
  354. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  355. FilmStrip.resizeThumbnails(thumbWidth, thumbHeight,
  356. animate, forceUpdate)
  357. .then(function () {
  358. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  359. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  360. });
  361. return {thumbWidth, thumbHeight};
  362. },
  363. /**
  364. * On audio muted event.
  365. */
  366. onAudioMute (id, isMuted) {
  367. if (APP.conference.isLocalId(id)) {
  368. localVideoThumbnail.showAudioIndicator(isMuted);
  369. } else {
  370. remoteVideos[id].showAudioIndicator(isMuted);
  371. if (APP.conference.isModerator) {
  372. remoteVideos[id].updateRemoteVideoMenu(isMuted);
  373. }
  374. }
  375. },
  376. /**
  377. * On video muted event.
  378. */
  379. onVideoMute (id, value) {
  380. if (APP.conference.isLocalId(id)) {
  381. localVideoThumbnail.setMutedView(value);
  382. } else {
  383. var remoteVideo = remoteVideos[id];
  384. remoteVideo.setMutedView(value);
  385. }
  386. if (this.isCurrentlyOnLarge(id)) {
  387. // large video will show avatar instead of muted stream
  388. this.updateLargeVideo(id, true);
  389. }
  390. },
  391. /**
  392. * Display name changed.
  393. */
  394. onDisplayNameChanged (id, displayName, status) {
  395. if (id === 'localVideoContainer' ||
  396. APP.conference.isLocalId(id)) {
  397. localVideoThumbnail.setDisplayName(displayName);
  398. } else {
  399. remoteVideos[id].setDisplayName(displayName, status);
  400. }
  401. },
  402. /**
  403. * On dominant speaker changed event.
  404. */
  405. onDominantSpeakerChanged (id) {
  406. if (id === currentDominantSpeaker) {
  407. return;
  408. }
  409. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  410. // We ignore local user events, but just unmark remote user as dominant
  411. // while we are talking
  412. if (APP.conference.isLocalId(id)) {
  413. if(oldSpeakerRemoteVideo)
  414. {
  415. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  416. localVideoThumbnail.updateDominantSpeakerIndicator(true);
  417. currentDominantSpeaker = null;
  418. }
  419. return;
  420. }
  421. let remoteVideo = remoteVideos[id];
  422. if (!remoteVideo) {
  423. return;
  424. }
  425. // Update the current dominant speaker.
  426. remoteVideo.updateDominantSpeakerIndicator(true);
  427. localVideoThumbnail.updateDominantSpeakerIndicator(false);
  428. // let's remove the indications from the remote video if any
  429. if (oldSpeakerRemoteVideo) {
  430. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  431. }
  432. currentDominantSpeaker = id;
  433. // Local video will not have container found, but that's ok
  434. // since we don't want to switch to local video.
  435. // Update the large video if the video source is already available,
  436. // otherwise wait for the "videoactive.jingle" event.
  437. if (!focusedVideoResourceJid && remoteVideo.hasVideoStarted()) {
  438. this.updateLargeVideo(id);
  439. }
  440. },
  441. /**
  442. * On last N change event.
  443. *
  444. * @param lastNEndpoints the list of last N endpoints
  445. * @param endpointsEnteringLastN the list currently entering last N
  446. * endpoints
  447. */
  448. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  449. if (lastNCount !== lastNEndpoints.length)
  450. lastNCount = lastNEndpoints.length;
  451. lastNEndpointsCache = lastNEndpoints;
  452. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  453. //
  454. // If LastN drops to, say, 2, because of adaptivity, then E should see
  455. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  456. // so E sees them. C is only in E's local LastN set.
  457. //
  458. // If F starts talking and LastN = 3, then E should see thumbnails for
  459. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  460. // enters E's local LastN ejecting C.
  461. // Increase the local LastN set size, if necessary.
  462. if (lastNCount > localLastNCount) {
  463. localLastNCount = lastNCount;
  464. }
  465. // Update the local LastN set preserving the order in which the
  466. // endpoints appeared in the LastN/local LastN set.
  467. var nextLocalLastNSet = lastNEndpoints.slice(0);
  468. for (var i = 0; i < localLastNSet.length; i++) {
  469. if (nextLocalLastNSet.length >= localLastNCount) {
  470. break;
  471. }
  472. var resourceJid = localLastNSet[i];
  473. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  474. nextLocalLastNSet.push(resourceJid);
  475. }
  476. }
  477. localLastNSet = nextLocalLastNSet;
  478. var updateLargeVideo = false;
  479. // Handle LastN/local LastN changes.
  480. FilmStrip.getThumbs().each(( index, element ) => {
  481. var resourceJid = getPeerContainerResourceId(element);
  482. var smallVideo = remoteVideos[resourceJid];
  483. // We do not want to process any logic for our own(local) video
  484. // because the local participant is never in the lastN set.
  485. // The code of this function might detect that the local participant
  486. // has been dropped out of the lastN set and will update the large
  487. // video
  488. // Detected from avatar tests, where lastN event override
  489. // local video pinning
  490. if(APP.conference.isLocalId(resourceJid))
  491. return;
  492. var isReceived = true;
  493. if (resourceJid &&
  494. lastNEndpoints.indexOf(resourceJid) < 0 &&
  495. localLastNSet.indexOf(resourceJid) < 0) {
  496. console.log("Remove from last N", resourceJid);
  497. if (smallVideo)
  498. smallVideo.showPeerContainer('hide');
  499. else if (!APP.conference.isLocalId(resourceJid))
  500. console.error("No remote video for: " + resourceJid);
  501. isReceived = false;
  502. } else if (resourceJid &&
  503. smallVideo.isVisible() &&
  504. lastNEndpoints.indexOf(resourceJid) < 0 &&
  505. localLastNSet.indexOf(resourceJid) >= 0) {
  506. if (smallVideo)
  507. smallVideo.showPeerContainer('avatar');
  508. else if (!APP.conference.isLocalId(resourceJid))
  509. console.error("No remote video for: " + resourceJid);
  510. isReceived = false;
  511. }
  512. if (!isReceived) {
  513. // resourceJid has dropped out of the server side lastN set, so
  514. // it is no longer being received. If resourceJid was being
  515. // displayed in the large video we have to switch to another
  516. // user.
  517. if (!updateLargeVideo &&
  518. this.isCurrentlyOnLarge(resourceJid)) {
  519. updateLargeVideo = true;
  520. }
  521. }
  522. });
  523. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  524. endpointsEnteringLastN = lastNEndpoints;
  525. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  526. endpointsEnteringLastN.forEach(function (resourceJid) {
  527. var remoteVideo = remoteVideos[resourceJid];
  528. if (remoteVideo)
  529. remoteVideo.showPeerContainer('show');
  530. if (!remoteVideo.isVisible()) {
  531. console.log("Add to last N", resourceJid);
  532. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  533. if (lastNPickupId == resourceJid) {
  534. // Clean up the lastN pickup id.
  535. lastNPickupId = null;
  536. // Don't fire the events again, they've already
  537. // been fired in the contact list click handler.
  538. VideoLayout.handleVideoThumbClicked(
  539. false,
  540. resourceJid);
  541. updateLargeVideo = false;
  542. }
  543. remoteVideo.waitForPlayback(
  544. remoteVideo.selectVideoElement()[0],
  545. remoteVideo.videoStream);
  546. }
  547. });
  548. }
  549. // The endpoint that was being shown in the large video has dropped out
  550. // of the lastN set and there was no lastN pickup jid. We need to update
  551. // the large video now.
  552. if (updateLargeVideo) {
  553. var resource;
  554. // Find out which endpoint to show in the large video.
  555. for (i = 0; i < lastNEndpoints.length; i++) {
  556. resource = lastNEndpoints[i];
  557. if (!resource || APP.conference.isLocalId(resource))
  558. continue;
  559. // videoSrcToSsrc needs to be update for this call to succeed.
  560. this.updateLargeVideo(resource);
  561. break;
  562. }
  563. }
  564. },
  565. /**
  566. * Updates local stats
  567. * @param percent
  568. * @param object
  569. */
  570. updateLocalConnectionStats (percent, object) {
  571. let resolutions = object.resolution;
  572. object.resolution = resolutions[APP.conference.localId];
  573. localVideoThumbnail.updateStatsIndicator(percent, object);
  574. Object.keys(resolutions).forEach(function (id) {
  575. if (APP.conference.isLocalId(id)) {
  576. return;
  577. }
  578. let resolution = resolutions[id];
  579. let remoteVideo = remoteVideos[id];
  580. if (resolution && remoteVideo) {
  581. remoteVideo.updateResolution(resolution);
  582. }
  583. });
  584. },
  585. /**
  586. * Updates remote stats.
  587. * @param id the id associated with the stats
  588. * @param percent the connection quality percent
  589. * @param object the stats data
  590. */
  591. updateConnectionStats (id, percent, object) {
  592. if (remoteVideos[id]) {
  593. remoteVideos[id].updateStatsIndicator(percent, object);
  594. }
  595. },
  596. /**
  597. * Hides the connection indicator
  598. * @param id
  599. */
  600. hideConnectionIndicator (id) {
  601. remoteVideos[id].hideConnectionIndicator();
  602. },
  603. /**
  604. * Hides all the indicators
  605. */
  606. hideStats () {
  607. for(var video in remoteVideos) {
  608. remoteVideos[video].hideIndicator();
  609. }
  610. localVideoThumbnail.hideIndicator();
  611. },
  612. removeParticipantContainer (id) {
  613. // Unlock large video
  614. if (focusedVideoResourceJid === id) {
  615. console.info("Focused video owner has left the conference");
  616. focusedVideoResourceJid = null;
  617. }
  618. if (currentDominantSpeaker === id) {
  619. console.info("Dominant speaker has left the conference");
  620. currentDominantSpeaker = null;
  621. }
  622. var remoteVideo = remoteVideos[id];
  623. if (remoteVideo) {
  624. // Remove remote video
  625. console.info("Removing remote video: " + id);
  626. delete remoteVideos[id];
  627. remoteVideo.remove();
  628. } else {
  629. console.warn("No remote video for " + id);
  630. }
  631. VideoLayout.resizeThumbnails();
  632. },
  633. onVideoTypeChanged (id, newVideoType) {
  634. if (remoteVideoTypes[id] === newVideoType) {
  635. return;
  636. }
  637. console.info("Peer video type changed: ", id, newVideoType);
  638. remoteVideoTypes[id] = newVideoType;
  639. var smallVideo;
  640. if (APP.conference.isLocalId(id)) {
  641. if (!localVideoThumbnail) {
  642. console.warn("Local video not ready yet");
  643. return;
  644. }
  645. smallVideo = localVideoThumbnail;
  646. } else if (remoteVideos[id]) {
  647. smallVideo = remoteVideos[id];
  648. } else {
  649. return;
  650. }
  651. smallVideo.setVideoType(newVideoType);
  652. if (this.isCurrentlyOnLarge(id)) {
  653. this.updateLargeVideo(id, true);
  654. }
  655. },
  656. showMore (id) {
  657. if (id === 'local') {
  658. localVideoThumbnail.connectionIndicator.showMore();
  659. } else {
  660. let remoteVideo = remoteVideos[id];
  661. if (remoteVideo) {
  662. remoteVideo.connectionIndicator.showMore();
  663. } else {
  664. console.info("Error - no remote video for id: " + id);
  665. }
  666. }
  667. },
  668. addRemoteVideoContainer (id) {
  669. return RemoteVideo.createContainer(id);
  670. },
  671. /**
  672. * Resizes the video area.
  673. *
  674. * @param isSideBarVisible indicates if the side bar is currently visible
  675. * @param forceUpdate indicates that hidden thumbnails will be shown
  676. * @param completeFunction a function to be called when the video area is
  677. * resized.
  678. */resizeVideoArea (isSideBarVisible,
  679. forceUpdate = false,
  680. animate = false,
  681. completeFunction = null) {
  682. if (largeVideo) {
  683. largeVideo.updateContainerSize(isSideBarVisible);
  684. largeVideo.resize(animate);
  685. }
  686. // Calculate available width and height.
  687. let availableHeight = window.innerHeight;
  688. let availableWidth = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  689. if (availableWidth < 0 || availableHeight < 0) {
  690. return;
  691. }
  692. // Resize the thumbnails first.
  693. this.resizeThumbnails(false, forceUpdate, availableWidth);
  694. // Resize the video area element.
  695. $('#videospace').animate({
  696. right: window.innerWidth - availableWidth,
  697. width: availableWidth,
  698. height: availableHeight
  699. }, {
  700. queue: false,
  701. duration: animate ? 500 : 1,
  702. complete: completeFunction
  703. });
  704. },
  705. getSmallVideo (id) {
  706. if (APP.conference.isLocalId(id)) {
  707. return localVideoThumbnail;
  708. } else {
  709. return remoteVideos[id];
  710. }
  711. },
  712. changeUserAvatar (id, avatarUrl) {
  713. var smallVideo = VideoLayout.getSmallVideo(id);
  714. if (smallVideo) {
  715. smallVideo.avatarChanged(avatarUrl);
  716. } else {
  717. console.warn(
  718. "Missed avatar update - no small video yet for " + id
  719. );
  720. }
  721. if (this.isCurrentlyOnLarge(id)) {
  722. largeVideo.updateAvatar(avatarUrl);
  723. }
  724. },
  725. /**
  726. * Indicates that the video has been interrupted.
  727. */
  728. onVideoInterrupted () {
  729. this.enableVideoProblemFilter(true);
  730. let reconnectingKey = "connection.RECONNECTING";
  731. $('#videoConnectionMessage')
  732. .attr("data-i18n", reconnectingKey)
  733. .text(APP.translation.translateString(reconnectingKey))
  734. .css({display: "block"});
  735. },
  736. /**
  737. * Indicates that the video has been restored.
  738. */
  739. onVideoRestored () {
  740. this.enableVideoProblemFilter(false);
  741. $('#videoConnectionMessage').css({display: "none"});
  742. },
  743. enableVideoProblemFilter (enable) {
  744. if (!largeVideo) {
  745. return;
  746. }
  747. largeVideo.enableVideoProblemFilter(enable);
  748. },
  749. isLargeVideoVisible () {
  750. return this.isLargeContainerTypeVisible(VideoContainerType);
  751. },
  752. isCurrentlyOnLarge (id) {
  753. return largeVideo && largeVideo.id === id;
  754. },
  755. updateLargeVideo (id, forceUpdate) {
  756. if (!largeVideo) {
  757. return;
  758. }
  759. let isOnLarge = this.isCurrentlyOnLarge(id);
  760. let currentId = largeVideo.id;
  761. if (!isOnLarge || forceUpdate) {
  762. if (id !== currentId) {
  763. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  764. }
  765. if (currentId) {
  766. var oldSmallVideo = this.getSmallVideo(currentId);
  767. }
  768. let smallVideo = this.getSmallVideo(id);
  769. let videoType = this.getRemoteVideoType(id);
  770. largeVideo.updateLargeVideo(
  771. id,
  772. smallVideo.videoStream,
  773. videoType
  774. ).then(function() {
  775. // update current small video and the old one
  776. smallVideo.updateView();
  777. oldSmallVideo && oldSmallVideo.updateView();
  778. }, function () {
  779. // use clicked other video during update, nothing to do.
  780. });
  781. } else if (currentId) {
  782. let currentSmallVideo = this.getSmallVideo(currentId);
  783. currentSmallVideo.updateView();
  784. }
  785. },
  786. addLargeVideoContainer (type, container) {
  787. largeVideo && largeVideo.addContainer(type, container);
  788. },
  789. removeLargeVideoContainer (type) {
  790. largeVideo && largeVideo.removeContainer(type);
  791. },
  792. /**
  793. * @returns Promise
  794. */
  795. showLargeVideoContainer (type, show) {
  796. if (!largeVideo) {
  797. return Promise.reject();
  798. }
  799. let isVisible = this.isLargeContainerTypeVisible(type);
  800. if (isVisible === show) {
  801. return Promise.resolve();
  802. }
  803. // if !show then use default type - large video
  804. return largeVideo.showContainer(show ? type : VideoContainerType);
  805. },
  806. isLargeContainerTypeVisible (type) {
  807. return largeVideo && largeVideo.state === type;
  808. },
  809. /**
  810. * Returns the id of the current video shown on large.
  811. * Currently used by tests (troture).
  812. */
  813. getLargeVideoID () {
  814. return largeVideo.id;
  815. }
  816. };
  817. export default VideoLayout;