Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoLayout.js 32KB

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