Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VideoLayout.js 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 a participant container for the given id and smallVideo.
  325. *
  326. * @param id the id of the participant to add
  327. * @param {SmallVideo} smallVideo optional small video instance to add as a
  328. * remote video, if undefined RemoteVideo will be created
  329. */
  330. addParticipantContainer (id, smallVideo) {
  331. let remoteVideo;
  332. if(smallVideo)
  333. remoteVideo = smallVideo;
  334. else
  335. remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  336. remoteVideos[id] = remoteVideo;
  337. let videoType = VideoLayout.getRemoteVideoType(id);
  338. if (!videoType) {
  339. // make video type the default one (camera)
  340. // FIXME container type is not a video type
  341. videoType = VIDEO_CONTAINER_TYPE;
  342. }
  343. remoteVideo.setVideoType(videoType);
  344. // In case this is not currently in the last n we don't show it.
  345. if (localLastNCount && localLastNCount > 0 &&
  346. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  347. remoteVideo.showPeerContainer('hide');
  348. } else {
  349. VideoLayout.resizeThumbnails(false, true);
  350. }
  351. },
  352. videoactive (videoelem, resourceJid) {
  353. console.info(resourceJid + " video is now active", videoelem);
  354. VideoLayout.resizeThumbnails(
  355. false, false, function() {$(videoelem).show();});
  356. // Update the large video to the last added video only if there's no
  357. // current dominant, focused speaker or update it to
  358. // the current dominant speaker.
  359. if ((!pinnedId &&
  360. !currentDominantSpeaker &&
  361. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  362. pinnedId === resourceJid ||
  363. (!pinnedId && resourceJid &&
  364. currentDominantSpeaker === resourceJid) ||
  365. /* Playback started while we're on the stage - may need to update
  366. video source with the new stream */
  367. this.isCurrentlyOnLarge(resourceJid)) {
  368. this.updateLargeVideo(resourceJid, true);
  369. }
  370. },
  371. /**
  372. * Shows the presence status message for the given video.
  373. */
  374. setPresenceStatus (id, statusMsg) {
  375. let remoteVideo = remoteVideos[id];
  376. if (remoteVideo)
  377. remoteVideo.setPresenceStatus(statusMsg);
  378. },
  379. /**
  380. * Shows a visual indicator for the moderator of the conference.
  381. * On local or remote participants.
  382. */
  383. showModeratorIndicator () {
  384. let isModerator = APP.conference.isModerator;
  385. if (isModerator) {
  386. localVideoThumbnail.createModeratorIndicatorElement();
  387. } else {
  388. localVideoThumbnail.removeModeratorIndicatorElement();
  389. }
  390. APP.conference.listMembers().forEach(function (member) {
  391. let id = member.getId();
  392. let remoteVideo = remoteVideos[id];
  393. if (!remoteVideo)
  394. return;
  395. if (member.isModerator()) {
  396. remoteVideo.createModeratorIndicatorElement();
  397. }
  398. if (isModerator) {
  399. // We are moderator, but user is not - add menu
  400. if(!remoteVideo.hasRemoteVideoMenu) {
  401. remoteVideo.addRemoteVideoMenu();
  402. }
  403. }
  404. });
  405. },
  406. /*
  407. * Shows or hides the audio muted indicator over the local thumbnail video.
  408. * @param {boolean} isMuted
  409. */
  410. showLocalAudioIndicator (isMuted) {
  411. localVideoThumbnail.showAudioIndicator(isMuted);
  412. },
  413. /**
  414. * Resizes thumbnails.
  415. */
  416. resizeThumbnails ( animate = false,
  417. forceUpdate = false,
  418. onComplete = null) {
  419. let { localVideo, remoteVideo }
  420. = FilmStrip.calculateThumbnailSize();
  421. let {thumbWidth, thumbHeight} = remoteVideo;
  422. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  423. animate, forceUpdate)
  424. .then(function () {
  425. AudioLevels.updateCanvasSize(localVideo, remoteVideo);
  426. if (onComplete && typeof onComplete === "function")
  427. onComplete();
  428. });
  429. return { localVideo, remoteVideo };
  430. },
  431. /**
  432. * On audio muted event.
  433. */
  434. onAudioMute (id, isMuted) {
  435. if (APP.conference.isLocalId(id)) {
  436. localVideoThumbnail.showAudioIndicator(isMuted);
  437. } else {
  438. let remoteVideo = remoteVideos[id];
  439. if (!remoteVideo)
  440. return;
  441. remoteVideo.showAudioIndicator(isMuted);
  442. if (APP.conference.isModerator) {
  443. remoteVideo.updateRemoteVideoMenu(isMuted);
  444. }
  445. }
  446. },
  447. /**
  448. * On video muted event.
  449. */
  450. onVideoMute (id, value) {
  451. if (APP.conference.isLocalId(id)) {
  452. localVideoThumbnail.setMutedView(value);
  453. } else {
  454. let remoteVideo = remoteVideos[id];
  455. if (remoteVideo)
  456. remoteVideo.setMutedView(value);
  457. }
  458. if (this.isCurrentlyOnLarge(id)) {
  459. // large video will show avatar instead of muted stream
  460. this.updateLargeVideo(id, true);
  461. }
  462. },
  463. /**
  464. * Display name changed.
  465. */
  466. onDisplayNameChanged (id, displayName, status) {
  467. if (id === 'localVideoContainer' ||
  468. APP.conference.isLocalId(id)) {
  469. localVideoThumbnail.setDisplayName(displayName);
  470. } else {
  471. let remoteVideo = remoteVideos[id];
  472. if (remoteVideo)
  473. remoteVideo.setDisplayName(displayName, status);
  474. }
  475. },
  476. /**
  477. * Sets the "raised hand" status for a participant identified by 'id'.
  478. */
  479. setRaisedHandStatus(id, raisedHandStatus) {
  480. var video
  481. = APP.conference.isLocalId(id)
  482. ? localVideoThumbnail : remoteVideos[id];
  483. if (video) {
  484. video.showRaisedHandIndicator(raisedHandStatus);
  485. }
  486. },
  487. /**
  488. * On dominant speaker changed event.
  489. */
  490. onDominantSpeakerChanged (id) {
  491. if (id === currentDominantSpeaker) {
  492. return;
  493. }
  494. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  495. // We ignore local user events, but just unmark remote user as dominant
  496. // while we are talking
  497. if (APP.conference.isLocalId(id)) {
  498. if(oldSpeakerRemoteVideo)
  499. {
  500. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  501. currentDominantSpeaker = null;
  502. }
  503. localVideoThumbnail.showDominantSpeakerIndicator(true);
  504. return;
  505. }
  506. let remoteVideo = remoteVideos[id];
  507. if (!remoteVideo) {
  508. return;
  509. }
  510. // Update the current dominant speaker.
  511. remoteVideo.showDominantSpeakerIndicator(true);
  512. localVideoThumbnail.showDominantSpeakerIndicator(false);
  513. // let's remove the indications from the remote video if any
  514. if (oldSpeakerRemoteVideo) {
  515. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  516. }
  517. currentDominantSpeaker = id;
  518. // Local video will not have container found, but that's ok
  519. // since we don't want to switch to local video.
  520. // Update the large video if the video source is already available,
  521. // otherwise wait for the "videoactive.jingle" event.
  522. if (!pinnedId
  523. && remoteVideo.hasVideoStarted()
  524. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  525. this.updateLargeVideo(id);
  526. }
  527. },
  528. /**
  529. * On last N change event.
  530. *
  531. * @param lastNEndpoints the list of last N endpoints
  532. * @param endpointsEnteringLastN the list currently entering last N
  533. * endpoints
  534. */
  535. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  536. if (this.lastNCount !== lastNEndpoints.length)
  537. this.lastNCount = lastNEndpoints.length;
  538. lastNEndpointsCache = lastNEndpoints;
  539. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  540. //
  541. // If LastN drops to, say, 2, because of adaptivity, then E should see
  542. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  543. // so E sees them. C is only in E's local LastN set.
  544. //
  545. // If F starts talking and LastN = 3, then E should see thumbnails for
  546. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  547. // enters E's local LastN ejecting C.
  548. // Increase the local LastN set size, if necessary.
  549. if (this.lastNCount > localLastNCount) {
  550. localLastNCount = this.lastNCount;
  551. }
  552. // Update the local LastN set preserving the order in which the
  553. // endpoints appeared in the LastN/local LastN set.
  554. var nextLocalLastNSet = lastNEndpoints.slice(0);
  555. for (var i = 0; i < localLastNSet.length; i++) {
  556. if (nextLocalLastNSet.length >= localLastNCount) {
  557. break;
  558. }
  559. var resourceJid = localLastNSet[i];
  560. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  561. nextLocalLastNSet.push(resourceJid);
  562. }
  563. }
  564. localLastNSet = nextLocalLastNSet;
  565. var updateLargeVideo = false;
  566. // Handle LastN/local LastN changes.
  567. FilmStrip.getThumbs().remoteThumbs.each(( index, element ) => {
  568. var resourceJid = getPeerContainerResourceId(element);
  569. var smallVideo = remoteVideos[resourceJid];
  570. // We do not want to process any logic for our own(local) video
  571. // because the local participant is never in the lastN set.
  572. // The code of this function might detect that the local participant
  573. // has been dropped out of the lastN set and will update the large
  574. // video
  575. // Detected from avatar tests, where lastN event override
  576. // local video pinning
  577. if(APP.conference.isLocalId(resourceJid))
  578. return;
  579. var isReceived = true;
  580. if (resourceJid &&
  581. lastNEndpoints.indexOf(resourceJid) < 0 &&
  582. localLastNSet.indexOf(resourceJid) < 0) {
  583. console.log("Remove from last N", resourceJid);
  584. if (smallVideo)
  585. smallVideo.showPeerContainer('hide');
  586. else if (!APP.conference.isLocalId(resourceJid))
  587. console.error("No remote video for: " + resourceJid);
  588. isReceived = false;
  589. } else if (resourceJid &&
  590. //TOFIX: smallVideo may be undefined
  591. smallVideo.isVisible() &&
  592. lastNEndpoints.indexOf(resourceJid) < 0 &&
  593. localLastNSet.indexOf(resourceJid) >= 0) {
  594. // TOFIX: if we're here we already know that the smallVideo
  595. // exists. Look at the previous FIX above.
  596. if (smallVideo)
  597. smallVideo.showPeerContainer('avatar');
  598. else if (!APP.conference.isLocalId(resourceJid))
  599. console.error("No remote video for: " + resourceJid);
  600. isReceived = false;
  601. }
  602. if (!isReceived) {
  603. // resourceJid has dropped out of the server side lastN set, so
  604. // it is no longer being received. If resourceJid was being
  605. // displayed in the large video we have to switch to another
  606. // user.
  607. if (!updateLargeVideo &&
  608. this.isCurrentlyOnLarge(resourceJid)) {
  609. updateLargeVideo = true;
  610. }
  611. }
  612. });
  613. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  614. endpointsEnteringLastN = lastNEndpoints;
  615. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  616. endpointsEnteringLastN.forEach(function (resourceJid) {
  617. var remoteVideo = remoteVideos[resourceJid];
  618. if (remoteVideo)
  619. remoteVideo.showPeerContainer('show');
  620. if (!remoteVideo.isVisible()) {
  621. console.log("Add to last N", resourceJid);
  622. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  623. if (lastNPickupId == resourceJid) {
  624. // Clean up the lastN pickup id.
  625. lastNPickupId = null;
  626. VideoLayout.handleVideoThumbClicked(resourceJid);
  627. updateLargeVideo = false;
  628. }
  629. remoteVideo.waitForPlayback(
  630. remoteVideo.selectVideoElement()[0],
  631. remoteVideo.videoStream);
  632. }
  633. });
  634. }
  635. // The endpoint that was being shown in the large video has dropped out
  636. // of the lastN set and there was no lastN pickup jid. We need to update
  637. // the large video now.
  638. if (updateLargeVideo) {
  639. var resource;
  640. // Find out which endpoint to show in the large video.
  641. for (i = 0; i < lastNEndpoints.length; i++) {
  642. resource = lastNEndpoints[i];
  643. if (!resource || APP.conference.isLocalId(resource))
  644. continue;
  645. // videoSrcToSsrc needs to be update for this call to succeed.
  646. this.updateLargeVideo(resource);
  647. break;
  648. }
  649. }
  650. },
  651. /**
  652. * Updates local stats
  653. * @param percent
  654. * @param object
  655. */
  656. updateLocalConnectionStats (percent, object) {
  657. let resolutions = object.resolution;
  658. object.resolution = resolutions[APP.conference.getMyUserId()];
  659. localVideoThumbnail.updateStatsIndicator(percent, object);
  660. Object.keys(resolutions).forEach(function (id) {
  661. if (APP.conference.isLocalId(id)) {
  662. return;
  663. }
  664. let resolution = resolutions[id];
  665. let remoteVideo = remoteVideos[id];
  666. if (resolution && remoteVideo) {
  667. remoteVideo.updateResolution(resolution);
  668. }
  669. });
  670. },
  671. /**
  672. * Updates remote stats.
  673. * @param id the id associated with the stats
  674. * @param percent the connection quality percent
  675. * @param object the stats data
  676. */
  677. updateConnectionStats (id, percent, object) {
  678. let remoteVideo = remoteVideos[id];
  679. if (remoteVideo) {
  680. remoteVideo.updateStatsIndicator(percent, object);
  681. }
  682. },
  683. /**
  684. * Hides the connection indicator
  685. * @param id
  686. */
  687. hideConnectionIndicator (id) {
  688. let remoteVideo = remoteVideos[id];
  689. if (remoteVideo)
  690. remoteVideo.hideConnectionIndicator();
  691. },
  692. /**
  693. * Hides all the indicators
  694. */
  695. hideStats () {
  696. for (var video in remoteVideos) {
  697. let remoteVideo = remoteVideos[video];
  698. if (remoteVideo)
  699. remoteVideo.hideIndicator();
  700. }
  701. localVideoThumbnail.hideIndicator();
  702. },
  703. removeParticipantContainer (id) {
  704. // Unlock large video
  705. if (pinnedId === id) {
  706. console.info("Focused video owner has left the conference");
  707. pinnedId = null;
  708. }
  709. if (currentDominantSpeaker === id) {
  710. console.info("Dominant speaker has left the conference");
  711. currentDominantSpeaker = null;
  712. }
  713. var remoteVideo = remoteVideos[id];
  714. if (remoteVideo) {
  715. // Remove remote video
  716. console.info("Removing remote video: " + id);
  717. delete remoteVideos[id];
  718. remoteVideo.remove();
  719. } else {
  720. console.warn("No remote video for " + id);
  721. }
  722. VideoLayout.resizeThumbnails();
  723. },
  724. onVideoTypeChanged (id, newVideoType) {
  725. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  726. return;
  727. }
  728. console.info("Peer video type changed: ", id, newVideoType);
  729. var smallVideo;
  730. if (APP.conference.isLocalId(id)) {
  731. if (!localVideoThumbnail) {
  732. console.warn("Local video not ready yet");
  733. return;
  734. }
  735. smallVideo = localVideoThumbnail;
  736. } else if (remoteVideos[id]) {
  737. smallVideo = remoteVideos[id];
  738. } else {
  739. return;
  740. }
  741. smallVideo.setVideoType(newVideoType);
  742. if (this.isCurrentlyOnLarge(id)) {
  743. this.updateLargeVideo(id, true);
  744. }
  745. },
  746. showMore (id) {
  747. if (id === 'local') {
  748. localVideoThumbnail.connectionIndicator.showMore();
  749. } else {
  750. let remoteVideo = remoteVideos[id];
  751. if (remoteVideo) {
  752. remoteVideo.connectionIndicator.showMore();
  753. } else {
  754. console.info("Error - no remote video for id: " + id);
  755. }
  756. }
  757. },
  758. /**
  759. * Resizes the video area.
  760. *
  761. * @param forceUpdate indicates that hidden thumbnails will be shown
  762. * @param completeFunction a function to be called when the video area is
  763. * resized.
  764. */
  765. resizeVideoArea (forceUpdate = false,
  766. animate = false,
  767. completeFunction = null) {
  768. if (largeVideo) {
  769. largeVideo.updateContainerSize();
  770. largeVideo.resize(animate);
  771. }
  772. // Calculate available width and height.
  773. let availableHeight = window.innerHeight;
  774. let availableWidth = UIUtil.getAvailableVideoWidth();
  775. if (availableWidth < 0 || availableHeight < 0) {
  776. return;
  777. }
  778. // Resize the thumbnails first.
  779. this.resizeThumbnails(false, forceUpdate);
  780. // Resize the video area element.
  781. $('#videospace').animate({
  782. right: window.innerWidth - availableWidth,
  783. width: availableWidth,
  784. height: availableHeight
  785. }, {
  786. queue: false,
  787. duration: animate ? 500 : 1,
  788. complete: completeFunction
  789. });
  790. },
  791. getSmallVideo (id) {
  792. if (APP.conference.isLocalId(id)) {
  793. return localVideoThumbnail;
  794. } else {
  795. return remoteVideos[id];
  796. }
  797. },
  798. changeUserAvatar (id, avatarUrl) {
  799. var smallVideo = VideoLayout.getSmallVideo(id);
  800. if (smallVideo) {
  801. smallVideo.avatarChanged(avatarUrl);
  802. } else {
  803. console.warn(
  804. "Missed avatar update - no small video yet for " + id
  805. );
  806. }
  807. if (this.isCurrentlyOnLarge(id)) {
  808. largeVideo.updateAvatar(avatarUrl);
  809. }
  810. },
  811. /**
  812. * Indicates that the video has been interrupted.
  813. */
  814. onVideoInterrupted () {
  815. this.enableVideoProblemFilter(true);
  816. let reconnectingKey = "connection.RECONNECTING";
  817. $('#videoConnectionMessage')
  818. .attr("data-i18n", reconnectingKey)
  819. .text(APP.translation.translateString(reconnectingKey))
  820. .css({display: "block"});
  821. },
  822. /**
  823. * Indicates that the video has been restored.
  824. */
  825. onVideoRestored () {
  826. this.enableVideoProblemFilter(false);
  827. $('#videoConnectionMessage').css({display: "none"});
  828. },
  829. enableVideoProblemFilter (enable) {
  830. if (!largeVideo) {
  831. return;
  832. }
  833. largeVideo.enableVideoProblemFilter(enable);
  834. },
  835. isLargeVideoVisible () {
  836. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  837. },
  838. /**
  839. * @return {LargeContainer} the currently displayed container on large
  840. * video.
  841. */
  842. getCurrentlyOnLargeContainer () {
  843. return largeVideo.getContainer(largeVideo.state);
  844. },
  845. isCurrentlyOnLarge (id) {
  846. return largeVideo && largeVideo.id === id;
  847. },
  848. updateLargeVideo (id, forceUpdate) {
  849. if (!largeVideo) {
  850. return;
  851. }
  852. let isOnLarge = this.isCurrentlyOnLarge(id);
  853. let currentId = largeVideo.id;
  854. if (!isOnLarge || forceUpdate) {
  855. let videoType = this.getRemoteVideoType(id);
  856. // FIXME video type is not the same thing as container type
  857. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  858. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  859. }
  860. let smallVideo = this.getSmallVideo(id);
  861. let oldSmallVideo;
  862. if (currentId) {
  863. oldSmallVideo = this.getSmallVideo(currentId);
  864. }
  865. smallVideo.waitForResolutionChange();
  866. if (oldSmallVideo)
  867. oldSmallVideo.waitForResolutionChange();
  868. largeVideo.updateLargeVideo(
  869. id,
  870. smallVideo.videoStream,
  871. videoType
  872. ).then(function() {
  873. // update current small video and the old one
  874. smallVideo.updateView();
  875. oldSmallVideo && oldSmallVideo.updateView();
  876. }, function () {
  877. // use clicked other video during update, nothing to do.
  878. });
  879. } else if (currentId) {
  880. let currentSmallVideo = this.getSmallVideo(currentId);
  881. currentSmallVideo.updateView();
  882. }
  883. },
  884. addLargeVideoContainer (type, container) {
  885. largeVideo && largeVideo.addContainer(type, container);
  886. },
  887. removeLargeVideoContainer (type) {
  888. largeVideo && largeVideo.removeContainer(type);
  889. },
  890. /**
  891. * @returns Promise
  892. */
  893. showLargeVideoContainer (type, show) {
  894. if (!largeVideo) {
  895. return Promise.reject();
  896. }
  897. let isVisible = this.isLargeContainerTypeVisible(type);
  898. if (isVisible === show) {
  899. return Promise.resolve();
  900. }
  901. let currentId = largeVideo.id;
  902. if(currentId) {
  903. var oldSmallVideo = this.getSmallVideo(currentId);
  904. }
  905. let containerTypeToShow = type;
  906. // if we are hiding a container and there is focusedVideo
  907. // (pinned remote video) use its video type,
  908. // if not then use default type - large video
  909. if (!show) {
  910. if(pinnedId)
  911. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  912. else
  913. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  914. }
  915. return largeVideo.showContainer(containerTypeToShow)
  916. .then(() => {
  917. if(oldSmallVideo)
  918. oldSmallVideo && oldSmallVideo.updateView();
  919. });
  920. },
  921. isLargeContainerTypeVisible (type) {
  922. return largeVideo && largeVideo.state === type;
  923. },
  924. /**
  925. * Returns the id of the current video shown on large.
  926. * Currently used by tests (torture).
  927. */
  928. getLargeVideoID () {
  929. return largeVideo.id;
  930. },
  931. /**
  932. * Returns the the current video shown on large.
  933. * Currently used by tests (torture).
  934. */
  935. getLargeVideo () {
  936. return largeVideo;
  937. },
  938. /**
  939. * Updates the resolution label, indicating to the user that the large
  940. * video stream is currently HD.
  941. */
  942. updateResolutionLabel(isResolutionHD) {
  943. let videoResolutionLabel = $("#videoResolutionLabel");
  944. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  945. videoResolutionLabel.css({display: "block"});
  946. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  947. videoResolutionLabel.css({display: "none"});
  948. },
  949. /**
  950. * Sets the flipX state of the local video.
  951. * @param {boolean} true for flipped otherwise false;
  952. */
  953. setLocalFlipX: function (val) {
  954. this.localFlipX = val;
  955. },
  956. getEventEmitter: () => {return eventEmitter;}
  957. };
  958. export default VideoLayout;