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

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