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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /* global config, APP, $, interfaceConfig, JitsiMeetJS */
  2. /* jshint -W101 */
  3. import Avatar from "../avatar/Avatar";
  4. import FilmStrip from "./FilmStrip";
  5. import UIEvents from "../../../service/UI/UIEvents";
  6. import UIUtil from "../util/UIUtil";
  7. import RemoteVideo from "./RemoteVideo";
  8. import LargeVideoManager from "./LargeVideoManager";
  9. import {VIDEO_CONTAINER_TYPE} from "./VideoContainer";
  10. import {SHARED_VIDEO_CONTAINER_TYPE} from '../shared_video/SharedVideo';
  11. import LocalVideo from "./LocalVideo";
  12. const RTCUIUtil = JitsiMeetJS.util.RTCUIHelper;
  13. var remoteVideos = {};
  14. var localVideoThumbnail = null;
  15. var currentDominantSpeaker = null;
  16. var localLastNCount = config.channelLastN;
  17. var localLastNSet = [];
  18. var lastNEndpointsCache = [];
  19. var lastNPickupId = null;
  20. var eventEmitter = null;
  21. /**
  22. * Currently focused video jid
  23. * @type {String}
  24. */
  25. var pinnedId = null;
  26. /**
  27. * flipX state of the localVideo
  28. */
  29. let localFlipX = null;
  30. /**
  31. * On contact list item clicked.
  32. */
  33. function onContactClicked (id) {
  34. if (APP.conference.isLocalId(id)) {
  35. $("#localVideoContainer").click();
  36. return;
  37. }
  38. let remoteVideo = remoteVideos[id];
  39. if (remoteVideo && remoteVideo.hasVideo()) {
  40. // It is not always the case that a videoThumb exists (if there is
  41. // no actual video).
  42. if (remoteVideo.hasVideoStarted()) {
  43. // We have a video src, great! Let's update the large video
  44. // now.
  45. VideoLayout.handleVideoThumbClicked(id);
  46. } else {
  47. // If we don't have a video src for jid, there's absolutely
  48. // no point in calling handleVideoThumbClicked; Quite
  49. // simply, it won't work because it needs an src to attach
  50. // to the large video.
  51. //
  52. // Instead, we trigger the pinned endpoint changed event to
  53. // let the bridge adjust its lastN set for myjid and store
  54. // the pinned user in the lastNPickupId variable to be
  55. // picked up later by the lastN changed event handler.
  56. lastNPickupId = id;
  57. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, remoteVideo, true);
  58. }
  59. }
  60. }
  61. /**
  62. * Returns the corresponding resource id to the given peer container
  63. * DOM element.
  64. *
  65. * @return the corresponding resource id to the given peer container
  66. * DOM element
  67. */
  68. function getPeerContainerResourceId (containerElement) {
  69. if (localVideoThumbnail.container === containerElement) {
  70. return localVideoThumbnail.id;
  71. }
  72. let i = containerElement.id.indexOf('participant_');
  73. if (i >= 0) {
  74. return containerElement.id.substring(i + 12);
  75. }
  76. }
  77. let largeVideo;
  78. var VideoLayout = {
  79. init (emitter) {
  80. eventEmitter = emitter;
  81. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED, function (val) {
  82. localFlipX = val;
  83. if(largeVideo)
  84. largeVideo.onLocalFlipXChange(val);
  85. });
  86. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  87. // sets default video type of local video
  88. // FIXME container type is totally different thing from the video type
  89. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  90. // if we do not resize the thumbs here, if there is no video device
  91. // the local video thumb maybe one pixel
  92. let { localVideo } = this.resizeThumbnails(false, true);
  93. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  94. this.lastNCount = config.channelLastN;
  95. },
  96. initLargeVideo () {
  97. largeVideo = new LargeVideoManager(eventEmitter);
  98. if(localFlipX) {
  99. largeVideo.onLocalFlipXChange(localFlipX);
  100. }
  101. largeVideo.updateContainerSize();
  102. },
  103. /**
  104. * Sets the audio level of the video elements associated to the given id.
  105. *
  106. * @param id the video identifier in the form it comes from the library
  107. * @param lvl the new audio level to update to
  108. */
  109. setAudioLevel(id, lvl) {
  110. let smallVideo = this.getSmallVideo(id);
  111. if (smallVideo)
  112. smallVideo.updateAudioLevelIndicator(lvl);
  113. if (largeVideo && id === largeVideo.id)
  114. largeVideo.updateLargeVideoAudioLevel(lvl);
  115. },
  116. isInLastN (resource) {
  117. return this.lastNCount < 0 || // lastN is disabled
  118. // lastNEndpoints cache not built yet
  119. (this.lastNCount > 0 && !lastNEndpointsCache.length) ||
  120. (lastNEndpointsCache &&
  121. lastNEndpointsCache.indexOf(resource) !== -1);
  122. },
  123. changeLocalAudio (stream) {
  124. let localAudio = document.getElementById('localAudio');
  125. localAudio = stream.attach(localAudio);
  126. // Now when Temasys plugin is converting also <audio> elements to
  127. // plugin's <object>s, in current layout it will capture click events
  128. // before it reaches the local video object. We hide it here in order
  129. // to prevent that.
  130. //if (RTCBrowserType.isIExplorer()) {
  131. // The issue is not present on Safari. Also if we hide it in Safari
  132. // then the local audio track will have 'enabled' flag set to false
  133. // which will result in audio mute issues
  134. // $(localAudio).hide();
  135. localAudio.width = 1;
  136. localAudio.height = 1;
  137. //}
  138. },
  139. changeLocalVideo (stream) {
  140. let localId = APP.conference.getMyUserId();
  141. this.onVideoTypeChanged(localId, stream.videoType);
  142. if (!stream.isMuted()) {
  143. localVideoThumbnail.changeVideo(stream);
  144. }
  145. /* force update if we're currently being displayed */
  146. if (this.isCurrentlyOnLarge(localId)) {
  147. this.updateLargeVideo(localId, true);
  148. }
  149. },
  150. /**
  151. * Get's the localID of the conference and set it to the local video
  152. * (small one). This needs to be called as early as possible, when muc is
  153. * actually joined. Otherwise events can come with information like email
  154. * and setting them assume the id is already set.
  155. */
  156. mucJoined () {
  157. if (largeVideo && !largeVideo.id) {
  158. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  159. }
  160. },
  161. /**
  162. * Adds or removes icons for not available camera and microphone.
  163. * @param resourceJid the jid of user
  164. * @param devices available devices
  165. */
  166. setDeviceAvailabilityIcons (id, devices) {
  167. if (APP.conference.isLocalId(id)) {
  168. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  169. return;
  170. }
  171. let video = remoteVideos[id];
  172. if (!video) {
  173. return;
  174. }
  175. video.setDeviceAvailabilityIcons(devices);
  176. },
  177. /**
  178. * Enables/disables device availability icons for the given participant id.
  179. * The default value is {true}.
  180. * @param id the identifier of the participant
  181. * @param enable {true} to enable device availability icons
  182. */
  183. enableDeviceAvailabilityIcons (id, enable) {
  184. let video;
  185. if (APP.conference.isLocalId(id)) {
  186. video = localVideoThumbnail;
  187. }
  188. else {
  189. video = remoteVideos[id];
  190. }
  191. if (video)
  192. video.enableDeviceAvailabilityIcons(enable);
  193. },
  194. /**
  195. * Shows/hides local video.
  196. * @param {boolean} true to make the local video visible, false - otherwise
  197. */
  198. setLocalVideoVisible(visible) {
  199. localVideoThumbnail.setVisible(visible);
  200. },
  201. /**
  202. * Checks if removed video is currently displayed and tries to display
  203. * another one instead.
  204. * Uses focusedID if any or dominantSpeakerID if any,
  205. * otherwise elects new video, in this order.
  206. */
  207. updateAfterThumbRemoved (id) {
  208. if (!this.isCurrentlyOnLarge(id)) {
  209. return;
  210. }
  211. let newId;
  212. if (pinnedId)
  213. newId = pinnedId;
  214. else if (currentDominantSpeaker)
  215. newId = currentDominantSpeaker;
  216. else // Otherwise select last visible video
  217. newId = this.electLastVisibleVideo();
  218. this.updateLargeVideo(newId);
  219. },
  220. electLastVisibleVideo () {
  221. // pick the last visible video in the row
  222. // if nobody else is left, this picks the local video
  223. let remoteThumbs = FilmStrip.getThumbs(true).remoteThumbs;
  224. let thumbs = remoteThumbs.filter('[id!="mixedstream"]');
  225. let lastVisible = thumbs.filter(':visible:last');
  226. if (lastVisible.length) {
  227. let id = getPeerContainerResourceId(lastVisible[0]);
  228. if (remoteVideos[id]) {
  229. console.info("electLastVisibleVideo: " + id);
  230. return id;
  231. }
  232. // The RemoteVideo was removed (but the DOM elements may still
  233. // exist).
  234. }
  235. console.info("Last visible video no longer exists");
  236. thumbs = FilmStrip.getThumbs().remoteThumbs;
  237. if (thumbs.length) {
  238. let id = getPeerContainerResourceId(thumbs[0]);
  239. if (remoteVideos[id]) {
  240. console.info("electLastVisibleVideo: " + id);
  241. return id;
  242. }
  243. // The RemoteVideo was removed (but the DOM elements may
  244. // still exist).
  245. }
  246. // Go with local video
  247. console.info("Fallback to local video...");
  248. let id = APP.conference.getMyUserId();
  249. console.info("electLastVisibleVideo: " + id);
  250. return id;
  251. },
  252. onRemoteStreamAdded (stream) {
  253. let id = stream.getParticipantId();
  254. let remoteVideo = remoteVideos[id];
  255. if (!remoteVideo)
  256. return;
  257. remoteVideo.addRemoteStreamElement(stream);
  258. // if track is muted make sure we reflect that
  259. if(stream.isMuted())
  260. {
  261. if(stream.getType() === "audio")
  262. this.onAudioMute(stream.getParticipantId(), true);
  263. else
  264. this.onVideoMute(stream.getParticipantId(), true);
  265. }
  266. },
  267. onRemoteStreamRemoved (stream) {
  268. let id = stream.getParticipantId();
  269. let remoteVideo = remoteVideos[id];
  270. if (remoteVideo) { // remote stream may be removed after participant left the conference
  271. remoteVideo.removeRemoteStreamElement(stream);
  272. }
  273. },
  274. /**
  275. * Return the type of the remote video.
  276. * @param id the id for the remote video
  277. * @returns {String} the video type video or screen.
  278. */
  279. getRemoteVideoType (id) {
  280. let smallVideo = VideoLayout.getSmallVideo(id);
  281. return smallVideo ? smallVideo.getVideoType() : null;
  282. },
  283. isPinned (id) {
  284. return (pinnedId) ? (id === pinnedId) : false;
  285. },
  286. getPinnedId () {
  287. return pinnedId;
  288. },
  289. /**
  290. * Handles the click on a video thumbnail.
  291. *
  292. * @param id the identifier of the video thumbnail
  293. */
  294. handleVideoThumbClicked (id) {
  295. if(pinnedId) {
  296. var oldSmallVideo = VideoLayout.getSmallVideo(pinnedId);
  297. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  298. oldSmallVideo.focus(false);
  299. }
  300. var smallVideo = VideoLayout.getSmallVideo(id);
  301. // Unpin if currently pinned.
  302. if (pinnedId === id)
  303. {
  304. pinnedId = null;
  305. // Enable the currently set dominant speaker.
  306. if (currentDominantSpeaker) {
  307. if(smallVideo && smallVideo.hasVideo()) {
  308. this.updateLargeVideo(currentDominantSpeaker);
  309. }
  310. }
  311. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  312. return;
  313. }
  314. // Lock new video
  315. pinnedId = id;
  316. // Update focused/pinned interface.
  317. if (id) {
  318. if (smallVideo && !interfaceConfig.filmStripOnly)
  319. smallVideo.focus(true);
  320. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  321. }
  322. this.updateLargeVideo(id);
  323. },
  324. /**
  325. * Creates or adds a participant container for the given id and smallVideo.
  326. *
  327. * @param {JitsiParticipant} user the participant to add
  328. * @param {SmallVideo} smallVideo optional small video instance to add as a
  329. * remote video, if undefined <tt>RemoteVideo</tt> will be created
  330. */
  331. addParticipantContainer (user, smallVideo) {
  332. let id = user.getId();
  333. let remoteVideo;
  334. if(smallVideo)
  335. remoteVideo = smallVideo;
  336. else
  337. remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  338. this.addRemoteVideoContainer(id, remoteVideo);
  339. },
  340. /**
  341. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  342. *
  343. * @param {string} the id of the video to add
  344. * @param {SmallVideo} smallVideo the small video instance to add as a
  345. * remote video
  346. */
  347. addRemoteVideoContainer (id, remoteVideo) {
  348. remoteVideos[id] = remoteVideo;
  349. let videoType = VideoLayout.getRemoteVideoType(id);
  350. if (!videoType) {
  351. // make video type the default one (camera)
  352. // FIXME container type is not a video type
  353. videoType = VIDEO_CONTAINER_TYPE;
  354. }
  355. remoteVideo.setVideoType(videoType);
  356. // In case this is not currently in the last n we don't show it.
  357. if (localLastNCount && localLastNCount > 0 &&
  358. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  359. remoteVideo.showPeerContainer('hide');
  360. } else {
  361. VideoLayout.resizeThumbnails(false, true);
  362. }
  363. // Initialize the view
  364. remoteVideo.updateView();
  365. },
  366. videoactive (videoelem, resourceJid) {
  367. console.info(resourceJid + " video is now active", videoelem);
  368. VideoLayout.resizeThumbnails(
  369. false, false, function() {$(videoelem).show();});
  370. // Update the large video to the last added video only if there's no
  371. // current dominant, focused speaker or update it to
  372. // the current dominant speaker.
  373. if ((!pinnedId &&
  374. !currentDominantSpeaker &&
  375. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  376. pinnedId === resourceJid ||
  377. (!pinnedId && resourceJid &&
  378. currentDominantSpeaker === resourceJid) ||
  379. /* Playback started while we're on the stage - may need to update
  380. video source with the new stream */
  381. this.isCurrentlyOnLarge(resourceJid)) {
  382. this.updateLargeVideo(resourceJid, true);
  383. }
  384. },
  385. /**
  386. * Shows the presence status message for the given video.
  387. */
  388. setPresenceStatus (id, statusMsg) {
  389. let remoteVideo = remoteVideos[id];
  390. if (remoteVideo)
  391. remoteVideo.setPresenceStatus(statusMsg);
  392. },
  393. /**
  394. * Shows a visual indicator for the moderator of the conference.
  395. * On local or remote participants.
  396. */
  397. showModeratorIndicator () {
  398. let isModerator = APP.conference.isModerator;
  399. if (isModerator) {
  400. localVideoThumbnail.addModeratorIndicator();
  401. } else {
  402. localVideoThumbnail.removeModeratorIndicator();
  403. }
  404. APP.conference.listMembers().forEach(function (member) {
  405. let id = member.getId();
  406. let remoteVideo = remoteVideos[id];
  407. if (!remoteVideo)
  408. return;
  409. if (member.isModerator()) {
  410. remoteVideo.addModeratorIndicator();
  411. }
  412. if (isModerator) {
  413. // We are moderator, but user is not - add menu
  414. if(!remoteVideo.hasRemoteVideoMenu) {
  415. remoteVideo.addRemoteVideoMenu();
  416. }
  417. }
  418. });
  419. },
  420. /*
  421. * Shows or hides the audio muted indicator over the local thumbnail video.
  422. * @param {boolean} isMuted
  423. */
  424. showLocalAudioIndicator (isMuted) {
  425. localVideoThumbnail.showAudioIndicator(isMuted);
  426. },
  427. /**
  428. * Shows/hides the indication about local connection being interrupted.
  429. *
  430. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  431. * currently in the interrupted state or <tt>false</tt> if the connection
  432. * is fine.
  433. */
  434. showLocalConnectionInterrupted (isInterrupted) {
  435. localVideoThumbnail.connectionIndicator
  436. .updateConnectionStatusIndicator(!isInterrupted);
  437. },
  438. /**
  439. * Resizes thumbnails.
  440. */
  441. resizeThumbnails ( animate = false,
  442. forceUpdate = false,
  443. onComplete = null) {
  444. let { localVideo, remoteVideo }
  445. = FilmStrip.calculateThumbnailSize();
  446. let {thumbWidth, thumbHeight} = remoteVideo;
  447. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  448. animate, forceUpdate)
  449. .then(function () {
  450. if (onComplete && typeof onComplete === "function")
  451. onComplete();
  452. });
  453. return { localVideo, remoteVideo };
  454. },
  455. /**
  456. * On audio muted event.
  457. */
  458. onAudioMute (id, isMuted) {
  459. if (APP.conference.isLocalId(id)) {
  460. localVideoThumbnail.showAudioIndicator(isMuted);
  461. } else {
  462. let remoteVideo = remoteVideos[id];
  463. if (!remoteVideo)
  464. return;
  465. remoteVideo.showAudioIndicator(isMuted);
  466. if (APP.conference.isModerator) {
  467. remoteVideo.updateRemoteVideoMenu(isMuted);
  468. }
  469. }
  470. },
  471. /**
  472. * On video muted event.
  473. */
  474. onVideoMute (id, value) {
  475. if (APP.conference.isLocalId(id)) {
  476. localVideoThumbnail.setVideoMutedView(value);
  477. } else {
  478. let remoteVideo = remoteVideos[id];
  479. if (remoteVideo)
  480. remoteVideo.setVideoMutedView(value);
  481. }
  482. if (this.isCurrentlyOnLarge(id)) {
  483. // large video will show avatar instead of muted stream
  484. this.updateLargeVideo(id, true);
  485. }
  486. },
  487. /**
  488. * Display name changed.
  489. */
  490. onDisplayNameChanged (id, displayName, status) {
  491. if (id === 'localVideoContainer' ||
  492. APP.conference.isLocalId(id)) {
  493. localVideoThumbnail.setDisplayName(displayName);
  494. } else {
  495. let remoteVideo = remoteVideos[id];
  496. if (remoteVideo)
  497. remoteVideo.setDisplayName(displayName, status);
  498. }
  499. },
  500. /**
  501. * Sets the "raised hand" status for a participant identified by 'id'.
  502. */
  503. setRaisedHandStatus(id, raisedHandStatus) {
  504. var video
  505. = APP.conference.isLocalId(id)
  506. ? localVideoThumbnail : remoteVideos[id];
  507. if (video) {
  508. video.showRaisedHandIndicator(raisedHandStatus);
  509. }
  510. },
  511. /**
  512. * On dominant speaker changed event.
  513. */
  514. onDominantSpeakerChanged (id) {
  515. if (id === currentDominantSpeaker) {
  516. return;
  517. }
  518. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  519. // We ignore local user events, but just unmark remote user as dominant
  520. // while we are talking
  521. if (APP.conference.isLocalId(id)) {
  522. if(oldSpeakerRemoteVideo)
  523. {
  524. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  525. currentDominantSpeaker = null;
  526. }
  527. localVideoThumbnail.showDominantSpeakerIndicator(true);
  528. return;
  529. }
  530. let remoteVideo = remoteVideos[id];
  531. if (!remoteVideo) {
  532. return;
  533. }
  534. // Update the current dominant speaker.
  535. remoteVideo.showDominantSpeakerIndicator(true);
  536. localVideoThumbnail.showDominantSpeakerIndicator(false);
  537. // let's remove the indications from the remote video if any
  538. if (oldSpeakerRemoteVideo) {
  539. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  540. }
  541. currentDominantSpeaker = id;
  542. // Local video will not have container found, but that's ok
  543. // since we don't want to switch to local video.
  544. // Update the large video if the video source is already available,
  545. // otherwise wait for the "videoactive.jingle" event.
  546. if (!pinnedId
  547. && remoteVideo.hasVideoStarted()
  548. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  549. this.updateLargeVideo(id);
  550. }
  551. },
  552. /**
  553. * Shows/hides warning about remote user's connectivity issues.
  554. *
  555. * @param {string} id the ID of the remote participant(MUC nickname)
  556. * @param {boolean} isActive true if the connection is ok or false when
  557. * the user is having connectivity issues.
  558. */
  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;