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

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