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

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