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

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