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

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