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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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.localId;
  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.localId, 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.localId;
  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. }
  389. APP.conference.listMembers().forEach(function (member) {
  390. let id = member.getId();
  391. let remoteVideo = remoteVideos[id];
  392. if (!remoteVideo)
  393. return;
  394. if (member.isModerator()) {
  395. remoteVideo.removeRemoteVideoMenu();
  396. remoteVideo.createModeratorIndicatorElement();
  397. } else if (isModerator) {
  398. // We are moderator, but user is not - add menu
  399. if ($(`#remote_popupmenu_${id}`).length <= 0) {
  400. remoteVideo.addRemoteVideoMenu();
  401. }
  402. }
  403. });
  404. },
  405. /*
  406. * Shows or hides the audio muted indicator over the local thumbnail video.
  407. * @param {boolean} isMuted
  408. */
  409. showLocalAudioIndicator (isMuted) {
  410. localVideoThumbnail.showAudioIndicator(isMuted);
  411. },
  412. /**
  413. * Resizes thumbnails.
  414. */
  415. resizeThumbnails ( animate = false,
  416. forceUpdate = false,
  417. isSideBarVisible = null,
  418. onComplete = null) {
  419. isSideBarVisible
  420. = (isSideBarVisible !== null)
  421. ? isSideBarVisible : PanelToggler.isVisible();
  422. let {thumbWidth, thumbHeight}
  423. = FilmStrip.calculateThumbnailSize(isSideBarVisible);
  424. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  425. FilmStrip.resizeThumbnails(thumbWidth, thumbHeight,
  426. animate, forceUpdate)
  427. .then(function () {
  428. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  429. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  430. if (onComplete && typeof onComplete === "function")
  431. onComplete();
  432. });
  433. return {thumbWidth, thumbHeight};
  434. },
  435. /**
  436. * On audio muted event.
  437. */
  438. onAudioMute (id, isMuted) {
  439. if (APP.conference.isLocalId(id)) {
  440. localVideoThumbnail.showAudioIndicator(isMuted);
  441. } else {
  442. let remoteVideo = remoteVideos[id];
  443. if (!remoteVideo)
  444. return;
  445. remoteVideo.showAudioIndicator(isMuted);
  446. if (APP.conference.isModerator) {
  447. remoteVideo.updateRemoteVideoMenu(isMuted);
  448. }
  449. }
  450. },
  451. /**
  452. * On video muted event.
  453. */
  454. onVideoMute (id, value) {
  455. if (APP.conference.isLocalId(id)) {
  456. localVideoThumbnail.setMutedView(value);
  457. } else {
  458. let remoteVideo = remoteVideos[id];
  459. if (remoteVideo)
  460. remoteVideo.setMutedView(value);
  461. }
  462. if (this.isCurrentlyOnLarge(id)) {
  463. // large video will show avatar instead of muted stream
  464. this.updateLargeVideo(id, true);
  465. }
  466. },
  467. /**
  468. * Display name changed.
  469. */
  470. onDisplayNameChanged (id, displayName, status) {
  471. if (id === 'localVideoContainer' ||
  472. APP.conference.isLocalId(id)) {
  473. localVideoThumbnail.setDisplayName(displayName);
  474. } else {
  475. let remoteVideo = remoteVideos[id];
  476. if (remoteVideo)
  477. remoteVideo.setDisplayName(displayName, status);
  478. }
  479. },
  480. /**
  481. * On dominant speaker changed event.
  482. */
  483. onDominantSpeakerChanged (id) {
  484. if (id === currentDominantSpeaker) {
  485. return;
  486. }
  487. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  488. // We ignore local user events, but just unmark remote user as dominant
  489. // while we are talking
  490. if (APP.conference.isLocalId(id)) {
  491. if(oldSpeakerRemoteVideo)
  492. {
  493. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  494. currentDominantSpeaker = null;
  495. }
  496. localVideoThumbnail.updateDominantSpeakerIndicator(true);
  497. return;
  498. }
  499. let remoteVideo = remoteVideos[id];
  500. if (!remoteVideo) {
  501. return;
  502. }
  503. // Update the current dominant speaker.
  504. remoteVideo.updateDominantSpeakerIndicator(true);
  505. localVideoThumbnail.updateDominantSpeakerIndicator(false);
  506. // let's remove the indications from the remote video if any
  507. if (oldSpeakerRemoteVideo) {
  508. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  509. }
  510. currentDominantSpeaker = id;
  511. // Local video will not have container found, but that's ok
  512. // since we don't want to switch to local video.
  513. // Update the large video if the video source is already available,
  514. // otherwise wait for the "videoactive.jingle" event.
  515. if (!pinnedId
  516. && remoteVideo.hasVideoStarted()
  517. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  518. this.updateLargeVideo(id);
  519. }
  520. },
  521. /**
  522. * On last N change event.
  523. *
  524. * @param lastNEndpoints the list of last N endpoints
  525. * @param endpointsEnteringLastN the list currently entering last N
  526. * endpoints
  527. */
  528. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  529. if (this.lastNCount !== lastNEndpoints.length)
  530. this.lastNCount = lastNEndpoints.length;
  531. lastNEndpointsCache = lastNEndpoints;
  532. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  533. //
  534. // If LastN drops to, say, 2, because of adaptivity, then E should see
  535. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  536. // so E sees them. C is only in E's local LastN set.
  537. //
  538. // If F starts talking and LastN = 3, then E should see thumbnails for
  539. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  540. // enters E's local LastN ejecting C.
  541. // Increase the local LastN set size, if necessary.
  542. if (this.lastNCount > localLastNCount) {
  543. localLastNCount = this.lastNCount;
  544. }
  545. // Update the local LastN set preserving the order in which the
  546. // endpoints appeared in the LastN/local LastN set.
  547. var nextLocalLastNSet = lastNEndpoints.slice(0);
  548. for (var i = 0; i < localLastNSet.length; i++) {
  549. if (nextLocalLastNSet.length >= localLastNCount) {
  550. break;
  551. }
  552. var resourceJid = localLastNSet[i];
  553. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  554. nextLocalLastNSet.push(resourceJid);
  555. }
  556. }
  557. localLastNSet = nextLocalLastNSet;
  558. var updateLargeVideo = false;
  559. // Handle LastN/local LastN changes.
  560. FilmStrip.getThumbs().each(( index, element ) => {
  561. var resourceJid = getPeerContainerResourceId(element);
  562. var smallVideo = remoteVideos[resourceJid];
  563. // We do not want to process any logic for our own(local) video
  564. // because the local participant is never in the lastN set.
  565. // The code of this function might detect that the local participant
  566. // has been dropped out of the lastN set and will update the large
  567. // video
  568. // Detected from avatar tests, where lastN event override
  569. // local video pinning
  570. if(APP.conference.isLocalId(resourceJid))
  571. return;
  572. var isReceived = true;
  573. if (resourceJid &&
  574. lastNEndpoints.indexOf(resourceJid) < 0 &&
  575. localLastNSet.indexOf(resourceJid) < 0) {
  576. console.log("Remove from last N", resourceJid);
  577. if (smallVideo)
  578. smallVideo.showPeerContainer('hide');
  579. else if (!APP.conference.isLocalId(resourceJid))
  580. console.error("No remote video for: " + resourceJid);
  581. isReceived = false;
  582. } else if (resourceJid &&
  583. //TOFIX: smallVideo may be undefined
  584. smallVideo.isVisible() &&
  585. lastNEndpoints.indexOf(resourceJid) < 0 &&
  586. localLastNSet.indexOf(resourceJid) >= 0) {
  587. // TOFIX: if we're here we already know that the smallVideo
  588. // exists. Look at the previous FIX above.
  589. if (smallVideo)
  590. smallVideo.showPeerContainer('avatar');
  591. else if (!APP.conference.isLocalId(resourceJid))
  592. console.error("No remote video for: " + resourceJid);
  593. isReceived = false;
  594. }
  595. if (!isReceived) {
  596. // resourceJid has dropped out of the server side lastN set, so
  597. // it is no longer being received. If resourceJid was being
  598. // displayed in the large video we have to switch to another
  599. // user.
  600. if (!updateLargeVideo &&
  601. this.isCurrentlyOnLarge(resourceJid)) {
  602. updateLargeVideo = true;
  603. }
  604. }
  605. });
  606. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  607. endpointsEnteringLastN = lastNEndpoints;
  608. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  609. endpointsEnteringLastN.forEach(function (resourceJid) {
  610. var remoteVideo = remoteVideos[resourceJid];
  611. if (remoteVideo)
  612. remoteVideo.showPeerContainer('show');
  613. if (!remoteVideo.isVisible()) {
  614. console.log("Add to last N", resourceJid);
  615. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  616. if (lastNPickupId == resourceJid) {
  617. // Clean up the lastN pickup id.
  618. lastNPickupId = null;
  619. VideoLayout.handleVideoThumbClicked(resourceJid);
  620. updateLargeVideo = false;
  621. }
  622. remoteVideo.waitForPlayback(
  623. remoteVideo.selectVideoElement()[0],
  624. remoteVideo.videoStream);
  625. }
  626. });
  627. }
  628. // The endpoint that was being shown in the large video has dropped out
  629. // of the lastN set and there was no lastN pickup jid. We need to update
  630. // the large video now.
  631. if (updateLargeVideo) {
  632. var resource;
  633. // Find out which endpoint to show in the large video.
  634. for (i = 0; i < lastNEndpoints.length; i++) {
  635. resource = lastNEndpoints[i];
  636. if (!resource || APP.conference.isLocalId(resource))
  637. continue;
  638. // videoSrcToSsrc needs to be update for this call to succeed.
  639. this.updateLargeVideo(resource);
  640. break;
  641. }
  642. }
  643. },
  644. /**
  645. * Updates local stats
  646. * @param percent
  647. * @param object
  648. */
  649. updateLocalConnectionStats (percent, object) {
  650. let resolutions = object.resolution;
  651. object.resolution = resolutions[APP.conference.localId];
  652. localVideoThumbnail.updateStatsIndicator(percent, object);
  653. Object.keys(resolutions).forEach(function (id) {
  654. if (APP.conference.isLocalId(id)) {
  655. return;
  656. }
  657. let resolution = resolutions[id];
  658. let remoteVideo = remoteVideos[id];
  659. if (resolution && remoteVideo) {
  660. remoteVideo.updateResolution(resolution);
  661. }
  662. });
  663. },
  664. /**
  665. * Updates remote stats.
  666. * @param id the id associated with the stats
  667. * @param percent the connection quality percent
  668. * @param object the stats data
  669. */
  670. updateConnectionStats (id, percent, object) {
  671. let remoteVideo = remoteVideos[id];
  672. if (remoteVideo) {
  673. remoteVideo.updateStatsIndicator(percent, object);
  674. }
  675. },
  676. /**
  677. * Hides the connection indicator
  678. * @param id
  679. */
  680. hideConnectionIndicator (id) {
  681. let remoteVideo = remoteVideos[id];
  682. if (remoteVideo)
  683. remoteVideo.hideConnectionIndicator();
  684. },
  685. /**
  686. * Hides all the indicators
  687. */
  688. hideStats () {
  689. for (var video in remoteVideos) {
  690. let remoteVideo = remoteVideos[video];
  691. if (remoteVideo)
  692. remoteVideo.hideIndicator();
  693. }
  694. localVideoThumbnail.hideIndicator();
  695. },
  696. removeParticipantContainer (id) {
  697. // Unlock large video
  698. if (pinnedId === id) {
  699. console.info("Focused video owner has left the conference");
  700. pinnedId = null;
  701. }
  702. if (currentDominantSpeaker === id) {
  703. console.info("Dominant speaker has left the conference");
  704. currentDominantSpeaker = null;
  705. }
  706. var remoteVideo = remoteVideos[id];
  707. if (remoteVideo) {
  708. // Remove remote video
  709. console.info("Removing remote video: " + id);
  710. delete remoteVideos[id];
  711. remoteVideo.remove();
  712. } else {
  713. console.warn("No remote video for " + id);
  714. }
  715. VideoLayout.resizeThumbnails();
  716. },
  717. onVideoTypeChanged (id, newVideoType) {
  718. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  719. return;
  720. }
  721. console.info("Peer video type changed: ", id, newVideoType);
  722. var smallVideo;
  723. if (APP.conference.isLocalId(id)) {
  724. if (!localVideoThumbnail) {
  725. console.warn("Local video not ready yet");
  726. return;
  727. }
  728. smallVideo = localVideoThumbnail;
  729. } else if (remoteVideos[id]) {
  730. smallVideo = remoteVideos[id];
  731. } else {
  732. return;
  733. }
  734. smallVideo.setVideoType(newVideoType);
  735. if (this.isCurrentlyOnLarge(id)) {
  736. this.updateLargeVideo(id, true);
  737. }
  738. },
  739. showMore (id) {
  740. if (id === 'local') {
  741. localVideoThumbnail.connectionIndicator.showMore();
  742. } else {
  743. let remoteVideo = remoteVideos[id];
  744. if (remoteVideo) {
  745. remoteVideo.connectionIndicator.showMore();
  746. } else {
  747. console.info("Error - no remote video for id: " + id);
  748. }
  749. }
  750. },
  751. /**
  752. * Resizes the video area.
  753. *
  754. * @param isSideBarVisible indicates if the side bar is currently visible
  755. * @param forceUpdate indicates that hidden thumbnails will be shown
  756. * @param completeFunction a function to be called when the video area is
  757. * resized.
  758. */
  759. resizeVideoArea (isSideBarVisible,
  760. forceUpdate = false,
  761. animate = false,
  762. completeFunction = null) {
  763. if (largeVideo) {
  764. largeVideo.updateContainerSize(isSideBarVisible);
  765. largeVideo.resize(animate);
  766. }
  767. // Calculate available width and height.
  768. let availableHeight = window.innerHeight;
  769. let availableWidth = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  770. if (availableWidth < 0 || availableHeight < 0) {
  771. return;
  772. }
  773. // Resize the thumbnails first.
  774. this.resizeThumbnails(false, forceUpdate, isSideBarVisible);
  775. // Resize the video area element.
  776. $('#videospace').animate({
  777. right: window.innerWidth - availableWidth,
  778. width: availableWidth,
  779. height: availableHeight
  780. }, {
  781. queue: false,
  782. duration: animate ? 500 : 1,
  783. complete: completeFunction
  784. });
  785. },
  786. getSmallVideo (id) {
  787. if (APP.conference.isLocalId(id)) {
  788. return localVideoThumbnail;
  789. } else {
  790. return remoteVideos[id];
  791. }
  792. },
  793. changeUserAvatar (id, avatarUrl) {
  794. var smallVideo = VideoLayout.getSmallVideo(id);
  795. if (smallVideo) {
  796. smallVideo.avatarChanged(avatarUrl);
  797. } else {
  798. console.warn(
  799. "Missed avatar update - no small video yet for " + id
  800. );
  801. }
  802. if (this.isCurrentlyOnLarge(id)) {
  803. largeVideo.updateAvatar(avatarUrl);
  804. }
  805. },
  806. /**
  807. * Indicates that the video has been interrupted.
  808. */
  809. onVideoInterrupted () {
  810. this.enableVideoProblemFilter(true);
  811. let reconnectingKey = "connection.RECONNECTING";
  812. $('#videoConnectionMessage')
  813. .attr("data-i18n", reconnectingKey)
  814. .text(APP.translation.translateString(reconnectingKey))
  815. .css({display: "block"});
  816. },
  817. /**
  818. * Indicates that the video has been restored.
  819. */
  820. onVideoRestored () {
  821. this.enableVideoProblemFilter(false);
  822. $('#videoConnectionMessage').css({display: "none"});
  823. },
  824. enableVideoProblemFilter (enable) {
  825. if (!largeVideo) {
  826. return;
  827. }
  828. largeVideo.enableVideoProblemFilter(enable);
  829. },
  830. isLargeVideoVisible () {
  831. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  832. },
  833. /**
  834. * @return {LargeContainer} the currently displayed container on large
  835. * video.
  836. */
  837. getCurrentlyOnLargeContainer () {
  838. return largeVideo.getContainer(largeVideo.state);
  839. },
  840. isCurrentlyOnLarge (id) {
  841. return largeVideo && largeVideo.id === id;
  842. },
  843. updateLargeVideo (id, forceUpdate) {
  844. if (!largeVideo) {
  845. return;
  846. }
  847. let isOnLarge = this.isCurrentlyOnLarge(id);
  848. let currentId = largeVideo.id;
  849. if (!isOnLarge || forceUpdate) {
  850. let videoType = this.getRemoteVideoType(id);
  851. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  852. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  853. }
  854. if (currentId) {
  855. var oldSmallVideo = this.getSmallVideo(currentId);
  856. }
  857. let smallVideo = this.getSmallVideo(id);
  858. largeVideo.updateLargeVideo(
  859. id,
  860. smallVideo.videoStream,
  861. videoType
  862. ).then(function() {
  863. // update current small video and the old one
  864. smallVideo.updateView();
  865. oldSmallVideo && oldSmallVideo.updateView();
  866. }, function () {
  867. // use clicked other video during update, nothing to do.
  868. });
  869. } else if (currentId) {
  870. let currentSmallVideo = this.getSmallVideo(currentId);
  871. currentSmallVideo.updateView();
  872. }
  873. },
  874. addLargeVideoContainer (type, container) {
  875. largeVideo && largeVideo.addContainer(type, container);
  876. },
  877. removeLargeVideoContainer (type) {
  878. largeVideo && largeVideo.removeContainer(type);
  879. },
  880. /**
  881. * @returns Promise
  882. */
  883. showLargeVideoContainer (type, show) {
  884. if (!largeVideo) {
  885. return Promise.reject();
  886. }
  887. let isVisible = this.isLargeContainerTypeVisible(type);
  888. if (isVisible === show) {
  889. return Promise.resolve();
  890. }
  891. let currentId = largeVideo.id;
  892. if(currentId) {
  893. var oldSmallVideo = this.getSmallVideo(currentId);
  894. }
  895. let containerTypeToShow = type;
  896. // if we are hiding a container and there is focusedVideo
  897. // (pinned remote video) use its video type,
  898. // if not then use default type - large video
  899. if (!show) {
  900. if(pinnedId)
  901. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  902. else
  903. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  904. }
  905. return largeVideo.showContainer(containerTypeToShow)
  906. .then(() => {
  907. if(oldSmallVideo)
  908. oldSmallVideo && oldSmallVideo.updateView();
  909. });
  910. },
  911. isLargeContainerTypeVisible (type) {
  912. return largeVideo && largeVideo.state === type;
  913. },
  914. /**
  915. * Returns the id of the current video shown on large.
  916. * Currently used by tests (torture).
  917. */
  918. getLargeVideoID () {
  919. return largeVideo.id;
  920. },
  921. /**
  922. * Returns the the current video shown on large.
  923. * Currently used by tests (torture).
  924. */
  925. getLargeVideo () {
  926. return largeVideo;
  927. },
  928. /**
  929. * Updates the resolution label, indicating to the user that the large
  930. * video stream is currently HD.
  931. */
  932. updateResolutionLabel(isResolutionHD) {
  933. let videoResolutionLabel = $("#videoResolutionLabel");
  934. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  935. videoResolutionLabel.css({display: "block"});
  936. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  937. videoResolutionLabel.css({display: "none"});
  938. },
  939. /**
  940. * Sets the flipX state of the local video.
  941. * @param {boolean} true for flipped otherwise false;
  942. */
  943. setLocalFlipX: function (val) {
  944. this.localFlipX = val;
  945. }
  946. };
  947. export default VideoLayout;