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

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