jiti-meet/react/features/filmstrip/components/native/Filmstrip.js

331 lines
11 KiB
JavaScript
Raw Normal View History

2017-11-03 20:14:38 +00:00
// @flow
import React, { PureComponent } from 'react';
import { FlatList } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { getLocalParticipant } from '../../../base/participants';
import { Platform } from '../../../base/react';
2019-03-21 16:38:29 +00:00
import { connect } from '../../../base/redux';
import { ASPECT_RATIO_NARROW } from '../../../base/responsive-ui/constants';
import { shouldHideSelfView } from '../../../base/settings/functions.any';
import { isToolboxVisible } from '../../../toolbox/functions';
import { setVisibleRemoteParticipants } from '../../actions';
import { isFilmstripVisible, shouldRemoteVideosBeVisible } from '../../functions';
2018-06-14 09:14:32 +00:00
import LocalThumbnail from './LocalThumbnail';
import Thumbnail from './Thumbnail';
2020-05-20 10:57:03 +00:00
import styles from './styles';
// Immutable reference to avoid re-renders.
const NO_REMOTE_VIDEOS = [];
/**
* Filmstrip component's property types.
*/
type Props = {
/**
* Application's aspect ratio.
*/
_aspectRatio: Symbol,
_clientWidth: number,
_clientHeight: number,
/**
* Whether or not to hide the self view.
*/
_disableSelfView: boolean,
/**
* Whether or not the toolbox is displayed.
*/
_isToolboxVisible: Boolean,
_localParticipantId: string,
/**
* The participants in the conference.
*/
_participants: Array<any>,
/**
* The indicator which determines whether the filmstrip is visible.
*/
_visible: boolean,
/**
* Invoked to trigger state changes in Redux.
*/
dispatch: Function,
};
/**
* Implements a React {@link Component} which represents the filmstrip on
* mobile/React Native.
*
2021-11-04 21:10:43 +00:00
* @augments Component
*/
class Filmstrip extends PureComponent<Props> {
/**
* Whether the local participant should be rendered separately from the
2021-11-04 21:10:43 +00:00
* remote participants ie outside of their {@link ScrollView}.
*/
_separateLocalThumbnail: boolean;
/**
* The FlatList's viewabilityConfig.
*/
_viewabilityConfig: Object;
/**
* Constructor of the component.
*
* @inheritdoc
*/
constructor(props) {
super(props);
// XXX Our current design is to have the local participant separate from
// the remote participants. Unfortunately, Android's Video
// implementation cannot accommodate that because remote participants'
// videos appear on top of the local participant's video at times.
// That's because Android's Video utilizes EGL and EGL gives us only two
// practical layers in which we can place our participants' videos:
// layer #0 sits behind the window, creates a hole in the window, and
// there we render the LargeVideo; layer #1 is known as media overlay in
// EGL terms, renders on top of layer #0, and, consequently, is for the
// Filmstrip. With the separate LocalThumnail, we should have left the
// remote participants' Thumbnails in layer #1 and utilized layer #2 for
// LocalThumbnail. Unfortunately, layer #2 is not practical (that's why
// I said we had two practical layers only) because it renders on top of
// everything which in our case means on top of participant-related
// indicators such as moderator, audio and video muted, etc. For now we
// do not have much of a choice but to continue rendering LocalThumbnail
// as any other remote Thumbnail on Android.
this._separateLocalThumbnail = Platform.OS !== 'android';
this._viewabilityConfig = {
itemVisiblePercentThreshold: 30,
minimumViewTime: 500
};
this._keyExtractor = this._keyExtractor.bind(this);
this._getItemLayout = this._getItemLayout.bind(this);
this._onViewableItemsChanged = this._onViewableItemsChanged.bind(this);
this._renderThumbnail = this._renderThumbnail.bind(this);
}
_keyExtractor: string => string;
/**
* Returns a key for a passed item of the list.
*
* @param {string} item - The user ID.
* @returns {string} - The user ID.
*/
_keyExtractor(item) {
return item;
}
/**
* Calculates the width and height of the filmstrip based on the screen size and aspect ratio.
*
* @returns {Object} - The width and the height.
*/
_getDimensions() {
const { _aspectRatio, _clientWidth, _clientHeight } = this.props;
const { height, width, margin } = styles.thumbnail;
if (_aspectRatio === ASPECT_RATIO_NARROW) {
return {
height,
width: this._separateLocalThumbnail ? _clientWidth - width - (margin * 2) : _clientWidth
};
}
return {
height: this._separateLocalThumbnail ? _clientHeight - height - (margin * 2) : _clientHeight,
width
};
}
_getItemLayout: (?Array<string>, number) => {length: number, offset: number, index: number};
/**
* Optimization for FlatList. Returns the length, offset and index for an item.
*
* @param {Array<string>} data - The data array with user IDs.
* @param {number} index - The index number of the item.
* @returns {Object}
*/
_getItemLayout(data, index) {
const { _aspectRatio } = this.props;
const isNarrowAspectRatio = _aspectRatio === ASPECT_RATIO_NARROW;
const length = isNarrowAspectRatio ? styles.thumbnail.width : styles.thumbnail.height;
return {
length,
offset: length * index,
index
};
}
_onViewableItemsChanged: Object => void;
/**
* A handler for visible items changes.
*
* @param {Object} data - The visible items data.
* @param {Array<Object>} data.viewableItems - The visible items array.
* @returns {void}
*/
_onViewableItemsChanged({ viewableItems = [] }) {
const { _disableSelfView } = this.props;
if (!this._separateLocalThumbnail && !_disableSelfView && viewableItems[0]?.index === 0) {
// Skip the local thumbnail.
viewableItems.shift();
}
if (viewableItems.length === 0) {
// User might be fast-scrolling, it will stabilize.
return;
}
let startIndex = viewableItems[0].index;
let endIndex = viewableItems[viewableItems.length - 1].index;
if (!this._separateLocalThumbnail && !_disableSelfView) {
// We are off by one in the remote participants array.
startIndex -= 1;
endIndex -= 1;
}
this.props.dispatch(setVisibleRemoteParticipants(startIndex, endIndex));
}
_renderThumbnail: Object => Object;
/**
* Creates React Element to display each participant in a thumbnail.
*
* @private
* @returns {ReactElement}
*/
_renderThumbnail({ item /* , index , separators */ }) {
return (
<Thumbnail
key = { item }
participantID = { item } />)
;
}
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
const {
_aspectRatio,
_disableSelfView,
_isToolboxVisible,
_localParticipantId,
_participants,
_visible
} = this.props;
if (!_visible) {
return null;
}
const isNarrowAspectRatio = _aspectRatio === ASPECT_RATIO_NARROW;
const filmstripStyle = isNarrowAspectRatio ? styles.filmstripNarrow : styles.filmstripWide;
const { height, width } = this._getDimensions();
const { height: thumbnailHeight, width: thumbnailWidth, margin } = styles.thumbnail;
const initialNumToRender = Math.ceil(isNarrowAspectRatio
? width / (thumbnailWidth + (2 * margin))
: height / (thumbnailHeight + (2 * margin))
);
let participants;
if (this._separateLocalThumbnail || _disableSelfView) {
participants = _participants;
} else if (isNarrowAspectRatio) {
participants = [ ..._participants, _localParticipantId ];
} else {
participants = [ _localParticipantId, ..._participants ];
}
return (
<SafeAreaView
edges = { [
!_isToolboxVisible && 'bottom',
'top',
'left',
'right'
].filter(Boolean) }
style = { filmstripStyle }>
{
this._separateLocalThumbnail
&& !isNarrowAspectRatio
&& !_disableSelfView
&& <LocalThumbnail />
}
<FlatList
bounces = { false }
data = { participants }
getItemLayout = { this._getItemLayout }
horizontal = { isNarrowAspectRatio }
initialNumToRender = { initialNumToRender }
key = { isNarrowAspectRatio ? 'narrow' : 'wide' }
keyExtractor = { this._keyExtractor }
onViewableItemsChanged = { this._onViewableItemsChanged }
renderItem = { this._renderThumbnail }
showsHorizontalScrollIndicator = { false }
showsVerticalScrollIndicator = { false }
style = { styles.flatListStageView }
viewabilityConfig = { this._viewabilityConfig }
windowSize = { 2 } />
{
this._separateLocalThumbnail
&& isNarrowAspectRatio
&& !_disableSelfView
&& <LocalThumbnail />
}
</SafeAreaView>
);
}
}
/**
* Maps (parts of) the redux state to the associated {@code Filmstrip}'s props.
*
* @param {Object} state - The redux state.
* @private
* @returns {Props}
*/
function _mapStateToProps(state) {
feat: Participants optimisations (#9515) * fix(participants): Change from array to Map * fix(unload): optimise * feat: Introduces new states for e2ee feature. Stores everyoneSupportsE2EE and everyoneEnabledE2EE to minimize looping through participants list. squash: Uses participants map and go over the elements only once. * feat: Optimizes isEveryoneModerator to do less frequent checks in all participants. * fix: Drops deep equal from participants pane and uses the map. * fix(SharedVideo): isVideoPlaying * fix(participants): Optimise isEveryoneModerator * fix(e2e): Optimise everyoneEnabledE2EE * fix: JS errors. * ref(participants): remove getParticipants * fix(participants): Prepare for PR. * fix: Changes participants pane to be component. The functional component was always rendered: `prev props: {} !== {} :next props`. * feat: Optimization to skip participants list on pane closed. * fix: The participants list shows and the local participant. * fix: Fix wrong action name for av-moderation. * fix: Minimizes the number of render calls of av moderation notification. * fix: Fix iterating over remote participants. * fix: Fixes lint error. * fix: Reflects participant updates for av-moderation. * fix(ParticipantPane): to work with IDs. * fix(av-moderation): on PARTCIPANT_UPDATE * fix(ParticipantPane): close delay. * fix: address code review comments * fix(API): mute-everyone * fix: bugs * fix(Thumbnail): on mobile. * fix(ParticipantPane): Close context menu on click. * fix: Handles few error when local participant is undefined. * feat: Hides AV moderation if not supported. * fix: Show mute all video. * fix: Fixes updating participant for av moderation. Co-authored-by: damencho <damencho@jitsi.org>
2021-07-09 12:36:19 +00:00
const { enabled, remoteParticipants } = state['features/filmstrip'];
const disableSelfView = shouldHideSelfView(state);
const showRemoteVideos = shouldRemoteVideosBeVisible(state);
const responsiveUI = state['features/base/responsive-ui'];
return {
_aspectRatio: state['features/base/responsive-ui'].aspectRatio,
_clientHeight: responsiveUI.clientHeight,
_clientWidth: responsiveUI.clientWidth,
_disableSelfView: disableSelfView,
_isToolboxVisible: isToolboxVisible(state),
_localParticipantId: getLocalParticipant(state)?.id,
_participants: showRemoteVideos ? remoteParticipants : NO_REMOTE_VIDEOS,
_visible: enabled && isFilmstripVisible(state)
};
}
export default connect(_mapStateToProps)(Filmstrip);