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

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