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.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // @flow
  2. import {
  3. createSelectParticipantFailedEvent,
  4. sendAnalytics
  5. } from '../analytics';
  6. import { _handleParticipantError } from '../base/conference';
  7. import { MEDIA_TYPE } from '../base/media';
  8. import { getParticipants } from '../base/participants';
  9. import { reportError } from '../base/util';
  10. import { shouldDisplayTileView } from '../video-layout';
  11. import {
  12. SELECT_LARGE_VIDEO_PARTICIPANT,
  13. UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION
  14. } from './actionTypes';
  15. declare var APP: Object;
  16. /**
  17. * Signals conference to select a participant.
  18. *
  19. * @returns {Function}
  20. */
  21. export function selectParticipant() {
  22. return (dispatch: Dispatch<*>, 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 a
  42. * variety of factors: if there is a dominant or pinned speaker, or if there are
  43. * remote tracks, etc.
  44. *
  45. * @returns {Function}
  46. */
  47. export function selectParticipantInLargeVideo() {
  48. return (dispatch: Dispatch<*>, getState: Function) => {
  49. const state = getState();
  50. const participantId = _electParticipantInLargeVideo(state);
  51. const largeVideo = state['features/large-video'];
  52. if (participantId !== largeVideo.participantId) {
  53. dispatch({
  54. type: SELECT_LARGE_VIDEO_PARTICIPANT,
  55. participantId
  56. });
  57. dispatch(selectParticipant());
  58. }
  59. };
  60. }
  61. /**
  62. * Updates the currently seen resolution of the video displayed on large video.
  63. *
  64. * @param {number} resolution - The current resolution (height) of the video.
  65. * @returns {{
  66. * type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  67. * resolution: number
  68. * }}
  69. */
  70. export function updateKnownLargeVideoResolution(resolution: number) {
  71. return {
  72. type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  73. resolution
  74. };
  75. }
  76. /**
  77. * Returns the most recent existing remote video track.
  78. *
  79. * @param {Track[]} tracks - All current tracks.
  80. * @private
  81. * @returns {(Track|undefined)}
  82. */
  83. function _electLastVisibleRemoteVideo(tracks) {
  84. // First we try to get most recent remote video track.
  85. for (let i = tracks.length - 1; i >= 0; --i) {
  86. const track = tracks[i];
  87. if (!track.local && track.mediaType === MEDIA_TYPE.VIDEO) {
  88. return track;
  89. }
  90. }
  91. }
  92. /**
  93. * Returns the identifier of the participant who is to be on the stage i.e.
  94. * should be displayed in {@code LargeVideo}.
  95. *
  96. * @param {Object} state - The Redux state from which the participant to be
  97. * displayed in {@code LargeVideo} is to be elected.
  98. * @private
  99. * @returns {(string|undefined)}
  100. */
  101. function _electParticipantInLargeVideo(state) {
  102. // 1. If a participant is pinned, they will be shown in the LargeVideo (
  103. // regardless of whether they are local or remote).
  104. const participants = state['features/base/participants'];
  105. let participant = participants.find(p => p.pinned);
  106. let id = participant && participant.id;
  107. if (!id) {
  108. // 2. No participant is pinned so get the dominant speaker. But the
  109. // local participant won't be displayed in LargeVideo even if she is
  110. // the dominant speaker.
  111. participant = participants.find(p => p.dominantSpeaker && !p.local);
  112. id = participant && participant.id;
  113. if (!id) {
  114. // 3. There is no dominant speaker so select the remote participant
  115. // who last had visible video.
  116. const tracks = state['features/base/tracks'];
  117. const videoTrack = _electLastVisibleRemoteVideo(tracks);
  118. id = videoTrack && videoTrack.participantId;
  119. if (!id) {
  120. // 4. It's possible there is no participant with visible video.
  121. // This can happen for a number of reasons:
  122. // - there is only one participant (i.e. the local user),
  123. // - other participants joined with video muted.
  124. // As a last resort, pick the last participant who joined the
  125. // conference (regardless of whether they are local or
  126. // remote).
  127. participant = participants[participants.length - 1];
  128. id = participant && participant.id;
  129. }
  130. }
  131. }
  132. return id;
  133. }