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.

actions.any.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { MEDIA_TYPE } from '../base/media';
  4. import {
  5. SELECT_LARGE_VIDEO_PARTICIPANT,
  6. UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION
  7. } from './actionTypes';
  8. /**
  9. * Action to select the participant to be displayed in LargeVideo based on the
  10. * participant id provided. If a participant id is not provided, the LargeVideo
  11. * participant will be selected based on a variety of factors: If there is a
  12. * dominant or pinned speaker, or if there are remote tracks, etc.
  13. *
  14. * @param {string} participant - The participant id of the user that needs to be
  15. * displayed on the large video.
  16. * @returns {Function}
  17. */
  18. export function selectParticipantInLargeVideo(participant: ?string) {
  19. return (dispatch: Dispatch<any>, getState: Function) => {
  20. const state = getState();
  21. const participantId = participant ?? _electParticipantInLargeVideo(state);
  22. const largeVideo = state['features/large-video'];
  23. const remoteScreenShares = state['features/video-layout'].remoteScreenShares;
  24. let latestScreenshareParticipantId;
  25. if (remoteScreenShares && remoteScreenShares.length) {
  26. latestScreenshareParticipantId = remoteScreenShares[remoteScreenShares.length - 1];
  27. }
  28. // When trying to auto pin screenshare, always select the endpoint even though it happens to be
  29. // the large video participant in redux (for the reasons listed above in the large video selection
  30. // logic above). The auto pin screenshare logic kicks in after the track is added
  31. // (which updates the large video participant and selects all endpoints because of the auto tile
  32. // view mode). If the screenshare endpoint is not among the forwarded endpoints from the bridge,
  33. // it needs to be selected again at this point.
  34. if (participantId !== largeVideo.participantId || participantId === latestScreenshareParticipantId) {
  35. dispatch({
  36. type: SELECT_LARGE_VIDEO_PARTICIPANT,
  37. participantId
  38. });
  39. }
  40. };
  41. }
  42. /**
  43. * Updates the currently seen resolution of the video displayed on large video.
  44. *
  45. * @param {number} resolution - The current resolution (height) of the video.
  46. * @returns {{
  47. * type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  48. * resolution: number
  49. * }}
  50. */
  51. export function updateKnownLargeVideoResolution(resolution: number) {
  52. return {
  53. type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  54. resolution
  55. };
  56. }
  57. /**
  58. * Returns the most recent existing remote video track.
  59. *
  60. * @param {Track[]} tracks - All current tracks.
  61. * @private
  62. * @returns {(Track|undefined)}
  63. */
  64. function _electLastVisibleRemoteVideo(tracks) {
  65. // First we try to get most recent remote video track.
  66. for (let i = tracks.length - 1; i >= 0; --i) {
  67. const track = tracks[i];
  68. if (!track.local && track.mediaType === MEDIA_TYPE.VIDEO) {
  69. return track;
  70. }
  71. }
  72. }
  73. /**
  74. * Returns the identifier of the participant who is to be on the stage and
  75. * should be displayed in {@code LargeVideo}.
  76. *
  77. * @param {Object} state - The Redux state from which the participant to be
  78. * displayed in {@code LargeVideo} is to be elected.
  79. * @private
  80. * @returns {(string|undefined)}
  81. */
  82. function _electParticipantInLargeVideo(state) {
  83. // 1. If a participant is pinned, they will be shown in the LargeVideo
  84. // (regardless of whether they are local or remote).
  85. const participants = state['features/base/participants'];
  86. let participant = participants.find(p => p.pinned);
  87. if (participant) {
  88. return participant.id;
  89. }
  90. // 2. Next, pick the most recent remote screenshare that was added to the conference.
  91. const remoteScreenShares = state['features/video-layout'].remoteScreenShares;
  92. if (remoteScreenShares?.length) {
  93. return remoteScreenShares[remoteScreenShares.length - 1];
  94. }
  95. // 3. Next, pick the dominant speaker (other than self).
  96. participant = participants.find(p => p.dominantSpeaker && !p.local);
  97. if (participant) {
  98. return participant.id;
  99. }
  100. // 4. Next, pick the most recent participant with video.
  101. const tracks = state['features/base/tracks'];
  102. const videoTrack = _electLastVisibleRemoteVideo(tracks);
  103. if (videoTrack) {
  104. return videoTrack.participantId;
  105. }
  106. // 5. As a last resort, select the participant that joined last (other than poltergist or other bot type
  107. // participants).
  108. for (let i = participants.length; i > 0 && !participant; i--) {
  109. const p = participants[i - 1];
  110. !p.botType && (participant = p);
  111. }
  112. if (participant) {
  113. return participant.id;
  114. }
  115. return participants.find(p => p.local)?.id;
  116. }