選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VideoLayout.js 33KB

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