選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

subscriber.js 12KB

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