2017-07-18 21:41:39 +00:00
|
|
|
/* global APP */
|
|
|
|
|
2019-07-13 13:59:58 +00:00
|
|
|
import JitsiMeetJS, { JitsiTrackErrors, browser } from '../lib-jitsi-meet';
|
2017-07-31 16:27:34 +00:00
|
|
|
import { MEDIA_TYPE } from '../media';
|
2019-05-07 08:53:01 +00:00
|
|
|
import {
|
|
|
|
getUserSelectedCameraDeviceId,
|
|
|
|
getUserSelectedMicDeviceId
|
|
|
|
} from '../settings';
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2020-01-28 10:06:03 +00:00
|
|
|
import loadEffects from './loadEffects';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2017-07-18 21:41:39 +00:00
|
|
|
|
2019-11-26 10:57:03 +00:00
|
|
|
/**
|
|
|
|
* Creates a local video track for presenter. The constraints are computed based
|
|
|
|
* on the height of the desktop that is being shared.
|
|
|
|
*
|
|
|
|
* @param {Object} options - The options with which the local presenter track
|
|
|
|
* is to be created.
|
|
|
|
* @param {string|null} [options.cameraDeviceId] - Camera device id or
|
|
|
|
* {@code undefined} to use app's settings.
|
|
|
|
* @param {number} desktopHeight - The height of the desktop that is being
|
|
|
|
* shared.
|
|
|
|
* @returns {Promise<JitsiLocalTrack>}
|
|
|
|
*/
|
|
|
|
export async function createLocalPresenterTrack(options, desktopHeight) {
|
|
|
|
const { cameraDeviceId } = options;
|
|
|
|
|
|
|
|
// compute the constraints of the camera track based on the resolution
|
|
|
|
// of the desktop screen that is being shared.
|
2020-02-19 19:48:28 +00:00
|
|
|
const cameraHeights = [ 180, 270, 360, 540, 720 ];
|
|
|
|
const proportion = 5;
|
2019-11-26 10:57:03 +00:00
|
|
|
const result = cameraHeights.find(
|
|
|
|
height => (desktopHeight / proportion) < height);
|
|
|
|
const constraints = {
|
|
|
|
video: {
|
|
|
|
aspectRatio: 4 / 3,
|
|
|
|
height: {
|
2019-12-04 20:28:42 +00:00
|
|
|
ideal: result
|
2019-11-26 10:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const [ videoTrack ] = await JitsiMeetJS.createLocalTracks(
|
|
|
|
{
|
|
|
|
cameraDeviceId,
|
|
|
|
constraints,
|
|
|
|
devices: [ 'video' ]
|
|
|
|
});
|
|
|
|
|
|
|
|
videoTrack.type = MEDIA_TYPE.PRESENTER;
|
|
|
|
|
|
|
|
return videoTrack;
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:41:39 +00:00
|
|
|
/**
|
|
|
|
* Create local tracks of specific types.
|
|
|
|
*
|
|
|
|
* @param {Object} options - The options with which the local tracks are to be
|
|
|
|
* created.
|
|
|
|
* @param {string|null} [options.cameraDeviceId] - Camera device id or
|
|
|
|
* {@code undefined} to use app's settings.
|
|
|
|
* @param {string[]} options.devices - Required track types such as 'audio'
|
|
|
|
* and/or 'video'.
|
|
|
|
* @param {string|null} [options.micDeviceId] - Microphone device id or
|
|
|
|
* {@code undefined} to use app's settings.
|
|
|
|
* @param {boolean} [firePermissionPromptIsShownEvent] - Whether lib-jitsi-meet
|
|
|
|
* should check for a {@code getUserMedia} permission prompt and fire a
|
|
|
|
* corresponding event.
|
|
|
|
* @param {Object} store - The redux store in the context of which the function
|
|
|
|
* is to execute and from which state such as {@code config} is to be retrieved.
|
|
|
|
* @returns {Promise<JitsiLocalTrack[]>}
|
|
|
|
*/
|
2020-01-28 10:04:46 +00:00
|
|
|
export function createLocalTracksF(options = {}, firePermissionPromptIsShownEvent, store) {
|
2017-07-18 21:41:39 +00:00
|
|
|
let { cameraDeviceId, micDeviceId } = options;
|
|
|
|
|
|
|
|
if (typeof APP !== 'undefined') {
|
|
|
|
// TODO The app's settings should go in the redux store and then the
|
|
|
|
// reliance on the global variable APP will go away.
|
2018-04-12 19:58:20 +00:00
|
|
|
store || (store = APP.store); // eslint-disable-line no-param-reassign
|
|
|
|
|
2019-05-07 08:53:01 +00:00
|
|
|
const state = store.getState();
|
2018-04-12 19:58:20 +00:00
|
|
|
|
2017-07-18 21:41:39 +00:00
|
|
|
if (typeof cameraDeviceId === 'undefined' || cameraDeviceId === null) {
|
2019-05-07 08:53:01 +00:00
|
|
|
cameraDeviceId = getUserSelectedCameraDeviceId(state);
|
2017-07-18 21:41:39 +00:00
|
|
|
}
|
|
|
|
if (typeof micDeviceId === 'undefined' || micDeviceId === null) {
|
2019-05-07 08:53:01 +00:00
|
|
|
micDeviceId = getUserSelectedMicDeviceId(state);
|
2017-07-18 21:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:59:23 +00:00
|
|
|
const state = store.getState();
|
2017-07-18 21:41:39 +00:00
|
|
|
const {
|
2018-02-20 21:23:41 +00:00
|
|
|
desktopSharingFrameRate,
|
2017-07-18 21:41:39 +00:00
|
|
|
firefox_fake_device, // eslint-disable-line camelcase
|
|
|
|
resolution
|
2019-07-08 10:59:23 +00:00
|
|
|
} = state['features/base/config'];
|
2020-01-28 10:06:03 +00:00
|
|
|
const constraints = options.constraints ?? state['features/base/config'].constraints;
|
2017-07-18 21:41:39 +00:00
|
|
|
|
|
|
|
return (
|
2020-01-28 10:06:03 +00:00
|
|
|
loadEffects(store).then(effectsArray => {
|
2019-09-19 13:28:57 +00:00
|
|
|
// Filter any undefined values returned by Promise.resolve().
|
|
|
|
const effects = effectsArray.filter(effect => Boolean(effect));
|
|
|
|
|
|
|
|
return JitsiMeetJS.createLocalTracks(
|
2019-07-08 10:59:23 +00:00
|
|
|
{
|
|
|
|
cameraDeviceId,
|
|
|
|
constraints,
|
|
|
|
desktopSharingFrameRate,
|
|
|
|
desktopSharingSourceDevice:
|
|
|
|
options.desktopSharingSourceDevice,
|
|
|
|
desktopSharingSources: options.desktopSharingSources,
|
2017-07-18 21:41:39 +00:00
|
|
|
|
2019-07-08 10:59:23 +00:00
|
|
|
// Copy array to avoid mutations inside library.
|
|
|
|
devices: options.devices.slice(0),
|
|
|
|
effects,
|
|
|
|
firefox_fake_device, // eslint-disable-line camelcase
|
|
|
|
micDeviceId,
|
|
|
|
resolution
|
|
|
|
},
|
|
|
|
firePermissionPromptIsShownEvent)
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Failed to create local tracks', options.devices, err);
|
2017-07-18 21:41:39 +00:00
|
|
|
|
2019-07-08 10:59:23 +00:00
|
|
|
return Promise.reject(err);
|
2019-09-19 13:28:57 +00:00
|
|
|
});
|
|
|
|
}));
|
2017-07-18 21:41:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Returns local audio track.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @returns {(Track|undefined)}
|
|
|
|
*/
|
|
|
|
export function getLocalAudioTrack(tracks) {
|
|
|
|
return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns local track by media type.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - Media type.
|
2018-04-06 20:04:25 +00:00
|
|
|
* @param {boolean} [includePending] - Indicates whether a local track is to be
|
|
|
|
* returned if it is still pending. A local track is pending if
|
|
|
|
* {@code getUserMedia} is still executing to create it and, consequently, its
|
|
|
|
* {@code jitsiTrack} property is {@code undefined}. By default a pending local
|
|
|
|
* track is not returned.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {(Track|undefined)}
|
|
|
|
*/
|
2018-04-06 20:04:25 +00:00
|
|
|
export function getLocalTrack(tracks, mediaType, includePending = false) {
|
|
|
|
return (
|
|
|
|
getLocalTracks(tracks, includePending)
|
|
|
|
.find(t => t.mediaType === mediaType));
|
2017-11-09 21:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-06 20:04:25 +00:00
|
|
|
* Returns an array containing the local tracks with or without a (valid)
|
2017-11-13 22:10:54 +00:00
|
|
|
* {@code JitsiTrack}.
|
2017-11-09 21:59:31 +00:00
|
|
|
*
|
2017-11-13 22:10:54 +00:00
|
|
|
* @param {Track[]} tracks - An array containing all local tracks.
|
2018-04-06 20:04:25 +00:00
|
|
|
* @param {boolean} [includePending] - Indicates whether a local track is to be
|
|
|
|
* returned if it is still pending. A local track is pending if
|
|
|
|
* {@code getUserMedia} is still executing to create it and, consequently, its
|
|
|
|
* {@code jitsiTrack} property is {@code undefined}. By default a pending local
|
|
|
|
* track is not returned.
|
2017-11-09 21:59:31 +00:00
|
|
|
* @returns {Track[]}
|
|
|
|
*/
|
2018-04-06 20:04:25 +00:00
|
|
|
export function getLocalTracks(tracks, includePending = false) {
|
2017-11-13 22:10:54 +00:00
|
|
|
// XXX A local track is considered ready only once it has its `jitsiTrack`
|
|
|
|
// property set by the `TRACK_ADDED` action. Until then there is a stub
|
|
|
|
// added just before the `getUserMedia` call with a cancellable
|
|
|
|
// `gumInProgress` property which then can be used to destroy the track that
|
|
|
|
// has not yet been added to the redux store. Once GUM is cancelled, it will
|
|
|
|
// never make it to the store nor there will be any
|
|
|
|
// `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
|
2018-04-06 20:04:25 +00:00
|
|
|
return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns local video track.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @returns {(Track|undefined)}
|
|
|
|
*/
|
|
|
|
export function getLocalVideoTrack(tracks) {
|
|
|
|
return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:57:03 +00:00
|
|
|
/**
|
|
|
|
* Returns the media type of the local video, presenter or video.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @returns {MEDIA_TYPE}
|
|
|
|
*/
|
|
|
|
export function getLocalVideoType(tracks) {
|
|
|
|
const presenterTrack = getLocalTrack(tracks, MEDIA_TYPE.PRESENTER);
|
|
|
|
|
|
|
|
return presenterTrack ? MEDIA_TYPE.PRESENTER : MEDIA_TYPE.VIDEO;
|
|
|
|
}
|
|
|
|
|
2020-06-19 07:03:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the stored local video track.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function getLocalJitsiVideoTrack(state) {
|
|
|
|
const track = getLocalVideoTrack(state['features/base/tracks']);
|
|
|
|
|
|
|
|
return track?.jitsiTrack;
|
|
|
|
}
|
|
|
|
|
2020-06-26 08:54:12 +00:00
|
|
|
/**
|
|
|
|
* Returns the stored local audio track.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function getLocalJitsiAudioTrack(state) {
|
|
|
|
const track = getLocalAudioTrack(state['features/base/tracks']);
|
|
|
|
|
|
|
|
return track?.jitsiTrack;
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Returns track of specified media type for specified participant id.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - Media type.
|
|
|
|
* @param {string} participantId - Participant ID.
|
|
|
|
* @returns {(Track|undefined)}
|
|
|
|
*/
|
|
|
|
export function getTrackByMediaTypeAndParticipant(
|
|
|
|
tracks,
|
|
|
|
mediaType,
|
|
|
|
participantId) {
|
|
|
|
return tracks.find(
|
|
|
|
t => t.participantId === participantId && t.mediaType === mediaType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the track if any which corresponds to a specific instance
|
|
|
|
* of JitsiLocalTrack or JitsiRemoteTrack.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
|
|
|
|
* @returns {(Track|undefined)}
|
|
|
|
*/
|
|
|
|
export function getTrackByJitsiTrack(tracks, jitsiTrack) {
|
|
|
|
return tracks.find(t => t.jitsiTrack === jitsiTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns tracks of specified media type.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - Media type.
|
|
|
|
* @returns {Track[]}
|
|
|
|
*/
|
|
|
|
export function getTracksByMediaType(tracks, mediaType) {
|
|
|
|
return tracks.filter(t => t.mediaType === mediaType);
|
|
|
|
}
|
2017-08-18 11:30:30 +00:00
|
|
|
|
2019-11-26 10:57:03 +00:00
|
|
|
/**
|
|
|
|
* Checks if the local video track in the given set of tracks is muted.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @returns {Track[]}
|
|
|
|
*/
|
|
|
|
export function isLocalVideoTrackMuted(tracks) {
|
|
|
|
const presenterTrack = getLocalTrack(tracks, MEDIA_TYPE.PRESENTER);
|
|
|
|
const videoTrack = getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
|
|
|
|
|
|
|
|
// Make sure we check the mute status of only camera tracks, i.e.,
|
|
|
|
// presenter track when it exists, camera track when the presenter
|
|
|
|
// track doesn't exist.
|
|
|
|
if (presenterTrack) {
|
|
|
|
return isLocalTrackMuted(tracks, MEDIA_TYPE.PRESENTER);
|
|
|
|
} else if (videoTrack) {
|
|
|
|
return videoTrack.videoType === 'camera'
|
|
|
|
? isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO) : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-18 11:30:30 +00:00
|
|
|
/**
|
|
|
|
* Checks if the first local track in the given tracks set is muted.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
|
|
|
|
* @returns {boolean} True if local track is muted or false if the track is
|
|
|
|
* unmuted or if there are no local tracks of the given media type in the given
|
|
|
|
* set of tracks.
|
|
|
|
*/
|
|
|
|
export function isLocalTrackMuted(tracks, mediaType) {
|
|
|
|
const track = getLocalTrack(tracks, mediaType);
|
|
|
|
|
2019-01-05 16:49:21 +00:00
|
|
|
return !track || track.muted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the remote track of the given media type and the given
|
|
|
|
* participant is muted, false otherwise.
|
|
|
|
*
|
|
|
|
* @param {Track[]} tracks - List of all tracks.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
|
|
|
|
* @param {*} participantId - Participant ID.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isRemoteTrackMuted(tracks, mediaType, participantId) {
|
|
|
|
const track = getTrackByMediaTypeAndParticipant(
|
|
|
|
tracks, mediaType, participantId);
|
|
|
|
|
2017-08-18 11:30:30 +00:00
|
|
|
return !track || track.muted;
|
|
|
|
}
|
|
|
|
|
2019-07-10 11:02:27 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the current environment needs a user interaction with
|
|
|
|
* the page before any unmute can occur.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isUserInteractionRequiredForUnmute(state) {
|
2019-07-13 13:59:58 +00:00
|
|
|
return browser.isUserInteractionRequiredForUnmute()
|
2019-07-10 11:02:27 +00:00
|
|
|
&& window
|
|
|
|
&& window.self !== window.top
|
|
|
|
&& !state['features/base/user-interaction'].interacted;
|
|
|
|
}
|
|
|
|
|
2017-08-18 11:30:30 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
|
|
|
|
* the specified {@code track} is already in accord with the specified
|
|
|
|
* {@code muted} value, then does nothing.
|
2017-08-18 11:30:30 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
|
2017-08-18 11:30:30 +00:00
|
|
|
* unmute.
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {boolean} muted - If the specified {@code track} is to be muted, then
|
|
|
|
* {@code true}; otherwise, {@code false}.
|
2017-08-18 11:30:30 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
export function setTrackMuted(track, muted) {
|
|
|
|
muted = Boolean(muted); // eslint-disable-line no-param-reassign
|
|
|
|
|
|
|
|
if (track.isMuted() === muted) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const f = muted ? 'mute' : 'unmute';
|
|
|
|
|
|
|
|
return track[f]().catch(error => {
|
2017-11-21 13:38:53 +00:00
|
|
|
// Track might be already disposed so ignore such an error.
|
|
|
|
if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
|
|
|
|
// FIXME Emit mute failed, so that the app can show error dialog.
|
2018-07-30 14:38:25 +00:00
|
|
|
logger.error(`set track ${f} failed`, error);
|
2017-11-21 13:38:53 +00:00
|
|
|
}
|
2017-08-18 11:30:30 +00:00
|
|
|
});
|
|
|
|
}
|