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

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