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.

subscriber.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. import debounce from 'lodash/debounce';
  2. import { IStore } from '../app/types';
  3. import { _handleParticipantError } from '../base/conference/functions';
  4. import { MEDIA_TYPE } from '../base/media/constants';
  5. import { getLocalParticipant } from '../base/participants/functions';
  6. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  7. import { getTrackSourceNameByMediaTypeAndParticipant } from '../base/tracks/functions';
  8. import { reportError } from '../base/util/helpers';
  9. import {
  10. getActiveParticipantsIds,
  11. getScreenshareFilmstripParticipantId,
  12. isTopPanelEnabled
  13. // @ts-ignore
  14. } from '../filmstrip/functions';
  15. import { LAYOUTS } from '../video-layout/constants';
  16. import {
  17. getCurrentLayout,
  18. getVideoQualityForLargeVideo,
  19. getVideoQualityForResizableFilmstripThumbnails,
  20. getVideoQualityForScreenSharingFilmstrip,
  21. getVideoQualityForStageThumbnails,
  22. shouldDisplayTileView
  23. } from '../video-layout/functions';
  24. import {
  25. setMaxReceiverVideoQualityForLargeVideo,
  26. setMaxReceiverVideoQualityForScreenSharingFilmstrip,
  27. setMaxReceiverVideoQualityForStageFilmstrip,
  28. setMaxReceiverVideoQualityForTileView,
  29. setMaxReceiverVideoQualityForVerticalFilmstrip
  30. } from './actions';
  31. import { MAX_VIDEO_QUALITY, VIDEO_QUALITY_LEVELS, VIDEO_QUALITY_UNLIMITED } from './constants';
  32. import { getReceiverVideoQualityLevel } from './functions';
  33. import logger from './logger';
  34. import { getMinHeightForQualityLvlMap } from './selector';
  35. /**
  36. * Handles changes in the visible participants in the filmstrip. The listener is debounced
  37. * so that the client doesn't end up sending too many bridge messages when the user is
  38. * scrolling through the thumbnails prompting updates to the selected endpoints.
  39. */
  40. StateListenerRegistry.register(
  41. /* selector */ state => state['features/filmstrip'].visibleRemoteParticipants,
  42. /* listener */ debounce((visibleRemoteParticipants, store) => {
  43. _updateReceiverVideoConstraints(store);
  44. }, 100));
  45. StateListenerRegistry.register(
  46. /* selector */ state => state['features/base/tracks'],
  47. /* listener */(remoteTracks, store) => {
  48. _updateReceiverVideoConstraints(store);
  49. });
  50. /**
  51. * Handles the use case when the on-stage participant has changed.
  52. */
  53. StateListenerRegistry.register(
  54. state => state['features/large-video'].participantId,
  55. (participantId, store) => {
  56. _updateReceiverVideoConstraints(store);
  57. }
  58. );
  59. /**
  60. * Handles the use case when we have set some of the constraints in redux but the conference object wasn't available
  61. * and we haven't been able to pass the constraints to lib-jitsi-meet.
  62. */
  63. StateListenerRegistry.register(
  64. state => state['features/base/conference'].conference,
  65. (conference, store) => {
  66. _updateReceiverVideoConstraints(store);
  67. }
  68. );
  69. /**
  70. * StateListenerRegistry provides a reliable way of detecting changes to
  71. * lastn state and dispatching additional actions.
  72. */
  73. StateListenerRegistry.register(
  74. /* selector */ state => state['features/base/lastn'].lastN,
  75. /* listener */ (lastN, store) => {
  76. _updateReceiverVideoConstraints(store);
  77. });
  78. /**
  79. * Updates the receiver constraints when the stage participants change.
  80. */
  81. StateListenerRegistry.register(
  82. state => getActiveParticipantsIds(state).sort(),
  83. (_, store) => {
  84. _updateReceiverVideoConstraints(store);
  85. }, {
  86. deepEquals: true
  87. }
  88. );
  89. /**
  90. * StateListenerRegistry provides a reliable way of detecting changes to
  91. * maxReceiverVideoQuality* and preferredVideoQuality state and dispatching additional actions.
  92. */
  93. StateListenerRegistry.register(
  94. /* selector */ state => {
  95. const {
  96. maxReceiverVideoQualityForLargeVideo,
  97. maxReceiverVideoQualityForScreenSharingFilmstrip,
  98. maxReceiverVideoQualityForStageFilmstrip,
  99. maxReceiverVideoQualityForTileView,
  100. maxReceiverVideoQualityForVerticalFilmstrip,
  101. preferredVideoQuality
  102. } = state['features/video-quality'];
  103. return {
  104. maxReceiverVideoQualityForLargeVideo,
  105. maxReceiverVideoQualityForScreenSharingFilmstrip,
  106. maxReceiverVideoQualityForStageFilmstrip,
  107. maxReceiverVideoQualityForTileView,
  108. maxReceiverVideoQualityForVerticalFilmstrip,
  109. preferredVideoQuality
  110. };
  111. },
  112. /* listener */ (currentState, store, previousState = {}) => {
  113. const { preferredVideoQuality } = currentState;
  114. const changedPreferredVideoQuality = preferredVideoQuality !== previousState.preferredVideoQuality;
  115. if (changedPreferredVideoQuality) {
  116. _setSenderVideoConstraint(preferredVideoQuality, store);
  117. typeof APP !== 'undefined' && APP.API.notifyVideoQualityChanged(preferredVideoQuality);
  118. }
  119. _updateReceiverVideoConstraints(store);
  120. }, {
  121. deepEquals: true
  122. });
  123. /**
  124. * Implements a state listener in order to calculate max receiver video quality.
  125. */
  126. StateListenerRegistry.register(
  127. /* selector */ state => {
  128. const { reducedUI } = state['features/base/responsive-ui'];
  129. const _shouldDisplayTileView = shouldDisplayTileView(state);
  130. const tileViewThumbnailSize = state['features/filmstrip']?.tileViewDimensions?.thumbnailSize;
  131. const { visibleRemoteParticipants } = state['features/filmstrip'];
  132. const { height: largeVideoHeight } = state['features/large-video'];
  133. const activeParticipantsIds = getActiveParticipantsIds(state);
  134. const {
  135. screenshareFilmstripDimensions: {
  136. thumbnailSize
  137. }
  138. } = state['features/filmstrip'];
  139. const screenshareFilmstripParticipantId = getScreenshareFilmstripParticipantId(state);
  140. return {
  141. activeParticipantsCount: activeParticipantsIds?.length,
  142. displayTileView: _shouldDisplayTileView,
  143. largeVideoHeight,
  144. participantCount: visibleRemoteParticipants?.size || 0,
  145. reducedUI,
  146. screenSharingFilmstripHeight:
  147. screenshareFilmstripParticipantId && getCurrentLayout(state) === LAYOUTS.STAGE_FILMSTRIP_VIEW
  148. ? thumbnailSize?.height : undefined,
  149. stageFilmstripThumbnailHeight: state['features/filmstrip'].stageFilmstripDimensions?.thumbnailSize?.height,
  150. tileViewThumbnailHeight: tileViewThumbnailSize?.height,
  151. verticalFilmstripThumbnailHeight:
  152. state['features/filmstrip'].verticalViewDimensions?.gridView?.thumbnailSize?.height
  153. };
  154. },
  155. /* listener */ ({
  156. activeParticipantsCount,
  157. displayTileView,
  158. largeVideoHeight,
  159. participantCount,
  160. reducedUI,
  161. screenSharingFilmstripHeight,
  162. stageFilmstripThumbnailHeight,
  163. tileViewThumbnailHeight,
  164. verticalFilmstripThumbnailHeight
  165. }, store, previousState = {}) => {
  166. const { dispatch, getState } = store;
  167. const state = getState();
  168. const {
  169. maxReceiverVideoQualityForLargeVideo,
  170. maxReceiverVideoQualityForScreenSharingFilmstrip,
  171. maxReceiverVideoQualityForStageFilmstrip,
  172. maxReceiverVideoQualityForTileView,
  173. maxReceiverVideoQualityForVerticalFilmstrip
  174. } = state['features/video-quality'];
  175. const { maxFullResolutionParticipants = 2 } = state['features/base/config'];
  176. let maxVideoQualityChanged = false;
  177. if (displayTileView) {
  178. let newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.STANDARD;
  179. if (reducedUI) {
  180. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.LOW;
  181. } else if (typeof tileViewThumbnailHeight === 'number' && !Number.isNaN(tileViewThumbnailHeight)) {
  182. newMaxRecvVideoQuality
  183. = getReceiverVideoQualityLevel(tileViewThumbnailHeight, getMinHeightForQualityLvlMap(state));
  184. // Override HD level calculated for the thumbnail height when # of participants threshold is exceeded
  185. if (maxFullResolutionParticipants !== -1) {
  186. const override
  187. = participantCount > maxFullResolutionParticipants
  188. && newMaxRecvVideoQuality > VIDEO_QUALITY_LEVELS.STANDARD;
  189. logger.info(`Video quality level for thumbnail height: ${tileViewThumbnailHeight}, `
  190. + `is: ${newMaxRecvVideoQuality}, `
  191. + `override: ${String(override)}, `
  192. + `max full res N: ${maxFullResolutionParticipants}`);
  193. if (override) {
  194. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.STANDARD;
  195. }
  196. }
  197. }
  198. if (maxReceiverVideoQualityForTileView !== newMaxRecvVideoQuality) {
  199. maxVideoQualityChanged = true;
  200. dispatch(setMaxReceiverVideoQualityForTileView(newMaxRecvVideoQuality));
  201. }
  202. } else {
  203. let newMaxRecvVideoQualityForStageFilmstrip;
  204. let newMaxRecvVideoQualityForVerticalFilmstrip;
  205. let newMaxRecvVideoQualityForLargeVideo;
  206. let newMaxRecvVideoQualityForScreenSharingFilmstrip;
  207. if (reducedUI) {
  208. newMaxRecvVideoQualityForVerticalFilmstrip
  209. = newMaxRecvVideoQualityForStageFilmstrip
  210. = newMaxRecvVideoQualityForLargeVideo
  211. = newMaxRecvVideoQualityForScreenSharingFilmstrip
  212. = VIDEO_QUALITY_LEVELS.LOW;
  213. } else {
  214. newMaxRecvVideoQualityForStageFilmstrip
  215. = getVideoQualityForStageThumbnails(stageFilmstripThumbnailHeight, state);
  216. newMaxRecvVideoQualityForVerticalFilmstrip
  217. = getVideoQualityForResizableFilmstripThumbnails(verticalFilmstripThumbnailHeight, state);
  218. newMaxRecvVideoQualityForLargeVideo = getVideoQualityForLargeVideo(largeVideoHeight);
  219. newMaxRecvVideoQualityForScreenSharingFilmstrip
  220. = getVideoQualityForScreenSharingFilmstrip(screenSharingFilmstripHeight, state);
  221. // Override HD level calculated for the thumbnail height when # of participants threshold is exceeded
  222. if (maxFullResolutionParticipants !== -1) {
  223. if (activeParticipantsCount > 0
  224. && newMaxRecvVideoQualityForStageFilmstrip > VIDEO_QUALITY_LEVELS.STANDARD) {
  225. const isScreenSharingFilmstripParticipantFullResolution
  226. = newMaxRecvVideoQualityForScreenSharingFilmstrip > VIDEO_QUALITY_LEVELS.STANDARD;
  227. if (activeParticipantsCount > maxFullResolutionParticipants
  228. - (isScreenSharingFilmstripParticipantFullResolution ? 1 : 0)) {
  229. newMaxRecvVideoQualityForStageFilmstrip = VIDEO_QUALITY_LEVELS.STANDARD;
  230. newMaxRecvVideoQualityForVerticalFilmstrip
  231. = Math.min(VIDEO_QUALITY_LEVELS.STANDARD, newMaxRecvVideoQualityForVerticalFilmstrip);
  232. } else if (newMaxRecvVideoQualityForVerticalFilmstrip > VIDEO_QUALITY_LEVELS.STANDARD
  233. && participantCount > maxFullResolutionParticipants - activeParticipantsCount) {
  234. newMaxRecvVideoQualityForVerticalFilmstrip = VIDEO_QUALITY_LEVELS.STANDARD;
  235. }
  236. } else if (newMaxRecvVideoQualityForVerticalFilmstrip > VIDEO_QUALITY_LEVELS.STANDARD
  237. && participantCount > maxFullResolutionParticipants
  238. - (newMaxRecvVideoQualityForLargeVideo > VIDEO_QUALITY_LEVELS.STANDARD ? 1 : 0)) {
  239. newMaxRecvVideoQualityForVerticalFilmstrip = VIDEO_QUALITY_LEVELS.STANDARD;
  240. }
  241. }
  242. }
  243. if (maxReceiverVideoQualityForStageFilmstrip !== newMaxRecvVideoQualityForStageFilmstrip) {
  244. maxVideoQualityChanged = true;
  245. dispatch(setMaxReceiverVideoQualityForStageFilmstrip(newMaxRecvVideoQualityForStageFilmstrip));
  246. }
  247. if (maxReceiverVideoQualityForVerticalFilmstrip !== newMaxRecvVideoQualityForVerticalFilmstrip) {
  248. maxVideoQualityChanged = true;
  249. dispatch(setMaxReceiverVideoQualityForVerticalFilmstrip(newMaxRecvVideoQualityForVerticalFilmstrip));
  250. }
  251. if (maxReceiverVideoQualityForLargeVideo !== newMaxRecvVideoQualityForLargeVideo) {
  252. maxVideoQualityChanged = true;
  253. dispatch(setMaxReceiverVideoQualityForLargeVideo(newMaxRecvVideoQualityForLargeVideo));
  254. }
  255. if (maxReceiverVideoQualityForScreenSharingFilmstrip !== newMaxRecvVideoQualityForScreenSharingFilmstrip) {
  256. maxVideoQualityChanged = true;
  257. dispatch(
  258. setMaxReceiverVideoQualityForScreenSharingFilmstrip(
  259. newMaxRecvVideoQualityForScreenSharingFilmstrip));
  260. }
  261. }
  262. if (!maxVideoQualityChanged && Boolean(displayTileView) !== Boolean(previousState.displayTileView)) {
  263. _updateReceiverVideoConstraints(store);
  264. }
  265. }, {
  266. deepEquals: true
  267. });
  268. /**
  269. * Helper function for updating the preferred sender video constraint, based on the user preference.
  270. *
  271. * @param {number} preferred - The user preferred max frame height.
  272. * @returns {void}
  273. */
  274. function _setSenderVideoConstraint(preferred: number, { getState }: IStore) {
  275. const state = getState();
  276. const { conference } = state['features/base/conference'];
  277. if (!conference) {
  278. return;
  279. }
  280. logger.info(`Setting sender resolution to ${preferred}`);
  281. conference.setSenderVideoConstraint(preferred)
  282. .catch((error: any) => {
  283. _handleParticipantError(error);
  284. reportError(error, `Changing sender resolution to ${preferred} failed.`);
  285. });
  286. }
  287. /**
  288. * Private helper to calculate the receiver video constraints and set them on the bridge channel.
  289. *
  290. * @param {*} store - The redux store.
  291. * @returns {void}
  292. */
  293. function _updateReceiverVideoConstraints({ getState }: IStore) {
  294. const state = getState();
  295. const { conference } = state['features/base/conference'];
  296. if (!conference) {
  297. return;
  298. }
  299. const { lastN } = state['features/base/lastn'];
  300. const {
  301. maxReceiverVideoQualityForTileView,
  302. maxReceiverVideoQualityForStageFilmstrip,
  303. maxReceiverVideoQualityForVerticalFilmstrip,
  304. maxReceiverVideoQualityForLargeVideo,
  305. maxReceiverVideoQualityForScreenSharingFilmstrip,
  306. preferredVideoQuality
  307. } = state['features/video-quality'];
  308. const { participantId: largeVideoParticipantId = '' } = state['features/large-video'];
  309. const maxFrameHeightForTileView = Math.min(maxReceiverVideoQualityForTileView, preferredVideoQuality);
  310. const maxFrameHeightForStageFilmstrip = Math.min(maxReceiverVideoQualityForStageFilmstrip, preferredVideoQuality);
  311. const maxFrameHeightForVerticalFilmstrip
  312. = Math.min(maxReceiverVideoQualityForVerticalFilmstrip, preferredVideoQuality);
  313. const maxFrameHeightForLargeVideo
  314. = Math.min(maxReceiverVideoQualityForLargeVideo, preferredVideoQuality);
  315. const maxFrameHeightForScreenSharingFilmstrip
  316. = Math.min(maxReceiverVideoQualityForScreenSharingFilmstrip, preferredVideoQuality);
  317. const { remoteScreenShares } = state['features/video-layout'];
  318. const { visibleRemoteParticipants } = state['features/filmstrip'];
  319. const tracks = state['features/base/tracks'];
  320. const localParticipantId = getLocalParticipant(state)?.id;
  321. const activeParticipantsIds = getActiveParticipantsIds(state);
  322. const screenshareFilmstripParticipantId = isTopPanelEnabled(state) && getScreenshareFilmstripParticipantId(state);
  323. const receiverConstraints: any = {
  324. constraints: {},
  325. defaultConstraints: { 'maxHeight': VIDEO_QUALITY_LEVELS.NONE },
  326. lastN
  327. };
  328. const activeParticipantsSources: string[] = [];
  329. const visibleRemoteTrackSourceNames: string[] = [];
  330. let largeVideoSourceName: string | undefined;
  331. receiverConstraints.onStageSources = [];
  332. receiverConstraints.selectedSources = [];
  333. if (visibleRemoteParticipants?.size) {
  334. visibleRemoteParticipants.forEach(participantId => {
  335. let sourceName;
  336. if (remoteScreenShares.includes(participantId)) {
  337. sourceName = participantId;
  338. } else {
  339. sourceName = getTrackSourceNameByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  340. }
  341. if (sourceName) {
  342. visibleRemoteTrackSourceNames.push(sourceName);
  343. }
  344. });
  345. }
  346. if (activeParticipantsIds?.length > 0) {
  347. activeParticipantsIds.forEach((participantId: string) => {
  348. let sourceName;
  349. if (remoteScreenShares.includes(participantId)) {
  350. sourceName = participantId;
  351. } else {
  352. sourceName = getTrackSourceNameByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  353. }
  354. if (sourceName) {
  355. activeParticipantsSources.push(sourceName);
  356. }
  357. });
  358. }
  359. if (localParticipantId !== largeVideoParticipantId) {
  360. if (remoteScreenShares.includes(largeVideoParticipantId)) {
  361. largeVideoSourceName = largeVideoParticipantId;
  362. } else {
  363. largeVideoSourceName = getTrackSourceNameByMediaTypeAndParticipant(
  364. tracks, MEDIA_TYPE.VIDEO, largeVideoParticipantId
  365. );
  366. }
  367. }
  368. // Tile view.
  369. if (shouldDisplayTileView(state)) {
  370. if (!visibleRemoteTrackSourceNames?.length) {
  371. return;
  372. }
  373. visibleRemoteTrackSourceNames.forEach(sourceName => {
  374. receiverConstraints.constraints[sourceName] = { 'maxHeight': maxFrameHeightForTileView };
  375. });
  376. // Prioritize screenshare in tile view.
  377. if (remoteScreenShares?.length) {
  378. receiverConstraints.selectedSources = remoteScreenShares;
  379. }
  380. // Stage view.
  381. } else {
  382. if (!visibleRemoteTrackSourceNames?.length && !largeVideoSourceName && !activeParticipantsSources?.length) {
  383. return;
  384. }
  385. if (visibleRemoteTrackSourceNames?.length) {
  386. visibleRemoteTrackSourceNames.forEach(sourceName => {
  387. receiverConstraints.constraints[sourceName] = { 'maxHeight': maxFrameHeightForVerticalFilmstrip };
  388. });
  389. }
  390. if (getCurrentLayout(state) === LAYOUTS.STAGE_FILMSTRIP_VIEW && activeParticipantsSources.length > 0) {
  391. const onStageSources = [ ...activeParticipantsSources ];
  392. activeParticipantsSources.forEach(sourceName => {
  393. const isScreenSharing = remoteScreenShares.includes(sourceName);
  394. const quality
  395. = isScreenSharing && preferredVideoQuality >= MAX_VIDEO_QUALITY
  396. ? VIDEO_QUALITY_UNLIMITED : maxFrameHeightForStageFilmstrip;
  397. receiverConstraints.constraints[sourceName] = { 'maxHeight': quality };
  398. });
  399. if (screenshareFilmstripParticipantId) {
  400. onStageSources.push(screenshareFilmstripParticipantId);
  401. receiverConstraints.constraints[screenshareFilmstripParticipantId]
  402. = {
  403. 'maxHeight':
  404. preferredVideoQuality >= MAX_VIDEO_QUALITY
  405. ? VIDEO_QUALITY_UNLIMITED : maxFrameHeightForScreenSharingFilmstrip
  406. };
  407. }
  408. receiverConstraints.onStageSources = onStageSources;
  409. } else if (largeVideoSourceName) {
  410. let quality = VIDEO_QUALITY_UNLIMITED;
  411. if (preferredVideoQuality < MAX_VIDEO_QUALITY
  412. || !remoteScreenShares.find(id => id === largeVideoParticipantId)) {
  413. quality = maxFrameHeightForLargeVideo;
  414. }
  415. receiverConstraints.constraints[largeVideoSourceName] = { 'maxHeight': quality };
  416. receiverConstraints.onStageSources = [ largeVideoSourceName ];
  417. }
  418. }
  419. try {
  420. conference.setReceiverConstraints(receiverConstraints);
  421. } catch (error: any) {
  422. _handleParticipantError(error);
  423. reportError(error, `Failed to set receiver video constraints ${JSON.stringify(receiverConstraints)}`);
  424. }
  425. }