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

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