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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { _handleParticipantError } from '../base/conference';
  3. import { MEDIA_TYPE, VIDEO_TYPE } from '../base/media';
  4. import {
  5. getLocalVideoTrack,
  6. getTrackByMediaTypeAndParticipant
  7. } from '../base/tracks';
  8. import {
  9. SELECT_LARGE_VIDEO_PARTICIPANT,
  10. UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION
  11. } from './actionTypes';
  12. /**
  13. * Signals conference to select a participant.
  14. *
  15. * @returns {Function}
  16. */
  17. export function selectParticipant() {
  18. return (dispatch: Dispatch<*>, getState: Function) => {
  19. const state = getState();
  20. const { conference } = state['features/base/conference'];
  21. if (conference) {
  22. const largeVideo = state['features/large-video'];
  23. const tracks = state['features/base/tracks'];
  24. const id = largeVideo.participantId;
  25. const videoTrack
  26. = getTrackByMediaTypeAndParticipant(
  27. tracks,
  28. MEDIA_TYPE.VIDEO,
  29. id);
  30. try {
  31. conference.selectParticipant(
  32. videoTrack && videoTrack.videoType === VIDEO_TYPE.CAMERA
  33. ? id
  34. : null);
  35. } catch (err) {
  36. _handleParticipantError(err);
  37. }
  38. }
  39. };
  40. }
  41. /**
  42. * Action to select the participant to be displayed in LargeVideo based on a
  43. * variety of factors: if there is a dominant or pinned speaker, or if there are
  44. * remote tracks, etc.
  45. *
  46. * @returns {Function}
  47. */
  48. export function selectParticipantInLargeVideo() {
  49. return (dispatch: Dispatch<*>, getState: Function) => {
  50. const state = getState();
  51. const participantId = _electParticipantInLargeVideo(state);
  52. const largeVideo = state['features/large-video'];
  53. if (participantId !== largeVideo.participantId) {
  54. dispatch({
  55. type: SELECT_LARGE_VIDEO_PARTICIPANT,
  56. participantId
  57. });
  58. dispatch(selectParticipant());
  59. }
  60. };
  61. }
  62. /**
  63. * Updates the currently seen resolution of the video displayed on large video.
  64. *
  65. * @param {number} resolution - The current resolution (height) of the video.
  66. * @returns {{
  67. * type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  68. * resolution: number
  69. * }}
  70. */
  71. export function updateKnownLargeVideoResolution(resolution: number) {
  72. return {
  73. type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  74. resolution
  75. };
  76. }
  77. /**
  78. * Returns the most recent existing video track. It can be local or remote
  79. * video.
  80. *
  81. * @param {Track[]} tracks - All current tracks.
  82. * @private
  83. * @returns {(Track|undefined)}
  84. */
  85. function _electLastVisibleVideo(tracks) {
  86. // First we try to get most recent remote video track.
  87. for (let i = tracks.length - 1; i >= 0; --i) {
  88. const track = tracks[i];
  89. if (!track.local && track.mediaType === MEDIA_TYPE.VIDEO) {
  90. return track;
  91. }
  92. }
  93. // And if no remote video tracks are available, we select the local one.
  94. return getLocalVideoTrack(tracks);
  95. }
  96. /**
  97. * Returns the identifier of the participant who is to be on the stage i.e.
  98. * should be displayed in {@code LargeVideo}.
  99. *
  100. * @param {Object} state - The Redux state from which the participant to be
  101. * displayed in {@code LargeVideo} is to be elected.
  102. * @private
  103. * @returns {(string|undefined)}
  104. */
  105. function _electParticipantInLargeVideo(state) {
  106. // First get the pinned participant. If the local participant is pinned,
  107. // he/she will be shown in LargeVideo.
  108. const participants = state['features/base/participants'];
  109. let participant = participants.find(p => p.pinned);
  110. let id = participant ? participant.id : undefined;
  111. if (!id) {
  112. // No participant is pinned so get the dominant speaker. But the local
  113. // participant won't be displayed in LargeVideo even if he/she is the
  114. // dominant speaker.
  115. participant = participants.find(p => p.dominantSpeaker && !p.local);
  116. if (participant) {
  117. id = participant.id;
  118. }
  119. if (!id) {
  120. // There is no dominant speaker so get the participant with the last
  121. // visible video track. This may turn out to be the local
  122. // participant.
  123. const tracks = state['features/base/tracks'];
  124. const videoTrack = _electLastVisibleVideo(tracks);
  125. id = videoTrack && videoTrack.participantId;
  126. }
  127. }
  128. return id;
  129. }