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.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // @flow
  2. import debounce from 'lodash/debounce';
  3. import { _handleParticipantError } from '../base/conference';
  4. import { getSourceNameSignalingFeatureFlag } from '../base/config';
  5. import { MEDIA_TYPE } from '../base/media';
  6. import { getLocalParticipant, getParticipantCount } from '../base/participants';
  7. import { StateListenerRegistry } from '../base/redux';
  8. import { getTrackSourceNameByMediaTypeAndParticipant } from '../base/tracks';
  9. import { reportError } from '../base/util';
  10. import { getActiveParticipantsIds } from '../filmstrip/functions.web';
  11. import {
  12. getVideoQualityForLargeVideo,
  13. getVideoQualityForResizableFilmstripThumbnails,
  14. getVideoQualityForStageThumbnails,
  15. shouldDisplayTileView
  16. } from '../video-layout';
  17. import { setMaxReceiverVideoQuality } from './actions';
  18. import { VIDEO_QUALITY_LEVELS } from './constants';
  19. import { getReceiverVideoQualityLevel } from './functions';
  20. import logger from './logger';
  21. import { getMinHeightForQualityLvlMap } from './selector';
  22. declare var APP: Object;
  23. /**
  24. * Handles changes in the visible participants in the filmstrip. The listener is debounced
  25. * so that the client doesn't end up sending too many bridge messages when the user is
  26. * scrolling through the thumbnails prompting updates to the selected endpoints.
  27. */
  28. StateListenerRegistry.register(
  29. /* selector */ state => state['features/filmstrip'].visibleRemoteParticipants,
  30. /* listener */ debounce((visibleRemoteParticipants, store) => {
  31. _updateReceiverVideoConstraints(store);
  32. }, 100));
  33. StateListenerRegistry.register(
  34. /* selector */ state => state['features/base/tracks'],
  35. /* listener */(remoteTracks, store) => {
  36. _updateReceiverVideoConstraints(store);
  37. });
  38. /**
  39. * Handles the use case when the on-stage participant has changed.
  40. */
  41. StateListenerRegistry.register(
  42. state => state['features/large-video'].participantId,
  43. (participantId, store) => {
  44. _updateReceiverVideoConstraints(store);
  45. }
  46. );
  47. /**
  48. * Handles the use case when we have set some of the constraints in redux but the conference object wasn't available
  49. * and we haven't been able to pass the constraints to lib-jitsi-meet.
  50. */
  51. StateListenerRegistry.register(
  52. state => state['features/base/conference'].conference,
  53. (conference, store) => {
  54. _updateReceiverVideoConstraints(store);
  55. }
  56. );
  57. /**
  58. * Updates the receiver constraints when the layout changes. When we are in stage view we need to handle the
  59. * on-stage participant differently.
  60. */
  61. StateListenerRegistry.register(
  62. /* selector */ state => state['features/video-layout'].tileViewEnabled,
  63. /* listener */ (tileViewEnabled, store) => {
  64. _updateReceiverVideoConstraints(store);
  65. }
  66. );
  67. /**
  68. * StateListenerRegistry provides a reliable way of detecting changes to
  69. * lastn state and dispatching additional actions.
  70. */
  71. StateListenerRegistry.register(
  72. /* selector */ state => state['features/base/lastn'].lastN,
  73. /* listener */ (lastN, store) => {
  74. _updateReceiverVideoConstraints(store);
  75. });
  76. /**
  77. * Updates the receiver constraints when the tiles in the resizable filmstrip change dimensions.
  78. */
  79. StateListenerRegistry.register(
  80. state => getVideoQualityForResizableFilmstripThumbnails(state),
  81. (_, store) => {
  82. _updateReceiverVideoConstraints(store);
  83. }
  84. );
  85. /**
  86. * Updates the receiver constraints when the stage participants change.
  87. */
  88. StateListenerRegistry.register(
  89. state => getActiveParticipantsIds(state).sort()
  90. .join(),
  91. (_, store) => {
  92. _updateReceiverVideoConstraints(store);
  93. }
  94. );
  95. /**
  96. * StateListenerRegistry provides a reliable way of detecting changes to
  97. * maxReceiverVideoQuality and preferredVideoQuality state and dispatching additional actions.
  98. */
  99. StateListenerRegistry.register(
  100. /* selector */ state => {
  101. const {
  102. maxReceiverVideoQuality,
  103. preferredVideoQuality
  104. } = state['features/video-quality'];
  105. return {
  106. maxReceiverVideoQuality,
  107. preferredVideoQuality
  108. };
  109. },
  110. /* listener */ (currentState, store, previousState = {}) => {
  111. const { maxReceiverVideoQuality, preferredVideoQuality } = currentState;
  112. const changedPreferredVideoQuality = preferredVideoQuality !== previousState.preferredVideoQuality;
  113. const changedReceiverVideoQuality = maxReceiverVideoQuality !== previousState.maxReceiverVideoQuality;
  114. if (changedPreferredVideoQuality) {
  115. _setSenderVideoConstraint(preferredVideoQuality, store);
  116. typeof APP !== 'undefined' && APP.API.notifyVideoQualityChanged(preferredVideoQuality);
  117. }
  118. changedReceiverVideoQuality && _updateReceiverVideoConstraints(store);
  119. }, {
  120. deepEquals: true
  121. });
  122. /**
  123. * Implements a state listener in order to calculate max receiver video quality.
  124. */
  125. StateListenerRegistry.register(
  126. /* selector */ state => {
  127. const { reducedUI } = state['features/base/responsive-ui'];
  128. const _shouldDisplayTileView = shouldDisplayTileView(state);
  129. const thumbnailSize = state['features/filmstrip']?.tileViewDimensions?.thumbnailSize;
  130. const participantCount = getParticipantCount(state);
  131. return {
  132. displayTileView: _shouldDisplayTileView,
  133. participantCount,
  134. reducedUI,
  135. thumbnailHeight: thumbnailSize?.height
  136. };
  137. },
  138. /* listener */ ({ displayTileView, participantCount, reducedUI, thumbnailHeight }, { dispatch, getState }) => {
  139. const state = getState();
  140. const { maxReceiverVideoQuality } = state['features/video-quality'];
  141. const { maxFullResolutionParticipants = 2 } = state['features/base/config'];
  142. let newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.ULTRA;
  143. if (reducedUI) {
  144. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.LOW;
  145. } else if (displayTileView && !Number.isNaN(thumbnailHeight)) {
  146. newMaxRecvVideoQuality = getReceiverVideoQualityLevel(thumbnailHeight, getMinHeightForQualityLvlMap(state));
  147. // Override HD level calculated for the thumbnail height when # of participants threshold is exceeded
  148. if (maxReceiverVideoQuality !== newMaxRecvVideoQuality && maxFullResolutionParticipants !== -1) {
  149. const override
  150. = participantCount > maxFullResolutionParticipants
  151. && newMaxRecvVideoQuality > VIDEO_QUALITY_LEVELS.STANDARD;
  152. logger.info(`Video quality level for thumbnail height: ${thumbnailHeight}, `
  153. + `is: ${newMaxRecvVideoQuality}, `
  154. + `override: ${String(override)}, `
  155. + `max full res N: ${maxFullResolutionParticipants}`);
  156. if (override) {
  157. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.STANDARD;
  158. }
  159. }
  160. }
  161. if (maxReceiverVideoQuality !== newMaxRecvVideoQuality) {
  162. dispatch(setMaxReceiverVideoQuality(newMaxRecvVideoQuality));
  163. }
  164. }, {
  165. deepEquals: true
  166. });
  167. /**
  168. * Helper function for updating the preferred sender video constraint, based on the user preference.
  169. *
  170. * @param {number} preferred - The user preferred max frame height.
  171. * @returns {void}
  172. */
  173. function _setSenderVideoConstraint(preferred, { getState }) {
  174. const state = getState();
  175. const { conference } = state['features/base/conference'];
  176. if (!conference) {
  177. return;
  178. }
  179. logger.info(`Setting sender resolution to ${preferred}`);
  180. conference.setSenderVideoConstraint(preferred)
  181. .catch(error => {
  182. _handleParticipantError(error);
  183. reportError(error, `Changing sender resolution to ${preferred} failed.`);
  184. });
  185. }
  186. /**
  187. * Private helper to calculate the receiver video constraints and set them on the bridge channel.
  188. *
  189. * @param {*} store - The redux store.
  190. * @returns {void}
  191. */
  192. function _updateReceiverVideoConstraints({ getState }) {
  193. const state = getState();
  194. const { conference } = state['features/base/conference'];
  195. if (!conference) {
  196. return;
  197. }
  198. const { lastN } = state['features/base/lastn'];
  199. const { maxReceiverVideoQuality, preferredVideoQuality } = state['features/video-quality'];
  200. const { participantId: largeVideoParticipantId } = state['features/large-video'];
  201. const maxFrameHeight = Math.min(maxReceiverVideoQuality, preferredVideoQuality);
  202. const { remoteScreenShares } = state['features/video-layout'];
  203. const { visibleRemoteParticipants } = state['features/filmstrip'];
  204. const tracks = state['features/base/tracks'];
  205. const sourceNameSignaling = getSourceNameSignalingFeatureFlag(state);
  206. const localParticipantId = getLocalParticipant(state).id;
  207. const activeParticipantsIds = getActiveParticipantsIds(state);
  208. let receiverConstraints;
  209. if (sourceNameSignaling) {
  210. receiverConstraints = {
  211. constraints: {},
  212. defaultConstraints: { 'maxHeight': VIDEO_QUALITY_LEVELS.NONE },
  213. lastN,
  214. onStageSources: [],
  215. selectedSources: []
  216. };
  217. const visibleRemoteTrackSourceNames = [];
  218. let largeVideoSourceName;
  219. const activeParticipantsSources = [];
  220. if (visibleRemoteParticipants?.size) {
  221. visibleRemoteParticipants.forEach(participantId => {
  222. let sourceName;
  223. if (remoteScreenShares.includes(participantId)) {
  224. sourceName = participantId;
  225. } else {
  226. sourceName = getTrackSourceNameByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  227. }
  228. if (sourceName) {
  229. visibleRemoteTrackSourceNames.push(sourceName);
  230. if (activeParticipantsIds.find(id => id === participantId)) {
  231. activeParticipantsSources.push(sourceName);
  232. }
  233. }
  234. });
  235. }
  236. if (localParticipantId !== largeVideoParticipantId) {
  237. if (remoteScreenShares.includes(largeVideoParticipantId)) {
  238. largeVideoSourceName = largeVideoParticipantId;
  239. } else {
  240. largeVideoSourceName = getTrackSourceNameByMediaTypeAndParticipant(
  241. tracks, MEDIA_TYPE.VIDEO,
  242. largeVideoParticipantId
  243. );
  244. }
  245. }
  246. // Tile view.
  247. if (shouldDisplayTileView(state)) {
  248. if (!visibleRemoteTrackSourceNames?.length) {
  249. return;
  250. }
  251. visibleRemoteTrackSourceNames.forEach(sourceName => {
  252. receiverConstraints.constraints[sourceName] = { 'maxHeight': maxFrameHeight };
  253. });
  254. // Prioritize screenshare in tile view.
  255. if (remoteScreenShares?.length) {
  256. receiverConstraints.selectedSources = remoteScreenShares;
  257. }
  258. // Stage view.
  259. } else {
  260. if (!visibleRemoteTrackSourceNames?.length && !largeVideoSourceName) {
  261. return;
  262. }
  263. if (visibleRemoteTrackSourceNames?.length) {
  264. const qualityLevel = getVideoQualityForResizableFilmstripThumbnails(state);
  265. const stageParticipantsLevel = getVideoQualityForStageThumbnails(state);
  266. visibleRemoteTrackSourceNames.forEach(sourceName => {
  267. const isStageParticipant = activeParticipantsSources.find(name => name === sourceName);
  268. const quality = Math.min(maxFrameHeight, isStageParticipant
  269. ? stageParticipantsLevel : qualityLevel);
  270. receiverConstraints.constraints[sourceName] = { 'maxHeight': quality };
  271. });
  272. }
  273. if (largeVideoSourceName) {
  274. let quality = maxFrameHeight;
  275. if (navigator.product !== 'ReactNative'
  276. && !remoteScreenShares.find(id => id === largeVideoParticipantId)) {
  277. quality = getVideoQualityForLargeVideo();
  278. }
  279. receiverConstraints.constraints[largeVideoSourceName] = { 'maxHeight': quality };
  280. receiverConstraints.onStageSources = [ largeVideoSourceName ];
  281. }
  282. }
  283. } else {
  284. receiverConstraints = {
  285. constraints: {},
  286. defaultConstraints: { 'maxHeight': VIDEO_QUALITY_LEVELS.NONE },
  287. lastN,
  288. onStageEndpoints: [],
  289. selectedEndpoints: []
  290. };
  291. // Tile view.
  292. if (shouldDisplayTileView(state)) {
  293. if (!visibleRemoteParticipants?.size) {
  294. return;
  295. }
  296. visibleRemoteParticipants.forEach(participantId => {
  297. receiverConstraints.constraints[participantId] = { 'maxHeight': maxFrameHeight };
  298. });
  299. // Prioritize screenshare in tile view.
  300. remoteScreenShares?.length && (receiverConstraints.selectedEndpoints = remoteScreenShares);
  301. // Stage view.
  302. } else {
  303. if (!visibleRemoteParticipants?.size && !largeVideoParticipantId) {
  304. return;
  305. }
  306. if (visibleRemoteParticipants?.size > 0) {
  307. const qualityLevel = getVideoQualityForResizableFilmstripThumbnails(state);
  308. const stageParticipantsLevel = getVideoQualityForStageThumbnails(state);
  309. visibleRemoteParticipants.forEach(participantId => {
  310. const isStageParticipant = activeParticipantsIds.find(id => id === participantId);
  311. const quality = Math.min(maxFrameHeight, isStageParticipant
  312. ? stageParticipantsLevel : qualityLevel);
  313. receiverConstraints.constraints[participantId] = { 'maxHeight': quality };
  314. });
  315. }
  316. if (largeVideoParticipantId) {
  317. let quality = maxFrameHeight;
  318. if (navigator.product !== 'ReactNative'
  319. && !remoteScreenShares.find(id => id === largeVideoParticipantId)) {
  320. quality = getVideoQualityForLargeVideo();
  321. }
  322. receiverConstraints.constraints[largeVideoParticipantId] = { 'maxHeight': quality };
  323. receiverConstraints.onStageEndpoints = [ largeVideoParticipantId ];
  324. }
  325. }
  326. }
  327. try {
  328. conference.setReceiverConstraints(receiverConstraints);
  329. } catch (error) {
  330. _handleParticipantError(error);
  331. reportError(error, `Failed to set receiver video constraints ${JSON.stringify(receiverConstraints)}`);
  332. }
  333. }