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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 { getRemoteScreenSharesSourceNames, 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. (_, store) => {
  91. _updateReceiverVideoConstraints(store);
  92. }, {
  93. deepEquals: true
  94. }
  95. );
  96. /**
  97. * StateListenerRegistry provides a reliable way of detecting changes to
  98. * maxReceiverVideoQuality and preferredVideoQuality state and dispatching additional actions.
  99. */
  100. StateListenerRegistry.register(
  101. /* selector */ state => {
  102. const {
  103. maxReceiverVideoQuality,
  104. preferredVideoQuality
  105. } = state['features/video-quality'];
  106. return {
  107. maxReceiverVideoQuality,
  108. preferredVideoQuality
  109. };
  110. },
  111. /* listener */ (currentState, store, previousState = {}) => {
  112. const { maxReceiverVideoQuality, preferredVideoQuality } = currentState;
  113. const changedPreferredVideoQuality = preferredVideoQuality !== previousState.preferredVideoQuality;
  114. const changedReceiverVideoQuality = maxReceiverVideoQuality !== previousState.maxReceiverVideoQuality;
  115. if (changedPreferredVideoQuality) {
  116. _setSenderVideoConstraint(preferredVideoQuality, store);
  117. typeof APP !== 'undefined' && APP.API.notifyVideoQualityChanged(preferredVideoQuality);
  118. }
  119. changedReceiverVideoQuality && _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 thumbnailSize = state['features/filmstrip']?.tileViewDimensions?.thumbnailSize;
  131. const participantCount = getParticipantCount(state);
  132. return {
  133. displayTileView: _shouldDisplayTileView,
  134. participantCount,
  135. reducedUI,
  136. thumbnailHeight: thumbnailSize?.height
  137. };
  138. },
  139. /* listener */ ({ displayTileView, participantCount, reducedUI, thumbnailHeight }, { dispatch, getState }) => {
  140. const state = getState();
  141. const { maxReceiverVideoQuality } = state['features/video-quality'];
  142. const { maxFullResolutionParticipants = 2 } = state['features/base/config'];
  143. let newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.ULTRA;
  144. if (reducedUI) {
  145. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.LOW;
  146. } else if (displayTileView && !Number.isNaN(thumbnailHeight)) {
  147. newMaxRecvVideoQuality = getReceiverVideoQualityLevel(thumbnailHeight, getMinHeightForQualityLvlMap(state));
  148. // Override HD level calculated for the thumbnail height when # of participants threshold is exceeded
  149. if (maxReceiverVideoQuality !== newMaxRecvVideoQuality && maxFullResolutionParticipants !== -1) {
  150. const override
  151. = participantCount > maxFullResolutionParticipants
  152. && newMaxRecvVideoQuality > VIDEO_QUALITY_LEVELS.STANDARD;
  153. logger.info(`Video quality level for thumbnail height: ${thumbnailHeight}, `
  154. + `is: ${newMaxRecvVideoQuality}, `
  155. + `override: ${String(override)}, `
  156. + `max full res N: ${maxFullResolutionParticipants}`);
  157. if (override) {
  158. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.STANDARD;
  159. }
  160. }
  161. }
  162. if (maxReceiverVideoQuality !== newMaxRecvVideoQuality) {
  163. dispatch(setMaxReceiverVideoQuality(newMaxRecvVideoQuality));
  164. }
  165. }, {
  166. deepEquals: true
  167. });
  168. /**
  169. * Helper function for updating the preferred sender video constraint, based on the user preference.
  170. *
  171. * @param {number} preferred - The user preferred max frame height.
  172. * @returns {void}
  173. */
  174. function _setSenderVideoConstraint(preferred, { getState }) {
  175. const state = getState();
  176. const { conference } = state['features/base/conference'];
  177. if (!conference) {
  178. return;
  179. }
  180. logger.info(`Setting sender resolution to ${preferred}`);
  181. conference.setSenderVideoConstraint(preferred)
  182. .catch(error => {
  183. _handleParticipantError(error);
  184. reportError(error, `Changing sender resolution to ${preferred} failed.`);
  185. });
  186. }
  187. /**
  188. * Private helper to calculate the receiver video constraints and set them on the bridge channel.
  189. *
  190. * @param {*} store - The redux store.
  191. * @returns {void}
  192. */
  193. function _updateReceiverVideoConstraints({ getState }) {
  194. const state = getState();
  195. const { conference } = state['features/base/conference'];
  196. if (!conference) {
  197. return;
  198. }
  199. const { lastN } = state['features/base/lastn'];
  200. const { maxReceiverVideoQuality, preferredVideoQuality } = state['features/video-quality'];
  201. const { participantId: largeVideoParticipantId } = state['features/large-video'];
  202. const maxFrameHeight = Math.min(maxReceiverVideoQuality, preferredVideoQuality);
  203. const { remoteScreenShares } = state['features/video-layout'];
  204. const { visibleRemoteParticipants } = state['features/filmstrip'];
  205. const tracks = state['features/base/tracks'];
  206. const sourceNameSignaling = getSourceNameSignalingFeatureFlag(state);
  207. const localParticipantId = getLocalParticipant(state).id;
  208. const activeParticipantsIds = getActiveParticipantsIds(state);
  209. let receiverConstraints;
  210. if (sourceNameSignaling) {
  211. const remoteScreenSharesSourceNames = getRemoteScreenSharesSourceNames(state, remoteScreenShares);
  212. receiverConstraints = {
  213. constraints: {},
  214. defaultConstraints: { 'maxHeight': VIDEO_QUALITY_LEVELS.NONE },
  215. lastN,
  216. onStageSources: [],
  217. selectedSources: []
  218. };
  219. const visibleRemoteTrackSourceNames = [];
  220. let largeVideoSourceName;
  221. const activeParticipantsSources = [];
  222. if (visibleRemoteParticipants?.size) {
  223. visibleRemoteParticipants.forEach(participantId => {
  224. let sourceName;
  225. if (remoteScreenSharesSourceNames.includes(participantId)) {
  226. sourceName = participantId;
  227. } else {
  228. sourceName = getTrackSourceNameByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  229. }
  230. if (sourceName) {
  231. visibleRemoteTrackSourceNames.push(sourceName);
  232. if (activeParticipantsIds.find(id => id === participantId)) {
  233. activeParticipantsSources.push(sourceName);
  234. }
  235. }
  236. });
  237. }
  238. if (localParticipantId !== largeVideoParticipantId) {
  239. if (remoteScreenSharesSourceNames.includes(largeVideoParticipantId)) {
  240. largeVideoSourceName = largeVideoParticipantId;
  241. } else {
  242. largeVideoSourceName = getTrackSourceNameByMediaTypeAndParticipant(
  243. tracks, MEDIA_TYPE.VIDEO, largeVideoParticipantId
  244. );
  245. }
  246. }
  247. // Tile view.
  248. if (shouldDisplayTileView(state)) {
  249. if (!visibleRemoteTrackSourceNames?.length) {
  250. return;
  251. }
  252. visibleRemoteTrackSourceNames.forEach(sourceName => {
  253. receiverConstraints.constraints[sourceName] = { 'maxHeight': maxFrameHeight };
  254. });
  255. // Prioritize screenshare in tile view.
  256. if (remoteScreenSharesSourceNames?.length) {
  257. receiverConstraints.selectedSources = remoteScreenSharesSourceNames;
  258. }
  259. // Stage view.
  260. } else {
  261. if (!visibleRemoteTrackSourceNames?.length && !largeVideoSourceName) {
  262. return;
  263. }
  264. if (visibleRemoteTrackSourceNames?.length) {
  265. const qualityLevel = getVideoQualityForResizableFilmstripThumbnails(state);
  266. const stageParticipantsLevel = getVideoQualityForStageThumbnails(state);
  267. visibleRemoteTrackSourceNames.forEach(sourceName => {
  268. const isStageParticipant = activeParticipantsSources.find(name => name === sourceName);
  269. const quality = Math.min(maxFrameHeight, isStageParticipant
  270. ? stageParticipantsLevel : qualityLevel);
  271. receiverConstraints.constraints[sourceName] = { 'maxHeight': quality };
  272. });
  273. }
  274. if (largeVideoSourceName) {
  275. let quality = maxFrameHeight;
  276. if (navigator.product !== 'ReactNative'
  277. && !remoteScreenShares.find(id => id === largeVideoParticipantId)) {
  278. quality = getVideoQualityForLargeVideo();
  279. }
  280. receiverConstraints.constraints[largeVideoSourceName] = { 'maxHeight': quality };
  281. receiverConstraints.onStageSources = [ largeVideoSourceName ];
  282. }
  283. }
  284. if (remoteScreenSharesSourceNames?.length) {
  285. remoteScreenSharesSourceNames.forEach(sourceName => {
  286. receiverConstraints.constraints[sourceName] = { 'maxHeight': VIDEO_QUALITY_LEVELS.ULTRA };
  287. });
  288. }
  289. } else {
  290. receiverConstraints = {
  291. constraints: {},
  292. defaultConstraints: { 'maxHeight': VIDEO_QUALITY_LEVELS.NONE },
  293. lastN,
  294. onStageEndpoints: [],
  295. selectedEndpoints: []
  296. };
  297. // Tile view.
  298. if (shouldDisplayTileView(state)) {
  299. if (!visibleRemoteParticipants?.size) {
  300. return;
  301. }
  302. visibleRemoteParticipants.forEach(participantId => {
  303. receiverConstraints.constraints[participantId] = { 'maxHeight': maxFrameHeight };
  304. });
  305. // Prioritize screenshare in tile view.
  306. remoteScreenShares?.length && (receiverConstraints.selectedEndpoints = remoteScreenShares);
  307. // Stage view.
  308. } else {
  309. if (!visibleRemoteParticipants?.size && !largeVideoParticipantId) {
  310. return;
  311. }
  312. if (visibleRemoteParticipants?.size > 0) {
  313. const qualityLevel = getVideoQualityForResizableFilmstripThumbnails(state);
  314. const stageParticipantsLevel = getVideoQualityForStageThumbnails(state);
  315. visibleRemoteParticipants.forEach(participantId => {
  316. const isStageParticipant = activeParticipantsIds.find(id => id === participantId);
  317. const quality = Math.min(maxFrameHeight, isStageParticipant
  318. ? stageParticipantsLevel : qualityLevel);
  319. receiverConstraints.constraints[participantId] = { 'maxHeight': quality };
  320. });
  321. }
  322. if (largeVideoParticipantId) {
  323. let quality = maxFrameHeight;
  324. if (navigator.product !== 'ReactNative'
  325. && !remoteScreenShares.find(id => id === largeVideoParticipantId)) {
  326. quality = getVideoQualityForLargeVideo();
  327. }
  328. receiverConstraints.constraints[largeVideoParticipantId] = { 'maxHeight': quality };
  329. receiverConstraints.onStageEndpoints = [ largeVideoParticipantId ];
  330. }
  331. }
  332. }
  333. try {
  334. conference.setReceiverConstraints(receiverConstraints);
  335. } catch (error) {
  336. _handleParticipantError(error);
  337. reportError(error, `Failed to set receiver video constraints ${JSON.stringify(receiverConstraints)}`);
  338. }
  339. }