You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoLayout.js 32KB

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