bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/screenshare/component.jsx

692 lines
20 KiB
React
Raw Normal View History

import React from 'react';
import { injectIntl } from 'react-intl';
2019-02-08 01:47:28 +08:00
import PropTypes from 'prop-types';
2023-08-10 00:26:42 +08:00
import { debounce } from '/imports/utils/debounce';
import FullscreenButtonContainer from '/imports/ui/components/common/fullscreen-button/container';
import SwitchButtonContainer from './switch-button/container';
2021-11-06 00:36:03 +08:00
import Styled from './styles';
import * as PluginSdk from 'bigbluebutton-html-plugin-sdk';
import VolumeSlider from '../external-video-player/volume-slider/component';
import PluginButtonContainer from './plugin-button/container';
import AutoplayOverlay from '../media/autoplay-overlay/component';
import logger from '/imports/startup/client/logger';
import playAndRetry from '/imports/utils/mediaElementPlayRetry';
import { notify } from '/imports/ui/services/notification';
import {
SCREENSHARE_MEDIA_ELEMENT_NAME,
isMediaFlowing,
screenshareHasEnded,
screenshareHasStarted,
setOutputDeviceId,
getMediaElement,
getMediaElementDimensions,
attachLocalPreviewStream,
setVolume,
getVolume,
getStats,
} from '/imports/ui/components/screenshare/service';
import {
isStreamStateHealthy,
subscribeToStreamStateChange,
unsubscribeFromStreamStateChange,
} from '/imports/ui/services/bbb-webrtc-sfu/stream-state-service';
import { ACTIONS, PRESENTATION_AREA } from '/imports/ui/components/layout/enums';
import { getSettingsSingletonInstance } from '/imports/ui/services/settings';
import deviceInfo from '/imports/utils/deviceInfo';
2023-02-23 22:23:51 +08:00
import { uniqueId } from '/imports/utils/string-utils';
import Session from '/imports/ui/services/storage/in-memory';
const MOBILE_HOVER_TIMEOUT = 5000;
const MEDIA_FLOW_PROBE_INTERVAL = 500;
const SCREEN_SIZE_DISPATCH_INTERVAL = 500;
const renderPluginItems = (pluginItems, bottom, right) => {
if (pluginItems !== undefined) {
return (
<>
{
pluginItems.map((pluginItem) => {
const returnComponent = (
<PluginButtonContainer
key={`${pluginItem.type}-${pluginItem.id}-${pluginItem.label}`}
dark
bottom={bottom}
right={right}
icon={pluginItem.icon}
label={pluginItem.label}
onClick={pluginItem.onClick}
/>
);
return returnComponent;
})
}
</>
);
}
return (<></>);
};
class ScreenshareComponent extends React.Component {
2021-08-09 22:24:02 +08:00
static renderScreenshareContainerInside(mainText) {
return (
2021-11-06 00:36:03 +08:00
<Styled.ScreenshareContainerInside>
<Styled.MainText>{mainText}</Styled.MainText>
</Styled.ScreenshareContainerInside>
2021-08-09 22:24:02 +08:00
);
}
constructor(props) {
super();
this.state = {
loaded: false,
autoplayBlocked: false,
2024-06-15 01:38:50 +08:00
mediaFlowing: true,
switched: false,
// Volume control hover toolbar
showHoverToolBar: false,
};
this.onLoadedData = this.onLoadedData.bind(this);
this.onLoadedMetadata = this.onLoadedMetadata.bind(this);
this.onVideoResize = this.onVideoResize.bind(this);
this.handleAllowAutoplay = this.handleAllowAutoplay.bind(this);
this.handlePlayElementFailed = this.handlePlayElementFailed.bind(this);
this.failedMediaElements = [];
this.onStreamStateChange = this.onStreamStateChange.bind(this);
this.onSwitched = this.onSwitched.bind(this);
this.handleOnVolumeChanged = this.handleOnVolumeChanged.bind(this);
2023-04-28 00:38:27 +08:00
this.dispatchScreenShareSize = this.dispatchScreenShareSize.bind(this);
this.handleOnMuted = this.handleOnMuted.bind(this);
this.dispatchScreenShareSize = this.dispatchScreenShareSize.bind(this);
this.renderScreenshareButtons = this.renderScreenshareButtons.bind(this);
this.splitPluginItems = this.splitPluginItems.bind(this);
2023-03-01 21:39:04 +08:00
this.debouncedDispatchScreenShareSize = debounce(
2023-04-28 00:38:27 +08:00
this.dispatchScreenShareSize,
2023-08-10 00:26:42 +08:00
SCREEN_SIZE_DISPATCH_INTERVAL,
{ leading: false, trailing: true },
);
2023-04-28 00:38:27 +08:00
const { locales, icon } = props;
this.locales = locales;
this.icon = icon;
this.volume = getVolume();
this.mobileHoverSetTimeout = null;
this.mediaFlowMonitor = null;
}
componentDidMount() {
const {
isLayoutSwapped,
layoutContextDispatch,
intl,
2021-11-24 00:43:49 +08:00
isPresenter,
startPreviewSizeBig,
outputDeviceId,
isSharedNotesPinned,
2024-06-14 22:35:23 +08:00
hasAudio,
} = this.props;
2024-06-14 22:35:23 +08:00
screenshareHasStarted(hasAudio, isPresenter, { outputDeviceId });
// Autoplay failure handling
window.addEventListener('screensharePlayFailed', this.handlePlayElementFailed);
// Stream health state tracker to propagate UI changes on reconnections
subscribeToStreamStateChange('screenshare', this.onStreamStateChange);
// Attaches the local stream if it exists to serve as the local presenter preview
attachLocalPreviewStream(getMediaElement());
this.setState({ switched: startPreviewSizeBig });
notify(intl.formatMessage(this.locales.started), 'info', this.icon);
layoutContextDispatch({
type: ACTIONS.SET_PILE_CONTENT_FOR_PRESENTATION_AREA,
value: {
content: PRESENTATION_AREA.SCREEN_SHARE,
open: true,
},
});
2022-02-26 01:56:03 +08:00
Session.setItem('pinnedNotesLastState', isSharedNotesPinned);
}
componentDidUpdate(prevProps) {
const { isPresenter, outputDeviceId } = this.props;
if (prevProps.isPresenter && !isPresenter) {
screenshareHasEnded();
}
if (prevProps.outputDeviceId !== outputDeviceId && !isPresenter) {
setOutputDeviceId(outputDeviceId);
}
}
componentWillUnmount() {
2021-11-06 00:36:03 +08:00
const {
intl,
fullscreenContext,
layoutContextDispatch,
2021-11-06 00:36:03 +08:00
} = this.props;
screenshareHasEnded();
window.removeEventListener('screensharePlayFailed', this.handlePlayElementFailed);
unsubscribeFromStreamStateChange('screenshare', this.onStreamStateChange);
const Settings = getSettingsSingletonInstance();
2022-02-08 01:46:45 +08:00
if (Settings.dataSaving.viewScreenshare) {
notify(intl.formatMessage(this.locales.ended), 'info', this.icon);
2022-02-08 01:46:45 +08:00
} else {
notify(intl.formatMessage(this.locales.endedDueToDataSaving), 'info', this.icon);
}
layoutContextDispatch({
type: ACTIONS.SET_PILE_CONTENT_FOR_PRESENTATION_AREA,
value: {
content: PRESENTATION_AREA.SCREEN_SHARE,
open: false,
},
});
if (fullscreenContext) {
layoutContextDispatch({
type: ACTIONS.SET_FULLSCREEN_ELEMENT,
value: {
element: '',
group: '',
},
});
}
2021-09-28 03:57:02 +08:00
this.clearMediaFlowingMonitor();
layoutContextDispatch({
type: ACTIONS.SET_PRESENTATION_IS_OPEN,
value: Session.getItem('presentationLastState'),
});
}
clearMediaFlowingMonitor() {
if (this.mediaFlowMonitor) {
2024-07-03 01:58:58 +08:00
clearInterval(this.mediaFlowMonitor);
this.mediaFlowMonitor = null;
}
}
handleAllowAutoplay() {
const { autoplayBlocked } = this.state;
logger.info({
logCode: 'screenshare_autoplay_allowed',
}, 'Screenshare media autoplay allowed by the user');
window.removeEventListener('screensharePlayFailed', this.handlePlayElementFailed);
while (this.failedMediaElements.length) {
const mediaElement = this.failedMediaElements.shift();
if (mediaElement) {
const played = playAndRetry(mediaElement);
if (!played) {
logger.error({
logCode: 'screenshare_autoplay_handling_failed',
}, 'Screenshare autoplay handling failed to play media');
} else {
logger.info({
logCode: 'screenshare_viewer_media_play_success',
}, 'Screenshare viewer media played successfully');
}
}
}
if (autoplayBlocked) { this.setState({ autoplayBlocked: false }); }
}
handlePlayElementFailed(e) {
const { mediaElement } = e.detail;
const { autoplayBlocked } = this.state;
e.stopPropagation();
this.failedMediaElements.push(mediaElement);
if (!autoplayBlocked) {
logger.info({
logCode: 'screenshare_autoplay_prompt',
}, 'Prompting user for action to play screenshare media');
this.setState({ autoplayBlocked: true });
}
}
async monitorMediaFlow() {
let previousStats = await getStats();
2024-07-03 01:58:58 +08:00
this.mediaFlowMonitor = setInterval(async () => {
const { mediaFlowing: prevMediaFlowing } = this.state;
let mediaFlowing;
2021-08-09 22:24:02 +08:00
const currentStats = await getStats();
try {
mediaFlowing = isMediaFlowing(previousStats, currentStats);
} catch (error) {
// Stats processing failed for whatever reason - maintain previous state
mediaFlowing = prevMediaFlowing;
logger.warn({
logCode: 'screenshare_media_monitor_stats_failed',
extraInfo: {
errorName: error.name,
errorMessage: error.message,
},
}, 'Failed to collect screenshare stats, flow monitor');
}
previousStats = currentStats;
if (prevMediaFlowing !== mediaFlowing) this.setState({ mediaFlowing });
}, MEDIA_FLOW_PROBE_INTERVAL);
2021-08-09 22:24:02 +08:00
}
dispatchScreenShareSize() {
const {
layoutContextDispatch,
} = this.props;
const { width, height } = getMediaElementDimensions();
const value = {
width,
height,
browserWidth: window.innerWidth,
browserHeight: window.innerHeight,
2023-04-28 00:38:27 +08:00
};
layoutContextDispatch({
type: ACTIONS.SET_SCREEN_SHARE_SIZE,
value,
});
}
onLoadedMetadata() {
const element = getMediaElement();
// Track HTMLVideo's resize event to propagate stream size changes to the
// layout engine. See this.onVideoResize;
if (element && typeof element.onresize !== 'function') {
element.onresize = this.onVideoResize;
}
// Dispatch the initial stream size to the layout engine
this.dispatchScreenShareSize();
}
2021-08-09 22:24:02 +08:00
onLoadedData() {
this.setState({ loaded: true });
}
onSwitched() {
this.setState((prevState) => ({ switched: !prevState.switched }));
}
handleOnVolumeChanged(volume) {
this.volume = volume;
setVolume(volume);
}
handleOnMuted(muted) {
if (muted) {
setVolume(0);
} else {
setVolume(this.volume);
}
}
2023-04-28 00:38:27 +08:00
onVideoResize() {
// Debounced version of the dispatcher to pace things out - we don't want
// to hog the CPU just for resize recalculations...
this.debouncedDispatchScreenShareSize();
}
onStreamStateChange(event) {
const { streamState } = event.detail;
const { mediaFlowing } = this.state;
const isStreamHealthy = isStreamStateHealthy(streamState);
event.stopPropagation();
if (isStreamHealthy) {
this.clearMediaFlowingMonitor();
// Current state is media not flowing - stream is now healthy so flip it
if (!mediaFlowing) this.setState({ mediaFlowing: isStreamHealthy });
} else if (this.mediaFlowMonitor == null) this.monitorMediaFlow();
}
renderFullscreenButton() {
2021-08-12 00:29:09 +08:00
const { intl, fullscreenElementId, fullscreenContext } = this.props;
const ALLOW_FULLSCREEN = window.meetingClientSettings.public.app.allowFullscreen;
if (!ALLOW_FULLSCREEN) return null;
return (
<Styled.FullscreenButtonWrapperForScreenshare>
<FullscreenButtonContainer
key={uniqueId('fullscreenButton-')}
elementName={intl.formatMessage(this.locales.label)}
fullscreenRef={this.screenshareContainer}
elementId={fullscreenElementId}
isFullscreen={fullscreenContext}
dark
/>
</Styled.FullscreenButtonWrapperForScreenshare>
);
}
renderAutoplayOverlay() {
const { intl } = this.props;
return (
<AutoplayOverlay
key={uniqueId('screenshareAutoplayOverlay')}
autoplayBlockedDesc={intl.formatMessage(this.locales.autoplayBlockedDesc)}
autoplayAllowLabel={intl.formatMessage(this.locales.autoplayAllowLabel)}
handleAllowAutoplay={this.handleAllowAutoplay}
/>
);
}
renderSwitchButton() {
const { showSwitchPreviewSizeButton } = this.props;
const { switched } = this.state;
if (!showSwitchPreviewSizeButton) return null;
return (
<SwitchButtonContainer
handleSwitch={this.onSwitched}
switched={switched}
dark
/>
);
}
renderMobileVolumeControlOverlay () {
return (
<Styled.MobileControlsOverlay
key="mobile-overlay-screenshare"
ref={(ref) => { this.overlay = ref; }}
onTouchStart={() => {
clearTimeout(this.mobileHoverSetTimeout);
this.setState({ showHoverToolBar: true });
}}
onTouchEnd={() => {
this.mobileHoverSetTimeout = setTimeout(
() => this.setState({ showHoverToolBar: false }),
MOBILE_HOVER_TIMEOUT,
);
}}
/>
);
}
renderVolumeSlider() {
const { showHoverToolBar } = this.state;
let toolbarStyle = 'hoverToolbar';
if (deviceInfo.isMobile && !showHoverToolBar) {
toolbarStyle = 'dontShowMobileHoverToolbar';
}
if (deviceInfo.isMobile && showHoverToolBar) {
toolbarStyle = 'showMobileHoverToolbar';
}
return [(
<Styled.HoverToolbar
toolbarStyle={toolbarStyle}
key="hover-toolbar-screenshare"
>
<VolumeSlider
volume={getVolume()}
muted={getVolume() === 0}
onVolumeChanged={this.handleOnVolumeChanged}
onMuted={this.handleOnMuted}
/>
</Styled.HoverToolbar>
),
(deviceInfo.isMobile) && this.renderMobileVolumeControlOverlay(),
];
}
renderVideo(switched) {
const { isGloballyBroadcasting } = this.props;
const { mediaFlowing } = this.state;
return (
2021-11-06 00:36:03 +08:00
<Styled.ScreenshareVideo
id={SCREENSHARE_MEDIA_ELEMENT_NAME}
key={SCREENSHARE_MEDIA_ELEMENT_NAME}
unhealthyStream={!isGloballyBroadcasting || !mediaFlowing}
style={switched
? { maxHeight: '100%', width: '100%', height: '100%' }
: { maxHeight: '25%', width: '25%', height: '25%' }}
playsInline
onLoadedData={this.onLoadedData}
onLoadedMetadata={this.onLoadedMetadata}
ref={(ref) => {
this.videoTag = ref;
}}
muted
/>
);
}
splitPluginItems() {
const { pluginScreenshareHelperItems } = this.props;
return pluginScreenshareHelperItems.reduce((result, item) => {
switch (item.position) {
case PluginSdk.ScreenshareHelperItemPosition.TOP_RIGHT:
result.topRightPluginItems.push(item);
break;
case PluginSdk.ScreenshareHelperItemPosition.TOP_LEFT:
result.topLeftPluginItems.push(item);
break;
case PluginSdk.ScreenshareHelperItemPosition.BOTTOM_RIGHT:
result.bottomRightPluginItems.push(item);
break;
case PluginSdk.ScreenshareHelperItemPosition.BOTTOM_LEFT:
result.bottomLeftPluginItems.push(item);
break;
default:
break;
}
return result;
}, {
topRightPluginItems: [],
topLeftPluginItems: [],
bottomRightPluginItems: [],
bottomLeftPluginItems: [],
});
}
renderScreensharePresenter() {
const { switched } = this.state;
const { isGloballyBroadcasting, intl } = this.props;
return (
<>
{this.renderVideo(switched)}
2021-08-09 22:24:02 +08:00
{
isGloballyBroadcasting
? (
<div data-test="isSharingScreen">
2021-08-09 22:24:02 +08:00
{!switched
&& ScreenshareComponent.renderScreenshareContainerInside(
intl.formatMessage(this.locales.presenterSharingLabel),
2021-08-09 22:24:02 +08:00
)}
</div>
)
: ScreenshareComponent.renderScreenshareContainerInside(
intl.formatMessage(this.locales.presenterLoadingLabel),
2021-08-09 22:24:02 +08:00
)
}
</>
);
}
renderScreenshareDefault() {
const { intl, enableVolumeControl } = this.props;
2021-08-12 00:29:09 +08:00
const { loaded } = this.state;
return (
<>
{this.renderVideo(true)}
{loaded && enableVolumeControl && this.renderVolumeSlider() }
2021-11-06 00:36:03 +08:00
<Styled.ScreenshareContainerDefault>
2021-08-09 22:24:02 +08:00
{
!loaded
? ScreenshareComponent.renderScreenshareContainerInside(
intl.formatMessage(this.locales.viewerLoadingLabel),
2021-08-09 22:24:02 +08:00
)
: null
}
2021-11-06 00:36:03 +08:00
</Styled.ScreenshareContainerDefault>
</>
);
}
renderScreenshareButtons() {
const { isPresenter, isGloballyBroadcasting } = this.props;
const { loaded } = this.state;
const {
topRightPluginItems,
topLeftPluginItems,
bottomRightPluginItems,
bottomLeftPluginItems,
} = this.splitPluginItems();
return (
<>
<Styled.ScreenshareButtonsContainterWrapper
positionYAxis="top"
positionXAxis="left"
>
{renderPluginItems(topLeftPluginItems, false, false)}
</Styled.ScreenshareButtonsContainterWrapper>
<Styled.ScreenshareButtonsContainterWrapper
positionYAxis="top"
positionXAxis="right"
>
{isPresenter
// Presenter button:
? isGloballyBroadcasting && this.renderSwitchButton()
// Non-presenter button:
: loaded && this.renderFullscreenButton()}
{renderPluginItems(topRightPluginItems, false, true)}
</Styled.ScreenshareButtonsContainterWrapper>
<Styled.ScreenshareButtonsContainterWrapper
positionYAxis="bottom"
positionXAxis="left"
>
{renderPluginItems(bottomLeftPluginItems, true, false)}
</Styled.ScreenshareButtonsContainterWrapper>
<Styled.ScreenshareButtonsContainterWrapper
positionYAxis="bottom"
positionXAxis="right"
>
{renderPluginItems(bottomRightPluginItems, true, true)}
</Styled.ScreenshareButtonsContainterWrapper>
</>
);
}
render() {
const {
loaded,
autoplayBlocked,
mediaFlowing,
switched,
} = this.state;
2021-07-07 03:27:28 +08:00
const {
isPresenter,
isGloballyBroadcasting,
top,
left,
2021-07-27 04:28:05 +08:00
right,
2021-07-07 03:27:28 +08:00
width,
height,
zIndex,
fullscreenContext,
2021-07-07 03:27:28 +08:00
} = this.props;
// Conditions to render the (re)connecting dots and the unhealthy stream
// grayscale:
// 1 - The local media tag has not received any stream data yet
// 2 - The user is a presenter and the stream wasn't globally broadcasted yet
// 3 - The media was loaded, the stream was globally broadcasted BUT the stream
// state transitioned to an unhealthy stream. tl;dr: screen sharing reconnection
const shouldRenderConnectingState = !loaded
2024-06-15 01:38:50 +08:00
|| (isPresenter && !isGloballyBroadcasting)
|| (!mediaFlowing && loaded && isGloballyBroadcasting);
const display = (width > 0 && height > 0) ? 'inherit' : 'none';
const Settings = getSettingsSingletonInstance();
2021-11-06 00:36:03 +08:00
const { animations } = Settings.application;
return (
2021-06-15 19:51:44 +08:00
<div
style={
2021-08-05 12:22:07 +08:00
{
position: 'absolute',
display,
2021-08-05 12:22:07 +08:00
top,
left,
right,
height,
width,
zIndex: fullscreenContext ? zIndex : undefined,
2021-08-05 12:22:07 +08:00
backgroundColor: '#06172A',
}
2021-06-15 19:51:44 +08:00
}
>
{(shouldRenderConnectingState)
&& (
2021-11-06 00:36:03 +08:00
<Styled.SpinnerWrapper
2023-02-23 22:23:51 +08:00
key={uniqueId('screenshareArea-')}
2021-06-15 19:51:44 +08:00
data-test="screenshareConnecting"
>
2021-11-06 00:36:03 +08:00
<Styled.Spinner animations={animations}>
<Styled.Bounce1 animations={animations} />
<Styled.Bounce2 animations={animations} />
<div />
2021-11-06 00:36:03 +08:00
</Styled.Spinner>
</Styled.SpinnerWrapper>
2021-06-15 19:51:44 +08:00
)}
{autoplayBlocked ? this.renderAutoplayOverlay() : null}
<Styled.ScreenshareContainer
switched={isPresenter ? switched : true}
key="screenshareContainer"
ref={(ref) => {
this.screenshareContainer = ref;
}}
id="screenshareContainer"
>
{this.renderScreenshareButtons()}
{isPresenter
? this.renderScreensharePresenter()
: this.renderScreenshareDefault()}
</Styled.ScreenshareContainer>
2021-06-15 19:51:44 +08:00
</div>
);
}
}
export default injectIntl(ScreenshareComponent);
ScreenshareComponent.propTypes = {
2021-08-09 22:24:02 +08:00
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
pluginScreenshareHelperItems: PropTypes.arrayOf(PropTypes.objectOf({
position: PropTypes.string,
})).isRequired,
2019-02-08 01:47:28 +08:00
isPresenter: PropTypes.bool.isRequired,
2023-04-28 00:38:27 +08:00
layoutContextDispatch: PropTypes.func.isRequired,
enableVolumeControl: PropTypes.bool.isRequired,
outputDeviceId: PropTypes.string,
};