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.

functions.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* global APP */
  2. import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
  3. import { MEDIA_TYPE } from '../media';
  4. const logger = require('jitsi-meet-logger').getLogger(__filename);
  5. /**
  6. * Create local tracks of specific types.
  7. *
  8. * @param {Object} options - The options with which the local tracks are to be
  9. * created.
  10. * @param {string|null} [options.cameraDeviceId] - Camera device id or
  11. * {@code undefined} to use app's settings.
  12. * @param {string[]} options.devices - Required track types such as 'audio'
  13. * and/or 'video'.
  14. * @param {string|null} [options.micDeviceId] - Microphone device id or
  15. * {@code undefined} to use app's settings.
  16. * @param {boolean} [firePermissionPromptIsShownEvent] - Whether lib-jitsi-meet
  17. * should check for a {@code getUserMedia} permission prompt and fire a
  18. * corresponding event.
  19. * @param {Object} store - The redux store in the context of which the function
  20. * is to execute and from which state such as {@code config} is to be retrieved.
  21. * @returns {Promise<JitsiLocalTrack[]>}
  22. */
  23. export function createLocalTracksF(
  24. options,
  25. firePermissionPromptIsShownEvent,
  26. store) {
  27. options || (options = {}); // eslint-disable-line no-param-reassign
  28. let { cameraDeviceId, micDeviceId } = options;
  29. if (typeof APP !== 'undefined') {
  30. // TODO The app's settings should go in the redux store and then the
  31. // reliance on the global variable APP will go away.
  32. if (typeof cameraDeviceId === 'undefined' || cameraDeviceId === null) {
  33. cameraDeviceId = APP.settings.getCameraDeviceId();
  34. }
  35. if (typeof micDeviceId === 'undefined' || micDeviceId === null) {
  36. micDeviceId = APP.settings.getMicDeviceId();
  37. }
  38. store || (store = APP.store); // eslint-disable-line no-param-reassign
  39. }
  40. const {
  41. constraints,
  42. firefox_fake_device, // eslint-disable-line camelcase
  43. resolution
  44. } = store.getState()['features/base/config'];
  45. return (
  46. JitsiMeetJS.createLocalTracks(
  47. {
  48. cameraDeviceId,
  49. constraints,
  50. desktopSharingExtensionExternalInstallation:
  51. options.desktopSharingExtensionExternalInstallation,
  52. desktopSharingSources: options.desktopSharingSources,
  53. // Copy array to avoid mutations inside library.
  54. devices: options.devices.slice(0),
  55. firefox_fake_device, // eslint-disable-line camelcase
  56. micDeviceId,
  57. resolution
  58. },
  59. firePermissionPromptIsShownEvent)
  60. .then(tracks => {
  61. // TODO JitsiTrackEvents.NO_DATA_FROM_SOURCE should probably be
  62. // dispatched in the redux store here and then
  63. // APP.UI.showTrackNotWorkingDialog should be in a middleware
  64. // somewhere else.
  65. if (typeof APP !== 'undefined') {
  66. tracks.forEach(track =>
  67. track.on(
  68. JitsiTrackEvents.NO_DATA_FROM_SOURCE,
  69. APP.UI.showTrackNotWorkingDialog.bind(null, track)));
  70. }
  71. return tracks;
  72. })
  73. .catch(err => {
  74. logger.error('Failed to create local tracks', options.devices, err);
  75. return Promise.reject(err);
  76. }));
  77. }
  78. /**
  79. * Returns local audio track.
  80. *
  81. * @param {Track[]} tracks - List of all tracks.
  82. * @returns {(Track|undefined)}
  83. */
  84. export function getLocalAudioTrack(tracks) {
  85. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  86. }
  87. /**
  88. * Returns local track by media type.
  89. *
  90. * @param {Track[]} tracks - List of all tracks.
  91. * @param {MEDIA_TYPE} mediaType - Media type.
  92. * @returns {(Track|undefined)}
  93. */
  94. export function getLocalTrack(tracks, mediaType) {
  95. return tracks.find(t => t.local && t.mediaType === mediaType);
  96. }
  97. /**
  98. * Returns local video track.
  99. *
  100. * @param {Track[]} tracks - List of all tracks.
  101. * @returns {(Track|undefined)}
  102. */
  103. export function getLocalVideoTrack(tracks) {
  104. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  105. }
  106. /**
  107. * Returns track of specified media type for specified participant id.
  108. *
  109. * @param {Track[]} tracks - List of all tracks.
  110. * @param {MEDIA_TYPE} mediaType - Media type.
  111. * @param {string} participantId - Participant ID.
  112. * @returns {(Track|undefined)}
  113. */
  114. export function getTrackByMediaTypeAndParticipant(
  115. tracks,
  116. mediaType,
  117. participantId) {
  118. return tracks.find(
  119. t => t.participantId === participantId && t.mediaType === mediaType
  120. );
  121. }
  122. /**
  123. * Returns the track if any which corresponds to a specific instance
  124. * of JitsiLocalTrack or JitsiRemoteTrack.
  125. *
  126. * @param {Track[]} tracks - List of all tracks.
  127. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  128. * @returns {(Track|undefined)}
  129. */
  130. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  131. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  132. }
  133. /**
  134. * Returns tracks of specified media type.
  135. *
  136. * @param {Track[]} tracks - List of all tracks.
  137. * @param {MEDIA_TYPE} mediaType - Media type.
  138. * @returns {Track[]}
  139. */
  140. export function getTracksByMediaType(tracks, mediaType) {
  141. return tracks.filter(t => t.mediaType === mediaType);
  142. }
  143. /**
  144. * Checks if the first local track in the given tracks set is muted.
  145. *
  146. * @param {Track[]} tracks - List of all tracks.
  147. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  148. * @returns {boolean} True if local track is muted or false if the track is
  149. * unmuted or if there are no local tracks of the given media type in the given
  150. * set of tracks.
  151. */
  152. export function isLocalTrackMuted(tracks, mediaType) {
  153. const track = getLocalTrack(tracks, mediaType);
  154. return !track || track.muted;
  155. }
  156. /**
  157. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  158. * the specified {@code track} is already in accord with the specified
  159. * {@code muted} value, then does nothing.
  160. *
  161. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  162. * unmute.
  163. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  164. * {@code true}; otherwise, {@code false}.
  165. * @returns {Promise}
  166. */
  167. export function setTrackMuted(track, muted) {
  168. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  169. if (track.isMuted() === muted) {
  170. return Promise.resolve();
  171. }
  172. const f = muted ? 'mute' : 'unmute';
  173. return track[f]().catch(error => {
  174. // FIXME emit mute failed, so that the app can show error dialog
  175. console.error(`set track ${f} failed`, error);
  176. });
  177. }