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

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