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

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