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

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