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

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