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.

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