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 33KB

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