您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. }
  308. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  309. return;
  310. }
  311. // Lock new video
  312. pinnedId = id;
  313. // Update focused/pinned interface.
  314. if (id) {
  315. if (smallVideo && !interfaceConfig.filmStripOnly)
  316. smallVideo.focus(true);
  317. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  318. }
  319. this.updateLargeVideo(id);
  320. },
  321. /**
  322. * Creates or adds a participant container for the given id and smallVideo.
  323. *
  324. * @param {JitsiParticipant} user the participant to add
  325. * @param {SmallVideo} smallVideo optional small video instance to add as a
  326. * remote video, if undefined <tt>RemoteVideo</tt> will be created
  327. */
  328. addParticipantContainer (user, smallVideo) {
  329. let id = user.getId();
  330. let remoteVideo;
  331. if(smallVideo)
  332. remoteVideo = smallVideo;
  333. else
  334. remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  335. this.addRemoteVideoContainer(id, remoteVideo);
  336. },
  337. /**
  338. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  339. *
  340. * @param {string} the id of the video to add
  341. * @param {SmallVideo} smallVideo the small video instance to add as a
  342. * remote video
  343. */
  344. addRemoteVideoContainer (id, remoteVideo) {
  345. remoteVideos[id] = remoteVideo;
  346. if (!remoteVideo.getVideoType()) {
  347. // make video type the default one (camera)
  348. // FIXME container type is not a video type
  349. remoteVideo.setVideoType(VIDEO_CONTAINER_TYPE);
  350. }
  351. // In case this is not currently in the last n we don't show it.
  352. if (localLastNCount && localLastNCount > 0 &&
  353. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  354. remoteVideo.showPeerContainer('hide');
  355. } else {
  356. VideoLayout.resizeThumbnails(false, true);
  357. }
  358. // Initialize the view
  359. remoteVideo.updateView();
  360. },
  361. // FIXME: what does this do???
  362. remoteVideoActive(videoElement, resourceJid) {
  363. console.info(resourceJid + " video is now active", videoElement);
  364. VideoLayout.resizeThumbnails(
  365. false, false, function() {$(videoElement).show();});
  366. // Update the large video to the last added video only if there's no
  367. // current dominant, focused speaker or update it to
  368. // the current dominant speaker.
  369. if ((!pinnedId &&
  370. !currentDominantSpeaker &&
  371. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  372. pinnedId === resourceJid ||
  373. (!pinnedId && resourceJid &&
  374. currentDominantSpeaker === resourceJid) ||
  375. /* Playback started while we're on the stage - may need to update
  376. video source with the new stream */
  377. this.isCurrentlyOnLarge(resourceJid)) {
  378. this.updateLargeVideo(resourceJid, true);
  379. }
  380. },
  381. /**
  382. * Shows a visual indicator for the moderator of the conference.
  383. * On local or remote participants.
  384. */
  385. showModeratorIndicator () {
  386. let isModerator = APP.conference.isModerator;
  387. if (isModerator) {
  388. localVideoThumbnail.addModeratorIndicator();
  389. } else {
  390. localVideoThumbnail.removeModeratorIndicator();
  391. }
  392. APP.conference.listMembers().forEach(function (member) {
  393. let id = member.getId();
  394. let remoteVideo = remoteVideos[id];
  395. if (!remoteVideo)
  396. return;
  397. if (member.isModerator()) {
  398. remoteVideo.addModeratorIndicator();
  399. }
  400. if (isModerator) {
  401. // We are moderator, but user is not - add menu
  402. if(!remoteVideo.hasRemoteVideoMenu) {
  403. remoteVideo.addRemoteVideoMenu();
  404. }
  405. }
  406. });
  407. },
  408. /*
  409. * Shows or hides the audio muted indicator over the local thumbnail video.
  410. * @param {boolean} isMuted
  411. */
  412. showLocalAudioIndicator (isMuted) {
  413. localVideoThumbnail.showAudioIndicator(isMuted);
  414. },
  415. /**
  416. * Shows/hides the indication about local connection being interrupted.
  417. *
  418. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  419. * currently in the interrupted state or <tt>false</tt> if the connection
  420. * is fine.
  421. */
  422. showLocalConnectionInterrupted (isInterrupted) {
  423. localVideoThumbnail.connectionIndicator
  424. .updateConnectionStatusIndicator(!isInterrupted);
  425. },
  426. /**
  427. * Resizes thumbnails.
  428. */
  429. resizeThumbnails ( animate = false,
  430. forceUpdate = false,
  431. onComplete = null) {
  432. const { localVideo, remoteVideo }
  433. = FilmStrip.calculateThumbnailSize();
  434. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  435. animate, forceUpdate)
  436. .then(function () {
  437. if (onComplete && typeof onComplete === "function")
  438. onComplete();
  439. });
  440. return { localVideo, remoteVideo };
  441. },
  442. /**
  443. * On audio muted event.
  444. */
  445. onAudioMute (id, isMuted) {
  446. if (APP.conference.isLocalId(id)) {
  447. localVideoThumbnail.showAudioIndicator(isMuted);
  448. } else {
  449. let remoteVideo = remoteVideos[id];
  450. if (!remoteVideo)
  451. return;
  452. remoteVideo.showAudioIndicator(isMuted);
  453. if (APP.conference.isModerator) {
  454. remoteVideo.updateRemoteVideoMenu(isMuted);
  455. }
  456. }
  457. },
  458. /**
  459. * On video muted event.
  460. */
  461. onVideoMute (id, value) {
  462. if (APP.conference.isLocalId(id)) {
  463. localVideoThumbnail.setVideoMutedView(value);
  464. } else {
  465. let remoteVideo = remoteVideos[id];
  466. if (remoteVideo)
  467. remoteVideo.setVideoMutedView(value);
  468. }
  469. if (this.isCurrentlyOnLarge(id)) {
  470. // large video will show avatar instead of muted stream
  471. this.updateLargeVideo(id, true);
  472. }
  473. },
  474. /**
  475. * Display name changed.
  476. */
  477. onDisplayNameChanged (id, displayName, status) {
  478. if (id === 'localVideoContainer' ||
  479. APP.conference.isLocalId(id)) {
  480. localVideoThumbnail.setDisplayName(displayName);
  481. } else {
  482. let remoteVideo = remoteVideos[id];
  483. if (remoteVideo)
  484. remoteVideo.setDisplayName(displayName, status);
  485. }
  486. },
  487. /**
  488. * Sets the "raised hand" status for a participant identified by 'id'.
  489. */
  490. setRaisedHandStatus(id, raisedHandStatus) {
  491. var video
  492. = APP.conference.isLocalId(id)
  493. ? localVideoThumbnail : remoteVideos[id];
  494. if (video) {
  495. video.showRaisedHandIndicator(raisedHandStatus);
  496. if (raisedHandStatus) {
  497. video.showDominantSpeakerIndicator(false);
  498. }
  499. }
  500. },
  501. /**
  502. * On dominant speaker changed event.
  503. */
  504. onDominantSpeakerChanged (id) {
  505. if (id === currentDominantSpeaker) {
  506. return;
  507. }
  508. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  509. // We ignore local user events, but just unmark remote user as dominant
  510. // while we are talking
  511. if (APP.conference.isLocalId(id)) {
  512. if(oldSpeakerRemoteVideo)
  513. {
  514. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  515. currentDominantSpeaker = null;
  516. }
  517. localVideoThumbnail.showDominantSpeakerIndicator(true);
  518. return;
  519. }
  520. let remoteVideo = remoteVideos[id];
  521. if (!remoteVideo) {
  522. return;
  523. }
  524. // Update the current dominant speaker.
  525. remoteVideo.showDominantSpeakerIndicator(true);
  526. localVideoThumbnail.showDominantSpeakerIndicator(false);
  527. // let's remove the indications from the remote video if any
  528. if (oldSpeakerRemoteVideo) {
  529. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  530. }
  531. currentDominantSpeaker = id;
  532. // Local video will not have container found, but that's ok
  533. // since we don't want to switch to local video.
  534. // Update the large video if the video source is already available,
  535. // otherwise wait for the "videoactive.jingle" event.
  536. // FIXME: there is no "videoactive.jingle" event.
  537. if (!pinnedId
  538. && remoteVideo.hasVideoStarted()
  539. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  540. this.updateLargeVideo(id);
  541. }
  542. },
  543. /**
  544. * Shows/hides warning about remote user's connectivity issues.
  545. *
  546. * @param {string} id the ID of the remote participant(MUC nickname)
  547. * @param {boolean} isActive true if the connection is ok or false when
  548. * the user is having connectivity issues.
  549. */
  550. // eslint-disable-next-line no-unused-vars
  551. onParticipantConnectionStatusChanged (id, isActive) {
  552. // Show/hide warning on the large video
  553. if (this.isCurrentlyOnLarge(id)) {
  554. if (largeVideo) {
  555. // We have to trigger full large video update to transition from
  556. // avatar to video on connectivity restored.
  557. this.updateLargeVideo(id, true /* force update */);
  558. }
  559. }
  560. // Show/hide warning on the thumbnail
  561. let remoteVideo = remoteVideos[id];
  562. if (remoteVideo) {
  563. // Updating only connection status indicator is not enough, because
  564. // when we the connection is restored while the avatar was displayed
  565. // (due to 'muted while disconnected' condition) we may want to show
  566. // the video stream again and in order to do that the display mode
  567. // must be updated.
  568. //remoteVideo.updateConnectionStatusIndicator(isActive);
  569. remoteVideo.updateView();
  570. }
  571. },
  572. /**
  573. * On last N change event.
  574. *
  575. * @param lastNEndpoints the list of last N endpoints
  576. * @param endpointsEnteringLastN the list currently entering last N
  577. * endpoints
  578. */
  579. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  580. if (this.lastNCount !== lastNEndpoints.length)
  581. this.lastNCount = lastNEndpoints.length;
  582. lastNEndpointsCache = lastNEndpoints;
  583. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  584. //
  585. // If LastN drops to, say, 2, because of adaptivity, then E should see
  586. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  587. // so E sees them. C is only in E's local LastN set.
  588. //
  589. // If F starts talking and LastN = 3, then E should see thumbnails for
  590. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  591. // enters E's local LastN ejecting C.
  592. // Increase the local LastN set size, if necessary.
  593. if (this.lastNCount > localLastNCount) {
  594. localLastNCount = this.lastNCount;
  595. }
  596. // Update the local LastN set preserving the order in which the
  597. // endpoints appeared in the LastN/local LastN set.
  598. var nextLocalLastNSet = lastNEndpoints.slice(0);
  599. for (var i = 0; i < localLastNSet.length; i++) {
  600. if (nextLocalLastNSet.length >= localLastNCount) {
  601. break;
  602. }
  603. var resourceJid = localLastNSet[i];
  604. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  605. nextLocalLastNSet.push(resourceJid);
  606. }
  607. }
  608. localLastNSet = nextLocalLastNSet;
  609. var updateLargeVideo = false;
  610. // Handle LastN/local LastN changes.
  611. FilmStrip.getThumbs().remoteThumbs.each(( index, element ) => {
  612. var resourceJid = getPeerContainerResourceId(element);
  613. var smallVideo = remoteVideos[resourceJid];
  614. // We do not want to process any logic for our own(local) video
  615. // because the local participant is never in the lastN set.
  616. // The code of this function might detect that the local participant
  617. // has been dropped out of the lastN set and will update the large
  618. // video
  619. // Detected from avatar tests, where lastN event override
  620. // local video pinning
  621. if(APP.conference.isLocalId(resourceJid))
  622. return;
  623. var isReceived = true;
  624. if (resourceJid &&
  625. lastNEndpoints.indexOf(resourceJid) < 0 &&
  626. localLastNSet.indexOf(resourceJid) < 0) {
  627. console.log("Remove from last N", resourceJid);
  628. if (smallVideo)
  629. smallVideo.showPeerContainer('hide');
  630. else if (!APP.conference.isLocalId(resourceJid))
  631. console.error("No remote video for: " + resourceJid);
  632. isReceived = false;
  633. } else if (resourceJid &&
  634. //TOFIX: smallVideo may be undefined
  635. smallVideo.isVisible() &&
  636. lastNEndpoints.indexOf(resourceJid) < 0 &&
  637. localLastNSet.indexOf(resourceJid) >= 0) {
  638. // TOFIX: if we're here we already know that the smallVideo
  639. // exists. Look at the previous FIX above.
  640. if (smallVideo)
  641. smallVideo.showPeerContainer('avatar');
  642. else if (!APP.conference.isLocalId(resourceJid))
  643. console.error("No remote video for: " + resourceJid);
  644. isReceived = false;
  645. }
  646. if (!isReceived) {
  647. // resourceJid has dropped out of the server side lastN set, so
  648. // it is no longer being received. If resourceJid was being
  649. // displayed in the large video we have to switch to another
  650. // user.
  651. if (!updateLargeVideo &&
  652. this.isCurrentlyOnLarge(resourceJid)) {
  653. updateLargeVideo = true;
  654. }
  655. }
  656. });
  657. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  658. endpointsEnteringLastN = lastNEndpoints;
  659. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  660. endpointsEnteringLastN.forEach(function (resourceJid) {
  661. var remoteVideo = remoteVideos[resourceJid];
  662. if (remoteVideo)
  663. remoteVideo.showPeerContainer('show');
  664. if (!remoteVideo.isVisible()) {
  665. console.log("Add to last N", resourceJid);
  666. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  667. if (lastNPickupId == resourceJid) {
  668. // Clean up the lastN pickup id.
  669. lastNPickupId = null;
  670. VideoLayout.handleVideoThumbClicked(resourceJid);
  671. updateLargeVideo = false;
  672. }
  673. remoteVideo.waitForPlayback(
  674. remoteVideo.selectVideoElement()[0],
  675. remoteVideo.videoStream);
  676. }
  677. });
  678. }
  679. // The endpoint that was being shown in the large video has dropped out
  680. // of the lastN set and there was no lastN pickup jid. We need to update
  681. // the large video now.
  682. if (updateLargeVideo) {
  683. var resource;
  684. // Find out which endpoint to show in the large video.
  685. for (i = 0; i < lastNEndpoints.length; i++) {
  686. resource = lastNEndpoints[i];
  687. if (!resource || APP.conference.isLocalId(resource))
  688. continue;
  689. // videoSrcToSsrc needs to be update for this call to succeed.
  690. this.updateLargeVideo(resource);
  691. break;
  692. }
  693. }
  694. },
  695. /**
  696. * Updates local stats
  697. * @param percent
  698. * @param object
  699. */
  700. updateLocalConnectionStats (percent, object) {
  701. let resolutions = object.resolution;
  702. object.resolution = resolutions[APP.conference.getMyUserId()];
  703. localVideoThumbnail.updateStatsIndicator(percent, object);
  704. Object.keys(resolutions).forEach(function (id) {
  705. if (APP.conference.isLocalId(id)) {
  706. return;
  707. }
  708. let resolution = resolutions[id];
  709. let remoteVideo = remoteVideos[id];
  710. if (resolution && remoteVideo) {
  711. remoteVideo.updateResolution(resolution);
  712. }
  713. });
  714. },
  715. /**
  716. * Updates remote stats.
  717. * @param id the id associated with the stats
  718. * @param percent the connection quality percent
  719. * @param object the stats data
  720. */
  721. updateConnectionStats (id, percent, object) {
  722. let remoteVideo = remoteVideos[id];
  723. if (remoteVideo) {
  724. remoteVideo.updateStatsIndicator(percent, object);
  725. }
  726. },
  727. /**
  728. * Hides the connection indicator
  729. * @param id
  730. */
  731. hideConnectionIndicator (id) {
  732. let remoteVideo = remoteVideos[id];
  733. if (remoteVideo)
  734. remoteVideo.hideConnectionIndicator();
  735. },
  736. /**
  737. * Hides all the indicators
  738. */
  739. hideStats () {
  740. for (var video in remoteVideos) {
  741. let remoteVideo = remoteVideos[video];
  742. if (remoteVideo)
  743. remoteVideo.hideIndicator();
  744. }
  745. localVideoThumbnail.hideIndicator();
  746. },
  747. removeParticipantContainer (id) {
  748. // Unlock large video
  749. if (pinnedId === id) {
  750. console.info("Focused video owner has left the conference");
  751. pinnedId = null;
  752. }
  753. if (currentDominantSpeaker === id) {
  754. console.info("Dominant speaker has left the conference");
  755. currentDominantSpeaker = null;
  756. }
  757. var remoteVideo = remoteVideos[id];
  758. if (remoteVideo) {
  759. // Remove remote video
  760. console.info("Removing remote video: " + id);
  761. delete remoteVideos[id];
  762. remoteVideo.remove();
  763. } else {
  764. console.warn("No remote video for " + id);
  765. }
  766. VideoLayout.resizeThumbnails();
  767. },
  768. onVideoTypeChanged (id, newVideoType) {
  769. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  770. return;
  771. }
  772. console.info("Peer video type changed: ", id, newVideoType);
  773. var smallVideo;
  774. if (APP.conference.isLocalId(id)) {
  775. if (!localVideoThumbnail) {
  776. console.warn("Local video not ready yet");
  777. return;
  778. }
  779. smallVideo = localVideoThumbnail;
  780. } else if (remoteVideos[id]) {
  781. smallVideo = remoteVideos[id];
  782. } else {
  783. return;
  784. }
  785. smallVideo.setVideoType(newVideoType);
  786. if (this.isCurrentlyOnLarge(id)) {
  787. this.updateLargeVideo(id, true);
  788. }
  789. },
  790. showMore (id) {
  791. if (id === 'local') {
  792. localVideoThumbnail.connectionIndicator.showMore();
  793. } else {
  794. let remoteVideo = remoteVideos[id];
  795. if (remoteVideo) {
  796. remoteVideo.connectionIndicator.showMore();
  797. } else {
  798. console.info("Error - no remote video for id: " + id);
  799. }
  800. }
  801. },
  802. /**
  803. * Resizes the video area.
  804. *
  805. * @param forceUpdate indicates that hidden thumbnails will be shown
  806. * @param completeFunction a function to be called when the video area is
  807. * resized.
  808. */
  809. resizeVideoArea (forceUpdate = false,
  810. animate = false,
  811. completeFunction = null) {
  812. if (largeVideo) {
  813. largeVideo.updateContainerSize();
  814. largeVideo.resize(animate);
  815. }
  816. // Calculate available width and height.
  817. let availableHeight = window.innerHeight;
  818. let availableWidth = UIUtil.getAvailableVideoWidth();
  819. if (availableWidth < 0 || availableHeight < 0) {
  820. return;
  821. }
  822. // Resize the thumbnails first.
  823. this.resizeThumbnails(false, forceUpdate);
  824. // Resize the video area element.
  825. $('#videospace').animate({
  826. right: window.innerWidth - availableWidth,
  827. width: availableWidth,
  828. height: availableHeight
  829. }, {
  830. queue: false,
  831. duration: animate ? 500 : 1,
  832. complete: completeFunction
  833. });
  834. },
  835. getSmallVideo (id) {
  836. if (APP.conference.isLocalId(id)) {
  837. return localVideoThumbnail;
  838. } else {
  839. return remoteVideos[id];
  840. }
  841. },
  842. changeUserAvatar (id, avatarUrl) {
  843. var smallVideo = VideoLayout.getSmallVideo(id);
  844. if (smallVideo) {
  845. smallVideo.avatarChanged(avatarUrl);
  846. } else {
  847. console.warn(
  848. "Missed avatar update - no small video yet for " + id
  849. );
  850. }
  851. if (this.isCurrentlyOnLarge(id)) {
  852. largeVideo.updateAvatar(avatarUrl);
  853. }
  854. },
  855. /**
  856. * Indicates that the video has been interrupted.
  857. */
  858. onVideoInterrupted () {
  859. if (largeVideo) {
  860. largeVideo.onVideoInterrupted();
  861. }
  862. },
  863. /**
  864. * Indicates that the video has been restored.
  865. */
  866. onVideoRestored () {
  867. if (largeVideo) {
  868. largeVideo.onVideoRestored();
  869. }
  870. },
  871. isLargeVideoVisible () {
  872. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  873. },
  874. /**
  875. * @return {LargeContainer} the currently displayed container on large
  876. * video.
  877. */
  878. getCurrentlyOnLargeContainer () {
  879. return largeVideo.getContainer(largeVideo.state);
  880. },
  881. isCurrentlyOnLarge (id) {
  882. return largeVideo && largeVideo.id === id;
  883. },
  884. updateLargeVideo (id, forceUpdate) {
  885. if (!largeVideo) {
  886. return;
  887. }
  888. let isOnLarge = this.isCurrentlyOnLarge(id);
  889. let currentId = largeVideo.id;
  890. if (!isOnLarge || forceUpdate) {
  891. let videoType = this.getRemoteVideoType(id);
  892. // FIXME video type is not the same thing as container type
  893. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  894. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  895. }
  896. let smallVideo = this.getSmallVideo(id);
  897. let oldSmallVideo;
  898. if (currentId) {
  899. oldSmallVideo = this.getSmallVideo(currentId);
  900. }
  901. smallVideo.waitForResolutionChange();
  902. if (oldSmallVideo)
  903. oldSmallVideo.waitForResolutionChange();
  904. largeVideo.updateLargeVideo(
  905. id,
  906. smallVideo.videoStream,
  907. videoType
  908. ).then(function() {
  909. // update current small video and the old one
  910. smallVideo.updateView();
  911. oldSmallVideo && oldSmallVideo.updateView();
  912. }, function () {
  913. // use clicked other video during update, nothing to do.
  914. });
  915. } else if (currentId) {
  916. let currentSmallVideo = this.getSmallVideo(currentId);
  917. currentSmallVideo.updateView();
  918. }
  919. },
  920. addLargeVideoContainer (type, container) {
  921. largeVideo && largeVideo.addContainer(type, container);
  922. },
  923. removeLargeVideoContainer (type) {
  924. largeVideo && largeVideo.removeContainer(type);
  925. },
  926. /**
  927. * @returns Promise
  928. */
  929. showLargeVideoContainer (type, show) {
  930. if (!largeVideo) {
  931. return Promise.reject();
  932. }
  933. let isVisible = this.isLargeContainerTypeVisible(type);
  934. if (isVisible === show) {
  935. return Promise.resolve();
  936. }
  937. let currentId = largeVideo.id;
  938. if(currentId) {
  939. var oldSmallVideo = this.getSmallVideo(currentId);
  940. }
  941. let containerTypeToShow = type;
  942. // if we are hiding a container and there is focusedVideo
  943. // (pinned remote video) use its video type,
  944. // if not then use default type - large video
  945. if (!show) {
  946. if(pinnedId)
  947. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  948. else
  949. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  950. }
  951. return largeVideo.showContainer(containerTypeToShow)
  952. .then(() => {
  953. if(oldSmallVideo)
  954. oldSmallVideo && oldSmallVideo.updateView();
  955. });
  956. },
  957. isLargeContainerTypeVisible (type) {
  958. return largeVideo && largeVideo.state === type;
  959. },
  960. /**
  961. * Returns the id of the current video shown on large.
  962. * Currently used by tests (torture).
  963. */
  964. getLargeVideoID () {
  965. return largeVideo.id;
  966. },
  967. /**
  968. * Returns the the current video shown on large.
  969. * Currently used by tests (torture).
  970. */
  971. getLargeVideo () {
  972. return largeVideo;
  973. },
  974. /**
  975. * Updates the resolution label, indicating to the user that the large
  976. * video stream is currently HD.
  977. */
  978. updateResolutionLabel(isResolutionHD) {
  979. document.querySelector('#videoResolutionLabel')
  980. .classList.toggle('show', isResolutionHD);
  981. },
  982. /**
  983. * Sets the flipX state of the local video.
  984. * @param {boolean} true for flipped otherwise false;
  985. */
  986. setLocalFlipX: function (val) {
  987. this.localFlipX = val;
  988. },
  989. getEventEmitter: () => {return eventEmitter;}
  990. };
  991. export default VideoLayout;