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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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 || 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 (id, statusMsg) {
  326. remoteVideos[id].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. let animate = false;
  362. if (largeVideo) {
  363. largeVideo.updateContainerSize(isSideBarVisible);
  364. largeVideo.resize(animate);
  365. }
  366. this.resizeVideoSpace(animate, isSideBarVisible);
  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. if (id === currentDominantSpeaker) {
  460. return;
  461. }
  462. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  463. // We ignore local user events, but just unmark remote user as dominant
  464. // while we are talking
  465. if (APP.conference.isLocalId(id)) {
  466. if(oldSpeakerRemoteVideo)
  467. {
  468. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  469. currentDominantSpeaker = null;
  470. }
  471. return;
  472. }
  473. let remoteVideo = remoteVideos[id];
  474. if (!remoteVideo) {
  475. return;
  476. }
  477. // Update the current dominant speaker.
  478. remoteVideo.updateDominantSpeakerIndicator(true);
  479. // let's remove the indications from the remote video if any
  480. if (oldSpeakerRemoteVideo) {
  481. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  482. }
  483. currentDominantSpeaker = id;
  484. // Obtain container for new dominant speaker.
  485. let videoSel = remoteVideo.selectVideoElement();
  486. // Local video will not have container found, but that's ok
  487. // since we don't want to switch to local video.
  488. if (!focusedVideoResourceJid && videoSel.length) {
  489. // Update the large video if the video source is already available,
  490. // otherwise wait for the "videoactive.jingle" event.
  491. if (videoSel[0].currentTime > 0) {
  492. this.updateLargeVideo(id);
  493. }
  494. }
  495. },
  496. /**
  497. * On last N change event.
  498. *
  499. * @param lastNEndpoints the list of last N endpoints
  500. * @param endpointsEnteringLastN the list currently entering last N
  501. * endpoints
  502. */
  503. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  504. if (lastNCount !== lastNEndpoints.length)
  505. lastNCount = lastNEndpoints.length;
  506. lastNEndpointsCache = lastNEndpoints;
  507. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  508. //
  509. // If LastN drops to, say, 2, because of adaptivity, then E should see
  510. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  511. // so E sees them. C is only in E's local LastN set.
  512. //
  513. // If F starts talking and LastN = 3, then E should see thumbnails for
  514. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  515. // enters E's local LastN ejecting C.
  516. // Increase the local LastN set size, if necessary.
  517. if (lastNCount > localLastNCount) {
  518. localLastNCount = lastNCount;
  519. }
  520. // Update the local LastN set preserving the order in which the
  521. // endpoints appeared in the LastN/local LastN set.
  522. var nextLocalLastNSet = lastNEndpoints.slice(0);
  523. for (var i = 0; i < localLastNSet.length; i++) {
  524. if (nextLocalLastNSet.length >= localLastNCount) {
  525. break;
  526. }
  527. var resourceJid = localLastNSet[i];
  528. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  529. nextLocalLastNSet.push(resourceJid);
  530. }
  531. }
  532. localLastNSet = nextLocalLastNSet;
  533. var updateLargeVideo = false;
  534. // Handle LastN/local LastN changes.
  535. BottomToolbar.getThumbs().each(( index, element ) => {
  536. var resourceJid = getPeerContainerResourceId(element);
  537. // We do not want to process any logic for our own(local) video
  538. // because the local participant is never in the lastN set.
  539. // The code of this function might detect that the local participant
  540. // has been dropped out of the lastN set and will update the large
  541. // video
  542. // Detected from avatar tests, where lastN event override
  543. // local video pinning
  544. if(APP.conference.isLocalId(resourceJid))
  545. return;
  546. var isReceived = true;
  547. if (resourceJid &&
  548. lastNEndpoints.indexOf(resourceJid) < 0 &&
  549. localLastNSet.indexOf(resourceJid) < 0) {
  550. console.log("Remove from last N", resourceJid);
  551. if (remoteVideos[resourceJid])
  552. remoteVideos[resourceJid].showPeerContainer('hide');
  553. else if (!APP.conference.isLocalId(resourceJid))
  554. console.error("No remote video for: " + resourceJid);
  555. isReceived = false;
  556. } else if (resourceJid &&
  557. $('#participant_' + resourceJid).is(':visible') &&
  558. lastNEndpoints.indexOf(resourceJid) < 0 &&
  559. localLastNSet.indexOf(resourceJid) >= 0) {
  560. if (remoteVideos[resourceJid])
  561. remoteVideos[resourceJid].showPeerContainer('avatar');
  562. else if (!APP.conference.isLocalId(resourceJid))
  563. console.error("No remote video for: " + resourceJid);
  564. isReceived = false;
  565. }
  566. if (!isReceived) {
  567. // resourceJid has dropped out of the server side lastN set, so
  568. // it is no longer being received. If resourceJid was being
  569. // displayed in the large video we have to switch to another
  570. // user.
  571. if (!updateLargeVideo &&
  572. this.isCurrentlyOnLarge(resourceJid)) {
  573. updateLargeVideo = true;
  574. }
  575. }
  576. });
  577. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  578. endpointsEnteringLastN = lastNEndpoints;
  579. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  580. endpointsEnteringLastN.forEach(function (resourceJid) {
  581. var isVisible = $('#participant_' + resourceJid).is(':visible');
  582. var remoteVideo = remoteVideos[resourceJid];
  583. remoteVideo.showPeerContainer('show');
  584. if (!isVisible) {
  585. console.log("Add to last N", resourceJid);
  586. var jid = APP.xmpp.findJidFromResource(resourceJid);
  587. var mediaStream =
  588. APP.RTC.remoteStreams[jid]['video'];
  589. var sel = remoteVideo.selectVideoElement();
  590. APP.RTC.attachMediaStream(sel, mediaStream.stream);
  591. if (lastNPickupId == mediaStream.peerjid) {
  592. // Clean up the lastN pickup id.
  593. lastNPickupId = null;
  594. // Don't fire the events again, they've already
  595. // been fired in the contact list click handler.
  596. VideoLayout.handleVideoThumbClicked(
  597. false,
  598. Strophe.getResourceFromJid(mediaStream.peerjid));
  599. updateLargeVideo = false;
  600. }
  601. remoteVideos[resourceJid].
  602. waitForPlayback(sel, mediaStream);
  603. }
  604. });
  605. }
  606. // The endpoint that was being shown in the large video has dropped out
  607. // of the lastN set and there was no lastN pickup jid. We need to update
  608. // the large video now.
  609. if (updateLargeVideo) {
  610. var resource;
  611. // Find out which endpoint to show in the large video.
  612. for (i = 0; i < lastNEndpoints.length; i++) {
  613. resource = lastNEndpoints[i];
  614. if (!resource || APP.conference.isLocalId(resource))
  615. continue;
  616. // videoSrcToSsrc needs to be update for this call to succeed.
  617. this.updateLargeVideo(resource);
  618. break;
  619. }
  620. }
  621. },
  622. /**
  623. * Updates local stats
  624. * @param percent
  625. * @param object
  626. */
  627. updateLocalConnectionStats (percent, object) {
  628. let resolutions = {};
  629. if (object.resolution !== null) {
  630. resolutions = object.resolution;
  631. var id = Strophe.getResourceFromJid(
  632. APP.conference._room.room.session.me);
  633. object.resolution = resolutions[id];
  634. }
  635. localVideoThumbnail.updateStatsIndicator(percent, object);
  636. Object.keys(resolutions).forEach(function (id) {
  637. if (APP.conference.isLocalId(id)) {
  638. return;
  639. }
  640. let resolution = resolutions[id];
  641. let remoteVideo = remoteVideos[id];
  642. if (resolution && remoteVideo) {
  643. remoteVideo.updateResolution(resolution);
  644. }
  645. });
  646. },
  647. /**
  648. * Updates remote stats.
  649. * @param id the id associated with the stats
  650. * @param percent the connection quality percent
  651. * @param object the stats data
  652. */
  653. updateConnectionStats (id, percent, object) {
  654. if (remoteVideos[id]) {
  655. remoteVideos[id].updateStatsIndicator(percent, object);
  656. }
  657. },
  658. /**
  659. * Hides the connection indicator
  660. * @param id
  661. */
  662. hideConnectionIndicator (id) {
  663. remoteVideos[id].hideConnectionIndicator();
  664. },
  665. /**
  666. * Hides all the indicators
  667. */
  668. hideStats () {
  669. for(var video in remoteVideos) {
  670. remoteVideos[video].hideIndicator();
  671. }
  672. localVideoThumbnail.hideIndicator();
  673. },
  674. removeParticipantContainer (id) {
  675. // Unlock large video
  676. if (focusedVideoResourceJid === id) {
  677. console.info("Focused video owner has left the conference");
  678. focusedVideoResourceJid = null;
  679. }
  680. if (currentDominantSpeaker === id) {
  681. console.info("Dominant speaker has left the conference");
  682. currentDominantSpeaker = null;
  683. }
  684. var remoteVideo = remoteVideos[id];
  685. if (remoteVideo) {
  686. // Remove remote video
  687. console.info("Removing remote video: " + id);
  688. delete remoteVideos[id];
  689. remoteVideo.remove();
  690. } else {
  691. console.warn("No remote video for " + id);
  692. }
  693. VideoLayout.resizeThumbnails();
  694. },
  695. onVideoTypeChanged (id, newVideoType) {
  696. if (remoteVideoTypes[id] === newVideoType) {
  697. return;
  698. }
  699. console.info("Peer video type changed: ", id, newVideoType);
  700. remoteVideoTypes[id] = newVideoType;
  701. var smallVideo;
  702. if (APP.conference.isLocalId(id)) {
  703. if (!localVideoThumbnail) {
  704. console.warn("Local video not ready yet");
  705. return;
  706. }
  707. smallVideo = localVideoThumbnail;
  708. } else if (remoteVideos[id]) {
  709. smallVideo = remoteVideos[id];
  710. } else {
  711. return;
  712. }
  713. smallVideo.setVideoType(newVideoType);
  714. if (this.isCurrentlyOnLarge(id)) {
  715. this.updateLargeVideo(id, true);
  716. }
  717. },
  718. showMore (jid) {
  719. if (jid === 'local') {
  720. localVideoThumbnail.connectionIndicator.showMore();
  721. } else {
  722. var remoteVideo = remoteVideos[Strophe.getResourceFromJid(jid)];
  723. if (remoteVideo) {
  724. remoteVideo.connectionIndicator.showMore();
  725. } else {
  726. console.info("Error - no remote video for jid: " + jid);
  727. }
  728. }
  729. },
  730. addRemoteVideoContainer (id) {
  731. return RemoteVideo.createContainer(id);
  732. },
  733. /**
  734. * Resizes the video area.
  735. *
  736. * @param isSideBarVisible indicates if the side bar is currently visible
  737. * @param callback a function to be called when the video space is
  738. * resized.
  739. */
  740. resizeVideoArea (isSideBarVisible, callback) {
  741. let animate = true;
  742. if (largeVideo) {
  743. largeVideo.updateContainerSize(isSideBarVisible);
  744. largeVideo.resize(animate);
  745. this.resizeVideoSpace(animate, isSideBarVisible, callback);
  746. }
  747. VideoLayout.resizeThumbnails(animate);
  748. },
  749. /**
  750. * Resizes the #videospace html element
  751. * @param animate boolean property that indicates whether the resize should
  752. * be animated or not.
  753. * @param isChatVisible boolean property that indicates whether the chat
  754. * area is displayed or not.
  755. * If that parameter is null the method will check the chat panel
  756. * visibility.
  757. * @param completeFunction a function to be called when the video space
  758. * is resized.
  759. */
  760. resizeVideoSpace (animate, isChatVisible, completeFunction) {
  761. let availableHeight = window.innerHeight;
  762. let availableWidth = UIUtil.getAvailableVideoWidth(isChatVisible);
  763. if (availableWidth < 0 || availableHeight < 0) {
  764. return;
  765. }
  766. $('#videospace').animate({
  767. right: window.innerWidth - availableWidth,
  768. width: availableWidth,
  769. height: availableHeight
  770. }, {
  771. queue: false,
  772. duration: animate ? 500 : 1,
  773. complete: completeFunction
  774. });
  775. },
  776. getSmallVideo (id) {
  777. if (APP.conference.isLocalId(id)) {
  778. return localVideoThumbnail;
  779. } else {
  780. return remoteVideos[id];
  781. }
  782. },
  783. changeUserAvatar (id, avatarUrl) {
  784. var smallVideo = VideoLayout.getSmallVideo(id);
  785. if (smallVideo) {
  786. smallVideo.avatarChanged(avatarUrl);
  787. } else {
  788. console.warn(
  789. "Missed avatar update - no small video yet for " + id
  790. );
  791. }
  792. if (this.isCurrentlyOnLarge(id)) {
  793. largeVideo.updateAvatar(avatarUrl);
  794. }
  795. },
  796. /**
  797. * Indicates that the video has been interrupted.
  798. */
  799. onVideoInterrupted () {
  800. this.enableVideoProblemFilter(true);
  801. let reconnectingKey = "connection.RECONNECTING";
  802. $('#videoConnectionMessage')
  803. .attr("data-i18n", reconnectingKey)
  804. .text(APP.translation.translateString(reconnectingKey))
  805. .css({display: "block"});
  806. },
  807. /**
  808. * Indicates that the video has been restored.
  809. */
  810. onVideoRestored () {
  811. this.enableVideoProblemFilter(false);
  812. $('#videoConnectionMessage').css({display: "none"});
  813. },
  814. enableVideoProblemFilter (enable) {
  815. if (!largeVideo) {
  816. return;
  817. }
  818. largeVideo.enableVideoProblemFilter(enable);
  819. },
  820. isLargeVideoVisible () {
  821. return this.isLargeContainerTypeVisible(VideoContainerType);
  822. },
  823. isCurrentlyOnLarge (id) {
  824. return largeVideo && largeVideo.id === id;
  825. },
  826. updateLargeVideo (id, forceUpdate) {
  827. if (!largeVideo) {
  828. return;
  829. }
  830. let isOnLarge = this.isCurrentlyOnLarge(id);
  831. let currentId = largeVideo.id;
  832. if (!isOnLarge || forceUpdate) {
  833. if (id !== currentId) {
  834. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  835. }
  836. if (currentId) {
  837. var oldSmallVideo = this.getSmallVideo(currentId);
  838. }
  839. let smallVideo = this.getSmallVideo(id);
  840. let videoType = this.getRemoteVideoType(id);
  841. largeVideo.updateLargeVideo(
  842. smallVideo.stream,
  843. videoType
  844. ).then(function() {
  845. // update current small video and the old one
  846. smallVideo.updateView();
  847. oldSmallVideo && oldSmallVideo.updateView();
  848. }, function () {
  849. // use clicked other video during update, nothing to do.
  850. });
  851. } else if (currentId) {
  852. let currentSmallVideo = this.getSmallVideo(currentId);
  853. currentSmallVideo.updateView();
  854. }
  855. },
  856. addLargeVideoContainer (type, container) {
  857. largeVideo && largeVideo.addContainer(type, container);
  858. },
  859. removeLargeVideoContainer (type) {
  860. largeVideo && largeVideo.removeContainer(type);
  861. },
  862. /**
  863. * @returns Promise
  864. */
  865. showLargeVideoContainer (type, show) {
  866. if (!largeVideo) {
  867. return Promise.reject();
  868. }
  869. let isVisible = this.isLargeContainerTypeVisible(type);
  870. if (isVisible === show) {
  871. return Promise.resolve();
  872. }
  873. // if !show then use default type - large video
  874. return largeVideo.showContainer(show ? type : VideoContainerType);
  875. },
  876. isLargeContainerTypeVisible (type) {
  877. return largeVideo && largeVideo.state === type;
  878. },
  879. /**
  880. * Returns the id of the current video shown on large.
  881. * Currently used by tests (troture).
  882. */
  883. getLargeVideoID () {
  884. return largeVideo.id;
  885. }
  886. };
  887. export default VideoLayout;