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

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