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

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