Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/routes/Player/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const Player = () => {
const [player, videoParamsChanged, streamStateChanged, timeChanged, seek, pausedChanged, ended, nextVideo] = usePlayer(urlParams);
const [settings] = useSettings();
const streamingServer = useStreamingServer();
const statistics = useStatistics(player, streamingServer);
const video = useVideo();
const statistics = useStatistics(player, streamingServer, video);
const routeFocused = useRouteFocused();
const platform = usePlatform();
const toast = useToast();
Expand Down
253 changes: 219 additions & 34 deletions src/routes/Player/StatisticsMenu/StatisticsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,243 @@ const React = require('react');
const { useTranslation } = require('react-i18next');
const classNames = require('classnames');
const PropTypes = require('prop-types');
const { default: Icon } = require('@stremio/stremio-icons/react');
const { Button } = require('stremio/components');
const { useToast } = require('stremio/common');
const { formatDuration } = require('../streamQuality');
const styles = require('./styles.less');

const StatisticsMenu = React.memo(React.forwardRef(({ className, peers, speed, completed, infoHash }, ref) => {
const QUALITY = {
'checking': { tone: 'neutral', labelKey: 'PLAYER_QUALITY_CHECKING', labelFallback: 'Checking…', verdictKey: 'PLAYER_QUALITY_VERDICT_CHECKING', verdictFallback: 'Checking stream quality…' },
'cached': { tone: 'good', labelKey: 'PLAYER_QUALITY_GOOD', labelFallback: 'Good', verdictKey: 'PLAYER_QUALITY_VERDICT_CACHED', verdictFallback: 'Fully downloaded. It will not buffer.' },
'good': { tone: 'good', labelKey: 'PLAYER_QUALITY_GOOD', labelFallback: 'Good', verdictKey: 'PLAYER_QUALITY_VERDICT_GOOD', verdictFallback: 'Smooth playback. No pauses expected.' },
'fair': { tone: 'fair', labelKey: 'PLAYER_QUALITY_FAIR', labelFallback: 'Fair', verdictKey: 'PLAYER_QUALITY_VERDICT_FAIR', verdictFallback: 'Playing steadily. Should be fine.' },
'poor': { tone: 'poor', labelKey: 'PLAYER_QUALITY_POOR', labelFallback: 'Poor', verdictKey: 'PLAYER_QUALITY_VERDICT_POOR', verdictFallback: 'Playback may pause to keep loading.' },
'limited': { tone: 'neutral', labelKey: 'PLAYER_QUALITY_LIMITED', labelFallback: 'Limited', verdictKey: 'PLAYER_QUALITY_VERDICT_LIMITED', verdictFallback: 'Playing on this device. A full quality read is not available here.' },
};

const StatisticsMenu = React.memo(React.forwardRef(({ className, quality, bufferAheadSeconds, keepingUp, peers, speed, completed, infoHash }, ref) => {
const { t } = useTranslation();
const toast = useToast();
const [detailsOpen, setDetailsOpen] = React.useState(false);
const status = quality && quality.status ? quality.status : 'checking';
const state = QUALITY[status] || QUALITY.checking;
const ready = status !== 'checking';
const cached = status === 'cached';
const score = Math.max(0, Math.min(100, quality && typeof quality.score === 'number' ? quality.score : 0));

const onMouseDown = React.useCallback((event) => {
event.nativeEvent.statisticsMenuClosePrevented = true;
}, []);
const toggleDetails = React.useCallback(() => {
setDetailsOpen((open) => !open);
}, []);
const onToggleKeyDown = React.useCallback((event) => {
if (event.key === ' ' || event.key === 'Spacebar') {
event.preventDefault();
setDetailsOpen((open) => !open);
}
}, []);
const onCopyInfoHash = React.useCallback(() => {
if (!infoHash) {
return;
}
navigator.clipboard.writeText(infoHash)
.then(() => {
toast.show({
type: 'success',
title: t('COPIED', { defaultValue: 'Copied' }),
message: t('PLAYER_INFO_HASH_COPIED', { defaultValue: 'Info hash copied' }),
timeout: 3000
});
})
.catch((e) => {
console.error(e);
toast.show({
type: 'error',
title: t('ERROR', { defaultValue: 'Error' }),
message: infoHash,
timeout: 3000
});
});
}, [infoHash, t, toast]);
const onCopyKeyDown = React.useCallback((event) => {
if (event.key === ' ' || event.key === 'Spacebar') {
event.preventDefault();
onCopyInfoHash();
}
}, [onCopyInfoHash]);

const bufferKnown = Number.isFinite(bufferAheadSeconds);
const bufferValue = cached ?
t('PLAYER_SIGNAL_BUFFER_VALUE_CACHED', { defaultValue: 'Whole title' })
:
bufferKnown ?
formatDuration(bufferAheadSeconds)
:
t('PLAYER_SIGNAL_BUFFER_VALUE_UNKNOWN', { defaultValue: 'Not available' });
const bufferMeaning = cached ?
t('PLAYER_SIGNAL_BUFFER_MEANING_CACHED', { defaultValue: 'plays without buffering' })
:
bufferKnown ?
t('PLAYER_SIGNAL_BUFFER_MEANING', { defaultValue: 'safe to watch before it loads more' })
:
'';

const speedMeaning = cached ?
''
:
keepingUp === true ?
t('PLAYER_SIGNAL_SPEED_MEANING_OK', { defaultValue: 'faster than you’re watching' })
:
keepingUp === false ?
t('PLAYER_SIGNAL_SPEED_MEANING_SLOW', { defaultValue: 'slower than you’re watching' })
:
'';

return (
<div ref={ref} className={classNames(className, styles['statistics-menu-container'])} onMouseDown={onMouseDown}>
<div className={styles['title']}>
{t('PLAYER_STATISTICS')}
</div>
<div className={styles['stats']}>
<div className={styles['stat']}>
<div className={styles['label']}>
{t('PLAYER_PEERS')}
</div>
<div className={styles['value']}>
{ peers }
</div>
<div className={styles['header']}>
<div className={styles['title']}>
{t('PLAYER_STREAM_QUALITY', { defaultValue: 'Stream quality' })}
</div>
<div className={styles['stat']}>
<div className={styles['label']}>
{t('PLAYER_SPEED')}
</div>
<div className={styles['value']}>
{`${speed} ${t('MB_S')}`}
</div>
</div>
<div className={styles['stat']}>
<div className={styles['label']}>
{t('PLAYER_COMPLETED')}
</div>
<div className={styles['value']}>
{ Math.min(completed, 100) } %
</div>
<div className={classNames(styles['status-label'], styles[`tone-${state.tone}`])}>
{t(state.labelKey, { defaultValue: state.labelFallback })}
</div>
</div>
<div className={styles['info-hash']}>
<div className={styles['label']}>
{t('PLAYER_INFO_HASH')}
</div>
<div className={styles['value']}>
{ infoHash }
</div>
{
status !== 'limited' ?
<div className={styles['meter']}>
<div
className={styles['meter-track']}
role={'progressbar'}
aria-label={t('PLAYER_STREAM_QUALITY', { defaultValue: 'Stream quality' })}
aria-valuenow={score}
aria-valuemin={0}
aria-valuemax={100}
aria-valuetext={t(state.labelKey, { defaultValue: state.labelFallback })}
>
<div className={classNames(styles['meter-fill'], styles[`tone-${state.tone}`])} style={{ width: `${score}%` }} />
</div>
<div className={styles['meter-scale']}>
<span>{t('PLAYER_QUALITY_POOR', { defaultValue: 'Poor' })}</span>
<span>{t('PLAYER_QUALITY_FAIR', { defaultValue: 'Fair' })}</span>
<span>{t('PLAYER_QUALITY_GOOD', { defaultValue: 'Good' })}</span>
</div>
</div>
:
null
}
<div className={styles['verdict']}>
{t(state.verdictKey, { defaultValue: state.verdictFallback })}
</div>
{
ready ?
<React.Fragment>
<div className={styles['divider']} />
<div className={styles['signals']}>
<div className={styles['signal']}>
<div className={styles['signal-head']}>
<div className={styles['label']}>
{t('PLAYER_SIGNAL_SOURCES', { defaultValue: 'Sources' })}
</div>
<div className={styles['value']}>
{ peers }
</div>
</div>
<div className={styles['meaning']}>
{t('PLAYER_SIGNAL_SOURCES_MEANING', { defaultValue: 'where your video streams from' })}
</div>
</div>
<div className={styles['signal']}>
<div className={styles['signal-head']}>
<div className={styles['label']}>
{t('PLAYER_SIGNAL_BUFFER', { defaultValue: 'Buffer' })}
</div>
<div className={styles['value']}>
{ bufferValue }
</div>
</div>
{
bufferMeaning ?
<div className={styles['meaning']}>
{ bufferMeaning }
</div>
:
null
}
</div>
<div className={styles['signal']}>
<div className={styles['signal-head']}>
<div className={styles['label']}>
{t('PLAYER_SIGNAL_SPEED', { defaultValue: 'Speed' })}
</div>
<div className={styles['value']}>
{
cached ?
t('PLAYER_SIGNAL_SPEED_VALUE_CACHED', { defaultValue: 'Not needed' })
:
`${speed} ${t('MB_S', { defaultValue: 'MB/s' })}`
}
</div>
</div>
{
speedMeaning ?
<div className={styles['meaning']}>
{ speedMeaning }
</div>
:
null
}
</div>
</div>
<div className={styles['divider']} />
<div className={styles['technical']}>
<Button className={styles['technical-toggle']} onClick={toggleDetails} onKeyDown={onToggleKeyDown} role={'button'} aria-expanded={detailsOpen} aria-controls={'statistics-technical-details'}>
<Icon className={classNames(styles['chevron'], { [styles['open']]: detailsOpen })} name={'caret-down'} aria-hidden={true} />
<div className={styles['technical-label']}>
{t('PLAYER_MORE_DETAILS', { defaultValue: 'More details' })}
</div>
</Button>
<div className={classNames(styles['collapsible'], { [styles['open']]: detailsOpen })}>
<div className={styles['collapsible-inner']}>
<div className={styles['technical-content']} id={'statistics-technical-details'} aria-hidden={!detailsOpen}>
<div className={styles['detail-row']}>
<div className={styles['label']}>
{t('PLAYER_COMPLETED', { defaultValue: 'Completed' })}
</div>
<div className={styles['value']}>
{ Math.min(completed, 100) } %
</div>
</div>
<div className={styles['info-hash']}>
<div className={styles['label']}>
{t('PLAYER_INFO_HASH', { defaultValue: 'Info hash' })}
</div>
<Button className={styles['info-hash-value']} onClick={onCopyInfoHash} onKeyDown={onCopyKeyDown} role={'button'} tabIndex={detailsOpen ? 0 : -1} title={t('PLAYER_COPY_INFO_HASH', { defaultValue: 'Copy info hash' })} aria-label={t('PLAYER_COPY_INFO_HASH', { defaultValue: 'Copy info hash' })}>
{ infoHash }
</Button>
</div>
</div>
</div>
</div>
</div>
</React.Fragment>
:
null
}
</div>
);
}));

StatisticsMenu.propTypes = {
className: PropTypes.string,
quality: PropTypes.shape({
status: PropTypes.string,
score: PropTypes.number,
}),
bufferAheadSeconds: PropTypes.number,
keepingUp: PropTypes.bool,
peers: PropTypes.number,
speed: PropTypes.number,
completed: PropTypes.number,
Expand Down
Loading