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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 createLocalTracks(
  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. firefox_fake_device, // eslint-disable-line camelcase
  42. resolution
  43. } = store.getState()['features/base/config'];
  44. return (
  45. JitsiMeetJS.createLocalTracks(
  46. {
  47. cameraDeviceId,
  48. desktopSharingExtensionExternalInstallation:
  49. options.desktopSharingExtensionExternalInstallation,
  50. desktopSharingSources: options.desktopSharingSources,
  51. // Copy array to avoid mutations inside library.
  52. devices: options.devices.slice(0),
  53. firefox_fake_device, // eslint-disable-line camelcase
  54. micDeviceId,
  55. resolution
  56. },
  57. firePermissionPromptIsShownEvent)
  58. .then(tracks => {
  59. // TODO JitsiTrackEvents.NO_DATA_FROM_SOURCE should probably be
  60. // dispatched in the redux store here and then
  61. // APP.UI.showTrackNotWorkingDialog should be in a middleware
  62. // somewhere else.
  63. if (typeof APP !== 'undefined') {
  64. tracks.forEach(track =>
  65. track.on(
  66. JitsiTrackEvents.NO_DATA_FROM_SOURCE,
  67. APP.UI.showTrackNotWorkingDialog.bind(null, track)));
  68. }
  69. return tracks;
  70. })
  71. .catch(err => {
  72. logger.error('Failed to create local tracks', options.devices, err);
  73. return Promise.reject(err);
  74. }));
  75. }
  76. /**
  77. * Returns local audio track.
  78. *
  79. * @param {Track[]} tracks - List of all tracks.
  80. * @returns {(Track|undefined)}
  81. */
  82. export function getLocalAudioTrack(tracks) {
  83. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  84. }
  85. /**
  86. * Returns local track by media type.
  87. *
  88. * @param {Track[]} tracks - List of all tracks.
  89. * @param {MEDIA_TYPE} mediaType - Media type.
  90. * @returns {(Track|undefined)}
  91. */
  92. export function getLocalTrack(tracks, mediaType) {
  93. return tracks.find(t => t.local && t.mediaType === mediaType);
  94. }
  95. /**
  96. * Returns local video track.
  97. *
  98. * @param {Track[]} tracks - List of all tracks.
  99. * @returns {(Track|undefined)}
  100. */
  101. export function getLocalVideoTrack(tracks) {
  102. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  103. }
  104. /**
  105. * Returns track of specified media type for specified participant id.
  106. *
  107. * @param {Track[]} tracks - List of all tracks.
  108. * @param {MEDIA_TYPE} mediaType - Media type.
  109. * @param {string} participantId - Participant ID.
  110. * @returns {(Track|undefined)}
  111. */
  112. export function getTrackByMediaTypeAndParticipant(
  113. tracks,
  114. mediaType,
  115. participantId) {
  116. return tracks.find(
  117. t => t.participantId === participantId && t.mediaType === mediaType
  118. );
  119. }
  120. /**
  121. * Returns the track if any which corresponds to a specific instance
  122. * of JitsiLocalTrack or JitsiRemoteTrack.
  123. *
  124. * @param {Track[]} tracks - List of all tracks.
  125. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  126. * @returns {(Track|undefined)}
  127. */
  128. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  129. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  130. }
  131. /**
  132. * Returns tracks of specified media type.
  133. *
  134. * @param {Track[]} tracks - List of all tracks.
  135. * @param {MEDIA_TYPE} mediaType - Media type.
  136. * @returns {Track[]}
  137. */
  138. export function getTracksByMediaType(tracks, mediaType) {
  139. return tracks.filter(t => t.mediaType === mediaType);
  140. }
  141. /**
  142. * Checks if the first local track in the given tracks set is muted.
  143. *
  144. * @param {Track[]} tracks - List of all tracks.
  145. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  146. * @returns {boolean} True if local track is muted or false if the track is
  147. * unmuted or if there are no local tracks of the given media type in the given
  148. * set of tracks.
  149. */
  150. export function isLocalTrackMuted(tracks, mediaType) {
  151. const track = getLocalTrack(tracks, mediaType);
  152. return !track || track.muted;
  153. }
  154. /**
  155. * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
  156. * the specified <tt>track</tt> is already in accord with the specified
  157. * <tt>muted</tt> value, then does nothing.
  158. *
  159. * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
  160. * unmute.
  161. * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
  162. * <tt>true</tt>; otherwise, <tt>false</tt>.
  163. * @returns {Promise}
  164. */
  165. export function setTrackMuted(track, muted) {
  166. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  167. if (track.isMuted() === muted) {
  168. return Promise.resolve();
  169. }
  170. const f = muted ? 'mute' : 'unmute';
  171. return track[f]().catch(error => {
  172. // FIXME emit mute failed, so that the app can show error dialog
  173. console.error(`set track ${f} failed`, error);
  174. });
  175. }