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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 { getTrackByMediaTypeAndParticipant } from '../base/tracks';
  11. import { reportError } from '../base/util';
  12. import { shouldDisplayTileView } from '../video-layout';
  13. import {
  14. SELECT_LARGE_VIDEO_PARTICIPANT,
  15. UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION
  16. } from './actionTypes';
  17. /**
  18. * Captures a screenshot of the video displayed on the large video.
  19. *
  20. * @returns {Function}
  21. */
  22. export function captureLargeVideoScreenshot() {
  23. return (dispatch: Dispatch<any>, getState: Function): Promise<Object> => {
  24. const state = getState();
  25. const largeVideo = state['features/large-video'];
  26. if (!largeVideo) {
  27. return Promise.resolve();
  28. }
  29. const tracks = state['features/base/tracks'];
  30. const { jitsiTrack } = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, largeVideo.participantId);
  31. const videoStream = jitsiTrack.getOriginalStream();
  32. // Create a HTML canvas and draw video from the track on to the canvas.
  33. const [ track ] = videoStream.getVideoTracks();
  34. const { height, width } = track.getSettings() ?? track.getConstraints();
  35. const canvasElement = document.createElement('canvas');
  36. const ctx = canvasElement.getContext('2d');
  37. const videoElement = document.createElement('video');
  38. videoElement.height = parseInt(height, 10);
  39. videoElement.width = parseInt(width, 10);
  40. videoElement.autoplay = true;
  41. videoElement.srcObject = videoStream;
  42. canvasElement.height = videoElement.height;
  43. canvasElement.width = videoElement.width;
  44. // Wait for the video to load before drawing on to the canvas.
  45. const promise = new Promise(resolve => {
  46. videoElement.onloadeddata = () => resolve();
  47. });
  48. return promise.then(() => {
  49. ctx.drawImage(videoElement, 0, 0, videoElement.width, videoElement.height);
  50. const dataURL = canvasElement.toDataURL('image/png', 1.0);
  51. // Cleanup.
  52. ctx.clearRect(0, 0, videoElement.width, videoElement.height);
  53. videoElement.srcObject = null;
  54. canvasElement.remove();
  55. videoElement.remove();
  56. return Promise.resolve(dataURL);
  57. });
  58. };
  59. }
  60. /**
  61. * Signals conference to select a participant.
  62. *
  63. * @returns {Function}
  64. */
  65. export function selectParticipant() {
  66. return (dispatch: Dispatch<any>, getState: Function) => {
  67. const state = getState();
  68. const { conference } = state['features/base/conference'];
  69. if (conference) {
  70. const ids = shouldDisplayTileView(state)
  71. ? getParticipants(state).map(participant => participant.id)
  72. : [ state['features/large-video'].participantId ];
  73. try {
  74. conference.selectParticipants(ids);
  75. } catch (err) {
  76. _handleParticipantError(err);
  77. sendAnalytics(createSelectParticipantFailedEvent(err));
  78. reportError(
  79. err, `Failed to select participants ${ids.toString()}`);
  80. }
  81. }
  82. };
  83. }
  84. /**
  85. * Action to select the participant to be displayed in LargeVideo based on the
  86. * participant id provided. If a participant id is not provided, the LargeVideo
  87. * participant will be selected based on a variety of factors: If there is a
  88. * dominant or pinned speaker, or if there are remote tracks, etc.
  89. *
  90. * @param {string} participant - The participant id of the user that needs to be
  91. * displayed on the large video.
  92. * @returns {Function}
  93. */
  94. export function selectParticipantInLargeVideo(participant: ?string) {
  95. return (dispatch: Dispatch<any>, getState: Function) => {
  96. const state = getState();
  97. const participantId = participant ?? _electParticipantInLargeVideo(state);
  98. const largeVideo = state['features/large-video'];
  99. if (participantId !== largeVideo.participantId) {
  100. dispatch({
  101. type: SELECT_LARGE_VIDEO_PARTICIPANT,
  102. participantId
  103. });
  104. dispatch(selectParticipant());
  105. }
  106. };
  107. }
  108. /**
  109. * Updates the currently seen resolution of the video displayed on large video.
  110. *
  111. * @param {number} resolution - The current resolution (height) of the video.
  112. * @returns {{
  113. * type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  114. * resolution: number
  115. * }}
  116. */
  117. export function updateKnownLargeVideoResolution(resolution: number) {
  118. return {
  119. type: UPDATE_KNOWN_LARGE_VIDEO_RESOLUTION,
  120. resolution
  121. };
  122. }
  123. /**
  124. * Returns the most recent existing remote video track.
  125. *
  126. * @param {Track[]} tracks - All current tracks.
  127. * @private
  128. * @returns {(Track|undefined)}
  129. */
  130. function _electLastVisibleRemoteVideo(tracks) {
  131. // First we try to get most recent remote video track.
  132. for (let i = tracks.length - 1; i >= 0; --i) {
  133. const track = tracks[i];
  134. if (!track.local && track.mediaType === MEDIA_TYPE.VIDEO) {
  135. return track;
  136. }
  137. }
  138. }
  139. /**
  140. * Returns the identifier of the participant who is to be on the stage and
  141. * should be displayed in {@code LargeVideo}.
  142. *
  143. * @param {Object} state - The Redux state from which the participant to be
  144. * displayed in {@code LargeVideo} is to be elected.
  145. * @private
  146. * @returns {(string|undefined)}
  147. */
  148. function _electParticipantInLargeVideo(state) {
  149. // 1. If a participant is pinned, they will be shown in the LargeVideo (
  150. // regardless of whether they are local or remote).
  151. const participants = state['features/base/participants'];
  152. let participant = participants.find(p => p.pinned);
  153. let id = participant && participant.id;
  154. if (!id) {
  155. // 2. No participant is pinned so get the dominant speaker. But the
  156. // local participant won't be displayed in LargeVideo even if she is
  157. // the dominant speaker.
  158. participant = participants.find(p => p.dominantSpeaker && !p.local);
  159. id = participant && participant.id;
  160. if (!id) {
  161. // 3. There is no dominant speaker so select the remote participant
  162. // who last had visible video.
  163. const tracks = state['features/base/tracks'];
  164. const videoTrack = _electLastVisibleRemoteVideo(tracks);
  165. id = videoTrack && videoTrack.participantId;
  166. if (!id) {
  167. // 4. It's possible there is no participant with visible video.
  168. // This can happen for a number of reasons:
  169. // - there is only one participant (i.e. the local user),
  170. // - other participants joined with video muted.
  171. // As a last resort, pick the last participant who joined the
  172. // conference (regardless of whether they are local or
  173. // remote).
  174. //
  175. // HOWEVER: We don't want to show poltergeist or other bot type participants on stage
  176. // automatically, because it's misleading (users may think they are already
  177. // joined and maybe speaking).
  178. for (let i = participants.length; i > 0 && !participant; i--) {
  179. const p = participants[i - 1];
  180. !p.botType && (participant = p);
  181. }
  182. id = participant && participant.id;
  183. }
  184. }
  185. }
  186. return id;
  187. }