Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoLayout.js 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 = {};
  620. if (object.resolution !== null) {
  621. resolutions = object.resolution;
  622. var id = Strophe.getResourceFromJid(
  623. APP.conference._room.room.session.me);
  624. object.resolution = resolutions[id];
  625. }
  626. localVideoThumbnail.updateStatsIndicator(percent, object);
  627. Object.keys(resolutions).forEach(function (id) {
  628. if (APP.conference.isLocalId(id)) {
  629. return;
  630. }
  631. let resolution = resolutions[id];
  632. let remoteVideo = remoteVideos[id];
  633. if (resolution && remoteVideo) {
  634. remoteVideo.updateResolution(resolution);
  635. }
  636. });
  637. },
  638. /**
  639. * Updates remote stats.
  640. * @param id the id associated with the stats
  641. * @param percent the connection quality percent
  642. * @param object the stats data
  643. */
  644. updateConnectionStats (id, percent, object) {
  645. if (remoteVideos[id]) {
  646. remoteVideos[id].updateStatsIndicator(percent, object);
  647. }
  648. },
  649. /**
  650. * Hides the connection indicator
  651. * @param id
  652. */
  653. hideConnectionIndicator (id) {
  654. remoteVideos[id].hideConnectionIndicator();
  655. },
  656. /**
  657. * Hides all the indicators
  658. */
  659. hideStats () {
  660. for(var video in remoteVideos) {
  661. remoteVideos[video].hideIndicator();
  662. }
  663. localVideoThumbnail.hideIndicator();
  664. },
  665. removeParticipantContainer (id) {
  666. // Unlock large video
  667. if (focusedVideoResourceJid === id) {
  668. console.info("Focused video owner has left the conference");
  669. focusedVideoResourceJid = null;
  670. }
  671. if (currentDominantSpeaker === id) {
  672. console.info("Dominant speaker has left the conference");
  673. currentDominantSpeaker = null;
  674. }
  675. var remoteVideo = remoteVideos[id];
  676. if (remoteVideo) {
  677. // Remove remote video
  678. console.info("Removing remote video: " + id);
  679. delete remoteVideos[id];
  680. remoteVideo.remove();
  681. } else {
  682. console.warn("No remote video for " + id);
  683. }
  684. VideoLayout.resizeThumbnails();
  685. },
  686. onVideoTypeChanged (id, newVideoType) {
  687. if (remoteVideoTypes[id] === newVideoType) {
  688. return;
  689. }
  690. console.info("Peer video type changed: ", id, newVideoType);
  691. remoteVideoTypes[id] = newVideoType;
  692. var smallVideo;
  693. if (APP.conference.isLocalId(id)) {
  694. if (!localVideoThumbnail) {
  695. console.warn("Local video not ready yet");
  696. return;
  697. }
  698. smallVideo = localVideoThumbnail;
  699. } else if (remoteVideos[id]) {
  700. smallVideo = remoteVideos[id];
  701. } else {
  702. return;
  703. }
  704. smallVideo.setVideoType(newVideoType);
  705. if (this.isCurrentlyOnLarge(id)) {
  706. this.updateLargeVideo(id, true);
  707. }
  708. },
  709. showMore (jid) {
  710. if (jid === 'local') {
  711. localVideoThumbnail.connectionIndicator.showMore();
  712. } else {
  713. var remoteVideo = remoteVideos[Strophe.getResourceFromJid(jid)];
  714. if (remoteVideo) {
  715. remoteVideo.connectionIndicator.showMore();
  716. } else {
  717. console.info("Error - no remote video for jid: " + jid);
  718. }
  719. }
  720. },
  721. addRemoteVideoContainer (id) {
  722. return RemoteVideo.createContainer(id);
  723. },
  724. /**
  725. * Resizes the video area.
  726. *
  727. * @param isSideBarVisible indicates if the side bar is currently visible
  728. * @param callback a function to be called when the video space is
  729. * resized.
  730. */
  731. resizeVideoArea (isSideBarVisible, callback) {
  732. let animate = true;
  733. if (largeVideo) {
  734. largeVideo.updateContainerSize(isSideBarVisible);
  735. largeVideo.resize(animate);
  736. this.resizeVideoSpace(animate, isSideBarVisible, callback);
  737. }
  738. VideoLayout.resizeThumbnails(animate);
  739. },
  740. /**
  741. * Resizes the #videospace html element
  742. * @param animate boolean property that indicates whether the resize should
  743. * be animated or not.
  744. * @param isChatVisible boolean property that indicates whether the chat
  745. * area is displayed or not.
  746. * If that parameter is null the method will check the chat panel
  747. * visibility.
  748. * @param completeFunction a function to be called when the video space
  749. * is resized.
  750. */
  751. resizeVideoSpace (animate, isChatVisible, completeFunction) {
  752. let availableHeight = window.innerHeight;
  753. let availableWidth = UIUtil.getAvailableVideoWidth(isChatVisible);
  754. if (availableWidth < 0 || availableHeight < 0) {
  755. return;
  756. }
  757. $('#videospace').animate({
  758. right: window.innerWidth - availableWidth,
  759. width: availableWidth,
  760. height: availableHeight
  761. }, {
  762. queue: false,
  763. duration: animate ? 500 : 1,
  764. complete: completeFunction
  765. });
  766. },
  767. getSmallVideo (id) {
  768. if (APP.conference.isLocalId(id)) {
  769. return localVideoThumbnail;
  770. } else {
  771. return remoteVideos[id];
  772. }
  773. },
  774. changeUserAvatar (id, avatarUrl) {
  775. var smallVideo = VideoLayout.getSmallVideo(id);
  776. if (smallVideo) {
  777. smallVideo.avatarChanged(avatarUrl);
  778. } else {
  779. console.warn(
  780. "Missed avatar update - no small video yet for " + id
  781. );
  782. }
  783. if (this.isCurrentlyOnLarge(id)) {
  784. largeVideo.updateAvatar(avatarUrl);
  785. }
  786. },
  787. /**
  788. * Indicates that the video has been interrupted.
  789. */
  790. onVideoInterrupted () {
  791. this.enableVideoProblemFilter(true);
  792. let reconnectingKey = "connection.RECONNECTING";
  793. $('#videoConnectionMessage')
  794. .attr("data-i18n", reconnectingKey)
  795. .text(APP.translation.translateString(reconnectingKey))
  796. .css({display: "block"});
  797. },
  798. /**
  799. * Indicates that the video has been restored.
  800. */
  801. onVideoRestored () {
  802. this.enableVideoProblemFilter(false);
  803. $('#videoConnectionMessage').css({display: "none"});
  804. },
  805. enableVideoProblemFilter (enable) {
  806. if (!largeVideo) {
  807. return;
  808. }
  809. largeVideo.enableVideoProblemFilter(enable);
  810. },
  811. isLargeVideoVisible () {
  812. return this.isLargeContainerTypeVisible(VideoContainerType);
  813. },
  814. isCurrentlyOnLarge (id) {
  815. return largeVideo && largeVideo.id === id;
  816. },
  817. updateLargeVideo (id, forceUpdate) {
  818. if (!largeVideo) {
  819. return;
  820. }
  821. let isOnLarge = this.isCurrentlyOnLarge(id);
  822. let currentId = largeVideo.id;
  823. if (!isOnLarge || forceUpdate) {
  824. if (id !== currentId) {
  825. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  826. }
  827. if (currentId) {
  828. var oldSmallVideo = this.getSmallVideo(currentId);
  829. }
  830. let smallVideo = this.getSmallVideo(id);
  831. let videoType = this.getRemoteVideoType(id);
  832. largeVideo.updateLargeVideo(
  833. id,
  834. smallVideo.videoStream,
  835. videoType
  836. ).then(function() {
  837. // update current small video and the old one
  838. smallVideo.updateView();
  839. oldSmallVideo && oldSmallVideo.updateView();
  840. }, function () {
  841. // use clicked other video during update, nothing to do.
  842. });
  843. } else if (currentId) {
  844. let currentSmallVideo = this.getSmallVideo(currentId);
  845. currentSmallVideo.updateView();
  846. }
  847. },
  848. addLargeVideoContainer (type, container) {
  849. largeVideo && largeVideo.addContainer(type, container);
  850. },
  851. removeLargeVideoContainer (type) {
  852. largeVideo && largeVideo.removeContainer(type);
  853. },
  854. /**
  855. * @returns Promise
  856. */
  857. showLargeVideoContainer (type, show) {
  858. if (!largeVideo) {
  859. return Promise.reject();
  860. }
  861. let isVisible = this.isLargeContainerTypeVisible(type);
  862. if (isVisible === show) {
  863. return Promise.resolve();
  864. }
  865. // if !show then use default type - large video
  866. return largeVideo.showContainer(show ? type : VideoContainerType);
  867. },
  868. isLargeContainerTypeVisible (type) {
  869. return largeVideo && largeVideo.state === type;
  870. },
  871. /**
  872. * Returns the id of the current video shown on large.
  873. * Currently used by tests (troture).
  874. */
  875. getLargeVideoID () {
  876. return largeVideo.id;
  877. }
  878. };
  879. export default VideoLayout;