您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 33KB

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