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

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