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

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