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

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