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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 LocalVideo from "./LocalVideo";
  12. import PanelToggler from "../side_pannels/SidePanelToggler";
  13. const RTCUIUtil = JitsiMeetJS.util.RTCUIHelper;
  14. var remoteVideos = {};
  15. var remoteVideoTypes = {};
  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 focusedVideoResourceJid = 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(false, 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, id);
  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. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  81. this.lastNCount = config.channelLastN;
  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 this.lastNCount < 0 || // lastN is disabled
  98. // lastNEndpoints cache not built yet
  99. (this.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. let remoteVideo = remoteVideos[id];
  228. if (remoteVideo) { // remote stream may be removed after participant left the conference
  229. remoteVideo.removeRemoteStreamElement(stream);
  230. }
  231. },
  232. /**
  233. * Return the type of the remote video.
  234. * @param id the id for the remote video
  235. * @returns the video type video or screen.
  236. */
  237. getRemoteVideoType (id) {
  238. return remoteVideoTypes[id];
  239. },
  240. handleVideoThumbClicked (noPinnedEndpointChangedEvent,
  241. resourceJid) {
  242. if(focusedVideoResourceJid) {
  243. var oldSmallVideo
  244. = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  245. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  246. oldSmallVideo.focus(false);
  247. }
  248. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  249. // Unlock current focused.
  250. if (focusedVideoResourceJid === resourceJid)
  251. {
  252. focusedVideoResourceJid = null;
  253. // Enable the currently set dominant speaker.
  254. if (currentDominantSpeaker) {
  255. if(smallVideo && smallVideo.hasVideo()) {
  256. this.updateLargeVideo(currentDominantSpeaker);
  257. }
  258. }
  259. if (!noPinnedEndpointChangedEvent) {
  260. eventEmitter.emit(UIEvents.PINNED_ENDPOINT);
  261. }
  262. return;
  263. }
  264. // Lock new video
  265. focusedVideoResourceJid = resourceJid;
  266. // Update focused/pinned interface.
  267. if (resourceJid) {
  268. if (smallVideo && !interfaceConfig.filmStripOnly)
  269. smallVideo.focus(true);
  270. if (!noPinnedEndpointChangedEvent) {
  271. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  272. }
  273. }
  274. this.updateLargeVideo(resourceJid);
  275. },
  276. /**
  277. * Checks if container for participant identified by given id exists
  278. * in the document and creates it eventually.
  279. *
  280. * @return Returns <tt>true</tt> if the peer container exists,
  281. * <tt>false</tt> - otherwise
  282. */
  283. addParticipantContainer (id) {
  284. let remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  285. remoteVideos[id] = remoteVideo;
  286. let videoType = remoteVideoTypes[id];
  287. if (videoType) {
  288. remoteVideo.setVideoType(videoType);
  289. }
  290. // In case this is not currently in the last n we don't show it.
  291. if (localLastNCount && localLastNCount > 0 &&
  292. FilmStrip.getThumbs().length >= localLastNCount + 2) {
  293. remoteVideo.showPeerContainer('hide');
  294. } else {
  295. VideoLayout.resizeThumbnails(false, true);
  296. }
  297. },
  298. videoactive (videoelem, resourceJid) {
  299. console.info(resourceJid + " video is now active", videoelem);
  300. VideoLayout.resizeThumbnails(
  301. false, false, false, function() {$(videoelem).show();});
  302. // Update the large video to the last added video only if there's no
  303. // current dominant, focused speaker or update it to
  304. // the current dominant speaker.
  305. if ((!focusedVideoResourceJid &&
  306. !currentDominantSpeaker) ||
  307. focusedVideoResourceJid === resourceJid ||
  308. (resourceJid &&
  309. currentDominantSpeaker === resourceJid)) {
  310. this.updateLargeVideo(resourceJid, true);
  311. }
  312. },
  313. /**
  314. * Shows the presence status message for the given video.
  315. */
  316. setPresenceStatus (id, statusMsg) {
  317. remoteVideos[id].setPresenceStatus(statusMsg);
  318. },
  319. /**
  320. * Shows a visual indicator for the moderator of the conference.
  321. * On local or remote participants.
  322. */
  323. showModeratorIndicator () {
  324. let isModerator = APP.conference.isModerator;
  325. if (isModerator) {
  326. localVideoThumbnail.createModeratorIndicatorElement();
  327. }
  328. APP.conference.listMembers().forEach(function (member) {
  329. let id = member.getId();
  330. if (member.isModerator()) {
  331. remoteVideos[id].removeRemoteVideoMenu();
  332. remoteVideos[id].createModeratorIndicatorElement();
  333. } else if (isModerator) {
  334. // We are moderator, but user is not - add menu
  335. if ($(`#remote_popupmenu_${id}`).length <= 0) {
  336. remoteVideos[id].addRemoteVideoMenu();
  337. }
  338. }
  339. });
  340. },
  341. /*
  342. * Shows or hides the audio muted indicator over the local thumbnail video.
  343. * @param {boolean} isMuted
  344. */
  345. showLocalAudioIndicator (isMuted) {
  346. localVideoThumbnail.showAudioIndicator(isMuted);
  347. },
  348. /**
  349. * Resizes thumbnails.
  350. */
  351. resizeThumbnails ( animate = false,
  352. forceUpdate = false,
  353. isSideBarVisible = null,
  354. onComplete = null) {
  355. isSideBarVisible
  356. = (isSideBarVisible !== null)
  357. ? isSideBarVisible : PanelToggler.isVisible();
  358. let {thumbWidth, thumbHeight}
  359. = FilmStrip.calculateThumbnailSize(isSideBarVisible);
  360. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  361. FilmStrip.resizeThumbnails(thumbWidth, thumbHeight,
  362. animate, forceUpdate)
  363. .then(function () {
  364. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  365. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  366. if (onComplete && typeof onComplete === "function")
  367. onComplete();
  368. });
  369. return {thumbWidth, thumbHeight};
  370. },
  371. /**
  372. * On audio muted event.
  373. */
  374. onAudioMute (id, isMuted) {
  375. if (APP.conference.isLocalId(id)) {
  376. localVideoThumbnail.showAudioIndicator(isMuted);
  377. } else {
  378. remoteVideos[id].showAudioIndicator(isMuted);
  379. if (APP.conference.isModerator) {
  380. remoteVideos[id].updateRemoteVideoMenu(isMuted);
  381. }
  382. }
  383. },
  384. /**
  385. * On video muted event.
  386. */
  387. onVideoMute (id, value) {
  388. if (APP.conference.isLocalId(id)) {
  389. localVideoThumbnail.setMutedView(value);
  390. } else {
  391. var remoteVideo = remoteVideos[id];
  392. remoteVideo.setMutedView(value);
  393. }
  394. if (this.isCurrentlyOnLarge(id)) {
  395. // large video will show avatar instead of muted stream
  396. this.updateLargeVideo(id, true);
  397. }
  398. },
  399. /**
  400. * Display name changed.
  401. */
  402. onDisplayNameChanged (id, displayName, status) {
  403. if (id === 'localVideoContainer' ||
  404. APP.conference.isLocalId(id)) {
  405. localVideoThumbnail.setDisplayName(displayName);
  406. } else {
  407. remoteVideos[id].setDisplayName(displayName, status);
  408. }
  409. },
  410. /**
  411. * On dominant speaker changed event.
  412. */
  413. onDominantSpeakerChanged (id) {
  414. if (id === currentDominantSpeaker) {
  415. return;
  416. }
  417. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  418. // We ignore local user events, but just unmark remote user as dominant
  419. // while we are talking
  420. if (APP.conference.isLocalId(id)) {
  421. if(oldSpeakerRemoteVideo)
  422. {
  423. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  424. localVideoThumbnail.updateDominantSpeakerIndicator(true);
  425. currentDominantSpeaker = null;
  426. }
  427. return;
  428. }
  429. let remoteVideo = remoteVideos[id];
  430. if (!remoteVideo) {
  431. return;
  432. }
  433. // Update the current dominant speaker.
  434. remoteVideo.updateDominantSpeakerIndicator(true);
  435. localVideoThumbnail.updateDominantSpeakerIndicator(false);
  436. // let's remove the indications from the remote video if any
  437. if (oldSpeakerRemoteVideo) {
  438. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  439. }
  440. currentDominantSpeaker = id;
  441. // Local video will not have container found, but that's ok
  442. // since we don't want to switch to local video.
  443. // Update the large video if the video source is already available,
  444. // otherwise wait for the "videoactive.jingle" event.
  445. if (!focusedVideoResourceJid && remoteVideo.hasVideoStarted()) {
  446. this.updateLargeVideo(id);
  447. }
  448. },
  449. /**
  450. * On last N change event.
  451. *
  452. * @param lastNEndpoints the list of last N endpoints
  453. * @param endpointsEnteringLastN the list currently entering last N
  454. * endpoints
  455. */
  456. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  457. if (this.lastNCount !== lastNEndpoints.length)
  458. this.lastNCount = lastNEndpoints.length;
  459. lastNEndpointsCache = lastNEndpoints;
  460. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  461. //
  462. // If LastN drops to, say, 2, because of adaptivity, then E should see
  463. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  464. // so E sees them. C is only in E's local LastN set.
  465. //
  466. // If F starts talking and LastN = 3, then E should see thumbnails for
  467. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  468. // enters E's local LastN ejecting C.
  469. // Increase the local LastN set size, if necessary.
  470. if (this.lastNCount > localLastNCount) {
  471. localLastNCount = this.lastNCount;
  472. }
  473. // Update the local LastN set preserving the order in which the
  474. // endpoints appeared in the LastN/local LastN set.
  475. var nextLocalLastNSet = lastNEndpoints.slice(0);
  476. for (var i = 0; i < localLastNSet.length; i++) {
  477. if (nextLocalLastNSet.length >= localLastNCount) {
  478. break;
  479. }
  480. var resourceJid = localLastNSet[i];
  481. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  482. nextLocalLastNSet.push(resourceJid);
  483. }
  484. }
  485. localLastNSet = nextLocalLastNSet;
  486. var updateLargeVideo = false;
  487. // Handle LastN/local LastN changes.
  488. FilmStrip.getThumbs().each(( index, element ) => {
  489. var resourceJid = getPeerContainerResourceId(element);
  490. var smallVideo = remoteVideos[resourceJid];
  491. // We do not want to process any logic for our own(local) video
  492. // because the local participant is never in the lastN set.
  493. // The code of this function might detect that the local participant
  494. // has been dropped out of the lastN set and will update the large
  495. // video
  496. // Detected from avatar tests, where lastN event override
  497. // local video pinning
  498. if(APP.conference.isLocalId(resourceJid))
  499. return;
  500. var isReceived = true;
  501. if (resourceJid &&
  502. lastNEndpoints.indexOf(resourceJid) < 0 &&
  503. localLastNSet.indexOf(resourceJid) < 0) {
  504. console.log("Remove from last N", resourceJid);
  505. if (smallVideo)
  506. smallVideo.showPeerContainer('hide');
  507. else if (!APP.conference.isLocalId(resourceJid))
  508. console.error("No remote video for: " + resourceJid);
  509. isReceived = false;
  510. } else if (resourceJid &&
  511. smallVideo.isVisible() &&
  512. lastNEndpoints.indexOf(resourceJid) < 0 &&
  513. localLastNSet.indexOf(resourceJid) >= 0) {
  514. if (smallVideo)
  515. smallVideo.showPeerContainer('avatar');
  516. else if (!APP.conference.isLocalId(resourceJid))
  517. console.error("No remote video for: " + resourceJid);
  518. isReceived = false;
  519. }
  520. if (!isReceived) {
  521. // resourceJid has dropped out of the server side lastN set, so
  522. // it is no longer being received. If resourceJid was being
  523. // displayed in the large video we have to switch to another
  524. // user.
  525. if (!updateLargeVideo &&
  526. this.isCurrentlyOnLarge(resourceJid)) {
  527. updateLargeVideo = true;
  528. }
  529. }
  530. });
  531. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  532. endpointsEnteringLastN = lastNEndpoints;
  533. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  534. endpointsEnteringLastN.forEach(function (resourceJid) {
  535. var remoteVideo = remoteVideos[resourceJid];
  536. if (remoteVideo)
  537. remoteVideo.showPeerContainer('show');
  538. if (!remoteVideo.isVisible()) {
  539. console.log("Add to last N", resourceJid);
  540. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  541. if (lastNPickupId == resourceJid) {
  542. // Clean up the lastN pickup id.
  543. lastNPickupId = null;
  544. // Don't fire the events again, they've already
  545. // been fired in the contact list click handler.
  546. VideoLayout.handleVideoThumbClicked(
  547. false,
  548. resourceJid);
  549. updateLargeVideo = false;
  550. }
  551. remoteVideo.waitForPlayback(
  552. remoteVideo.selectVideoElement()[0],
  553. remoteVideo.videoStream);
  554. }
  555. });
  556. }
  557. // The endpoint that was being shown in the large video has dropped out
  558. // of the lastN set and there was no lastN pickup jid. We need to update
  559. // the large video now.
  560. if (updateLargeVideo) {
  561. var resource;
  562. // Find out which endpoint to show in the large video.
  563. for (i = 0; i < lastNEndpoints.length; i++) {
  564. resource = lastNEndpoints[i];
  565. if (!resource || APP.conference.isLocalId(resource))
  566. continue;
  567. // videoSrcToSsrc needs to be update for this call to succeed.
  568. this.updateLargeVideo(resource);
  569. break;
  570. }
  571. }
  572. },
  573. /**
  574. * Updates local stats
  575. * @param percent
  576. * @param object
  577. */
  578. updateLocalConnectionStats (percent, object) {
  579. let resolutions = object.resolution;
  580. object.resolution = resolutions[APP.conference.localId];
  581. localVideoThumbnail.updateStatsIndicator(percent, object);
  582. Object.keys(resolutions).forEach(function (id) {
  583. if (APP.conference.isLocalId(id)) {
  584. return;
  585. }
  586. let resolution = resolutions[id];
  587. let remoteVideo = remoteVideos[id];
  588. if (resolution && remoteVideo) {
  589. remoteVideo.updateResolution(resolution);
  590. }
  591. });
  592. },
  593. /**
  594. * Updates remote stats.
  595. * @param id the id associated with the stats
  596. * @param percent the connection quality percent
  597. * @param object the stats data
  598. */
  599. updateConnectionStats (id, percent, object) {
  600. if (remoteVideos[id]) {
  601. remoteVideos[id].updateStatsIndicator(percent, object);
  602. }
  603. },
  604. /**
  605. * Hides the connection indicator
  606. * @param id
  607. */
  608. hideConnectionIndicator (id) {
  609. remoteVideos[id].hideConnectionIndicator();
  610. },
  611. /**
  612. * Hides all the indicators
  613. */
  614. hideStats () {
  615. for(var video in remoteVideos) {
  616. remoteVideos[video].hideIndicator();
  617. }
  618. localVideoThumbnail.hideIndicator();
  619. },
  620. removeParticipantContainer (id) {
  621. // Unlock large video
  622. if (focusedVideoResourceJid === id) {
  623. console.info("Focused video owner has left the conference");
  624. focusedVideoResourceJid = null;
  625. }
  626. if (currentDominantSpeaker === id) {
  627. console.info("Dominant speaker has left the conference");
  628. currentDominantSpeaker = null;
  629. }
  630. var remoteVideo = remoteVideos[id];
  631. if (remoteVideo) {
  632. // Remove remote video
  633. console.info("Removing remote video: " + id);
  634. delete remoteVideos[id];
  635. remoteVideo.remove();
  636. } else {
  637. console.warn("No remote video for " + id);
  638. }
  639. VideoLayout.resizeThumbnails();
  640. },
  641. onVideoTypeChanged (id, newVideoType) {
  642. if (remoteVideoTypes[id] === newVideoType) {
  643. return;
  644. }
  645. console.info("Peer video type changed: ", id, newVideoType);
  646. remoteVideoTypes[id] = newVideoType;
  647. var smallVideo;
  648. if (APP.conference.isLocalId(id)) {
  649. if (!localVideoThumbnail) {
  650. console.warn("Local video not ready yet");
  651. return;
  652. }
  653. smallVideo = localVideoThumbnail;
  654. } else if (remoteVideos[id]) {
  655. smallVideo = remoteVideos[id];
  656. } else {
  657. return;
  658. }
  659. smallVideo.setVideoType(newVideoType);
  660. if (this.isCurrentlyOnLarge(id)) {
  661. this.updateLargeVideo(id, true);
  662. }
  663. },
  664. showMore (id) {
  665. if (id === 'local') {
  666. localVideoThumbnail.connectionIndicator.showMore();
  667. } else {
  668. let remoteVideo = remoteVideos[id];
  669. if (remoteVideo) {
  670. remoteVideo.connectionIndicator.showMore();
  671. } else {
  672. console.info("Error - no remote video for id: " + id);
  673. }
  674. }
  675. },
  676. /**
  677. * Resizes the video area.
  678. *
  679. * @param isSideBarVisible indicates if the side bar is currently visible
  680. * @param forceUpdate indicates that hidden thumbnails will be shown
  681. * @param completeFunction a function to be called when the video area is
  682. * resized.
  683. */resizeVideoArea (isSideBarVisible,
  684. forceUpdate = false,
  685. animate = false,
  686. completeFunction = null) {
  687. if (largeVideo) {
  688. largeVideo.updateContainerSize(isSideBarVisible);
  689. largeVideo.resize(animate);
  690. }
  691. // Calculate available width and height.
  692. let availableHeight = window.innerHeight;
  693. let availableWidth = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  694. if (availableWidth < 0 || availableHeight < 0) {
  695. return;
  696. }
  697. // Resize the thumbnails first.
  698. this.resizeThumbnails(false, forceUpdate, isSideBarVisible);
  699. // Resize the video area element.
  700. $('#videospace').animate({
  701. right: window.innerWidth - availableWidth,
  702. width: availableWidth,
  703. height: availableHeight
  704. }, {
  705. queue: false,
  706. duration: animate ? 500 : 1,
  707. complete: completeFunction
  708. });
  709. },
  710. getSmallVideo (id) {
  711. if (APP.conference.isLocalId(id)) {
  712. return localVideoThumbnail;
  713. } else {
  714. return remoteVideos[id];
  715. }
  716. },
  717. changeUserAvatar (id, avatarUrl) {
  718. var smallVideo = VideoLayout.getSmallVideo(id);
  719. if (smallVideo) {
  720. smallVideo.avatarChanged(avatarUrl);
  721. } else {
  722. console.warn(
  723. "Missed avatar update - no small video yet for " + id
  724. );
  725. }
  726. if (this.isCurrentlyOnLarge(id)) {
  727. largeVideo.updateAvatar(avatarUrl);
  728. }
  729. },
  730. /**
  731. * Indicates that the video has been interrupted.
  732. */
  733. onVideoInterrupted () {
  734. this.enableVideoProblemFilter(true);
  735. let reconnectingKey = "connection.RECONNECTING";
  736. $('#videoConnectionMessage')
  737. .attr("data-i18n", reconnectingKey)
  738. .text(APP.translation.translateString(reconnectingKey))
  739. .css({display: "block"});
  740. },
  741. /**
  742. * Indicates that the video has been restored.
  743. */
  744. onVideoRestored () {
  745. this.enableVideoProblemFilter(false);
  746. $('#videoConnectionMessage').css({display: "none"});
  747. },
  748. enableVideoProblemFilter (enable) {
  749. if (!largeVideo) {
  750. return;
  751. }
  752. largeVideo.enableVideoProblemFilter(enable);
  753. },
  754. isLargeVideoVisible () {
  755. return this.isLargeContainerTypeVisible(VideoContainerType);
  756. },
  757. isCurrentlyOnLarge (id) {
  758. return largeVideo && largeVideo.id === id;
  759. },
  760. updateLargeVideo (id, forceUpdate) {
  761. if (!largeVideo) {
  762. return;
  763. }
  764. let isOnLarge = this.isCurrentlyOnLarge(id);
  765. let currentId = largeVideo.id;
  766. if (!isOnLarge || forceUpdate) {
  767. if (id !== currentId) {
  768. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  769. }
  770. if (currentId) {
  771. var oldSmallVideo = this.getSmallVideo(currentId);
  772. }
  773. let smallVideo = this.getSmallVideo(id);
  774. let videoType = this.getRemoteVideoType(id);
  775. largeVideo.updateLargeVideo(
  776. id,
  777. smallVideo.videoStream,
  778. videoType
  779. ).then(function() {
  780. // update current small video and the old one
  781. smallVideo.updateView();
  782. oldSmallVideo && oldSmallVideo.updateView();
  783. }, function () {
  784. // use clicked other video during update, nothing to do.
  785. });
  786. } else if (currentId) {
  787. let currentSmallVideo = this.getSmallVideo(currentId);
  788. currentSmallVideo.updateView();
  789. }
  790. },
  791. addLargeVideoContainer (type, container) {
  792. largeVideo && largeVideo.addContainer(type, container);
  793. },
  794. removeLargeVideoContainer (type) {
  795. largeVideo && largeVideo.removeContainer(type);
  796. },
  797. /**
  798. * @returns Promise
  799. */
  800. showLargeVideoContainer (type, show) {
  801. if (!largeVideo) {
  802. return Promise.reject();
  803. }
  804. let isVisible = this.isLargeContainerTypeVisible(type);
  805. if (isVisible === show) {
  806. return Promise.resolve();
  807. }
  808. // if !show then use default type - large video
  809. return largeVideo.showContainer(show ? type : VideoContainerType);
  810. },
  811. isLargeContainerTypeVisible (type) {
  812. return largeVideo && largeVideo.state === type;
  813. },
  814. /**
  815. * Returns the id of the current video shown on large.
  816. * Currently used by tests (troture).
  817. */
  818. getLargeVideoID () {
  819. return largeVideo.id;
  820. }
  821. };
  822. export default VideoLayout;