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

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