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

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