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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /* global config, APP, $, interfaceConfig */
  2. import FilmStrip from "./FilmStrip";
  3. import UIEvents from "../../../service/UI/UIEvents";
  4. import UIUtil from "../util/UIUtil";
  5. import RemoteVideo from "./RemoteVideo";
  6. import LargeVideoManager from "./LargeVideoManager";
  7. import {VIDEO_CONTAINER_TYPE} from "./VideoContainer";
  8. import LocalVideo from "./LocalVideo";
  9. var remoteVideos = {};
  10. var localVideoThumbnail = null;
  11. var currentDominantSpeaker = null;
  12. var localLastNCount = config.channelLastN;
  13. var localLastNSet = [];
  14. var lastNEndpointsCache = [];
  15. var lastNPickupId = null;
  16. var eventEmitter = null;
  17. /**
  18. * Currently focused video jid
  19. * @type {String}
  20. */
  21. var pinnedId = null;
  22. /**
  23. * flipX state of the localVideo
  24. */
  25. let localFlipX = null;
  26. /**
  27. * On contact list item clicked.
  28. */
  29. function onContactClicked (id) {
  30. if (APP.conference.isLocalId(id)) {
  31. $("#localVideoContainer").click();
  32. return;
  33. }
  34. let remoteVideo = remoteVideos[id];
  35. if (remoteVideo && remoteVideo.hasVideo()) {
  36. // It is not always the case that a videoThumb exists (if there is
  37. // no actual video).
  38. if (remoteVideo.hasVideoStarted()) {
  39. // We have a video src, great! Let's update the large video
  40. // now.
  41. VideoLayout.handleVideoThumbClicked(id);
  42. } else {
  43. // If we don't have a video src for jid, there's absolutely
  44. // no point in calling handleVideoThumbClicked; Quite
  45. // simply, it won't work because it needs an src to attach
  46. // to the large video.
  47. //
  48. // Instead, we trigger the pinned endpoint changed event to
  49. // let the bridge adjust its lastN set for myjid and store
  50. // the pinned user in the lastNPickupId variable to be
  51. // picked up later by the lastN changed event handler.
  52. lastNPickupId = id;
  53. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, remoteVideo, true);
  54. }
  55. }
  56. }
  57. /**
  58. * Returns the corresponding resource id to the given peer container
  59. * DOM element.
  60. *
  61. * @return the corresponding resource id to the given peer container
  62. * DOM element
  63. */
  64. function getPeerContainerResourceId (containerElement) {
  65. if (localVideoThumbnail.container === containerElement) {
  66. return localVideoThumbnail.id;
  67. }
  68. let i = containerElement.id.indexOf('participant_');
  69. if (i >= 0) {
  70. return containerElement.id.substring(i + 12);
  71. }
  72. }
  73. let largeVideo;
  74. var VideoLayout = {
  75. init (emitter) {
  76. eventEmitter = emitter;
  77. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED, function (val) {
  78. localFlipX = val;
  79. if(largeVideo)
  80. largeVideo.onLocalFlipXChange(val);
  81. });
  82. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  83. // sets default video type of local video
  84. // FIXME container type is totally different thing from the video type
  85. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  86. // if we do not resize the thumbs here, if there is no video device
  87. // the local video thumb maybe one pixel
  88. this.resizeThumbnails(false, true);
  89. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  90. this.lastNCount = config.channelLastN;
  91. },
  92. initLargeVideo () {
  93. largeVideo = new LargeVideoManager(eventEmitter);
  94. if(localFlipX) {
  95. largeVideo.onLocalFlipXChange(localFlipX);
  96. }
  97. largeVideo.updateContainerSize();
  98. },
  99. /**
  100. * Sets the audio level of the video elements associated to the given id.
  101. *
  102. * @param id the video identifier in the form it comes from the library
  103. * @param lvl the new audio level to update to
  104. */
  105. setAudioLevel(id, lvl) {
  106. let smallVideo = this.getSmallVideo(id);
  107. if (smallVideo)
  108. smallVideo.updateAudioLevelIndicator(lvl);
  109. if (largeVideo && id === largeVideo.id)
  110. largeVideo.updateLargeVideoAudioLevel(lvl);
  111. },
  112. isInLastN (resource) {
  113. return this.lastNCount < 0 || // lastN is disabled
  114. // lastNEndpoints cache not built yet
  115. (this.lastNCount > 0 && !lastNEndpointsCache.length) ||
  116. (lastNEndpointsCache &&
  117. lastNEndpointsCache.indexOf(resource) !== -1);
  118. },
  119. changeLocalAudio (stream) {
  120. let localAudio = document.getElementById('localAudio');
  121. localAudio = stream.attach(localAudio);
  122. // Now when Temasys plugin is converting also <audio> elements to
  123. // plugin's <object>s, in current layout it will capture click events
  124. // before it reaches the local video object. We hide it here in order
  125. // to prevent that.
  126. //if (RTCBrowserType.isIExplorer()) {
  127. // The issue is not present on Safari. Also if we hide it in Safari
  128. // then the local audio track will have 'enabled' flag set to false
  129. // which will result in audio mute issues
  130. // $(localAudio).hide();
  131. localAudio.width = 1;
  132. localAudio.height = 1;
  133. //}
  134. },
  135. changeLocalVideo (stream) {
  136. let localId = APP.conference.getMyUserId();
  137. this.onVideoTypeChanged(localId, stream.videoType);
  138. if (!stream.isMuted()) {
  139. localVideoThumbnail.changeVideo(stream);
  140. }
  141. /* force update if we're currently being displayed */
  142. if (this.isCurrentlyOnLarge(localId)) {
  143. this.updateLargeVideo(localId, true);
  144. }
  145. },
  146. /**
  147. * Get's the localID of the conference and set it to the local video
  148. * (small one). This needs to be called as early as possible, when muc is
  149. * actually joined. Otherwise events can come with information like email
  150. * and setting them assume the id is already set.
  151. */
  152. mucJoined () {
  153. if (largeVideo && !largeVideo.id) {
  154. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  155. }
  156. },
  157. /**
  158. * Adds or removes icons for not available camera and microphone.
  159. * @param resourceJid the jid of user
  160. * @param devices available devices
  161. */
  162. setDeviceAvailabilityIcons (id, devices) {
  163. if (APP.conference.isLocalId(id)) {
  164. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  165. return;
  166. }
  167. let video = remoteVideos[id];
  168. if (!video) {
  169. return;
  170. }
  171. video.setDeviceAvailabilityIcons(devices);
  172. },
  173. /**
  174. * Enables/disables device availability icons for the given participant id.
  175. * The default value is {true}.
  176. * @param id the identifier of the participant
  177. * @param enable {true} to enable device availability icons
  178. */
  179. enableDeviceAvailabilityIcons (id, enable) {
  180. let video;
  181. if (APP.conference.isLocalId(id)) {
  182. video = localVideoThumbnail;
  183. }
  184. else {
  185. video = remoteVideos[id];
  186. }
  187. if (video)
  188. video.enableDeviceAvailabilityIcons(enable);
  189. },
  190. /**
  191. * Shows/hides local video.
  192. * @param {boolean} true to make the local video visible, false - otherwise
  193. */
  194. setLocalVideoVisible(visible) {
  195. localVideoThumbnail.setVisible(visible);
  196. },
  197. /**
  198. * Checks if removed video is currently displayed and tries to display
  199. * another one instead.
  200. * Uses focusedID if any or dominantSpeakerID if any,
  201. * otherwise elects new video, in this order.
  202. */
  203. updateAfterThumbRemoved (id) {
  204. if (!this.isCurrentlyOnLarge(id)) {
  205. return;
  206. }
  207. let newId;
  208. if (pinnedId)
  209. newId = pinnedId;
  210. else if (currentDominantSpeaker)
  211. newId = currentDominantSpeaker;
  212. else // Otherwise select last visible video
  213. newId = this.electLastVisibleVideo();
  214. this.updateLargeVideo(newId);
  215. },
  216. electLastVisibleVideo () {
  217. // pick the last visible video in the row
  218. // if nobody else is left, this picks the local video
  219. let remoteThumbs = FilmStrip.getThumbs(true).remoteThumbs;
  220. let thumbs = remoteThumbs.filter('[id!="mixedstream"]');
  221. let lastVisible = thumbs.filter(':visible:last');
  222. if (lastVisible.length) {
  223. let id = getPeerContainerResourceId(lastVisible[0]);
  224. if (remoteVideos[id]) {
  225. console.info("electLastVisibleVideo: " + id);
  226. return id;
  227. }
  228. // The RemoteVideo was removed (but the DOM elements may still
  229. // exist).
  230. }
  231. console.info("Last visible video no longer exists");
  232. thumbs = FilmStrip.getThumbs().remoteThumbs;
  233. if (thumbs.length) {
  234. let id = getPeerContainerResourceId(thumbs[0]);
  235. if (remoteVideos[id]) {
  236. console.info("electLastVisibleVideo: " + id);
  237. return id;
  238. }
  239. // The RemoteVideo was removed (but the DOM elements may
  240. // still exist).
  241. }
  242. // Go with local video
  243. console.info("Fallback to local video...");
  244. let id = APP.conference.getMyUserId();
  245. console.info("electLastVisibleVideo: " + id);
  246. return id;
  247. },
  248. onRemoteStreamAdded (stream) {
  249. let id = stream.getParticipantId();
  250. let remoteVideo = remoteVideos[id];
  251. if (!remoteVideo)
  252. return;
  253. remoteVideo.addRemoteStreamElement(stream);
  254. // if track is muted make sure we reflect that
  255. if(stream.isMuted())
  256. {
  257. if(stream.getType() === "audio")
  258. this.onAudioMute(stream.getParticipantId(), true);
  259. else
  260. this.onVideoMute(stream.getParticipantId(), true);
  261. }
  262. },
  263. onRemoteStreamRemoved (stream) {
  264. let id = stream.getParticipantId();
  265. let remoteVideo = remoteVideos[id];
  266. // Remote stream may be removed after participant left the conference.
  267. if (remoteVideo) {
  268. remoteVideo.removeRemoteStreamElement(stream);
  269. }
  270. },
  271. /**
  272. * Return the type of the remote video.
  273. * @param id the id for the remote video
  274. * @returns {String} the video type video or screen.
  275. */
  276. getRemoteVideoType (id) {
  277. let smallVideo = VideoLayout.getSmallVideo(id);
  278. return smallVideo ? smallVideo.getVideoType() : null;
  279. },
  280. isPinned (id) {
  281. return (pinnedId) ? (id === pinnedId) : false;
  282. },
  283. getPinnedId () {
  284. return pinnedId;
  285. },
  286. /**
  287. * Handles the click on a video thumbnail.
  288. *
  289. * @param id the identifier of the video thumbnail
  290. */
  291. handleVideoThumbClicked (id) {
  292. if(pinnedId) {
  293. var oldSmallVideo = VideoLayout.getSmallVideo(pinnedId);
  294. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  295. oldSmallVideo.focus(false);
  296. }
  297. var smallVideo = VideoLayout.getSmallVideo(id);
  298. // Unpin if currently pinned.
  299. if (pinnedId === id)
  300. {
  301. pinnedId = null;
  302. // Enable the currently set dominant speaker.
  303. if (currentDominantSpeaker) {
  304. if(smallVideo && smallVideo.hasVideo()) {
  305. this.updateLargeVideo(currentDominantSpeaker);
  306. }
  307. } else {
  308. // if there is no currentDominantSpeaker, it can also be
  309. // that local participant is the dominant speaker
  310. // we should act as a participant has left and was on large
  311. // and we should choose somebody (electLastVisibleVideo)
  312. this.updateLargeVideo(this.electLastVisibleVideo());
  313. }
  314. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  315. return;
  316. }
  317. // Lock new video
  318. pinnedId = id;
  319. // Update focused/pinned interface.
  320. if (id) {
  321. if (smallVideo && !interfaceConfig.filmStripOnly)
  322. smallVideo.focus(true);
  323. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  324. }
  325. this.updateLargeVideo(id);
  326. },
  327. /**
  328. * Creates or adds a participant container for the given id and smallVideo.
  329. *
  330. * @param {JitsiParticipant} user the participant to add
  331. * @param {SmallVideo} smallVideo optional small video instance to add as a
  332. * remote video, if undefined <tt>RemoteVideo</tt> will be created
  333. */
  334. addParticipantContainer (user, smallVideo) {
  335. let id = user.getId();
  336. let remoteVideo;
  337. if(smallVideo)
  338. remoteVideo = smallVideo;
  339. else
  340. remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  341. this.addRemoteVideoContainer(id, remoteVideo);
  342. },
  343. /**
  344. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  345. *
  346. * @param {string} the id of the video to add
  347. * @param {SmallVideo} smallVideo the small video instance to add as a
  348. * remote video
  349. */
  350. addRemoteVideoContainer (id, remoteVideo) {
  351. remoteVideos[id] = remoteVideo;
  352. if (!remoteVideo.getVideoType()) {
  353. // make video type the default one (camera)
  354. // FIXME container type is not a video type
  355. remoteVideo.setVideoType(VIDEO_CONTAINER_TYPE);
  356. }
  357. // In case this is not currently in the last n we don't show it.
  358. if (localLastNCount && localLastNCount > 0 &&
  359. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  360. remoteVideo.showPeerContainer('hide');
  361. } else {
  362. VideoLayout.resizeThumbnails(false, true);
  363. }
  364. // Initialize the view
  365. remoteVideo.updateView();
  366. },
  367. // FIXME: what does this do???
  368. remoteVideoActive(videoElement, resourceJid) {
  369. console.info(resourceJid + " video is now active", videoElement);
  370. VideoLayout.resizeThumbnails(
  371. false, false, function() {$(videoElement).show();});
  372. // Update the large video to the last added video only if there's no
  373. // current dominant, focused speaker or update it to
  374. // the current dominant speaker.
  375. if ((!pinnedId &&
  376. !currentDominantSpeaker &&
  377. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  378. pinnedId === resourceJid ||
  379. (!pinnedId && resourceJid &&
  380. currentDominantSpeaker === resourceJid) ||
  381. /* Playback started while we're on the stage - may need to update
  382. video source with the new stream */
  383. this.isCurrentlyOnLarge(resourceJid)) {
  384. this.updateLargeVideo(resourceJid, true);
  385. }
  386. },
  387. /**
  388. * Shows a visual indicator for the moderator of the conference.
  389. * On local or remote participants.
  390. */
  391. showModeratorIndicator () {
  392. let isModerator = APP.conference.isModerator;
  393. if (isModerator) {
  394. localVideoThumbnail.addModeratorIndicator();
  395. } else {
  396. localVideoThumbnail.removeModeratorIndicator();
  397. }
  398. APP.conference.listMembers().forEach(function (member) {
  399. let id = member.getId();
  400. let remoteVideo = remoteVideos[id];
  401. if (!remoteVideo)
  402. return;
  403. if (member.isModerator()) {
  404. remoteVideo.addModeratorIndicator();
  405. }
  406. if (isModerator) {
  407. // We are moderator, but user is not - add menu
  408. if(!remoteVideo.hasRemoteVideoMenu) {
  409. remoteVideo.addRemoteVideoMenu();
  410. }
  411. }
  412. });
  413. },
  414. /*
  415. * Shows or hides the audio muted indicator over the local thumbnail video.
  416. * @param {boolean} isMuted
  417. */
  418. showLocalAudioIndicator (isMuted) {
  419. localVideoThumbnail.showAudioIndicator(isMuted);
  420. },
  421. /**
  422. * Shows/hides the indication about local connection being interrupted.
  423. *
  424. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  425. * currently in the interrupted state or <tt>false</tt> if the connection
  426. * is fine.
  427. */
  428. showLocalConnectionInterrupted (isInterrupted) {
  429. localVideoThumbnail.connectionIndicator
  430. .updateConnectionStatusIndicator(!isInterrupted);
  431. },
  432. /**
  433. * Resizes thumbnails.
  434. */
  435. resizeThumbnails ( animate = false,
  436. forceUpdate = false,
  437. onComplete = null) {
  438. const { localVideo, remoteVideo }
  439. = FilmStrip.calculateThumbnailSize();
  440. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  441. animate, forceUpdate)
  442. .then(function () {
  443. if (onComplete && typeof onComplete === "function")
  444. onComplete();
  445. });
  446. return { localVideo, remoteVideo };
  447. },
  448. /**
  449. * On audio muted event.
  450. */
  451. onAudioMute (id, isMuted) {
  452. if (APP.conference.isLocalId(id)) {
  453. localVideoThumbnail.showAudioIndicator(isMuted);
  454. } else {
  455. let remoteVideo = remoteVideos[id];
  456. if (!remoteVideo)
  457. return;
  458. remoteVideo.showAudioIndicator(isMuted);
  459. if (APP.conference.isModerator) {
  460. remoteVideo.updateRemoteVideoMenu(isMuted);
  461. }
  462. }
  463. },
  464. /**
  465. * On video muted event.
  466. */
  467. onVideoMute (id, value) {
  468. if (APP.conference.isLocalId(id)) {
  469. localVideoThumbnail.setVideoMutedView(value);
  470. } else {
  471. let remoteVideo = remoteVideos[id];
  472. if (remoteVideo)
  473. remoteVideo.setVideoMutedView(value);
  474. }
  475. if (this.isCurrentlyOnLarge(id)) {
  476. // large video will show avatar instead of muted stream
  477. this.updateLargeVideo(id, true);
  478. }
  479. },
  480. /**
  481. * Display name changed.
  482. */
  483. onDisplayNameChanged (id, displayName, status) {
  484. if (id === 'localVideoContainer' ||
  485. APP.conference.isLocalId(id)) {
  486. localVideoThumbnail.setDisplayName(displayName);
  487. } else {
  488. let remoteVideo = remoteVideos[id];
  489. if (remoteVideo)
  490. remoteVideo.setDisplayName(displayName, status);
  491. }
  492. },
  493. /**
  494. * Sets the "raised hand" status for a participant identified by 'id'.
  495. */
  496. setRaisedHandStatus(id, raisedHandStatus) {
  497. var video
  498. = APP.conference.isLocalId(id)
  499. ? localVideoThumbnail : remoteVideos[id];
  500. if (video) {
  501. video.showRaisedHandIndicator(raisedHandStatus);
  502. if (raisedHandStatus) {
  503. video.showDominantSpeakerIndicator(false);
  504. }
  505. }
  506. },
  507. /**
  508. * On dominant speaker changed event.
  509. */
  510. onDominantSpeakerChanged (id) {
  511. if (id === currentDominantSpeaker) {
  512. return;
  513. }
  514. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  515. // We ignore local user events, but just unmark remote user as dominant
  516. // while we are talking
  517. if (APP.conference.isLocalId(id)) {
  518. if(oldSpeakerRemoteVideo)
  519. {
  520. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  521. currentDominantSpeaker = null;
  522. }
  523. localVideoThumbnail.showDominantSpeakerIndicator(true);
  524. return;
  525. }
  526. let remoteVideo = remoteVideos[id];
  527. if (!remoteVideo) {
  528. return;
  529. }
  530. // Update the current dominant speaker.
  531. remoteVideo.showDominantSpeakerIndicator(true);
  532. localVideoThumbnail.showDominantSpeakerIndicator(false);
  533. // let's remove the indications from the remote video if any
  534. if (oldSpeakerRemoteVideo) {
  535. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  536. }
  537. currentDominantSpeaker = id;
  538. // Local video will not have container found, but that's ok
  539. // since we don't want to switch to local video.
  540. // Update the large video if the video source is already available,
  541. // otherwise wait for the "videoactive.jingle" event.
  542. // FIXME: there is no "videoactive.jingle" event.
  543. if (!pinnedId
  544. && remoteVideo.hasVideoStarted()
  545. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  546. this.updateLargeVideo(id);
  547. }
  548. },
  549. /**
  550. * Shows/hides warning about remote user's connectivity issues.
  551. *
  552. * @param {string} id the ID of the remote participant(MUC nickname)
  553. * @param {boolean} isActive true if the connection is ok or false when
  554. * the user is having connectivity issues.
  555. */
  556. // eslint-disable-next-line no-unused-vars
  557. onParticipantConnectionStatusChanged (id, isActive) {
  558. // Show/hide warning on the large video
  559. if (this.isCurrentlyOnLarge(id)) {
  560. if (largeVideo) {
  561. // We have to trigger full large video update to transition from
  562. // avatar to video on connectivity restored.
  563. this.updateLargeVideo(id, true /* force update */);
  564. }
  565. }
  566. // Show/hide warning on the thumbnail
  567. let remoteVideo = remoteVideos[id];
  568. if (remoteVideo) {
  569. // Updating only connection status indicator is not enough, because
  570. // when we the connection is restored while the avatar was displayed
  571. // (due to 'muted while disconnected' condition) we may want to show
  572. // the video stream again and in order to do that the display mode
  573. // must be updated.
  574. //remoteVideo.updateConnectionStatusIndicator(isActive);
  575. remoteVideo.updateView();
  576. }
  577. },
  578. /**
  579. * On last N change event.
  580. *
  581. * @param lastNEndpoints the list of last N endpoints
  582. * @param endpointsEnteringLastN the list currently entering last N
  583. * endpoints
  584. */
  585. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  586. if (this.lastNCount !== lastNEndpoints.length)
  587. this.lastNCount = lastNEndpoints.length;
  588. lastNEndpointsCache = lastNEndpoints;
  589. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  590. //
  591. // If LastN drops to, say, 2, because of adaptivity, then E should see
  592. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  593. // so E sees them. C is only in E's local LastN set.
  594. //
  595. // If F starts talking and LastN = 3, then E should see thumbnails for
  596. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  597. // enters E's local LastN ejecting C.
  598. // Increase the local LastN set size, if necessary.
  599. if (this.lastNCount > localLastNCount) {
  600. localLastNCount = this.lastNCount;
  601. }
  602. // Update the local LastN set preserving the order in which the
  603. // endpoints appeared in the LastN/local LastN set.
  604. var nextLocalLastNSet = lastNEndpoints.slice(0);
  605. for (var i = 0; i < localLastNSet.length; i++) {
  606. if (nextLocalLastNSet.length >= localLastNCount) {
  607. break;
  608. }
  609. var resourceJid = localLastNSet[i];
  610. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  611. nextLocalLastNSet.push(resourceJid);
  612. }
  613. }
  614. localLastNSet = nextLocalLastNSet;
  615. var updateLargeVideo = false;
  616. // Handle LastN/local LastN changes.
  617. FilmStrip.getThumbs().remoteThumbs.each(( index, element ) => {
  618. var resourceJid = getPeerContainerResourceId(element);
  619. var smallVideo = remoteVideos[resourceJid];
  620. // We do not want to process any logic for our own(local) video
  621. // because the local participant is never in the lastN set.
  622. // The code of this function might detect that the local participant
  623. // has been dropped out of the lastN set and will update the large
  624. // video
  625. // Detected from avatar tests, where lastN event override
  626. // local video pinning
  627. if(APP.conference.isLocalId(resourceJid))
  628. return;
  629. var isReceived = true;
  630. if (resourceJid &&
  631. lastNEndpoints.indexOf(resourceJid) < 0 &&
  632. localLastNSet.indexOf(resourceJid) < 0) {
  633. console.log("Remove from last N", resourceJid);
  634. if (smallVideo)
  635. smallVideo.showPeerContainer('hide');
  636. else if (!APP.conference.isLocalId(resourceJid))
  637. console.error("No remote video for: " + resourceJid);
  638. isReceived = false;
  639. } else if (resourceJid &&
  640. //TOFIX: smallVideo may be undefined
  641. smallVideo.isVisible() &&
  642. lastNEndpoints.indexOf(resourceJid) < 0 &&
  643. localLastNSet.indexOf(resourceJid) >= 0) {
  644. // TOFIX: if we're here we already know that the smallVideo
  645. // exists. Look at the previous FIX above.
  646. if (smallVideo)
  647. smallVideo.showPeerContainer('avatar');
  648. else if (!APP.conference.isLocalId(resourceJid))
  649. console.error("No remote video for: " + resourceJid);
  650. isReceived = false;
  651. }
  652. if (!isReceived) {
  653. // resourceJid has dropped out of the server side lastN set, so
  654. // it is no longer being received. If resourceJid was being
  655. // displayed in the large video we have to switch to another
  656. // user.
  657. if (!updateLargeVideo &&
  658. this.isCurrentlyOnLarge(resourceJid)) {
  659. updateLargeVideo = true;
  660. }
  661. }
  662. });
  663. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  664. endpointsEnteringLastN = lastNEndpoints;
  665. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  666. endpointsEnteringLastN.forEach(function (resourceJid) {
  667. var remoteVideo = remoteVideos[resourceJid];
  668. if (remoteVideo)
  669. remoteVideo.showPeerContainer('show');
  670. if (!remoteVideo.isVisible()) {
  671. console.log("Add to last N", resourceJid);
  672. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  673. if (lastNPickupId == resourceJid) {
  674. // Clean up the lastN pickup id.
  675. lastNPickupId = null;
  676. VideoLayout.handleVideoThumbClicked(resourceJid);
  677. updateLargeVideo = false;
  678. }
  679. remoteVideo.waitForPlayback(
  680. remoteVideo.selectVideoElement()[0],
  681. remoteVideo.videoStream);
  682. }
  683. });
  684. }
  685. // The endpoint that was being shown in the large video has dropped out
  686. // of the lastN set and there was no lastN pickup jid. We need to update
  687. // the large video now.
  688. if (updateLargeVideo) {
  689. var resource;
  690. // Find out which endpoint to show in the large video.
  691. for (i = 0; i < lastNEndpoints.length; i++) {
  692. resource = lastNEndpoints[i];
  693. if (!resource || APP.conference.isLocalId(resource))
  694. continue;
  695. // videoSrcToSsrc needs to be update for this call to succeed.
  696. this.updateLargeVideo(resource);
  697. break;
  698. }
  699. }
  700. },
  701. /**
  702. * Updates local stats
  703. * @param percent
  704. * @param object
  705. */
  706. updateLocalConnectionStats (percent, object) {
  707. let resolutions = object.resolution;
  708. object.resolution = resolutions[APP.conference.getMyUserId()];
  709. localVideoThumbnail.updateStatsIndicator(percent, object);
  710. Object.keys(resolutions).forEach(function (id) {
  711. if (APP.conference.isLocalId(id)) {
  712. return;
  713. }
  714. let resolution = resolutions[id];
  715. let remoteVideo = remoteVideos[id];
  716. if (resolution && remoteVideo) {
  717. remoteVideo.updateResolution(resolution);
  718. }
  719. });
  720. },
  721. /**
  722. * Updates remote stats.
  723. * @param id the id associated with the stats
  724. * @param percent the connection quality percent
  725. * @param object the stats data
  726. */
  727. updateConnectionStats (id, percent, object) {
  728. let remoteVideo = remoteVideos[id];
  729. if (remoteVideo) {
  730. remoteVideo.updateStatsIndicator(percent, object);
  731. }
  732. },
  733. /**
  734. * Hides the connection indicator
  735. * @param id
  736. */
  737. hideConnectionIndicator (id) {
  738. let remoteVideo = remoteVideos[id];
  739. if (remoteVideo)
  740. remoteVideo.hideConnectionIndicator();
  741. },
  742. /**
  743. * Hides all the indicators
  744. */
  745. hideStats () {
  746. for (var video in remoteVideos) {
  747. let remoteVideo = remoteVideos[video];
  748. if (remoteVideo)
  749. remoteVideo.hideIndicator();
  750. }
  751. localVideoThumbnail.hideIndicator();
  752. },
  753. removeParticipantContainer (id) {
  754. // Unlock large video
  755. if (pinnedId === id) {
  756. console.info("Focused video owner has left the conference");
  757. pinnedId = null;
  758. }
  759. if (currentDominantSpeaker === id) {
  760. console.info("Dominant speaker has left the conference");
  761. currentDominantSpeaker = null;
  762. }
  763. var remoteVideo = remoteVideos[id];
  764. if (remoteVideo) {
  765. // Remove remote video
  766. console.info("Removing remote video: " + id);
  767. delete remoteVideos[id];
  768. remoteVideo.remove();
  769. } else {
  770. console.warn("No remote video for " + id);
  771. }
  772. VideoLayout.resizeThumbnails();
  773. },
  774. onVideoTypeChanged (id, newVideoType) {
  775. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  776. return;
  777. }
  778. console.info("Peer video type changed: ", id, newVideoType);
  779. var smallVideo;
  780. if (APP.conference.isLocalId(id)) {
  781. if (!localVideoThumbnail) {
  782. console.warn("Local video not ready yet");
  783. return;
  784. }
  785. smallVideo = localVideoThumbnail;
  786. } else if (remoteVideos[id]) {
  787. smallVideo = remoteVideos[id];
  788. } else {
  789. return;
  790. }
  791. smallVideo.setVideoType(newVideoType);
  792. if (this.isCurrentlyOnLarge(id)) {
  793. this.updateLargeVideo(id, true);
  794. }
  795. },
  796. showMore (id) {
  797. if (id === 'local') {
  798. localVideoThumbnail.connectionIndicator.showMore();
  799. } else {
  800. let remoteVideo = remoteVideos[id];
  801. if (remoteVideo) {
  802. remoteVideo.connectionIndicator.showMore();
  803. } else {
  804. console.info("Error - no remote video for id: " + id);
  805. }
  806. }
  807. },
  808. /**
  809. * Resizes the video area.
  810. *
  811. * @param forceUpdate indicates that hidden thumbnails will be shown
  812. * @param completeFunction a function to be called when the video area is
  813. * resized.
  814. */
  815. resizeVideoArea (forceUpdate = false,
  816. animate = false,
  817. completeFunction = null) {
  818. if (largeVideo) {
  819. largeVideo.updateContainerSize();
  820. largeVideo.resize(animate);
  821. }
  822. // Calculate available width and height.
  823. let availableHeight = window.innerHeight;
  824. let availableWidth = UIUtil.getAvailableVideoWidth();
  825. if (availableWidth < 0 || availableHeight < 0) {
  826. return;
  827. }
  828. // Resize the thumbnails first.
  829. this.resizeThumbnails(false, forceUpdate);
  830. // Resize the video area element.
  831. $('#videospace').animate({
  832. right: window.innerWidth - availableWidth,
  833. width: availableWidth,
  834. height: availableHeight
  835. }, {
  836. queue: false,
  837. duration: animate ? 500 : 1,
  838. complete: completeFunction
  839. });
  840. },
  841. getSmallVideo (id) {
  842. if (APP.conference.isLocalId(id)) {
  843. return localVideoThumbnail;
  844. } else {
  845. return remoteVideos[id];
  846. }
  847. },
  848. changeUserAvatar (id, avatarUrl) {
  849. var smallVideo = VideoLayout.getSmallVideo(id);
  850. if (smallVideo) {
  851. smallVideo.avatarChanged(avatarUrl);
  852. } else {
  853. console.warn(
  854. "Missed avatar update - no small video yet for " + id
  855. );
  856. }
  857. if (this.isCurrentlyOnLarge(id)) {
  858. largeVideo.updateAvatar(avatarUrl);
  859. }
  860. },
  861. /**
  862. * Indicates that the video has been interrupted.
  863. */
  864. onVideoInterrupted () {
  865. if (largeVideo) {
  866. largeVideo.onVideoInterrupted();
  867. }
  868. },
  869. /**
  870. * Indicates that the video has been restored.
  871. */
  872. onVideoRestored () {
  873. if (largeVideo) {
  874. largeVideo.onVideoRestored();
  875. }
  876. },
  877. isLargeVideoVisible () {
  878. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  879. },
  880. /**
  881. * @return {LargeContainer} the currently displayed container on large
  882. * video.
  883. */
  884. getCurrentlyOnLargeContainer () {
  885. return largeVideo.getContainer(largeVideo.state);
  886. },
  887. isCurrentlyOnLarge (id) {
  888. return largeVideo && largeVideo.id === id;
  889. },
  890. updateLargeVideo (id, forceUpdate) {
  891. if (!largeVideo) {
  892. return;
  893. }
  894. let isOnLarge = this.isCurrentlyOnLarge(id);
  895. let currentId = largeVideo.id;
  896. if (!isOnLarge || forceUpdate) {
  897. let videoType = this.getRemoteVideoType(id);
  898. // FIXME video type is not the same thing as container type
  899. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  900. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  901. }
  902. let smallVideo = this.getSmallVideo(id);
  903. let oldSmallVideo;
  904. if (currentId) {
  905. oldSmallVideo = this.getSmallVideo(currentId);
  906. }
  907. smallVideo.waitForResolutionChange();
  908. if (oldSmallVideo)
  909. oldSmallVideo.waitForResolutionChange();
  910. largeVideo.updateLargeVideo(
  911. id,
  912. smallVideo.videoStream,
  913. videoType
  914. ).then(function() {
  915. // update current small video and the old one
  916. smallVideo.updateView();
  917. oldSmallVideo && oldSmallVideo.updateView();
  918. }, function () {
  919. // use clicked other video during update, nothing to do.
  920. });
  921. } else if (currentId) {
  922. let currentSmallVideo = this.getSmallVideo(currentId);
  923. currentSmallVideo.updateView();
  924. }
  925. },
  926. addLargeVideoContainer (type, container) {
  927. largeVideo && largeVideo.addContainer(type, container);
  928. },
  929. removeLargeVideoContainer (type) {
  930. largeVideo && largeVideo.removeContainer(type);
  931. },
  932. /**
  933. * @returns Promise
  934. */
  935. showLargeVideoContainer (type, show) {
  936. if (!largeVideo) {
  937. return Promise.reject();
  938. }
  939. let isVisible = this.isLargeContainerTypeVisible(type);
  940. if (isVisible === show) {
  941. return Promise.resolve();
  942. }
  943. let currentId = largeVideo.id;
  944. if(currentId) {
  945. var oldSmallVideo = this.getSmallVideo(currentId);
  946. }
  947. let containerTypeToShow = type;
  948. // if we are hiding a container and there is focusedVideo
  949. // (pinned remote video) use its video type,
  950. // if not then use default type - large video
  951. if (!show) {
  952. if(pinnedId)
  953. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  954. else
  955. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  956. }
  957. return largeVideo.showContainer(containerTypeToShow)
  958. .then(() => {
  959. if(oldSmallVideo)
  960. oldSmallVideo && oldSmallVideo.updateView();
  961. });
  962. },
  963. isLargeContainerTypeVisible (type) {
  964. return largeVideo && largeVideo.state === type;
  965. },
  966. /**
  967. * Returns the id of the current video shown on large.
  968. * Currently used by tests (torture).
  969. */
  970. getLargeVideoID () {
  971. return largeVideo.id;
  972. },
  973. /**
  974. * Returns the the current video shown on large.
  975. * Currently used by tests (torture).
  976. */
  977. getLargeVideo () {
  978. return largeVideo;
  979. },
  980. /**
  981. * Updates the resolution label, indicating to the user that the large
  982. * video stream is currently HD.
  983. */
  984. updateResolutionLabel(isResolutionHD) {
  985. let id = 'videoResolutionLabel';
  986. if (isResolutionHD) {
  987. UIUtil.showElement(id);
  988. } else {
  989. UIUtil.hideElement(id);
  990. }
  991. },
  992. /**
  993. * Sets the flipX state of the local video.
  994. * @param {boolean} true for flipped otherwise false;
  995. */
  996. setLocalFlipX: function (val) {
  997. this.localFlipX = val;
  998. },
  999. getEventEmitter: () => {return eventEmitter;}
  1000. };
  1001. export default VideoLayout;