Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

subscriber.js 15KB

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