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

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